Simplifica set_int e set_string.
[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
30 void bem_free(void *pointer)
31 {
32         struct bem *bem = pointer;
33         if (bem->descricao)
34                 free(bem->descricao);
35         /* Imóvel */
36         if (bem->logradouro)
37                 free(bem->logradouro);
38         if (bem->numero)
39                 free(bem->numero);
40         if (bem->complemento)
41                 free(bem->complemento);
42         if (bem->bairro)
43                 free(bem->bairro);
44         if (bem->cep)
45                 free(bem->cep);
46         if (bem->uf)
47                 free(bem->uf);
48         if (bem->municipio)
49                 free(bem->municipio);
50         if (bem->matricula)
51                 free(bem->matricula);
52         if (bem->registro)
53                 free(bem->registro);
54         if (bem->cartorio)
55                 free(bem->cartorio);
56
57         free(bem);
58 }
59
60 static int bem_cmp(void *p1, void *p2)
61 {
62         struct bem *b1 = p1;
63         struct bem *b2 = p2;
64         /* O bem de valor maior vem primeiro. */
65         if (b1->valor > b2->valor)
66                 return -1;
67         else if (b1->valor < b2->valor)
68                 return 1;
69         return 0;
70 }
71
72 static struct bem * bem_new(char **args)
73 {
74         struct bem *bem;
75         int r = 0;
76         bem = malloc(sizeof(*bem));
77         memset(bem, 0, sizeof(*bem));
78         r += set_int(args[1], &bem->codigo);
79         r += set_string(args[2], &bem->descricao);
80         r += set_llong(args[3], &bem->valor_anterior);
81         r += set_llong(args[4], &bem->valor);
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);
97         if (!bem)
98                 return -ENOMEM;
99         r = list_insert_ordered(&dec->bens, bem, bem_cmp);
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\n",
119                         j->codigo, j->descricao, j->valor_anterior, j->valor);
120 }
121
122 static struct cmd cmd_bem = {
123         .name = "bem",
124         .run = run_bem,
125 };
126
127 int bem_cmd_init(void)
128 {
129         cmd_add(&cmd_bem);
130         return 0;
131 }