Permite definir um erro ao processar a declaração.
[cascardo/declara.git] / lib / declaracao.c
index 4c5f188..fd3c998 100644 (file)
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#define _GNU_SOURCE
 #include "declaracao.h"
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdarg.h>
 #include "list.h"
+#include "conjuge.h"
+#include "sistema.h"
 #include "rendimento.h"
+#include "isento.h"
 #include "pagamento.h"
 #include "bem.h"
+#include "dependente.h"
 #include "pmhash.h"
 
 struct declaracao * declaracao_new(int ano)
@@ -36,12 +43,18 @@ struct declaracao * declaracao_new(int ano)
        dec->rendimento = list_new();
        if (!dec->rendimento)
                goto out_rendimento;
+       dec->isentos = list_new();
+       if (!dec->isentos)
+               goto out_isentos;
        dec->pagamentos = list_new();
        if (!dec->pagamentos)
                goto out_pagamentos;
        dec->bens = list_new();
        if (!dec->bens)
                goto out_bens;
+       dec->dependentes = list_new();
+       if (!dec->dependentes)
+               goto out_dependentes;
        dec->totais = pmhash_new();
        if (!dec->totais)
                goto out_totais;
@@ -49,12 +62,17 @@ struct declaracao * declaracao_new(int ano)
        dec->devido = 0;
        dec->restituicao = 0;
        dec->tipo = SIMPLES;
+       dec->error = NULL;
        return dec;
 out_totais:
+       list_free(dec->dependentes, dependente_free);
+out_dependentes:
        list_free(dec->bens, bem_free);
 out_bens:
        list_free(dec->pagamentos, pagamento_free);
 out_pagamentos:
+       list_free(dec->isentos, isento_free);
+out_isentos:
        list_free(dec->rendimento, rendimento_free);
 out_rendimento:
        free(dec);
@@ -80,8 +98,25 @@ void declaracao_free(struct declaracao *dec)
        if (dec->dvconta)
                free(dec->dvconta);
        list_free(dec->rendimento, rendimento_free);
+       list_free(dec->isentos, isento_free);
        list_free(dec->pagamentos, pagamento_free);
        list_free(dec->bens, bem_free);
+       list_free(dec->dependentes, dependente_free);
        pmhash_del(dec->totais);
+       conjuge_free(dec);
+       sistema_free(dec);
+       if (dec->error)
+               free(dec->error);
        free(dec);
 }
+
+void dec_set_error(struct declaracao *dec, char *fmt, ...)
+{
+       va_list ap;
+       if (dec->error)
+               free(dec->error);
+       dec->error = NULL;
+       va_start(ap, fmt);
+       vasprintf(&dec->error, fmt, ap);
+       va_end(ap);
+}