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 \
*/
#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)
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)
if (dec->dvconta)
free(dec->dvconta);
list_free(dec->rendimento, rendimento_free);
+ pmhash_del(dec->totais);
free(dec);
}
char *contacorrente;
char *dvconta;
int linhas[100]; /* Número de linhas escritas de cada tipo. */
+ struct pmhash *totais;
};
struct declaracao * declaracao_new(int ano);
#include "cmd.h"
#include "list.h"
#include "util.h"
+#include "totais.h"
void rendimento_free(void *pointer)
{
rendimento_free(rendimento);
return r;
}
+ r = totais_add(dec, "RENDPJTIT", rendimento->rendimento);
+ if (r) {
+ rendimento_free(rendimento);
+ return r;
+ }
return 0;
}
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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