X-Git-Url: http://git.cascardo.eti.br/?p=cascardo%2Fdeclara.git;a=blobdiff_plain;f=lib%2Fbem.c;h=57c4ef3a8acef34d20d1c5d4d30e01b60db6fd91;hp=c90d17e67eed88e6ae948a7afc97abed1eb0c761;hb=58291e53443f45fa03f600617eeb3bf93a234272;hpb=0a6ad8ab3ea763f5fbba9caa5264b43bf3f7d77d diff --git a/lib/bem.c b/lib/bem.c index c90d17e..57c4ef3 100644 --- a/lib/bem.c +++ b/lib/bem.c @@ -26,36 +26,15 @@ #include "list.h" #include "util.h" #include "totais.h" +#include "attr.h" void bem_free(void *pointer) { struct bem *bem = pointer; if (bem->descricao) free(bem->descricao); - /* Imóvel */ - if (bem->logradouro) - free(bem->logradouro); - if (bem->numero) - free(bem->numero); - if (bem->complemento) - free(bem->complemento); - if (bem->bairro) - free(bem->bairro); - if (bem->cep) - free(bem->cep); - if (bem->uf) - free(bem->uf); - if (bem->municipio) - free(bem->municipio); - if (bem->matricula) - free(bem->matricula); - if (bem->registro) - free(bem->registro); - if (bem->cartorio) - free(bem->cartorio); - if (bem->iptu) - free(bem->iptu); - + if (bem->attr) + pmhash_del(bem->attr); free(bem); } @@ -71,16 +50,33 @@ static int bem_cmp(void *p1, void *p2) return 0; } -static struct bem * bem_new(char **args) +static struct bem * bem_new(char **args, int argc) { struct bem *bem; int r = 0; + int i; bem = malloc(sizeof(*bem)); memset(bem, 0, sizeof(*bem)); + bem->attr = pmhash_new(); + if (!bem->attr) { + bem_free(bem); + return NULL; + } + r += set_int(args[1], &bem->codigo); r += set_string(args[2], &bem->descricao); r += set_llong(args[3], &bem->valor_anterior); r += set_llong(args[4], &bem->valor); + + for (i = 5; i < argc; i++) { + printf("parsing arg %s\n", args[i]); + r = attr_parse(&bem->attr, args[i]); + if (r) { + bem_free(bem); + return NULL; + } + } + if (r < 0 || bem->codigo < 0 || bem->valor_anterior < 0 || bem->valor < 0) { bem_free(bem); @@ -93,9 +89,9 @@ static int run_bem(struct declaracao *dec, char **args, int argc) { struct bem *bem; int r; - if (argc != 5) + if (argc < 5) return -EINVAL; - bem = bem_new(args); + bem = bem_new(args, argc); if (!bem) return -ENOMEM; r = list_insert_ordered(&dec->bens, bem, bem_cmp); @@ -116,9 +112,12 @@ void bem_salva(struct declaracao *dec, FILE *f) { int i; struct bem *j; - for (i = 0; j = list_get(dec->bens, i); i++) - fprintf(f, "bem %d \"%s\" %lld %lld\n", + for (i = 0; j = list_get(dec->bens, i); i++) { + fprintf(f, "bem %d \"%s\" %lld %lld", j->codigo, j->descricao, j->valor_anterior, j->valor); + //attr_printf(f, bem->attr); + fprintf(f, "\n"); + } } static struct cmd cmd_bem = {