Skip to content
Snippets Groups Projects
Commit cd36d081 authored by orestis.malaspin's avatar orestis.malaspin
Browse files

Add result in C code

parent e558d069
Branches
No related tags found
No related merge requests found
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#define MAX_INT 100 #define MAX_INT 100
typedef enum _result { ok, err_invalid_size, err_is_null } result;
const char *usage_msg = const char *usage_msg =
"Error wrong number of arguments.\n" "Error wrong number of arguments.\n"
"Usage: ./min_list <num1> <num2> ...\n" "Usage: ./min_list <num1> <num2> ...\n"
...@@ -18,13 +20,13 @@ const char *usage_msg = ...@@ -18,13 +20,13 @@ const char *usage_msg =
int *list_find_min(int32_t *tab, int size); int *list_find_min(int32_t *tab, int size);
// Prints all the element in the tab array. // Prints all the element in the tab array.
// Returns -1 if size <= 0 // Returns err_invalid_size if size <= 0
// Returns -2 if tab is NULL // Returns err_is_null if tab is NULL
// Any other value means that everything went fine // Returns ok if everything went fine
int list_print(int32_t *tab, int size); result list_print(int32_t *tab, int size);
// Exits if the error code shows invalidity // Exits if the error code shows invalidity
void error_handling(int error_code, int32_t **tab); void error_handling(result error_code, int32_t **tab);
// Parses a string to an integer. // Parses a string to an integer.
// Returns a pointer to newly allocated data. // Returns a pointer to newly allocated data.
...@@ -57,10 +59,7 @@ int *read_input(int size, char *char_num[]) { ...@@ -57,10 +59,7 @@ int *read_input(int size, char *char_num[]) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc == 1) { if (argc == 1) {
fprintf(stderr, fprintf(stderr, "%s", usage_msg);
"Error wrong number of arguments.\n"
"%s",
usage_msg);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
...@@ -98,19 +97,19 @@ int min_i32(int32_t lhs, int32_t rhs) { ...@@ -98,19 +97,19 @@ int min_i32(int32_t lhs, int32_t rhs) {
} }
// Checks if size is valid and tab is not NULL // Checks if size is valid and tab is not NULL
int list_is_valid(int32_t *tab, int size) { result list_is_valid(int32_t *tab, int size) {
if (size <= 0) { if (size <= 0) {
return -1; return err_invalid_size;
} }
if (NULL == tab) { if (NULL == tab) {
return -2; return err_is_null;
} }
return 0; return ok;
} }
int list_print(int32_t *tab, int size) { result list_print(int32_t *tab, int size) {
int code = list_is_valid(tab, size); result code = list_is_valid(tab, size);
if (code < 0) { if (code != ok) {
return code; return code;
} }
...@@ -122,8 +121,8 @@ int list_print(int32_t *tab, int size) { ...@@ -122,8 +121,8 @@ int list_print(int32_t *tab, int size) {
} }
int32_t *list_find_min(int32_t *tab, int size) { int32_t *list_find_min(int32_t *tab, int size) {
int code = list_is_valid(tab, size); result code = list_is_valid(tab, size);
if (code < 0) { if (code != ok) {
return NULL; return NULL;
} }
...@@ -135,21 +134,21 @@ int32_t *list_find_min(int32_t *tab, int size) { ...@@ -135,21 +134,21 @@ int32_t *list_find_min(int32_t *tab, int size) {
return min; return min;
} }
void error_handling(int error_code, int32_t **tab) { void error_handling(result error_code, int32_t **tab) {
switch (error_code) { switch (error_code) {
case -1: case err_invalid_size:
fprintf(stderr, "Return value, %d. Size is <= 0.\n", error_code); fprintf(stderr, "Return value, %d. Size is <= 0.\n", error_code);
free(*tab); free(*tab);
*tab = NULL; *tab = NULL;
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
break; break;
case -2: case err_is_null:
fprintf(stderr, "Tab is NULL.\n"); fprintf(stderr, "Tab is NULL.\n");
free(*tab); free(*tab);
*tab = NULL; *tab = NULL;
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
break; break;
default: case ok:
break; break;
} }
} }
...@@ -163,7 +162,7 @@ int32_t *parse_int32(char *arg_to_transform) { ...@@ -163,7 +162,7 @@ int32_t *parse_int32(char *arg_to_transform) {
long arg = strtol(arg_to_transform, &remaining, long arg = strtol(arg_to_transform, &remaining,
10); // number is parsed in base 10 10); // number is parsed in base 10
if (*remaining != '\0' || errno != 0) { if (*remaining != '\0' || errno != 0) {
return NULL; // Empty string parsed or an error occurred return NULL; // Empty string parsed or an error occurred
} }
if (arg < INT_MIN || arg > INT_MAX) { if (arg < INT_MIN || arg > INT_MAX) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment