Suporta bens móveis.
[cascardo/declara.git] / lib / bem.c
diff --git a/lib/bem.c b/lib/bem.c
new file mode 100644 (file)
index 0000000..70828ba
--- /dev/null
+++ b/lib/bem.c
@@ -0,0 +1,135 @@
+/*
+ *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "bem.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#include "cmd.h"
+#include "list.h"
+#include "util.h"
+#include "totais.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);
+
+       free(bem);
+}
+
+static int bem_cmp(void *p1, void *p2)
+{
+       struct bem *b1 = p1;
+       struct bem *b2 = p2;
+       /* O bem de valor maior vem primeiro. */
+       if (b1->valor > b2->valor)
+               return -1;
+       else if (b1->valor < b2->valor)
+               return 1;
+       return 0;
+}
+
+static struct bem * bem_new(char **args)
+{
+       struct bem *bem;
+       int r = 0;
+       bem = malloc(sizeof(*bem));
+       bem->descricao = strdup(args[2]);
+       /* TODO: consertar set_int para funcionar como set_llong */
+       r += set_int(args, 2, &bem->codigo);
+       r += set_llong(args[3], &bem->valor_anterior);
+       r += set_llong(args[4], &bem->valor);
+       if (!bem->descricao) {
+               bem_free(bem);
+               return NULL;
+       }
+       if (r < 0 || bem->codigo < 0 ||
+           bem->valor_anterior < 0 || bem->valor < 0) {
+               bem_free(bem);
+               return NULL;
+       }
+       return bem;
+}
+
+static int run_bem(struct declaracao *dec, char **args, int argc)
+{
+       struct bem *bem;
+       int r;
+       if (argc != 5)
+               return -EINVAL;
+       bem = bem_new(args);
+       if (!bem)
+               return -ENOMEM;
+       r = list_insert_ordered(&dec->bens, bem, bem_cmp);
+       if (r < 0) {
+               bem_free(bem);
+               return r;
+       }
+       r = totais_add(dec, "BENSANTERIOR", bem->valor_anterior);
+       r += totais_add(dec, "BENS", bem->valor);
+       if (r) {
+               bem_free(bem);
+               return r;
+       }
+       return 0;
+}
+
+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",
+                       j->codigo, j->descricao, j->valor_anterior, j->valor);
+}
+
+static struct cmd cmd_bem = {
+       .name = "bem",
+       .run = run_bem,
+};
+
+int bem_cmd_init(void)
+{
+       cmd_add(&cmd_bem);
+       return 0;
+}