9511_project01

project 1 for algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
print_file.c (2728B)
   1 #include "print_file.h"
   2 
   3 const char formato_de_la_fecha[] = "%d %b %Y";
   4 
   5 int prev_month, prev_country;
   6 ulong infected_monthly;
   7 
   8 status_t print_file(FILE *dest, char **country_codes, uint *country, uint *date, uint *infected) {
   9 
  10     int month;
  11     char time_s[TIME_MAX_DIGITS];
  12     time_translator(*(date), time_s, sizeof(time_s), "%m");
  13 
  14     month = atoi(time_s);
  15     if((prev_month == -1) || (prev_country == -1)) {
  16         prev_month = month;
  17         prev_country = *country;
  18     }
  19 
  20 //    Imprime la suma de infectados por mes cada vez que cambia el pais;
  21     if((*(country) == prev_country) && (month == prev_month)) {
  22         infected_monthly += *(infected);
  23     }    
  24     else if(*(country) != prev_country) {
  25         fprintf_infected_monthly(dest);
  26         infected_monthly = *(infected);
  27 
  28         prev_country = *(country);
  29         prev_month = month;
  30     }
  31     else if(month != prev_month) {
  32         prev_month = month;
  33 
  34         fprintf_infected_monthly(dest);
  35         infected_monthly = *(infected);
  36     }
  37 
  38 //    Imprime datos segun el archivo de entrada;
  39     fprintf_country(dest, *(country), country_codes);
  40     fprintf_date(dest, *(date));
  41     fprintf_infected(dest, *(infected), '\n');
  42     
  43     return OK;
  44 }
  45 
  46 
  47 status_t fprintf_country(FILE *dest, size_t country_code, char **country_codes)
  48 {
  49     if((country_codes == NULL) || (dest == NULL))
  50         return ERROR_NULL_POINTER;
  51 
  52     fprintf(dest, COUNTRY_PROMPT": %s\n", country_codes[country_code]);
  53     return OK;
  54 }
  55 
  56 status_t fprintf_date(FILE *dest, size_t date)
  57 {
  58     char time_c[TIME_MAX_DIGITS];
  59     status_t st;
  60 
  61     if(dest == NULL)
  62         return ERROR_NULL_POINTER;
  63 
  64     if((st = time_translator(date, time_c, sizeof(time_c), formato_de_la_fecha)) != OK)
  65         return st;
  66 
  67     fprintf(dest, "Fecha: %s\n", time_c);
  68     return OK;
  69 }
  70 
  71 status_t fprintf_infected(FILE *dest, uint infected, char newline)
  72 {
  73     if(dest == NULL)
  74         return ERROR_NULL_POINTER;
  75 
  76     fprintf(dest, "Infectados: %u\n%c", infected, newline);
  77     return OK;
  78 }
  79 
  80 
  81 //    Traduce de la fecha de formato unix a format y lo guarda en res como
  82 //    como una cadena de caracteres;
  83 status_t time_translator(time_t unix_time, char *res, size_t size, const char *format) 
  84 {
  85     if(res == NULL || format == NULL)
  86         return ERROR_NULL_POINTER;
  87 
  88     
  89     struct tm *tmp = gmtime(&unix_time);
  90 
  91     if (strftime(res, size, format, tmp) == 0) 
  92         return ERROR_ALLOCATING_TIME;
  93 
  94     return OK;
  95 }
  96 
  97 void fprintf_infected_monthly(FILE *dest) 
  98 {
  99     char guion_medio[] = "-----------";
 100 
 101     int length = snprintf( NULL, 0, "%lu", infected_monthly );
 102     char* str_infected_monthly = (char *)malloc(length + 1 * sizeof(char));
 103     snprintf( str_infected_monthly, length + 1, "%lu", infected_monthly );
 104 
 105 
 106     fprintf(dest, "Infectados por mes: %lu\n", infected_monthly);    
 107     sprintf(str_infected_monthly, "%u", 10);
 108     fprintf(dest, "-------------------%.*s\n", (length + 1), guion_medio); 
 109 
 110     free(str_infected_monthly);
 111 }