Simplifica set_int e set_string.
[cascardo/declara.git] / lib / conjuge.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 "conjuge.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include "declaracao.h"
26 #include "cmd.h"
27 #include "util.h"
28
29 void conjuge_free(struct declaracao *dec)
30 {
31         if (dec->conjuge.cpf)
32                 free(dec->conjuge.cpf);
33 }
34
35 static int conjuge_parse(struct declaracao *dec, char **args)
36 {
37         int r = 0;
38
39         r += set_string(args[1], &dec->conjuge.cpf);
40         r += set_llong(args[2], &dec->conjuge.base);
41         r += set_llong(args[3], &dec->conjuge.imposto);
42         r += set_llong(args[4], &dec->conjuge.isento);
43         r += set_llong(args[5], &dec->conjuge.exclusivo);
44         r += set_llong(args[6], &dec->conjuge.rendpj_exigibilidade_suspensa);
45         r += set_llong(args[7], &dec->conjuge.total);
46         r += set_int(args[8], &dec->conjuge.entregou);
47
48         if (r < 0 || dec->conjuge.base < 0 || dec->conjuge.imposto < 0 ||
49             dec->conjuge.isento < 0 || dec->conjuge.exclusivo < 0 ||
50             dec->conjuge.rendpj_exigibilidade_suspensa < 0 ||
51             dec->conjuge.total < 0 || dec->conjuge.entregou < 0) {
52                 conjuge_free(dec);
53                 return -EINVAL;
54         }
55         return 0;
56 }
57
58 static int run_conjuge(struct declaracao *dec, char **args, int argc)
59 {
60         struct conjuge *conjuge;
61         int r;
62         if (argc != 9)
63                 return -EINVAL;
64         r = conjuge_parse(dec, args);
65         if (r < 0)
66                 return r;
67         return 0;
68 }
69
70 void conjuge_salva(struct declaracao *dec, FILE *f)
71 {
72         fprintf(f, "conjuge \"%s\" %lld %lld %lld %lld %lld %lld %lld %lld %d\n",
73                 dec->conjuge.cpf, dec->conjuge.base,
74                 dec->conjuge.imposto, dec->conjuge.isento,
75                 dec->conjuge.exclusivo,
76                 dec->conjuge.rendpj_exigibilidade_suspensa,
77                 dec->conjuge.total, dec->conjuge.entregou);
78 }
79
80 static struct cmd cmd_conjuge = {
81         .name = "conjuge",
82         .run = run_conjuge,
83 };
84
85 int conjuge_cmd_init(void)
86 {
87         cmd_add(&cmd_conjuge);
88         return 0;
89 }