Utiliza atributos para bens.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sun, 28 Jun 2020 00:30:26 +0000 (21:30 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sun, 28 Jun 2020 14:26:07 +0000 (11:26 -0300)
Muitos campos são utilizados em bens, muitos deles dependendo do tipo de bem.
Ao invés de suportar uma linha com um número muito grande de parâmetros
obrigatórios a toda linha de bens, utiliza o novo sistema de atributos.

Assim, as linhas de bens ficam mais legíveis e nem todos os parâmetros serão
exigidos para todos os tipos de bens.

lib/bem.c
lib/bem.h
lib/gera.c
lib/importa.c

index c90d17e..57c4ef3 100644 (file)
--- a/lib/bem.c
+++ b/lib/bem.c
 #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 = {
index f30d2db..aa59c41 100644 (file)
--- a/lib/bem.h
+++ b/lib/bem.h
@@ -21,6 +21,7 @@
 
 #include <stdio.h>
 #include "declaracao.h"
+#include "pmhash.h"
 
 struct bem {
        int codigo;
@@ -28,22 +29,7 @@ struct bem {
        long long valor_anterior;
        long long valor;
 
-       /* Endereço Imóvel */
-       char *logradouro;
-       char *numero;
-       char *complemento;
-       char *bairro;
-       char *cep;
-       char *uf;
-       int cd_municipio;
-       char *municipio;
-
-       /* Imóvel */
-       char *matricula;
-       char *registro;
-       long long area;
-       char *cartorio;
-       char *iptu;
+       struct pmhash *attr;
 };
 
 void bem_salva(struct declaracao *dec, FILE *f);
index d57a321..214037c 100644 (file)
@@ -33,6 +33,7 @@
 #include "totais.h"
 #include "sistema.h"
 #include "ano.h"
+#include "attr.h"
 
 static void gera_header(struct declaracao *dec, FILE *f)
 {
@@ -956,22 +957,22 @@ static void gera_bem(struct declaracao *dec, FILE *f)
        fprintf(f, "%013lld", b->valor);
 
        /* Imóvel */
-       fprintf(f, "%-40.40s", b->logradouro ?: "");
-       fprintf(f, "%-6.6s", b->numero ?: "");
-       fprintf(f, "%-40.40s", b->complemento ?: "");
-       fprintf(f, "%-40.40s", b->bairro ?: "");
-       fprintf(f, "%-9.9s", b->cep ?: "");
-       fprintf(f, "%-2.2s", b->uf ?: "");
-       fprintf(f, "%04d", b->cd_municipio);
-       fprintf(f, "%-40.40s", b->municipio ?: "");
+       attr_out(f, b->attr, "logradouro", 40);
+       attr_out(f, b->attr, "numero", 6);
+       attr_out(f, b->attr, "complemento", 40);
+       attr_out(f, b->attr, "bairro", 40);
+       attr_out(f, b->attr, "cep", 9);
+       attr_out(f, b->attr, "uf", 2);
+       attr_out(f, b->attr, "cd_municipio", 4);
+       attr_out(f, b->attr, "municipio", 40);
        /* FIXME: Registro de imóveis, Nao (0), Sim (1), Vazio (2) */
-       fprintf(f, "%d", b->registro ? 1 : 2);
-       fprintf(f, "%-40.40s", b->matricula ?: "");
-       fprintf(f, "%-40.40s", b->registro ?: "");
-       fprintf(f, "%011lld", b->area);
+       fprintf(f, "%d", attr_get(b->attr, "registro") ? 1 : 2);
+       attr_out(f, b->attr, "matricula", 40);
+       attr_out(f, b->attr, "registro", 40);
+       attr_out(f, b->attr, "area", 11);
        /* FIXME: Area, M2 (0), Ha (1), Vazio (2) */
-       fprintf(f, "%d", (b->area == 0) ? 2 : 0);
-       fprintf(f, "%-60.60s", b->cartorio ?: "");
+       fprintf(f, "%d", attr_get(b->attr, "area") ? 0 : 2);
+       attr_out(f, b->attr, "cartorio", 60);
 
        /* Número de chave */
        fprintf(f, "%05d", dec->linhas[27] + 1);
@@ -990,7 +991,7 @@ static void gera_bem(struct declaracao *dec, FILE *f)
        }
 
        if (dec->ano >= 2019) {
-               fprintf(f, "%-30.30s", ""); /* TODO: IPTU */
+               attr_out(f, b->attr, "iptu", 30);
        }
 }
 
index c78ca36..152fbb3 100644 (file)
@@ -33,6 +33,7 @@
 #include "totais.h"
 #include "sistema.h"
 #include "ano.h"
+#include "attr.h"
 
 #define COPY(size) \
        if (size > sizeof(buffer) - 2) \
        COPY(size); \
        field = strdup(buffer);
 
+#define COPYA(attr, key, size) \
+       COPY(size); \
+       attr_set(&attr, key, buffer);
+
 #define COPYDI(field, size) \
        COPYI(dec->field, size)
 
@@ -460,22 +465,22 @@ static int importa_bem(struct declaracao *dec, char *line, size_t len)
        COPYL(b->valor, 13);
 
        /* Imóvel */
-       COPYS(b->logradouro, 40);
-       COPYS(b->numero, 6);
-       COPYS(b->complemento, 40);
-       COPYS(b->bairro, 40);
-       COPYS(b->cep, 9);
-       COPYS(b->uf, 2);
-       COPYI(b->cd_municipio, 4);
-       COPYS(b->municipio, 40);
+       COPYA(b->attr, "logradouro", 40);
+       COPYA(b->attr, "numero", 6);
+       COPYA(b->attr, "complemento", 40);
+       COPYA(b->attr, "bairro", 40);
+       COPYA(b->attr, "cep", 9);
+       COPYA(b->attr, "uf", 2);
+       COPYA(b->attr, "cd_municipio", 4);
+       COPYA(b->attr, "municipio", 40);
        /* FIXME: Registro de imóveis, Nao (0), Sim (1), Vazio (2) */
        pos += 1;
-       COPYS(b->matricula, 40);
-       COPYS(b->registro, 40);
-       COPYL(b->area, 11);
+       COPYA(b->attr, "matricula", 40);
+       COPYA(b->attr, "registro", 40);
+       COPYA(b->attr, "area", 11);
        /* FIXME: Area, M2 (0), Ha (1), Vazio (2) */
        pos += 1;
-       COPYS(b->cartorio, 60);
+       COPYA(b->attr, "cartorio", 60);
 
        /* Número de chave */
        pos += 5;