Separa código em diretórios.
[cascardo/declara.git] / lib / declaracao.c
diff --git a/lib/declaracao.c b/lib/declaracao.c
new file mode 100644 (file)
index 0000000..53ff1e4
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ *  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 "declaracao.h"
+#include <stdlib.h>
+#include <string.h>
+#include "list.h"
+#include "rendimento.h"
+#include "pagamento.h"
+#include "pmhash.h"
+
+struct declaracao * declaracao_new(int ano)
+{
+       struct declaracao *dec;
+       dec = malloc(sizeof(*dec));
+       if (!dec)
+               return NULL;
+       memset(dec, 0, sizeof(*dec));
+       dec->ano = ano;
+       dec->rendimento = list_new();
+       if (!dec->rendimento)
+               goto out_rendimento;
+       dec->pagamentos = list_new();
+       if (!dec->pagamentos)
+               goto out_pagamentos;
+       dec->totais = pmhash_new();
+       if (!dec->totais)
+               goto out_totais;
+       dec->pago = 0;
+       dec->devido = 0;
+       dec->restituicao = 0;
+       dec->tipo = SIMPLES;
+       return dec;
+out_totais:
+       list_free(dec->pagamentos, pagamento_free);
+out_pagamentos:
+       list_free(dec->rendimento, rendimento_free);
+out_rendimento:
+       free(dec);
+       return NULL;
+}
+
+void declaracao_free(struct declaracao *dec)
+{
+       if (dec->cpf)
+               free(dec->cpf);
+       if (dec->nome)
+               free(dec->nome);
+       if (dec->recibo)
+               free(dec->recibo);
+       if (dec->retifica)
+               free(dec->retifica);
+       if (dec->banco)
+               free(dec->banco);
+       if (dec->agencia)
+               free(dec->agencia);
+       if (dec->contacorrente)
+               free(dec->contacorrente);
+       if (dec->dvconta)
+               free(dec->dvconta);
+       list_free(dec->rendimento, rendimento_free);
+       list_free(dec->pagamentos, pagamento_free);
+       pmhash_del(dec->totais);
+       free(dec);
+}