diff options
| author | Sopár Adrián <adrian.sopar@protonmail.com> | 2024-06-20 09:28:14 +0200 |
|---|---|---|
| committer | Sopár Adrián <adrian.sopar@protonmail.com> | 2024-06-20 09:28:14 +0200 |
| commit | 74ea6dc86646cee9915292d73d8c7afef01ef3e0 (patch) | |
| tree | 9a58866f7765dad8ba56f1f40b1fa031e9d2687d /src/utilities.c | |
First commit. This is mostly the state of the project as I left it around the end of 2019.HEADmaster
Diffstat (limited to 'src/utilities.c')
| -rw-r--r-- | src/utilities.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/utilities.c b/src/utilities.c new file mode 100644 index 0000000..1142ec4 --- /dev/null +++ b/src/utilities.c @@ -0,0 +1,56 @@ +#include "utilities.h" +#include <string.h> +#include <time.h> + +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); +} + +/* + * Find left and right ints. + */ +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 |
