9511_project01

project 1 for algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
commit d5e4a472e68c0b102c92927864280b2337c6c5e1
parent 0d34aeb51f77386af1ddb50f5c4526f1b0f9278c
Author: klewer-martin <mk@inspiron.localdomain>
Date:   Tue, 19 Jan 2021 16:55:49 -0300

Added a couuple of source code tests that I made to see how could I
implement different functions;

Diffstat:
Atests/ISO3166-1.h | 11+++++++++++
Atests/Makefile | 13+++++++++++++
Atests/input.csv | 10++++++++++
Atests/main | 0
Atests/pointer.c | 24++++++++++++++++++++++++
Atests/readlines.c | 146+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atests/readlines.o | 0
Atests/time_translator.c | 38++++++++++++++++++++++++++++++++++++++
8 files changed, 242 insertions(+), 0 deletions(-)
diff --git a/tests/ISO3166-1.h b/tests/ISO3166-1.h
@@ -0,0 +1,11 @@
+#ifndef ISO3166 
+#define ISO3166
+
+
+#define ARG "ARGENTINA"
+#define COL "COLOMBIA"
+#define GER "GERMANY"
+
+
+
+#endif
diff --git a/tests/Makefile b/tests/Makefile
@@ -0,0 +1,13 @@
+$(CC)=gcc
+
+all: make_main
+
+
+make_main: main.o 
+    $(CC) readlines.o -o main
+
+main.o: readlines.c ISO3166-1.h
+    $(CC) -c readlines.c
+
+clean: 
+    rm -f *.c *.out
diff --git a/tests/input.csv b/tests/input.csv
@@ -0,0 +1,10 @@
+PAIS, FECHA, INFECTADOS
+32,1577880000,2342
+32,1578657600,4923
+32,1579089600,9324
+170,1577880000,8234
+170,1578657600,9234
+170,1579089600,9423
+276,1577880000,8432
+276,1579089600,9129
+276,1579521600,4214 
diff --git a/tests/main b/tests/main
Binary files differ.
diff --git a/tests/pointer.c b/tests/pointer.c
@@ -0,0 +1,24 @@
+#include <stdio.h>
+#include <string.h>
+
+void clean(char * buffer)
+{
+    puts(buffer);
+    printf("%lu\n", strlen(buffer));
+    while(*buffer != '\0')
+    {
+        (*buffer) = ' ';
+        buffer++;
+    }
+}
+
+int main (void)
+{
+    char str[] = "Hello world!";
+    puts(str);
+
+    clean(str);
+    puts(str);
+
+    return 0;
+}
diff --git a/tests/readlines.c b/tests/readlines.c
@@ -0,0 +1,146 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.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"
+
+const char date_print_format[] = "%b %Y";
+
+typedef enum {
+    OK,
+    ERROR_PRINTING,
+    ERROR_READING_FILE,
+    ERROR_NULL_POINTER,
+    ERROR_ALLOCATING_TIME
+} status_t;
+
+typedef enum {
+    PAIS,
+    DATE,
+    INFECTED
+} data_t;
+
+status_t print_country(size_t country_code);
+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); 
+
+int main(void)
+{
+    size_t line, i, j;
+    FILE *fp;
+    char buff1[] = "                          ";
+    char buff2[] = "                          ";
+    data_t data;
+
+    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++)
+    {
+        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);
+            print_date(date);
+            print_infected(infected);
+        }
+    }
+
+
+    fclose(fp);
+    return 0;
+}
+
+
+
+status_t print_country(size_t country_code)
+{
+    switch(country_code)
+    {
+        case 32: printf("Pais: "ARG"\n"); break;
+        case 170: printf("Pais: "COL"\n"); break;
+        case 276: printf("Pais: "GER"\n"); break;
+    }
+    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/tests/readlines.o b/tests/readlines.o
Binary files differ.
diff --git a/tests/time_translator.c b/tests/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;
+}