c90d17e67eed88e6ae948a7afc97abed1eb0c761
[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         if (bem->iptu)
57                 free(bem->iptu);
58
59         free(bem);
60 }
61
62 static int bem_cmp(void *p1, void *p2)
63 {
64         struct bem *b1 = p1;
65         struct bem *b2 = p2;
66         /* O bem de valor maior vem primeiro. */
67         if (b1->valor > b2->valor)
68                 return -1;
69         else if (b1->valor < b2->valor)
70                 return 1;
71         return 0;
72 }
73
74 static struct bem * bem_new(char **args)
75 {
76         struct bem *bem;
77         int r = 0;
78         bem = malloc(sizeof(*bem));
79         memset(bem, 0, sizeof(*bem));
80         r += set_int(args[1], &bem->codigo);
81         r += set_string(args[2], &bem->descricao);
82         r += set_llong(args[3], &bem->valor_anterior);
83         r += set_llong(args[4], &bem->valor);
84         if (r < 0 || bem->codigo < 0 ||
85             bem->valor_anterior < 0 || bem->valor < 0) {
86                 bem_free(bem);
87                 return NULL;
88         }
89         return bem;
90 }
91
92 static int run_bem(struct declaracao *dec, char **args, int argc)
93 {
94         struct bem *bem;
95         int r;
96         if (argc != 5)
97                 return -EINVAL;
98         bem = bem_new(args);
99         if (!bem)
100                 return -ENOMEM;
101         r = list_insert_ordered(&dec->bens, bem, bem_cmp);
102         if (r < 0) {
103                 bem_free(bem);
104                 return r;
105         }
106         r = totais_add(dec, "BENSANTERIOR", bem->valor_anterior);
107         r += totais_add(dec, "BENS", bem->valor);
108         if (r) {
109                 bem_free(bem);
110                 return r;
111         }
112         return 0;
113 }
114
115 void bem_salva(struct declaracao *dec, FILE *f)
116 {
117         int i;
118         struct bem *j;
119         for (i = 0; j = list_get(dec->bens, i); i++)
120                 fprintf(f, "bem %d \"%s\" %lld %lld\n",
121                         j->codigo, j->descricao, j->valor_anterior, j->valor);
122 }
123
124 static struct cmd cmd_bem = {
125         .name = "bem",
126         .run = run_bem,
127 };
128
129 int bem_cmd_init(void)
130 {
131         cmd_add(&cmd_bem);
132         return 0;
133 }