Mercurial
diff dowa/d_memory.c @ 3:2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Wed, 24 Sep 2025 18:49:09 -0700 |
| parents | 8a43dedbe530 |
| children | 3e12bf044589 |
line wrap: on
line diff
--- a/dowa/d_memory.c Wed Sep 24 13:15:32 2025 -0700 +++ b/dowa/d_memory.c Wed Sep 24 18:49:09 2025 -0700 @@ -65,7 +65,7 @@ return hash_val % p_hash_map->capacity; } -void Dowa_HashMap_PushValue(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size) +void Dowa_HashMap_PushValueWithType(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size, Dowa_ValueType type) { int idx = Dowa_HashMap_GetPosition(p_hash_map, key); Dowa_PHashEntry entry = p_hash_map->entries[idx]; @@ -92,10 +92,16 @@ return; } entry->capacity = value_size; + entry->type = type; memcpy(entry->buffer, value, value_size); p_hash_map->current_capacity++; } +void Dowa_HashMap_PushValue(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size) +{ + Dowa_HashMap_PushValueWithType(p_hash_map, key, value, value_size, DOWA_TYPE_BUFFER); +} + void Dowa_HashMap_PopKey(Dowa_PHashMap p_hash_map, char *key) { int idx = Dowa_HashMap_GetPosition(p_hash_map, key); @@ -113,3 +119,101 @@ } } +#include <stdio.h> + +void Dowa_HashMap_Print(Dowa_PHashMap map) +{ + for (size_t i = 0; i < map->capacity; ++i) + { + Dowa_PHashEntry e = map->entries[i]; + if (!e) continue; + printf("%s: ", e->key); + switch (e->type) + { + case DOWA_TYPE_BUFFER: + { + unsigned char *p = e->buffer; + for (size_t j = 0; j < e->capacity; ++j) { + printf("%02x", p[j]); + } + printf("\n"); + } + break; + case DOWA_TYPE_STRING: + { + printf("%s\n", (char*)e->buffer); + } + break; + case DOWA_TYPE_INT: + { + printf("%d\n", *(int32_t*)e->buffer); + } + break; + default: + { + printf("<unknown type>\n"); + } + } + } +} + + + +int Dowa_Cache_Folder(Dowa_PHashMap map, const char *folder_path) +{ + DIR *dir = opendir(folder_path); + if (!dir) { + perror("opendir"); + return -1; + } + + struct dirent *entry; + while ((entry = readdir(dir))) { + // skip "." and ".." + if (entry->d_name[0] == '.' && + (entry->d_name[1] == '\0' || + (entry->d_name[1] == '.' && entry->d_name[2] == '\0'))) + continue; + + char fullpath[PATH_MAX]; + snprintf(fullpath, sizeof fullpath, "%s/%s", folder_path, entry->d_name); + + struct stat st; + if (stat(fullpath, &st) < 0 || !S_ISREG(st.st_mode)) + continue; // skip non-files or errors + + size_t size = (size_t)st.st_size; + FILE *f = fopen(fullpath, "rb"); + if (!f) + { + perror("fopen"); + continue; + } + + void *buf = malloc(size); + if (!buf) + { + perror("malloc"); + fclose(f); + closedir(dir); + return -1; + } + + if (fread(buf, 1, size, f) != size) + { + perror("fread"); + free(buf); + fclose(f); + continue; + } + fclose(f); + + // key = filename (d_name), value = file contents + Dowa_HashMap_PushValueWithType(map, entry->d_name, buf, size, DOWA_TYPE_STRING); + + free(buf); // Dowa_HashMap_PushValue made its own copy + } + + closedir(dir); + return 0; +}