--- /dev/null
+/*
+ * 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 "isento.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"
+
+static int isento_totais_update(struct declaracao *dec, struct isento *isento)
+{
+ int r = 0;
+ switch (isento->codigo) {
+ case 96:
+ r = totais_add(dec, "PLR", isento->valor);
+ r += totais_add(dec, "EXCLUSIVOS", isento->valor);
+ r += totais_add(dec, "EXCLUSIVOSTIT", isento->valor);
+ break;
+ case 98:
+ r = totais_add(dec, "POUPANCA", isento->valor);
+ r += totais_add(dec, "ISENTOS", isento->valor);
+ r += totais_add(dec, "ISENTOSTIT", isento->valor);
+ break;
+ }
+ return r;
+}
+
+void isento_free(void *pointer)
+{
+ struct isento *isento = pointer;
+ if (isento->cnpj)
+ free(isento->cnpj);
+ if (isento->nome)
+ free(isento->nome);
+ free(isento);
+}
+
+static int isento_cmp(void *p1, void *p2)
+{
+ struct isento *r1 = p1;
+ struct isento *r2 = p2;
+ /* O rendimento maior vem primeiro. */
+ if (r1->valor > r2->valor)
+ return -1;
+ else if (r1->valor < r2->valor)
+ return 1;
+ return 0;
+}
+
+static struct isento * isento_new(char **args)
+{
+ struct isento *isento;
+ int r = 0;
+ isento = malloc(sizeof(*isento));
+ isento->cnpj = strdup(args[2]);
+ isento->nome = strdup(args[3]);
+ /* TODO: consertar set_int para funcionar como set_llong */
+ r += set_int(args, 2, &isento->codigo);
+ r += set_llong(args[4], &isento->valor);
+ if (!isento->cnpj || !isento->nome) {
+ isento_free(isento);
+ return NULL;
+ }
+ if (r < 0 || isento->codigo < 0 ||
+ isento->valor < 0) {
+ isento_free(isento);
+ return NULL;
+ }
+ return isento;
+}
+
+static int run_isento(struct declaracao *dec, char **args, int argc)
+{
+ struct isento *isento;
+ int r;
+ if (argc != 5)
+ return -EINVAL;
+ isento = isento_new(args);
+ if (!isento)
+ return -ENOMEM;
+ r = list_insert_ordered(&dec->isentos, isento, isento_cmp);
+ if (r < 0) {
+ isento_free(isento);
+ return r;
+ }
+ r = isento_totais_update(dec, isento);
+ if (r) {
+ isento_free(isento);
+ return r;
+ }
+ return 0;
+}
+
+void isento_salva(struct declaracao *dec, FILE *f)
+{
+ int i;
+ struct isento *j;
+ for (i = 0; j = list_get(dec->isentos, i); i++)
+ fprintf(f, "isento %d \"%s\" \"%s\" %lld\n",
+ j->codigo, j->cnpj, j->nome, j->valor);
+}
+
+static struct cmd cmd_isento = {
+ .name = "isento",
+ .run = run_isento,
+};
+
+int isento_cmd_init(void)
+{
+ cmd_add(&cmd_isento);
+ return 0;
+}
+
+struct isento * isento_get(struct declaracao *dec, int codigo, int n)
+{
+ struct isento *isento;
+ int i;
+ int j = 0;
+ for (i = 0; (isento = list_get(dec->isentos, i)); i++) {
+ if (isento->codigo == codigo && j++ == n)
+ break;
+ }
+ return isento;
+}