From 8fb82c4f80554d8022e9544a3d7888bc74c1566c Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Sat, 1 Aug 2015 11:44:23 -0300 Subject: [PATCH] =?utf8?q?Cria=20dicion=C3=A1rio=20de=20totais.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Com este dicionário, cada módulo pode adicionar um valor a um total, que pode ser posteriormente usado no cálculo e na geração da declaração. --- Makefile.am | 1 + calcula.c | 13 ++++-------- declaracao.c | 15 +++++++++---- declaracao.h | 1 + rendimento.c | 6 ++++++ totais.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ totais.h | 27 ++++++++++++++++++++++++ 7 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 totais.c create mode 100644 totais.h diff --git a/Makefile.am b/Makefile.am index 2304b38..8e86311 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,6 +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 \ + totais.c totais.h \ calcula.c calcula.h \ gera.c gera.h \ base.c base.h \ diff --git a/calcula.c b/calcula.c index 80f6c6d..d5daf76 100644 --- a/calcula.c +++ b/calcula.c @@ -17,21 +17,16 @@ */ #include "calcula.h" +#include +#include #include "declaracao.h" #include "cmd.h" #include "rendimento.h" -#include -#include +#include "totais.h" static void total_rendimento(struct declaracao *dec) { - long long tr = 0; - struct rendimento *rendimento; - int i; - for (i = 0; rendimento = list_get(dec->rendimento, i); i++) { - tr += rendimento->rendimento; - } - dec->totalrendimento = tr; + dec->totalrendimento = totais_get(dec, "RENDPJTIT"); } static long long total_deducao(struct declaracao *dec) diff --git a/declaracao.c b/declaracao.c index 2fc3f0a..3d58380 100644 --- a/declaracao.c +++ b/declaracao.c @@ -31,16 +31,22 @@ struct declaracao * declaracao_new(int ano) memset(dec, 0, sizeof(*dec)); dec->ano = ano; dec->rendimento = list_new(); - if (!dec->rendimento) { - free(dec); - return NULL; - } + if (!dec->rendimento) + goto out_rendimento; + dec->totais = pmhash_new(); + if (!dec->totais) + goto out_totais; dec->totalrendimento = 0; dec->pago = 0; dec->devido = 0; dec->restituicao = 0; dec->tipo = SIMPLES; return dec; +out_totais: + list_free(dec->rendimento, rendimento_free); +out_rendimento: + free(dec); + return NULL; } void declaracao_free(struct declaracao *dec) @@ -62,5 +68,6 @@ void declaracao_free(struct declaracao *dec) if (dec->dvconta) free(dec->dvconta); list_free(dec->rendimento, rendimento_free); + pmhash_del(dec->totais); free(dec); } diff --git a/declaracao.h b/declaracao.h index ad28893..dab52ae 100644 --- a/declaracao.h +++ b/declaracao.h @@ -48,6 +48,7 @@ struct declaracao { char *contacorrente; char *dvconta; int linhas[100]; /* Número de linhas escritas de cada tipo. */ + struct pmhash *totais; }; struct declaracao * declaracao_new(int ano); diff --git a/rendimento.c b/rendimento.c index caa7a64..c3b2025 100644 --- a/rendimento.c +++ b/rendimento.c @@ -25,6 +25,7 @@ #include "cmd.h" #include "list.h" #include "util.h" +#include "totais.h" void rendimento_free(void *pointer) { @@ -90,6 +91,11 @@ static int run_rendimento(struct declaracao *dec, char **args, int argc) rendimento_free(rendimento); return r; } + r = totais_add(dec, "RENDPJTIT", rendimento->rendimento); + if (r) { + rendimento_free(rendimento); + return r; + } return 0; } diff --git a/totais.c b/totais.c new file mode 100644 index 0000000..ca79125 --- /dev/null +++ b/totais.c @@ -0,0 +1,59 @@ +/* + * 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 "totais.h" +#include +#include +#include +#include "pmhash.h" + +int totais_add(struct declaracao *dec, char *key, long long val) +{ + long long *p; + int r = -ENOMEM; + p = pmhash_get(dec->totais, key); + if (!p) { + p = malloc(sizeof(*p)); + if (!p) + goto out_p; + key = strdup(key); + if (!key) + goto out_key; + r = pmhash_add(&dec->totais, key, p); + if (r) + goto out_hash; + *p = 0; + } + *p += val; + return 0; +out_hash: + free(key); +out_key: + free(p); +out_p: + return r; +} + +long long totais_get(struct declaracao *dec, char *key) +{ + long long *p; + p = pmhash_get(dec->totais, key); + if (!p) + return 0; + return *p; +} diff --git a/totais.h b/totais.h new file mode 100644 index 0000000..45583f9 --- /dev/null +++ b/totais.h @@ -0,0 +1,27 @@ +/* + * 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 _TOTAIS_H +#define _TOTAIS_H + +#include "declaracao.h" + +int totais_add(struct declaracao *dec, char *key, long long val); +long long totais_get(struct declaracao *dec, char *key); + +#endif -- 2.20.1