From: Thadeu Lima de Souza Cascardo Date: Sun, 26 Apr 2015 19:17:39 +0000 (+0000) Subject: Adiciona comando de rendimento. X-Git-Url: http://git.cascardo.eti.br/?p=cascardo%2Fdeclara.git;a=commitdiff_plain;h=641730591d7dcbce71f577656a2487220d112f35 Adiciona comando de rendimento. Adiciona dados de rendimentos pagos por pessoa jurídica. --- diff --git a/Makefile.am b/Makefile.am index 59a2ab7..5fb2136 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,4 +2,7 @@ bin_PROGRAMS = declara declara_SOURCES = declara.c declaracao.c declaracao.h \ token.c token.h \ cmd.c cmd.h pmhash.c pmhash.h \ - base.c base.h + base.c base.h \ + list.c list.h \ + util.c util.h \ + rendimento.c rendimento.h diff --git a/base.c b/base.c index c6b057d..e3c60ef 100644 --- a/base.c +++ b/base.c @@ -21,6 +21,7 @@ #include #include #include +#include "rendimento.h" static int set_int(char **args, int argc, int *val) { @@ -85,6 +86,7 @@ static int run_dump(struct declaracao *dec, char **args, int argc) printf("ano: %d\n", dec->ano); printf("cpf: %s\n", dec->cpf); printf("nome: %s\n", dec->nome); + rendimento_dump(dec); return 0; } diff --git a/declara.c b/declara.c index bcda3d5..db113ad 100644 --- a/declara.c +++ b/declara.c @@ -26,6 +26,7 @@ #include "declaracao.h" #include "cmd.h" #include "base.h" +#include "rendimento.h" static int realprocess(struct declaracao *dec, int fd) { @@ -78,6 +79,7 @@ int main(int argc, char **argv) cmd_init(); base_cmd_init(); + rendimento_cmd_init(); filename = argv[1]; r = process(filename); diff --git a/declaracao.c b/declaracao.c index eb3c81f..5494339 100644 --- a/declaracao.c +++ b/declaracao.c @@ -19,6 +19,8 @@ #include "declaracao.h" #include #include +#include "list.h" +#include "rendimento.h" struct declaracao * declaracao_new(int ano) { @@ -28,6 +30,11 @@ struct declaracao * declaracao_new(int ano) return NULL; memset(dec, 0, sizeof(*dec)); dec->ano = ano; + dec->rendimento = list_new(); + if (!dec->rendimento) { + free(dec); + return NULL; + } return dec; } @@ -37,5 +44,6 @@ void declaracao_free(struct declaracao *dec) free(dec->cpf); if (dec->nome) free(dec->nome); + list_free(dec->rendimento, rendimento_free); free(dec); } diff --git a/declaracao.h b/declaracao.h index 01c91d0..e498d1d 100644 --- a/declaracao.h +++ b/declaracao.h @@ -19,10 +19,13 @@ #ifndef _DECLARACAO_H #define _DECLARACAO_H +#include "list.h" + struct declaracao { int ano; char *cpf; char *nome; + struct list *rendimento; }; struct declaracao * declaracao_new(int ano); diff --git a/rendimento.c b/rendimento.c new file mode 100644 index 0000000..d50e259 --- /dev/null +++ b/rendimento.c @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2015 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "rendimento.h" +#include +#include +#include +#include +#include +#include "cmd.h" +#include "list.h" +#include "util.h" + +struct rendimento { + char *cnpj; + char *nome; + long long rendimento; + long long previdencia; + long long decimoterceiro; + long long imposto; + char *saida; +}; + +void rendimento_free(void *pointer) +{ + struct rendimento *rendimento = pointer; + if (rendimento->cnpj) + free(rendimento->cnpj); + if (rendimento->nome) + free(rendimento->nome); + if (rendimento->saida) + free(rendimento->saida); + free(rendimento); +} + +static struct rendimento * rendimento_new(char **args) +{ + struct rendimento *rendimento; + int r = 0; + rendimento = malloc(sizeof(*rendimento)); + rendimento->cnpj = strdup(args[1]); + rendimento->nome = strdup(args[2]); + rendimento->saida = strdup(args[7]); + r += set_llong(args[3], &rendimento->rendimento); + r += set_llong(args[4], &rendimento->previdencia); + r += set_llong(args[5], &rendimento->decimoterceiro); + r += set_llong(args[6], &rendimento->imposto); + if (!rendimento->cnpj || !rendimento->nome || !rendimento->saida) { + rendimento_free(rendimento); + return NULL; + } + if (r < 0 || rendimento->rendimento < 0 || rendimento->previdencia < 0 || + rendimento->decimoterceiro < 0 || rendimento->imposto < 0) { + rendimento_free(rendimento); + return NULL; + } + return rendimento; +} + +static int run_rendimento(struct declaracao *dec, char **args, int argc) +{ + struct rendimento *rendimento; + int r; + if (argc != 8) + return -EINVAL; + rendimento = rendimento_new(args); + if (!rendimento) + return -ENOMEM; + r = list_add(&dec->rendimento, rendimento); + if (r < 0) { + rendimento_free(rendimento); + return r; + } + return 0; +} + +void rendimento_dump(struct declaracao *dec) +{ + int i; + struct rendimento *j; + for (i = 0; j = list_get(dec->rendimento, i); i++) + printf("rendimento: %s %s %013lld %013lld %013lld %013lld %s\n", + j->cnpj, j->nome, j->rendimento, j->previdencia, + j->decimoterceiro, j->imposto, j->saida); +} + +static struct cmd cmd_rendimento = { + .name = "rendimento", + .run = run_rendimento, +}; + +int rendimento_cmd_init(void) +{ + cmd_add(&cmd_rendimento); + return 0; +} diff --git a/rendimento.h b/rendimento.h new file mode 100644 index 0000000..e0e7cc7 --- /dev/null +++ b/rendimento.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2015 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _RENDIMENTO_H +#define _RENDIMENTO_H + +#include "declaracao.h" + +struct rendimento; + +void rendimento_dump(struct declaracao *dec); +void rendimento_free(void *pointer); + +int rendimento_cmd_init(void); + +#endif