From c454374b73b748dd711554b275ffaf62001b1fc9 Mon Sep 17 00:00:00 2001 From: Ákos Kőrösi Date: Mon, 18 May 2026 09:35:05 +0200 Subject: add utils.c --- exec.c | 3 +- exec.h | 2 +- lex.c | 2 +- lex.h | 2 +- parse.h | 2 +- typeutils.h | 156 ------------------------------------------------------------ utils.c | 64 +++++++++++++++++++++++++ utils.h | 102 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 172 insertions(+), 161 deletions(-) delete mode 100644 typeutils.h create mode 100644 utils.c create mode 100644 utils.h diff --git a/exec.c b/exec.c index 3a88901..061fa69 100644 --- a/exec.c +++ b/exec.c @@ -1,13 +1,14 @@ #include "lex.h" #include "parse.h" #include "exec.h" -#include "typeutils.h" +#include "utils.h" #include #include #include #include #include +#include char* get_var(char* varname, ShellState* shstate) { diff --git a/exec.h b/exec.h index e7ab064..b03c821 100644 --- a/exec.h +++ b/exec.h @@ -1,6 +1,6 @@ #pragma once -#include "typeutils.h" +#include "utils.h" #include "parse.h" diff --git a/lex.c b/lex.c index 8c64751..85b7120 100644 --- a/lex.c +++ b/lex.c @@ -6,7 +6,7 @@ #include #include "lex.h" -#include "typeutils.h" +#include "utils.h" diff --git a/lex.h b/lex.h index 7f43c8b..2f3cc8e 100644 --- a/lex.h +++ b/lex.h @@ -1,6 +1,6 @@ #pragma once -#include "typeutils.h" +#include "utils.h" typedef struct Region Region; diff --git a/parse.h b/parse.h index de06480..340fb9e 100644 --- a/parse.h +++ b/parse.h @@ -1,7 +1,7 @@ #pragma once #include "lex.h" -#include "typeutils.h" +#include "utils.h" typedef enum { STDIN_REDIR, diff --git a/typeutils.h b/typeutils.h deleted file mode 100644 index 7225fce..0000000 --- a/typeutils.h +++ /dev/null @@ -1,156 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include - - -#define DECLARE_VEC(T, Name) \ - typedef struct { \ - T **blocks; \ - size_t count; \ - size_t used; \ - size_t cap; \ - size_t block_size; \ - } Name; \ - \ - static inline Name make_##Name(size_t block_size) { \ - return (Name){ \ - .blocks = NULL, \ - .count = 0, \ - .used = 0, \ - .cap = 0, \ - .block_size = block_size, \ - }; \ - } \ - \ - static inline T *push_to_##Name(Name *vec, T item) { \ - size_t block_index = vec->count / vec->block_size; \ - size_t item_index = vec->count % vec->block_size; \ - \ - if (block_index == vec->used) { \ - if (vec->used == vec->cap) { \ - size_t new_cap = vec->cap \ - ? vec->cap * 2 \ - : 8; \ - \ - T **new_blocks = realloc( \ - vec->blocks, \ - new_cap * sizeof(T *) \ - ); \ - \ - if (!new_blocks) abort(); \ - \ - vec->blocks = new_blocks; \ - vec->cap = new_cap; \ - } \ - \ - vec->blocks[vec->used] = \ - malloc(vec->block_size * sizeof(T)); \ - \ - if (!vec->blocks[vec->used]) abort(); \ - \ - vec->used++; \ - } \ - \ - T *ptr = &vec->blocks[block_index][item_index]; \ - *ptr = item; \ - \ - vec->count++; \ - return ptr; \ - } \ - \ - static inline T* get_from_##Name(Name *vec, size_t index) { \ - if (index >= vec->count) return NULL; \ - \ - return &vec->blocks \ - [index / vec->block_size] \ - [index % vec->block_size]; \ - } - - -DECLARE_VEC(int, IntVec) - -DECLARE_VEC(char, String); -DECLARE_VEC(char*, StrVec) - - -static void append_str_to_String(String* target, const char* src_str) { - size_t len = strlen(src_str); - - for (size_t i = 0; i < len; i++) { - push_to_String(target, src_str[i]); - } -} - -static inline char* inner(String* str) { - char *out = malloc(str->count + 1); - if (!out) abort(); - - for (size_t i = 0; i < str->count; i++) { - out[i] = *get_from_String(str, i); - } - - out[str->count] = '\0'; - return out; -} - - -#define DECLARE_MAYBE(T, Name) \ - typedef struct { \ - bool some; \ - T value; \ - } Name; - -DECLARE_MAYBE(char*, MaybeStr) - - -typedef struct { - char* key; - char* val; -} StrPair; - -DECLARE_VEC(StrPair, KeyValMap) - -void set_map_pair(KeyValMap* map, char* key, char* val) { -for (int i = 0; i < map->count; i++) { - char* key_i = get_from_KeyValMap(map, i)->key; - if (key_i == NULL) continue; - if (strcmp(key_i, key) == 0) { - // TODO: maybe copy instead - strcpy( - map->blocks[i / map->block_size][i % map->block_size].val, - val - ); - break; - } - } -} - -MaybeStr get_map_value(KeyValMap* map, char* key) { - for (int i = 0; i < map->count; i++) { - char* key_i = get_from_KeyValMap(map, i)->key; - if (key_i == NULL) continue; - if (strcmp(key_i, key) == 0) { - return (MaybeStr){ - .some = true, - .value = get_from_KeyValMap(map, i)->val - }; - } - } - return (MaybeStr){.some = false}; -} - -void unset_map_key(KeyValMap* map, char* key) { - for (int i = 0; i < map->count; i++) { - char* key_i = get_from_KeyValMap(map, i)->key; - if (key_i == NULL) continue; - if (strcmp(key_i, key) == 0) { - map->blocks[i / map->block_size][i % map->block_size].key = NULL; - break; - } - } -} diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..5ecd0a8 --- /dev/null +++ b/utils.c @@ -0,0 +1,64 @@ +#include "utils.h" +#include +#include + +void append_str_to_String(String* target, const char* src_str) { + size_t len = strlen(src_str); + + for (size_t i = 0; i < len; i++) { + push_to_String(target, src_str[i]); + } +} + +char* inner(String* str) { + char *out = malloc(str->count + 1); + if (!out) abort(); + + for (size_t i = 0; i < str->count; i++) { + out[i] = *get_from_String(str, i); + } + + out[str->count] = '\0'; + return out; +} + + +void set_map_pair(KeyValMap* map, char* key, char* val) { +for (int i = 0; i < map->count; i++) { + char* key_i = get_from_KeyValMap(map, i)->key; + if (key_i == NULL) continue; + if (strcmp(key_i, key) == 0) { + // TODO: maybe copy instead + strcpy( + map->blocks[i / map->block_size][i % map->block_size].val, + val + ); + break; + } + } +} + +MaybeStr get_map_value(KeyValMap* map, char* key) { + for (int i = 0; i < map->count; i++) { + char* key_i = get_from_KeyValMap(map, i)->key; + if (key_i == NULL) continue; + if (strcmp(key_i, key) == 0) { + return (MaybeStr){ + .some = true, + .value = get_from_KeyValMap(map, i)->val + }; + } + } + return (MaybeStr){.some = false}; +} + +void unset_map_key(KeyValMap* map, char* key) { + for (int i = 0; i < map->count; i++) { + char* key_i = get_from_KeyValMap(map, i)->key; + if (key_i == NULL) continue; + if (strcmp(key_i, key) == 0) { + map->blocks[i / map->block_size][i % map->block_size].key = NULL; + break; + } + } +} diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..2463b45 --- /dev/null +++ b/utils.h @@ -0,0 +1,102 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + + +#define DECLARE_VEC(T, Name) \ + typedef struct { \ + T **blocks; \ + size_t count; \ + size_t used; \ + size_t cap; \ + size_t block_size; \ + } Name; \ + \ + static inline Name make_##Name(size_t block_size) { \ + return (Name){ \ + .blocks = NULL, \ + .count = 0, \ + .used = 0, \ + .cap = 0, \ + .block_size = block_size, \ + }; \ + } \ + \ + static inline T *push_to_##Name(Name *vec, T item) { \ + size_t block_index = vec->count / vec->block_size; \ + size_t item_index = vec->count % vec->block_size; \ + \ + if (block_index == vec->used) { \ + if (vec->used == vec->cap) { \ + size_t new_cap = vec->cap \ + ? vec->cap * 2 \ + : 8; \ + \ + T **new_blocks = realloc( \ + vec->blocks, \ + new_cap * sizeof(T *) \ + ); \ + \ + if (!new_blocks) abort(); \ + \ + vec->blocks = new_blocks; \ + vec->cap = new_cap; \ + } \ + \ + vec->blocks[vec->used] = \ + malloc(vec->block_size * sizeof(T)); \ + \ + if (!vec->blocks[vec->used]) abort(); \ + \ + vec->used++; \ + } \ + \ + T *ptr = &vec->blocks[block_index][item_index]; \ + *ptr = item; \ + \ + vec->count++; \ + return ptr; \ + } \ + \ + static inline T* get_from_##Name(Name *vec, size_t index) { \ + if (index >= vec->count) return NULL; \ + \ + return &vec->blocks \ + [index / vec->block_size] \ + [index % vec->block_size]; \ + } + + +DECLARE_VEC(int, IntVec) + +DECLARE_VEC(char, String); +DECLARE_VEC(char*, StrVec) + + +void append_str_to_String(String* target, const char* src_str); +char* inner(String* str); + + + +#define DECLARE_MAYBE(T, Name) \ + typedef struct { \ + bool some; \ + T value; \ + } Name; + +DECLARE_MAYBE(char*, MaybeStr) + + +typedef struct { + char* key; + char* val; +} StrPair; +DECLARE_VEC(StrPair, KeyValMap) +void set_map_pair(KeyValMap* map, char* key, char* val); +MaybeStr get_map_value(KeyValMap* map, char* key); +void unset_map_key(KeyValMap* map, char* key); -- cgit v1.2.3