2 * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
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.
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.
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.
19 #include "dependente.h"
31 void dependente_free(void *pointer)
33 struct dependente *dependente = pointer;
35 free(dependente->nome);
39 free(dependente->cpf);
43 static struct dependente * dependente_new(char **args)
45 struct dependente *dependente;
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);
57 if (r < 0 || dependente->codigo < 0) {
58 dependente_free(dependente);
64 static int run_dependente(struct declaracao *dec, char **args, int argc)
66 struct dependente *dependente;
70 dependente = dependente_new(args);
73 r = list_add(&dec->dependentes, dependente);
75 dependente_free(dependente);
78 r = totais_add(dec, "DEPENDENTES", deducao_dependente(dec));
82 void dependente_salva(struct declaracao *dec, FILE *f)
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);
92 static struct cmd cmd_dependente = {
94 .run = run_dependente,
97 int dependente_cmd_init(void)
99 cmd_add(&cmd_dependente);