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"
29 void dependente_free(void *pointer)
31 struct dependente *dependente = pointer;
33 free(dependente->nome);
37 free(dependente->cpf);
41 static struct dependente * dependente_new(char **args)
43 struct dependente *dependente;
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);
55 if (r < 0 || dependente->codigo < 0) {
56 dependente_free(dependente);
62 static int run_dependente(struct declaracao *dec, char **args, int argc)
64 struct dependente *dependente;
68 dependente = dependente_new(args);
71 r = list_add(&dec->dependentes, dependente);
73 dependente_free(dependente);
79 void dependente_salva(struct declaracao *dec, FILE *f)
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);
89 static struct cmd cmd_dependente = {
91 .run = run_dependente,
94 int dependente_cmd_init(void)
96 cmd_add(&cmd_dependente);