summaryrefslogtreecommitdiff
path: root/src/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
new file mode 100644
index 0000000..1f08445
--- /dev/null
+++ b/src/utils.c
@@ -0,0 +1,71 @@
+#include "utils.h"
+#include <string.h>
+#include <stdlib.h>
+
+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;
+ }
+ }
+}
+
+
+char* get_var(char* varname, ShellState* shstate) {
+ MaybeStr maybe_varval = get_map_value(&shstate->shell_vars, varname);
+ if (maybe_varval.some == true) return maybe_varval.value;
+ return getenv(varname);
+}