Adiciona suporte a cĂ´njuge.
[cascardo/declara.git] / lib / conjuge.c
diff --git a/lib/conjuge.c b/lib/conjuge.c
new file mode 100644 (file)
index 0000000..a0631b2
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ *  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 "conjuge.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#include "declaracao.h"
+#include "cmd.h"
+#include "util.h"
+
+void conjuge_free(struct declaracao *dec)
+{
+       if (dec->conjuge.cpf)
+               free(dec->conjuge.cpf);
+}
+
+static int conjuge_parse(struct declaracao *dec, char **args)
+{
+       int r = 0;
+
+       dec->conjuge.cpf = strdup(args[1]);
+
+       r += set_llong(args[2], &dec->conjuge.base);
+       r += set_llong(args[3], &dec->conjuge.imposto);
+       r += set_llong(args[4], &dec->conjuge.isento);
+       r += set_llong(args[5], &dec->conjuge.exclusivo);
+       r += set_llong(args[6], &dec->conjuge.rendpj_exigibilidade_suspensa);
+       r += set_llong(args[7], &dec->conjuge.total);
+       r += set_int(args + 7, 2, &dec->conjuge.entregou);
+
+       if (!dec->conjuge.cpf) {
+               conjuge_free(dec);
+               return -ENOMEM;
+       }
+       if (r < 0 || dec->conjuge.base < 0 || dec->conjuge.imposto < 0 ||
+           dec->conjuge.isento < 0 || dec->conjuge.exclusivo < 0 ||
+           dec->conjuge.rendpj_exigibilidade_suspensa < 0 ||
+           dec->conjuge.total < 0 || dec->conjuge.entregou < 0) {
+               conjuge_free(dec);
+               return -EINVAL;
+       }
+       return 0;
+}
+
+static int run_conjuge(struct declaracao *dec, char **args, int argc)
+{
+       struct conjuge *conjuge;
+       int r;
+       if (argc != 9)
+               return -EINVAL;
+       r = conjuge_parse(dec, args);
+       if (r < 0)
+               return r;
+       return 0;
+}
+
+void conjuge_salva(struct declaracao *dec, FILE *f)
+{
+       fprintf(f, "conjuge \"%s\" %lld %lld %lld %lld %lld %lld %lld %lld %d\n",
+               dec->conjuge.cpf, dec->conjuge.base,
+               dec->conjuge.imposto, dec->conjuge.isento,
+               dec->conjuge.exclusivo,
+               dec->conjuge.rendpj_exigibilidade_suspensa,
+               dec->conjuge.total, dec->conjuge.entregou);
+}
+
+static struct cmd cmd_conjuge = {
+       .name = "conjuge",
+       .run = run_conjuge,
+};
+
+int conjuge_cmd_init(void)
+{
+       cmd_add(&cmd_conjuge);
+       return 0;
+}