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

Merge branch 'c_result' into 'main'

Add result in C code

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