Função para iniciar todos os comandos.
[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 #include "base.h"
36 #include "calcula.h"
37 #include "gera.h"
38 #include "help.h"
39
40 struct declaracao * declaracao_new(int ano)
41 {
42         struct declaracao *dec;
43         dec = malloc(sizeof(*dec));
44         if (!dec)
45                 return NULL;
46         memset(dec, 0, sizeof(*dec));
47         dec->ano = ano;
48         dec->rendimento = list_new();
49         if (!dec->rendimento)
50                 goto out_rendimento;
51         dec->isentos = list_new();
52         if (!dec->isentos)
53                 goto out_isentos;
54         dec->pagamentos = list_new();
55         if (!dec->pagamentos)
56                 goto out_pagamentos;
57         dec->bens = list_new();
58         if (!dec->bens)
59                 goto out_bens;
60         dec->dependentes = list_new();
61         if (!dec->dependentes)
62                 goto out_dependentes;
63         dec->totais = pmhash_new();
64         if (!dec->totais)
65                 goto out_totais;
66         dec->pago = 0;
67         dec->devido = 0;
68         dec->restituicao = 0;
69         dec->tipo = SIMPLES;
70         dec->error = NULL;
71         return dec;
72 out_totais:
73         list_free(dec->dependentes, dependente_free);
74 out_dependentes:
75         list_free(dec->bens, bem_free);
76 out_bens:
77         list_free(dec->pagamentos, pagamento_free);
78 out_pagamentos:
79         list_free(dec->isentos, isento_free);
80 out_isentos:
81         list_free(dec->rendimento, rendimento_free);
82 out_rendimento:
83         free(dec);
84         return NULL;
85 }
86
87 void declaracao_free(struct declaracao *dec)
88 {
89         if (dec->cpf)
90                 free(dec->cpf);
91         if (dec->nome)
92                 free(dec->nome);
93         if (dec->recibo)
94                 free(dec->recibo);
95         if (dec->retifica)
96                 free(dec->retifica);
97         if (dec->banco)
98                 free(dec->banco);
99         if (dec->agencia)
100                 free(dec->agencia);
101         if (dec->contacorrente)
102                 free(dec->contacorrente);
103         if (dec->dvconta)
104                 free(dec->dvconta);
105         list_free(dec->rendimento, rendimento_free);
106         list_free(dec->isentos, isento_free);
107         list_free(dec->pagamentos, pagamento_free);
108         list_free(dec->bens, bem_free);
109         list_free(dec->dependentes, dependente_free);
110         pmhash_del(dec->totais);
111         conjuge_free(dec);
112         sistema_free(dec);
113         if (dec->error)
114                 free(dec->error);
115         free(dec);
116 }
117
118 void dec_set_error(struct declaracao *dec, char *fmt, ...)
119 {
120         va_list ap;
121         if (dec->error)
122                 free(dec->error);
123         dec->error = NULL;
124         va_start(ap, fmt);
125         vasprintf(&dec->error, fmt, ap);
126         va_end(ap);
127 }
128
129 void dec_cmd_init(void)
130 {
131         base_cmd_init();
132         contribuinte_cmd_init();
133         conjuge_cmd_init();
134         rendimento_cmd_init();
135         isento_cmd_init();
136         pagamento_cmd_init();
137         bem_cmd_init();
138         dependente_cmd_init();
139         calcula_cmd_init();
140         gera_cmd_init();
141         sistema_cmd_init();
142         help_cmd_init();
143 }