diff options
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/args.c | 83 | ||||
| -rw-r--r-- | src/utils/args.h | 28 | ||||
| -rw-r--r-- | src/utils/utilities.c | 68 | ||||
| -rw-r--r-- | src/utils/utilities.h | 42 |
4 files changed, 221 insertions, 0 deletions
diff --git a/src/utils/args.c b/src/utils/args.c new file mode 100644 index 0000000..e569b15 --- /dev/null +++ b/src/utils/args.c @@ -0,0 +1,83 @@ +#include "args.h" +#include <stdlib.h> +#include <string.h> +#include "utilities.h" + +#include <stdio.h> +enum alr process_args(int argc, char **argv, struct option *options, int opt_length, void *args) +{ + // Processing + for (int i = 1; i < argc; i++) + { + //printf("%d: Init variables.\n", i); + // Init variables. + char *arg_name = argv[i]; + char *arg_param = NULL; + bool long_name = false; + //printf("%d: Extract name.\n", i); + // Extract name. + if (arg_name[0] == '-') + arg_name++; + if (arg_name[0] == '-') + { + arg_name++; + long_name = true; + } + if (arg_name[0] == 0) + { + // Syntax error + } + + //printf("%d: Find option (%s)(%s).\n", i, arg_name, long_name ? "long" : "short"); + // Find option. + struct option *option = NULL; + for (int i = 0; i < opt_length; i++) + { + //printf("option[i]: %s\n", options[i].short_name); + if (long_name) + { + if (cmpstr_tillchar(arg_name, options[i].name, '\0', '=') == 0) + option = &options[i]; + } + else + { + if (cmpstr_tillchar(arg_name, options[i].short_name, '\0', '\0') == 0) + option = &options[i]; + } + } + if (option == NULL) + return ALR_INVALID_OPTION; + + //printf("%d: Find param.\n", i); + // Find param. + if (option->has_param) + { + if (long_name) + { + arg_param = strchr(arg_name, '='); + if (arg_param != NULL) + { + arg_param++; + if (arg_param[0] == '\0') + arg_param = NULL; + } + } + else + { + i++; + if (i < argc) + arg_param = argv[i]; + } + + if (arg_param == NULL) + { + // Error: Missing param. + } + } + + //printf("%d: Process.\n", i); + // Process. + option->handle(args, arg_param); + } + return ALR_OK; +}
\ No newline at end of file diff --git a/src/utils/args.h b/src/utils/args.h new file mode 100644 index 0000000..ddf086d --- /dev/null +++ b/src/utils/args.h @@ -0,0 +1,28 @@ +#ifndef UTILS_ARGS_H +#define UTILS_ARGS_H + +#include <stdbool.h> + +struct option +{ + char short_name[16]; + char name[32]; + bool has_param; + // first: args struct; second: param; + void (*handle)(void *, const char *); +}; + +// ALR stands for Args Load Result +enum alr +{ + ALR_OK, + ALR_INVALID_OPTION +}; + +/* + * opt_length: options length + * args: args struct + */ +enum alr process_args(int argc, char **argv, struct option *options, int opt_length, void *args); + +#endif
\ No newline at end of file diff --git a/src/utils/utilities.c b/src/utils/utilities.c new file mode 100644 index 0000000..717dce0 --- /dev/null +++ b/src/utils/utilities.c @@ -0,0 +1,68 @@ +#include "utils/utilities.h" +#include <string.h> +#include <time.h> + +bool monotonic(int a, int b, int c); + +// TODO: Handle the case when they are not equal (greater or smaller) +int cmpstr_tillchar(const char *a, const char *b, char a_until, char b_until) +{ + while (*a != 0 && *b != 0 && *a != a_until && *b != b_until) + { + if (*a != *b) + return -1; + a++; + b++; + } + return 0; +} + +static unsigned int random_number(unsigned int max) +{ + return rand() / (RAND_MAX / max); +} + +void shuffle(void *base, size_t nitems, size_t size) +{ + char *c_base = base; + char swap[size]; + for (int i = 0; i < nitems; i++) + { + char *a = c_base + (i * size); + char *b = c_base + (random_number(nitems - 1) * size); + memcpy(swap, a, size); + memcpy(a, b, size); + memcpy(b, swap, size); + } +} + +void init2d(void **array, size_t width, size_t height, size_t size) +{ + char **c_array = (char **)array; + char *start = (char *)array; + for (size_t x = 0; x < width; x++) + c_array[x] = start + (width * sizeof(int *)) + (x * height * size); +} + +size_t sizeof_2d(size_t width, size_t height, size_t size) +{ + return (width * sizeof(char *)) + (width * height * size); +} + +int find_lr_ints(char *text, char **left, char **right) +{ + *left = text; + while (**left != 0 && (**left < '0' || **left > '9')) + (*left)++; + if (**left == 0) + return 0; + + *right = text + strlen(text); + while (**right < '0' || **right > '9') + (*right)--; + + while (**right >= '0' && **right <= '9') + (*right)--; + (*right)++; + return 1; +}
\ No newline at end of file diff --git a/src/utils/utilities.h b/src/utils/utilities.h new file mode 100644 index 0000000..9447c1f --- /dev/null +++ b/src/utils/utilities.h @@ -0,0 +1,42 @@ +#ifndef UTILS_UTILITIES_H +#define UTILS_UTILITIES_H + +#include <stdlib.h> +#include <stdbool.h> + +/* + * Compare the part a before a_until and + * the part b before b_until + */ +int cmpstr_tillchar(const char *a, const char *b, char a_until, char b_until); + +//int cmpstr_tillindex(const char *a, const char *b, int a_until, int b_until); + +void shuffle(void *base, size_t nitems, size_t size); + +void init2d(void **array, size_t width, size_t height, size_t size); + +size_t sizeof_2d(size_t width, size_t height, size_t size); + +/* + * Find the first integer numbers from the left and the right + * and sets the pointers *left, *right to the first digit of + * left and right numbers respectively. + */ +int find_lr_ints(char *text, char **left, char **right); + +/* + * Returns ture, if + * a <= b <= c OR + * a >= b >= c + */ +inline bool monotonic(int a, int b, int c) +{ + if (a <= b && b <= c) + return true; + if (a >= b && b >= c) + return true; + return false; +} + +#endif
\ No newline at end of file |
