Permite definir um erro ao processar a declaração.
[cascardo/declara.git] / lib / declaracao.c
1 /*
2  *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #define _GNU_SOURCE
20 #include "declaracao.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdarg.h>
25 #include "list.h"
26 #include "conjuge.h"
27 #include "sistema.h"
28 #include "rendimento.h"
29 #include "isento.h"
30 #include "pagamento.h"
31 #include "bem.h"
32 #include "dependente.h"
33 #include "pmhash.h"
34
35 struct declaracao * declaracao_new(int ano)
36 {
37         struct declaracao *dec;
38         dec = malloc(sizeof(*dec));
39         if (!dec)
40                 return NULL;
41         memset(dec, 0, sizeof(*dec));
42         dec->ano = ano;
43         dec->rendimento = list_new();
44         if (!dec->rendimento)
45                 goto out_rendimento;
46         dec->isentos = list_new();
47         if (!dec->isentos)
48                 goto out_isentos;
49         dec->pagamentos = list_new();
50         if (!dec->pagamentos)
51                 goto out_pagamentos;
52         dec->bens = list_new();
53         if (!dec->bens)
54                 goto out_bens;
55         dec->dependentes = list_new();
56         if (!dec->dependentes)
57                 goto out_dependentes;
58         dec->totais = pmhash_new();
59         if (!dec->totais)
60                 goto out_totais;
61         dec->pago = 0;
62         dec->devido = 0;
63         dec->restituicao = 0;
64         dec->tipo = SIMPLES;
65         dec->error = NULL;
66         return dec;
67 out_totais:
68         list_free(dec->dependentes, dependente_free);
69 out_dependentes:
70         list_free(dec->bens, bem_free);
71 out_bens:
72         list_free(dec->pagamentos, pagamento_free);
73 out_pagamentos:
74         list_free(dec->isentos, isento_free);
75 out_isentos:
76         list_free(dec->rendimento, rendimento_free);
77 out_rendimento:
78         free(dec);
79         return NULL;
80 }
81
82 void declaracao_free(struct declaracao *dec)
83 {
84         if (dec->cpf)
85                 free(dec->cpf);
86         if (dec->nome)
87                 free(dec->nome);
88         if (dec->recibo)
89                 free(dec->recibo);
90         if (dec->retifica)
91                 free(dec->retifica);
92         if (dec->banco)
93                 free(dec->banco);
94         if (dec->agencia)
95                 free(dec->agencia);
96         if (dec->contacorrente)
97                 free(dec->contacorrente);
98         if (dec->dvconta)
99                 free(dec->dvconta);
100         list_free(dec->rendimento, rendimento_free);
101         list_free(dec->isentos, isento_free);
102         list_free(dec->pagamentos, pagamento_free);
103         list_free(dec->bens, bem_free);
104         list_free(dec->dependentes, dependente_free);
105         pmhash_del(dec->totais);
106         conjuge_free(dec);
107         sistema_free(dec);
108         if (dec->error)
109                 free(dec->error);
110         free(dec);
111 }
112
113 void dec_set_error(struct declaracao *dec, char *fmt, ...)
114 {
115         va_list ap;
116         if (dec->error)
117                 free(dec->error);
118         dec->error = NULL;
119         va_start(ap, fmt);
120         vasprintf(&dec->error, fmt, ap);
121         va_end(ap);
122 }