Mercurial
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 2:8a43dedbe530 | 3:2758f5527d2b |
|---|---|
| 63 hash_val = (hash_val << 5) + hash_val + c; | 63 hash_val = (hash_val << 5) + hash_val + c; |
| 64 } | 64 } |
| 65 return hash_val % p_hash_map->capacity; | 65 return hash_val % p_hash_map->capacity; |
| 66 } | 66 } |
| 67 | 67 |
| 68 void Dowa_HashMap_PushValue(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size) | 68 void Dowa_HashMap_PushValueWithType(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size, Dowa_ValueType type) |
| 69 { | 69 { |
| 70 int idx = Dowa_HashMap_GetPosition(p_hash_map, key); | 70 int idx = Dowa_HashMap_GetPosition(p_hash_map, key); |
| 71 Dowa_PHashEntry entry = p_hash_map->entries[idx]; | 71 Dowa_PHashEntry entry = p_hash_map->entries[idx]; |
| 72 if (entry) | 72 if (entry) |
| 73 { | 73 { |
| 90 { | 90 { |
| 91 perror("malloc"); | 91 perror("malloc"); |
| 92 return; | 92 return; |
| 93 } | 93 } |
| 94 entry->capacity = value_size; | 94 entry->capacity = value_size; |
| 95 entry->type = type; | |
| 95 memcpy(entry->buffer, value, value_size); | 96 memcpy(entry->buffer, value, value_size); |
| 96 p_hash_map->current_capacity++; | 97 p_hash_map->current_capacity++; |
| 98 } | |
| 99 | |
| 100 void Dowa_HashMap_PushValue(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size) | |
| 101 { | |
| 102 Dowa_HashMap_PushValueWithType(p_hash_map, key, value, value_size, DOWA_TYPE_BUFFER); | |
| 97 } | 103 } |
| 98 | 104 |
| 99 void Dowa_HashMap_PopKey(Dowa_PHashMap p_hash_map, char *key) | 105 void Dowa_HashMap_PopKey(Dowa_PHashMap p_hash_map, char *key) |
| 100 { | 106 { |
| 101 int idx = Dowa_HashMap_GetPosition(p_hash_map, key); | 107 int idx = Dowa_HashMap_GetPosition(p_hash_map, key); |
| 111 { | 117 { |
| 112 p_hash_map->current_capacity--; | 118 p_hash_map->current_capacity--; |
| 113 } | 119 } |
| 114 } | 120 } |
| 115 | 121 |
| 122 #include <stdio.h> | |
| 123 | |
| 124 void Dowa_HashMap_Print(Dowa_PHashMap map) | |
| 125 { | |
| 126 for (size_t i = 0; i < map->capacity; ++i) | |
| 127 { | |
| 128 Dowa_PHashEntry e = map->entries[i]; | |
| 129 if (!e) continue; | |
| 130 printf("%s: ", e->key); | |
| 131 switch (e->type) | |
| 132 { | |
| 133 case DOWA_TYPE_BUFFER: | |
| 134 { | |
| 135 unsigned char *p = e->buffer; | |
| 136 for (size_t j = 0; j < e->capacity; ++j) { | |
| 137 printf("%02x", p[j]); | |
| 138 } | |
| 139 printf("\n"); | |
| 140 } | |
| 141 break; | |
| 142 case DOWA_TYPE_STRING: | |
| 143 { | |
| 144 printf("%s\n", (char*)e->buffer); | |
| 145 } | |
| 146 break; | |
| 147 case DOWA_TYPE_INT: | |
| 148 { | |
| 149 printf("%d\n", *(int32_t*)e->buffer); | |
| 150 } | |
| 151 break; | |
| 152 default: | |
| 153 { | |
| 154 printf("<unknown type>\n"); | |
| 155 } | |
| 156 } | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 | |
| 161 | |
| 162 int Dowa_Cache_Folder(Dowa_PHashMap map, const char *folder_path) | |
| 163 { | |
| 164 DIR *dir = opendir(folder_path); | |
| 165 if (!dir) { | |
| 166 perror("opendir"); | |
| 167 return -1; | |
| 168 } | |
| 169 | |
| 170 struct dirent *entry; | |
| 171 while ((entry = readdir(dir))) { | |
| 172 // skip "." and ".." | |
| 173 if (entry->d_name[0] == '.' && | |
| 174 (entry->d_name[1] == '\0' || | |
| 175 (entry->d_name[1] == '.' && entry->d_name[2] == '\0'))) | |
| 176 continue; | |
| 177 | |
| 178 char fullpath[PATH_MAX]; | |
| 179 snprintf(fullpath, sizeof fullpath, "%s/%s", folder_path, entry->d_name); | |
| 180 | |
| 181 struct stat st; | |
| 182 if (stat(fullpath, &st) < 0 || !S_ISREG(st.st_mode)) | |
| 183 continue; // skip non-files or errors | |
| 184 | |
| 185 size_t size = (size_t)st.st_size; | |
| 186 FILE *f = fopen(fullpath, "rb"); | |
| 187 if (!f) | |
| 188 { | |
| 189 perror("fopen"); | |
| 190 continue; | |
| 191 } | |
| 192 | |
| 193 void *buf = malloc(size); | |
| 194 if (!buf) | |
| 195 { | |
| 196 perror("malloc"); | |
| 197 fclose(f); | |
| 198 closedir(dir); | |
| 199 return -1; | |
| 200 } | |
| 201 | |
| 202 if (fread(buf, 1, size, f) != size) | |
| 203 { | |
| 204 perror("fread"); | |
| 205 free(buf); | |
| 206 fclose(f); | |
| 207 continue; | |
| 208 } | |
| 209 fclose(f); | |
| 210 | |
| 211 // key = filename (d_name), value = file contents | |
| 212 Dowa_HashMap_PushValueWithType(map, entry->d_name, buf, size, DOWA_TYPE_STRING); | |
| 213 | |
| 214 free(buf); // Dowa_HashMap_PushValue made its own copy | |
| 215 } | |
| 216 | |
| 217 closedir(dir); | |
| 218 return 0; | |
| 219 } |