Utiliza atributos para bens.
[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         if (r < 0 || bem->codigo < 0 ||
81             bem->valor_anterior < 0 || bem->valor < 0) {
82                 bem_free(bem);
83                 return NULL;
84         }
85         return bem;
86 }
87
88 static int run_bem(struct declaracao *dec, char **args, int argc)
89 {
90         struct bem *bem;
91         int r;
92         if (argc < 5)
93                 return -EINVAL;
94         bem = bem_new(args, argc);
95         if (!bem)
96                 return -ENOMEM;
97         r = list_insert_ordered(&dec->bens, bem, bem_cmp);
98         if (r < 0) {
99                 bem_free(bem);
100                 return r;
101         }
102         r = totais_add(dec, "BENSANTERIOR", bem->valor_anterior);
103         r += totais_add(dec, "BENS", bem->valor);
104         if (r) {
105                 bem_free(bem);
106                 return r;
107         }
108         return 0;
109 }
110
111 void bem_salva(struct declaracao *dec, FILE *f)
112 {
113         int i;
114         struct bem *j;
115         for (i = 0; j = list_get(dec->bens, i); i++) {
116                 fprintf(f, "bem %d \"%s\" %lld %lld",
117                         j->codigo, j->descricao, j->valor_anterior, j->valor);
118                 //attr_printf(f, bem->attr);
119                 fprintf(f, "\n");
120         }
121 }
122
123 static struct cmd cmd_bem = {
124         .name = "bem",
125         .run = run_bem,
126 };
127
128 int bem_cmd_init(void)
129 {
130         cmd_add(&cmd_bem);
131         return 0;
132 }