From 1589be8e641f2844540e61e601ba8f1d69f3b33d Mon Sep 17 00:00:00 2001 From: Renge Date: Wed, 16 Feb 2022 18:17:09 -0500 Subject: [PATCH] fix: refactored validargs --- hw1/src/argo.c | 42 ++++++++++++++++++++++++++++++++++-------- hw1/src/validargs.c | 32 ++++++++------------------------ 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/hw1/src/argo.c b/hw1/src/argo.c index 8af68cd..111d90d 100644 --- a/hw1/src/argo.c +++ b/hw1/src/argo.c @@ -5,6 +5,7 @@ #include "global.h" #include "debug.h" +// ARGO_VALUE *argo_read_value_helper(FILE *f, ARGO_VALUE *this); /** * @brief Read JSON input from a specified input stream, parse it, * and return a data structure representing the corresponding value. @@ -27,10 +28,35 @@ * NULL if there is any error. */ // ARGO_VALUE *argo_read_value(FILE *f) { - // TO BE IMPLEMENTED. - // abort(); +// // TO BE IMPLEMENTED. +// argo_value_storage->next = NULL; +// argo_value_storage->prev = NULL; +// return argo_read_value_helper(f, argo_value_storage); // } +// ARGO_VALUE *argo_read_value_helper(FILE *f, ARGO_VALUE *this) { +// int i = fgetc(f); +// int started = 0; +// while (i != EOF) { +// switch (i) +// { +// case '\b': +// case '\f': +// case '\r': +// case '\t': +// break; +// case '\n': +// argo_lines_read++; +// argo_chars_read = 0; +// default: +// break; +// } +// i = fgetc(f); +// } +// return this; +// } + + /** * @brief Read JSON input from a specified input stream, attempt to * parse it as a JSON string literal, and return a data structure @@ -49,10 +75,10 @@ * @return Zero if the operation is completely successful, * nonzero if there is any error. */ -int argo_read_string(ARGO_STRING *s, FILE *f) { +// int argo_read_string(ARGO_STRING *s, FILE *f) { // TO BE IMPLEMENTED. - abort(); -} + // abort(); +// } /** * @brief Read JSON input from a specified input stream, attempt to @@ -76,10 +102,10 @@ int argo_read_string(ARGO_STRING *s, FILE *f) { * @return Zero if the operation is completely successful, * nonzero if there is any error. */ -int argo_read_number(ARGO_NUMBER *n, FILE *f) { +// int argo_read_number(ARGO_NUMBER *n, FILE *f) { // TO BE IMPLEMENTED. - abort(); -} + // abort(); +// } /** * @brief Write canonical JSON representing a specified value to diff --git a/hw1/src/validargs.c b/hw1/src/validargs.c index de4bab8..33de563 100644 --- a/hw1/src/validargs.c +++ b/hw1/src/validargs.c @@ -4,7 +4,7 @@ #include "global.h" #include "debug.h" -int parse_arg(char *arg); +char parse_arg(char *arg); int str2int(char *arg); /** @@ -30,18 +30,18 @@ int validargs(int argc, char **argv) char **ptr = argv; switch (parse_arg(*(++ptr))) { - case 8: + case 'h': global_options = HELP_OPTION; isValid = 1; break; - case 4: + case 'v': if (argc == 2) { global_options = VALIDATE_OPTION; isValid = 1; } break; - case 2: + case 'c': if (argc == 2) { global_options = CANONICALIZE_OPTION; @@ -49,7 +49,7 @@ int validargs(int argc, char **argv) } else { - if (parse_arg(*(++ptr)) == 1) + if (parse_arg(*(++ptr)) == 'p') { int indented_value; if (argc == 3) { @@ -77,33 +77,17 @@ int validargs(int argc, char **argv) return -1; } -int parse_arg(char *arg) +char parse_arg(char *arg) { - int ans = -1; if (*arg == '-') { char arg1 = *(++arg), arg2 = *(++arg); if (arg2 == '\0') { - if (arg1 == 'h') - { - ans = 8; - } - else if (arg1 == 'v') - { - ans = 4; - } - else if (arg1 == 'c') - { - ans = 2; - } - else if (arg1 == 'p') - { - ans = 1; - } + return arg1; } } - return ans; + return '\0'; } int str2int(char *arg)