Nova versão do IRPF 2021.
[cascardo/declara.git] / lib / dependente.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 #include "dependente.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include "cmd.h"
26 #include "list.h"
27 #include "util.h"
28 #include "totais.h"
29 #include "calcula.h"
30
31 void dependente_free(void *pointer)
32 {
33         struct dependente *dependente = pointer;
34         if (dependente->nome)
35                 free(dependente->nome);
36         if (dependente->dn)
37                 free(dependente->dn);
38         if (dependente->cpf)
39                 free(dependente->cpf);
40         free(dependente);
41 }
42
43 static struct dependente * dependente_new(char **args)
44 {
45         struct dependente *dependente;
46         int r = 0;
47         dependente = malloc(sizeof(*dependente));
48         r += set_int(args[1], &dependente->codigo);
49         r += set_string(args[2], &dependente->nome);
50         r += set_string(args[3], &dependente->dn);
51         r += set_string(args[4], &dependente->cpf);
52         if (r < 0 || dependente->codigo < 0) {
53                 dependente_free(dependente);
54                 return NULL;
55         }
56         return dependente;
57 }
58
59 static int run_dependente(struct declaracao *dec, char **args, int argc)
60 {
61         struct dependente *dependente;
62         int r;
63         if (argc != 5)
64                 return -EINVAL;
65         dependente = dependente_new(args);
66         if (!dependente)
67                 return -ENOMEM;
68         r = list_add(&dec->dependentes, dependente);
69         if (r < 0) {
70                 dependente_free(dependente);
71                 return r;
72         }
73         r = totais_add(dec, "DEPENDENTES", deducao_dependente(dec));
74         return 0;
75 }
76
77 void dependente_salva(struct declaracao *dec, FILE *f)
78 {
79         int i;
80         struct dependente *j;
81         for (i = 0; j = list_get(dec->dependentes, i); i++) {
82                 fprintf(f, "dependente %d \"%s\" \"%s\" \"%s\"\n",
83                         j->codigo, j->nome, j->dn, j->cpf);
84         }
85 }
86
87 static struct cmd cmd_dependente = {
88         .name = "dependente",
89         .run = run_dependente,
90 };
91
92 int dependente_cmd_init(void)
93 {
94         cmd_add(&cmd_dependente);
95         return 0;
96 }