9511_project03

project 3 for algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README LICENSE
commit 7d20b407d5adaf66abf69a8c987eb4a98cd14325
parent d6f24c8d7d39038b4042f41997881a140d2b2b36
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Fri,  9 Jul 2021 22:59:56 -0300

Added command line arguments verification

Diffstat:
MMakefile | 14++++++++++++++
Msource/cla.c | 44+++++++++++++++++++++++++++++++++++++++++---
Msource/cla.h | 10++++++++--
Msource/main.c | 10++++++++++
Msource/types.h | 2++
5 files changed, 75 insertions(+), 5 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,14 @@
+CC=gcc
+CFLAGS=-std=c99 -pedantic -Wall
+SRCFOLDER=source
+
+all: main
+
+main: main.o cla.o
+    $(CC) $(CFLAGS) main.o cla.o
+
+main.o:
+    $(CC) $(CFLAGS) -c $(SRCFOLDER)/main.c 
+
+cla.o:
+    $(CC) $(CFLAGS) -c $(SRCFOLDER)/cla.c
diff --git a/source/cla.c b/source/cla.c
@@ -1,12 +1,26 @@
 #include "cla.h"
-#include <bool.h>
+#include "types.h"
 
-status_t validate_arguments(int argc, char *argv[])
+#include <stdio.h>
+
+status_t validate_arguments(int argc,  char **argv)
 {
+    status_t st;
+
     if(argv == NULL) return ERROR_NULL_POINTER;
-    if(argc == NO_ARGS_ENTERED || argc != NORMAL_AMOUNT_ARGS)
+    if((argc == NO_ARGS_ENTERED) || (argc != NORMAL_AMOUNT_ARGS))
         return ERROR_MISSING_ARGS;
 
+    if((st = check_flags_position(argc, argv))) return st;
+
+    if((st = check_flags_repeated(argc, argv))) return st;    
+
+    return OK;
+}
+
+/* No chequea argumentos ya que se considera subfuncion de validate_arguments */
+status_t check_flags_position(int argc, char **argv)
+{
     bool prev_was_flag = 0;
     bool current_arg_is_flag = 0;
     for(size_t i = 1; i < argc; i++, prev_was_flag = current_arg_is_flag) {
@@ -15,6 +29,30 @@ status_t validate_arguments(int argc, char *argv[])
 
         if(current_arg_is_flag && prev_was_flag) return ERROR_WRONG_FLAGS;
     }
+    return OK;
+}
+
+/* Need to add a record for already founded flags */
+status_t check_flags_repeated(int argc, char **argv)
+{
+    size_t i, j, fflags_index;
+    int founded_flags[FLAGS_MAX];
 
+    /* Inicializa a -1 para evitar confuciones con 0 */
+    for(i = 0; i < FLAGS_MAX; i++) founded_flags[i] = -1;
+
+    for(i = 1, fflags_index = 0; i <= (argc - 2); i += 2) {
+        for(j = 0; j < FLAGS_MAX; j++) {
+            if(!strcmp(argv[i], available_flags[j])) {
+                for(size_t k = 0; k < FLAGS_MAX; k++) {
+                    if(founded_flags[k] == j) return ERROR_FLAG_REPEATED;
+                }
+                founded_flags[fflags_index++] = j;
+                break;
+            }
+            if((j + 1) == FLAGS_MAX) return ERROR_FLAG_NOT_FOUND;
+        }
+    }
     return OK;
 }
+
diff --git a/source/cla.h b/source/cla.h
@@ -1,13 +1,19 @@
 #ifndef CLA__H
 #define CLA__H
 
+#include <stdbool.h>
+#include <string.h>
+
 #include "types.h"
 
 #define NO_ARGS_ENTERED 1
 #define NORMAL_AMOUNT_ARGS 11
+#define FLAGS_MAX 5
 
-status_t validate_arguments(int argc, char *argv[]);
+status_t validate_arguments(int argc, char **argv);
+status_t check_flags_position(int argc, char **argv);
+status_t check_flags_repeated(int argc, char **argv);
 
-static char *available_flags[] = { "-fmt", "-out", "-in", "-ti", "-tf" };
+static const char *available_flags[FLAGS_MAX + 1] = { "-fmt", "-out", "-in", "-ti", "-tf" };
 
 #endif
diff --git a/source/main.c b/source/main.c
@@ -0,0 +1,10 @@
+#include "cla.h"
+
+int main (int argc, char *argv[])
+{
+    status_t st;
+
+    if((st = validate_arguments(argc, argv)) != OK) return st;
+
+    return 0;
+}
diff --git a/source/types.h b/source/types.h
@@ -5,6 +5,8 @@ typedef enum {
     OK,
     ERROR_MISSING_ARGS,
     ERROR_WRONG_FLAGS,
+    ERROR_FLAG_NOT_FOUND,
+    ERROR_FLAG_REPEATED,
     ERROR_NULL_POINTER
 } status_t;