Cria dicionário de totais.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sat, 1 Aug 2015 14:44:23 +0000 (11:44 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sat, 1 Aug 2015 14:44:23 +0000 (11:44 -0300)
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
calcula.c
declaracao.c
declaracao.h
rendimento.c
totais.c [new file with mode: 0644]
totais.h [new file with mode: 0644]

index 2304b38..8e86311 100644 (file)
@@ -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 \
index 80f6c6d..d5daf76 100644 (file)
--- a/calcula.c
+++ b/calcula.c
  */
 
 #include "calcula.h"
+#include <errno.h>
+#include <stdio.h>
 #include "declaracao.h"
 #include "cmd.h"
 #include "rendimento.h"
-#include <errno.h>
-#include <stdio.h>
+#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)
index 2fc3f0a..3d58380 100644 (file)
@@ -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);
 }
index ad28893..dab52ae 100644 (file)
@@ -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);
index caa7a64..c3b2025 100644 (file)
@@ -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 (file)
index 0000000..ca79125
--- /dev/null
+++ b/totais.c
@@ -0,0 +1,59 @@
+/*
+ *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ *  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 <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#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 (file)
index 0000000..45583f9
--- /dev/null
+++ b/totais.h
@@ -0,0 +1,27 @@
+/*
+ *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ *  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