Move macros de comandos para definir inteiros e strings.
[cascardo/declara.git] / rendimento.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 "rendimento.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 rendimento_free(void *pointer)
30 {
31         struct rendimento *rendimento = pointer;
32         if (rendimento->cnpj)
33                 free(rendimento->cnpj);
34         if (rendimento->nome)
35                 free(rendimento->nome);
36         if (rendimento->saida)
37                 free(rendimento->saida);
38         free(rendimento);
39 }
40
41 static struct rendimento * rendimento_new(char **args)
42 {
43         struct rendimento *rendimento;
44         int r = 0;
45         rendimento = malloc(sizeof(*rendimento));
46         rendimento->cnpj = strdup(args[1]);
47         rendimento->nome = strdup(args[2]);
48         rendimento->saida = strdup(args[7]);
49         r += set_llong(args[3], &rendimento->rendimento);
50         r += set_llong(args[4], &rendimento->previdencia);
51         r += set_llong(args[5], &rendimento->decimoterceiro);
52         r += set_llong(args[6], &rendimento->imposto);
53         if (!rendimento->cnpj || !rendimento->nome || !rendimento->saida) {
54                 rendimento_free(rendimento);
55                 return NULL;
56         }
57         if (r < 0 || rendimento->rendimento < 0 || rendimento->previdencia < 0 ||
58             rendimento->decimoterceiro < 0 || rendimento->imposto < 0) {
59                 rendimento_free(rendimento);
60                 return NULL;
61         }
62         return rendimento;
63 }
64
65 static int run_rendimento(struct declaracao *dec, char **args, int argc)
66 {
67         struct rendimento *rendimento;
68         int r;
69         if (argc != 8)
70                 return -EINVAL;
71         rendimento = rendimento_new(args);
72         if (!rendimento)
73                 return -ENOMEM;
74         r = list_add(&dec->rendimento, rendimento);
75         if (r < 0) {
76                 rendimento_free(rendimento);
77                 return r;
78         }
79         return 0;
80 }
81
82 void rendimento_dump(struct declaracao *dec)
83 {
84         int i;
85         struct rendimento *j;
86         for (i = 0; j = list_get(dec->rendimento, i); i++)
87                 printf("rendimento: %s %s %013lld %013lld %013lld %013lld %s\n",
88                         j->cnpj, j->nome, j->rendimento, j->previdencia,
89                         j->decimoterceiro, j->imposto, j->saida);
90 }
91
92 static struct cmd cmd_rendimento = {
93         .name = "rendimento",
94         .run = run_rendimento,
95 };
96
97 int rendimento_cmd_init(void)
98 {
99         cmd_add(&cmd_rendimento);
100         return 0;
101 }