Insere bens e rendimentos na ordem em que aparecem.
[cascardo/declara.git] / lib / bem.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 "bem.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 #include "totais.h"
29 #include "attr.h"
30
31 void bem_free(void *pointer)
32 {
33         struct bem *bem = pointer;
34         if (bem->descricao)
35                 free(bem->descricao);
36         if (bem->attr)
37                 pmhash_del(bem->attr);
38         free(bem);
39 }
40
41 static int bem_cmp(void *p1, void *p2)
42 {
43         struct bem *b1 = p1;
44         struct bem *b2 = p2;
45         /* O bem de valor maior vem primeiro. */
46         if (b1->valor > b2->valor)
47                 return -1;
48         else if (b1->valor < b2->valor)
49                 return 1;
50         return 0;
51 }
52
53 static struct bem * bem_new(char **args, int argc)
54 {
55         struct bem *bem;
56         int r = 0;
57         int i;
58         bem = malloc(sizeof(*bem));
59         memset(bem, 0, sizeof(*bem));
60         bem->attr = pmhash_new();
61         if (!bem->attr) {
62                 bem_free(bem);
63                 return NULL;
64         }
65
66         r += set_int(args[1], &bem->codigo);
67         r += set_string(args[2], &bem->descricao);
68         r += set_llong(args[3], &bem->valor_anterior);
69         r += set_llong(args[4], &bem->valor);
70
71         for (i = 5; i < argc; i++) {
72                 printf("parsing arg %s\n", args[i]);
73                 r = attr_parse(&bem->attr, args[i]);
74                 if (r) {
75                         bem_free(bem);
76                         return NULL;
77                 }
78         }
79
80         bem->dependente = 0; /* TODO */
81
82         if (r < 0 || bem->codigo < 0 ||
83             bem->valor_anterior < 0 || bem->valor < 0) {
84                 bem_free(bem);
85                 return NULL;
86         }
87         return bem;
88 }
89
90 static int run_bem(struct declaracao *dec, char **args, int argc)
91 {
92         struct bem *bem;
93         int r;
94         if (argc < 5)
95                 return -EINVAL;
96         bem = bem_new(args, argc);
97         if (!bem)
98                 return -ENOMEM;
99         r = list_add(&dec->bens, bem);
100         if (r < 0) {
101                 bem_free(bem);
102                 return r;
103         }
104         r = totais_add(dec, "BENSANTERIOR", bem->valor_anterior);
105         r += totais_add(dec, "BENS", bem->valor);
106         if (r) {
107                 bem_free(bem);
108                 return r;
109         }
110         return 0;
111 }
112
113 void bem_salva(struct declaracao *dec, FILE *f)
114 {
115         int i;
116         struct bem *j;
117         for (i = 0; j = list_get(dec->bens, i); i++) {
118                 fprintf(f, "bem %d \"%s\" %lld %lld",
119                         j->codigo, j->descricao, j->valor_anterior, j->valor);
120                 //attr_printf(f, bem->attr);
121                 fprintf(f, "\n");
122         }
123 }
124
125 static struct cmd cmd_bem = {
126         .name = "bem",
127         .run = run_bem,
128 };
129
130 int bem_cmd_init(void)
131 {
132         cmd_add(&cmd_bem);
133         return 0;
134 }