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