Adiciona suporte a linha de dependentes.
[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
29 void dependente_free(void *pointer)
30 {
31         struct dependente *dependente = pointer;
32         if (dependente->nome)
33                 free(dependente->nome);
34         if (dependente->dn)
35                 free(dependente->dn);
36         if (dependente->cpf)
37                 free(dependente->cpf);
38         free(dependente);
39 }
40
41 static struct dependente * dependente_new(char **args)
42 {
43         struct dependente *dependente;
44         int r = 0;
45         dependente = malloc(sizeof(*dependente));
46         /* TODO: consertar set_int para funcionar como set_llong */
47         r += set_int(args, 2, &dependente->codigo);
48         dependente->nome = strdup(args[2]);
49         dependente->dn = strdup(args[3]);
50         dependente->cpf = strdup(args[4]);
51         if (!dependente->nome || !dependente->dn || !dependente->cpf) {
52                 dependente_free(dependente);
53                 return NULL;
54         }
55         if (r < 0 || dependente->codigo < 0) {
56                 dependente_free(dependente);
57                 return NULL;
58         }
59         return dependente;
60 }
61
62 static int run_dependente(struct declaracao *dec, char **args, int argc)
63 {
64         struct dependente *dependente;
65         int r;
66         if (argc != 5)
67                 return -EINVAL;
68         dependente = dependente_new(args);
69         if (!dependente)
70                 return -ENOMEM;
71         r = list_add(&dec->dependentes, dependente);
72         if (r < 0) {
73                 dependente_free(dependente);
74                 return r;
75         }
76         return 0;
77 }
78
79 void dependente_salva(struct declaracao *dec, FILE *f)
80 {
81         int i;
82         struct dependente *j;
83         for (i = 0; j = list_get(dec->dependentes, i); i++) {
84                 fprintf(f, "dependente %d \"%s\" \"%s\" \"%s\"\n",
85                         j->codigo, j->nome, j->dn, j->cpf);
86         }
87 }
88
89 static struct cmd cmd_dependente = {
90         .name = "dependente",
91         .run = run_dependente,
92 };
93
94 int dependente_cmd_init(void)
95 {
96         cmd_add(&cmd_dependente);
97         return 0;
98 }