5a789c12b2fe716c816d49eb3e9d9de0ab5d404b
[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         /* TODO: consertar set_int para funcionar como set_llong */
49         r += set_int(args, 2, &dependente->codigo);
50         dependente->nome = strdup(args[2]);
51         dependente->dn = strdup(args[3]);
52         dependente->cpf = strdup(args[4]);
53         if (!dependente->nome || !dependente->dn || !dependente->cpf) {
54                 dependente_free(dependente);
55                 return NULL;
56         }
57         if (r < 0 || dependente->codigo < 0) {
58                 dependente_free(dependente);
59                 return NULL;
60         }
61         return dependente;
62 }
63
64 static int run_dependente(struct declaracao *dec, char **args, int argc)
65 {
66         struct dependente *dependente;
67         int r;
68         if (argc != 5)
69                 return -EINVAL;
70         dependente = dependente_new(args);
71         if (!dependente)
72                 return -ENOMEM;
73         r = list_add(&dec->dependentes, dependente);
74         if (r < 0) {
75                 dependente_free(dependente);
76                 return r;
77         }
78         r = totais_add(dec, "DEPENDENTES", deducao_dependente(dec));
79         return 0;
80 }
81
82 void dependente_salva(struct declaracao *dec, FILE *f)
83 {
84         int i;
85         struct dependente *j;
86         for (i = 0; j = list_get(dec->dependentes, i); i++) {
87                 fprintf(f, "dependente %d \"%s\" \"%s\" \"%s\"\n",
88                         j->codigo, j->nome, j->dn, j->cpf);
89         }
90 }
91
92 static struct cmd cmd_dependente = {
93         .name = "dependente",
94         .run = run_dependente,
95 };
96
97 int dependente_cmd_init(void)
98 {
99         cmd_add(&cmd_dependente);
100         return 0;
101 }