commit 92fa63cacb06ecce7ccea4c6bd6c91c4fd7993ae
parent e39cf4e127941c0833379a99abc02ab6e2a719d8
Author: klewer-martin <martin.cachari@gmail.com>
Date: Tue, 2 Feb 2021 21:34:26 -0300
Updated source code;
Diffstat:
13 files changed, 256 insertions(+), 39 deletions(-)
diff --git a/Makefile b/Makefile
@@ -3,8 +3,8 @@ $(CC)=gcc
all: main clean
-main: main.o arguments.o data.o load_country_codes.o
- $(CC) main.o arguments.o data.o load_country_codes.o -g -o main
+main: main.o arguments.o data.o load_country_codes.o readlines.o
+ $(CC) main.o arguments.o data.o load_country_codes.o readlines.o -g -o main
main.o: main.c main.h arguments.h macros.h
$(CC) -g -c main.c
@@ -18,8 +18,12 @@ data.o: main.c main.h
load_country_codes.o: load_country_codes.h main.h
$(CC) -c load_country_codes.c
+readlines.o: readlines.h main.h
+ $(CC) -c readlines.c
+
+
clean:
- rm -f *.o main
+ rm -f *.o
run:
./main -in input.csv
diff --git a/arguments.c b/arguments.c
@@ -1,5 +1,10 @@
#include "arguments.h"
+#define INPUT_ARGUMENT_FOUND_MSG "Encontre un -in\n"
+#define OUTPUT_ARGUMENT_FOUND_MSG "Encontre un -out\n"
+
+#define INPUT_FILE_NAME_MSG "\t -> Archivo de entrada: "
+#define OUTPUT_FILE_NAME_MSG "\t -> Archivo de salida: "
// Checks if the arguments are right;
status_t validate_arguments(int argc, char * argv[])
@@ -17,27 +22,28 @@ status_t set_files_name(int argc, char * argv[], char * src, char * dest)
{
int i;
status_t inputFile, outputFile;
- inputFile = outputFile = NOT_FOUND;
+ inputFile = outputFile = FILE_NOT_FOUND;
for(i = 1; i < argc; i++) {
- if(!strcmp(argv[i], SOURCE_ARGUMENT)) {
- printf("Encontre un '-in'\n");
- if(!strcmp(argv[i + 1], DESTINATION_ARGUMENT))
+ if(!strcmp(argv[i], INPUT_ARGUMENT)) {
+ printf(INPUT_ARGUMENT_FOUND_MSG);
+ if(!strcmp(argv[i + 1], OUTPUT_ARGUMENT))
return ERROR_INVOCATING_PROGRAM;
strcpy(src, argv[++i]);
- printf("\tthen input file: '%s'\n", src);
+ printf(INPUT_FILE_NAME_MSG"'%s'\n", src);
inputFile = OK;
- } else if(!strcmp(argv[i], DESTINATION_ARGUMENT)) {
- printf("Encontre un '-out'\n");
- if(!strcmp(argv[i + 1], SOURCE_ARGUMENT))
+ } else if(!strcmp(argv[i], OUTPUT_ARGUMENT)) {
+ printf(OUTPUT_ARGUMENT_FOUND_MSG);
+ if(!strcmp(argv[i + 1], INPUT_ARGUMENT))
return ERROR_INVOCATING_PROGRAM;
strcpy(dest, argv[++i]);
- printf("\tthen output file: '%s'\n", argv[i]);
+ printf(OUTPUT_FILE_NAME_MSG"'%s'\n", argv[i]);
outputFile = OK;
}
}
+
// Return error if it could get input or output file names;
if((inputFile && outputFile) != OK)
return ERROR_INVOCATING_PROGRAM;
diff --git a/input.csv b/input.csv
@@ -8,3 +8,18 @@ PAIS, FECHA, INFECTADOS
276,1577880000,8432
276,1579089600,9129
276,1579521600,4214
+834,1577880000,8432
+764,1579089600,9129
+704,1579521600,4214
+504,1577880000,2342
+104,1578657600,4923
+480,1579089600,9324
+484,1577880000,8234
+496,1578657600,9234
+554,1579089600,9423
+558,1577880000,8432
+574,1579089600,9129
+512,1579521600,4214
+586,1577880000,8432
+591,1579089600,9129
+620,1579521600,4214
diff --git a/input.csv b/input2.csv
diff --git a/iso3166-1_numbers_and_countries_short.csv b/iso3166-1_numbers_and_countries_short.csv
@@ -1,22 +0,0 @@
-4,Afghanistan
-248,Aland Islands
-8,Albania
-12,Algeria
-16,American Samoa
-20,Andorra
-24,Angola
-660,Anguilla
-10,Antarctica
-28,Antigua and Barbuda
-32,Argentina
-51,Armenia
-533,Aruba
-36,Australia
-40,Austria
-31,Azerbaijan
-44,Bahamas
-48,Bahrain
-50,Bangladesh
-52,Barbados
-112,Belarus
-56,Belgium
diff --git a/load_country_codes.h b/load_country_codes.h
@@ -11,6 +11,7 @@ typedef enum {
NAME
} part_t;
+status_t clean (char *buffer, size_t size);
status_t empty_country_codes(char country_codes[COUNTRIES_NUMBER][ARRAYS_LENGTH]);
status_t load_country_codes(char country_codes[COUNTRIES_NUMBER][ARRAYS_LENGTH]);
diff --git a/macros.h b/macros.h
@@ -4,8 +4,8 @@
#define NO_CMD_ARGUMENTS 1
#define MAX_CMD_ARGUMENTS 5
-#define SOURCE_ARGUMENT "-in"
-#define DESTINATION_ARGUMENT "-out"
+#define INPUT_ARGUMENT "-in"
+#define OUTPUT_ARGUMENT "-out"
#define INITIAL_SIZE 1000
diff --git a/main.c b/main.c
@@ -2,6 +2,7 @@
#include "arguments.h"
#include "macros.h"
#include "load_country_codes.h"
+#include "readlines.h"
#define CANTIDAD_DE_DATOS 3
@@ -34,7 +35,9 @@ int main(int argc, char * argv[])
return ERROR_LOADING_COUNTRY_CODES;
- printf("the country number 32 of iso 3166 is: %s\n", country_codes[32]);
+ putchar('\n');
+
+ readlines();
fpi = fopen(src, "r");
// fpo = fopen(dest, "w");
diff --git a/main.h b/main.h
@@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <time.h>
#define COUNTRY_CODES_FILE_NAME "iso3166-1_numbers_and_countries.csv"
@@ -11,8 +12,11 @@ typedef enum {
OK,
ERROR_INVOCATING_PROGRAM,
ERROR_NULL_POINTER,
- NOT_FOUND,
- ERROR_LOADING_COUNTRY_CODES
+ FILE_NOT_FOUND,
+ ERROR_LOADING_COUNTRY_CODES,
+ ERROR_PRINTING,
+ ERROR_READING_FILE,
+ ERROR_ALLOCATING_TIME
} status_t;
#endif
diff --git a/readlines.c b/readlines.c
@@ -0,0 +1,133 @@
+#include "readlines.h"
+
+#define COUNTRY_PROMPT "Pais"
+
+const char date_print_format[] = "%d %b %Y";
+
+status_t print_country(size_t country_code, char country_codes[COUNTRIES_NUMBER][ARRAYS_LENGTH]);
+status_t print_date(size_t data);
+status_t print_infected(size_t data);
+status_t clean_buffer(char *buffer);
+status_t time_translator(time_t unix_time, char *res, size_t size);
+
+status_t readlines(void)
+{
+ size_t line, i, j;
+ FILE *fp;
+ char buff1[] = " ";
+ char buff2[] = " ";
+ data_t data;
+
+ char country_codes[COUNTRIES_NUMBER][ARRAYS_LENGTH];
+ load_country_codes(country_codes);
+
+ unsigned long country;
+ unsigned long date;
+ unsigned long infected;
+
+ if((fp = fopen(INPUT_FILE_NAME, "r")) == NULL)
+ return ERROR_READING_FILE;
+
+ for(line = 0; fgets(buff1, sizeof(buff1), fp) != NULL; line++)
+ {
+// This 'if' is to skip the first line which doesn't contain information;
+ if(line != 0) {
+ for(i = 0, j = 0, data = PAIS; buff1[i] != '\0'; i++)
+ {
+ if((buff1[i] == ',') || (buff1[i] == '\n'))
+ {
+ i++;
+ switch(data)
+ {
+ case PAIS: country = atoi(buff2); break;
+ case DATE: date = atol(buff2); j++; break;
+ case INFECTED: infected = atol(buff2); break;
+ }
+ data++;
+ j = 0;
+ clean_buffer(buff2);
+ }
+
+ switch(data)
+ {
+ case PAIS: buff2[i] = buff1[i]; break;
+ case DATE: buff2[j] = buff1[i]; j++; break;
+ case INFECTED: buff2[j] = buff1[i]; j++; break;
+
+ }
+ }
+ print_country(country, country_codes);
+// print_date(date);
+// print_infected(infected);
+ }
+ }
+
+// char country_codes[COUNTRIES_NUMBER][ARRAYS_LENGTH];
+
+// if(load_country_codes(country_codes) != OK)
+// return ERROR_LOADING_COUNTRY_CODES;
+// empty_country_codes(country_codes);
+
+// size_t i;
+// for(i = 0; i < 500; i++)
+// printf("%s", *(country_codes + i));
+
+
+ fclose(fp);
+ return OK;
+}
+
+
+
+status_t print_country(size_t country_code, char country_codes[COUNTRIES_NUMBER][ARRAYS_LENGTH])
+{
+ printf(COUNTRY_PROMPT": %s\n", country_codes[country_code]);
+ return OK;
+}
+
+status_t print_date(size_t date)
+{
+ char time_c[TIME_MAX_DIGITS];
+
+ time_translator(date, time_c, sizeof(time_c));
+ printf("Fecha: %s\n", time_c);
+
+ return OK;
+}
+
+status_t print_infected(size_t infected)
+{
+ printf("Infectados: %lu\n\n", infected);
+
+ return OK;
+}
+
+status_t clean_buffer(char *buffer)
+{
+ if(buffer == NULL)
+ return ERROR_NULL_POINTER;
+
+ while(*buffer != '\0')
+ {
+ (*buffer) = ' ';
+ buffer++;
+ }
+ return OK;
+}
+
+
+status_t time_translator(time_t unix_time, char *res, size_t size)
+{
+ const char *format = date_print_format;
+ struct tm *tmp = gmtime(&unix_time);
+
+ if (strftime(res, size, format, tmp) == 0) {
+ (void) fprintf(stderr, "strftime(3): cannot format supplied "
+ "date/time into buffer of size %u "
+ "using: '%s'\n",
+ (unsigned int)sizeof(res), format);
+ return ERROR_ALLOCATING_TIME;
+ }
+ return OK;
+}
+
diff --git a/readlines.h b/readlines.h
@@ -0,0 +1,29 @@
+#ifndef READLINES
+#define READLINES
+
+#include "main.h"
+#include "load_country_codes.h"
+
+#define INPUT_FILE_NAME "input.csv"
+#define INITIAL_SIZE 1000
+#define TIME_MAX_DIGITS 1000
+
+#define ARG "Argentina"
+#define COL "Colombia"
+#define GER "Germany"
+
+typedef enum {
+ PAIS,
+ DATE,
+ INFECTED
+} data_t;
+
+
+status_t readlines(void);
+status_t print_date(size_t data);
+status_t print_infected(size_t data);
+status_t clean_buffer(char *buffer);
+status_t time_translator(time_t unix_time, char *res, size_t size);
+status_t print_country(size_t country_code, char country_codes[COUNTRIES_NUMBER][ARRAYS_LENGTH]);
+
+#endif
diff --git a/time_translator.c b/time_translator.c
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include <time.h>
+#include <string.h>
+
+const char default_format[] = "%B %d %Y";
+
+typedef enum {
+ OK,
+ ERROR_NULL_POINTER
+} status_t;
+
+status_t date_translator(time_t unix_time, char *res, size_t size)
+{
+ const char *format = default_format;
+ struct tm *tmp = gmtime(&unix_time);
+
+ if (strftime(res, size, format, tmp) == 0) {
+ (void) fprintf(stderr, "strftime(3): cannot format supplied "
+ "date/time into buffer of size %u "
+ "using: '%s'\n",
+ (unsigned int)sizeof(res), format);
+ return 1;
+ }
+ return 0;
+}
+
+int main (void)
+{
+ char res[32];
+
+ time_t t;
+
+ t = 1577880000;
+
+ date_translator(t, res, sizeof(res));
+ puts(res);
+ return 0;
+}
diff --git a/time_translator.h b/time_translator.h
@@ -0,0 +1,6 @@
+#ifndef TIME_TRANSLATOR
+#define TIME_TRANSLATOR
+
+
+
+#endif