Insere rendimentos de forma ordenada.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sat, 1 Aug 2015 13:37:04 +0000 (10:37 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sat, 1 Aug 2015 13:37:04 +0000 (10:37 -0300)
O cabeçalho da declaração contém os CNPJs das pessoas jurídicas que
pagaram os maiores rendimentos. Insere os rendimentos de forma ordenada,
com os maiores rendimentos primeiro, e cria uma função para retornar o
CNPJ de um rendimento dado o seu índice.

gera.c
rendimento.c
rendimento.h

diff --git a/gera.c b/gera.c
index c394fad..f77c22b 100644 (file)
--- a/gera.c
+++ b/gera.c
@@ -80,10 +80,10 @@ static void gera_header(struct declaracao *dec, FILE *f)
        fprintf(f, " "); /* RRA4 */
        fprintf(f, "%-11.11s", ""); /* CPF RRA4 */
        fprintf(f, "0000000000000"); /* TODO: Doacao ECA */
-       fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior */
-       fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior 2 */
-       fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior 3 */
-       fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior 4 */
+       fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 0)); /* TODO: CNPJ maior */
+       fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 1)); /* TODO: CNPJ maior 2 */
+       fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 2)); /* TODO: CNPJ maior 3 */
+       fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 3)); /* TODO: CNPJ maior 4 */
        fprintf(f, "%-11.11s", ""); /* CPF Dependente 1 */
        fprintf(f, "%-8.8s", ""); /* DN Dependente 1 */
        fprintf(f, "%-11.11s", ""); /* CPF Dependente 2 */
index 80311db..caa7a64 100644 (file)
@@ -38,6 +38,18 @@ void rendimento_free(void *pointer)
        free(rendimento);
 }
 
+static int rendimento_cmp(void *p1, void *p2)
+{
+       struct rendimento *r1 = p1;
+       struct rendimento *r2 = p2;
+       /* O rendimento maior vem primeiro. */
+       if (r1->rendimento > r2->rendimento)
+               return -1;
+       else if (r1->rendimento < r2->rendimento)
+               return 1;
+       return 0;
+}
+
 static struct rendimento * rendimento_new(char **args)
 {
        struct rendimento *rendimento;
@@ -73,7 +85,7 @@ static int run_rendimento(struct declaracao *dec, char **args, int argc)
        rendimento = rendimento_new(args);
        if (!rendimento)
                return -ENOMEM;
-       r = list_add(&dec->rendimento, rendimento);
+       r = list_insert_ordered(&dec->rendimento, rendimento, rendimento_cmp);
        if (r < 0) {
                rendimento_free(rendimento);
                return r;
@@ -101,3 +113,12 @@ int rendimento_cmd_init(void)
        cmd_add(&cmd_rendimento);
        return 0;
 }
+
+char * rendimento_cnpj_ordenado(struct declaracao *dec, int i)
+{
+       struct rendimento *rendimento;
+       rendimento = list_get(dec->rendimento, i);
+       if (!rendimento)
+               return "";
+       return rendimento->cnpj;
+}
index 0f183a7..2f96a2d 100644 (file)
@@ -38,4 +38,6 @@ void rendimento_free(void *pointer);
 
 int rendimento_cmd_init(void);
 
+char * rendimento_cnpj_ordenado(struct declaracao *dec, int i);
+
 #endif