annotate dowa/d_memory.c @ 20:0a9e67c7039a

[Seobeo] Chaning Function naming to be easily readable.
author June Park <parkjune1995@gmail.com>
date Mon, 06 Oct 2025 10:13:41 -0700
parents fa2b8af609d9
children 09def63429b9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
1 #include "dowa.h"
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
2
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
3 // --- Arena --- //
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
4 Dowa_PArena Dowa_Arena_Create(size_t capacity)
1
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
5 {
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
6 Dowa_PArena p_arena = malloc(sizeof(Dowa_Arena));
2
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
7 if (p_arena == NULL)
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
8 {
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
9 perror("malloc");
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
10 return NULL;
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
11 }
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
12 p_arena->buffer = malloc(capacity);
1
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
13 p_arena->offset = 0;
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
14 p_arena->capacity = capacity;
2
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
15 return p_arena;
1
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
16 }
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
17
2
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
18 void *Dowa_Arena_Allocate(Dowa_PArena p_arena, size_t size)
1
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
19 {
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
20 if (p_arena->offset + size > p_arena->capacity)
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
21 {
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
22 return NULL;
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
23 }
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
24 void *currnet_ptr = p_arena->buffer + p_arena->offset;
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
25 p_arena->offset += size;
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
26 return currnet_ptr;
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
27 }
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
28
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
29 void Dowa_Arena_Destory(Dowa_PArena p_arena)
1
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
30 {
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
31 if (p_arena)
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
32 {
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
33 if (p_arena->buffer)
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
34 free(p_arena->buffer);
1
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
35 free(p_arena);
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
36 }
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
37 }
2
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
38
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
39 // --- HashMap --- //
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
40 Dowa_PHashMap Dowa_HashMap_Create(size_t capacity)
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
41 {
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
42 Dowa_PHashMap p_hash_map;
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
43 p_hash_map = malloc(sizeof(Dowa_HashMap));
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
44 if (p_hash_map == NULL)
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
45 {
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
46 return NULL;
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
47 }
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
48 p_hash_map->entries = calloc(capacity, sizeof(*p_hash_map->entries));
2
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
49 if (p_hash_map->entries == NULL)
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
50 {
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
51 free(p_hash_map);
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
52 return NULL;
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
53 }
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
54 p_hash_map->capacity = capacity;
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
55 p_hash_map->current_capacity = 0;
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
56 return p_hash_map;
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
57 }
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
58
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
59 Dowa_PHashMap Dowa_HashMap_Create(size_t capacity, Dowa_PArena p_arena)
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
60 {
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
61 if (p_arena == NULL)
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
62 printf("Arena is NULL");
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
63 return Dowa_HashMap_Create(capacity);
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
64
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
65 Dowa_PHashMap p_hash_map;
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
66 p_hash_map = Dowa_Arena_Allocate(p_arena, sizeof(Dowa_HashMap));
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
67 if (p_hash_map == NULL)
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
68 {
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
69 return NULL;
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
70 }
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
71 p_hash_map->entries = Dowa_Arena_Allocate(p_arena, sizeof(*p_hash_map->entries) * capacity);
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
72 if (p_hash_map->entries == NULL)
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
73 {
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
74 free(p_hash_map);
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
75 return NULL;
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
76 }
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
77 p_hash_map->capacity = capacity;
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
78 p_hash_map->current_capacity = 0;
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
79 p_hash_map->p_arena = p_arena;
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
80 return p_hash_map;
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
81 }
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
82
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
83 void Dowa_HashMap_Destory(Dowa_PHashMap p_hash_map)
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
84 {
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
85 if (!p_hash_map) return;
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
86
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
87 if (p_hash_map->p_arena)
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
88 Dowa_Arena_Destory(p_hash_map->p_arena);
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
89 else
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
90 {
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
91 Dowa_PHashEntry entry;
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
92 if (p_hash_map->entries)
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
93 {
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
94 for (int idx=0; idx<p_hash_map->capacity; idx++)
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
95 {
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
96 entry = p_hash_map->entries[idx];
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
97 if (entry)
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
98 {
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
99 free(entry->key);
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
100 free(entry->buffer);
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
101 free(entry);
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
102 }
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
103 }
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
104 }
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
105 free(p_hash_map->entries);
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
106 }
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
107 free(p_hash_map);
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
108 }
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
109
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
110 int32 Dowa_HashMap_Get_Position(Dowa_PHashMap p_hash_map, char *key)
2
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
111 {
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
112 int32 hash_val = HASH_KEY_NUMBER;
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
113 int32 c;
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
114 while ((c = *key++))
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
115 {
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
116 hash_val = (hash_val << 5) + hash_val + c;
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
117 }
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
118 return hash_val % p_hash_map->capacity;
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
119 }
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
120
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
121 void *Dowa_HashMap_Get(Dowa_PHashMap p_hash_map, char *key)
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
122 {
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
123 int idx_foo = Dowa_HashMap_Get_Position(p_hash_map, key);
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
124 void *value = p_hash_map->entries[idx_foo];
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 6
diff changeset
125 if (value == NULL || strcmp(((Dowa_PHashEntry) value)->key, key) != 0)
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
126 {
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
127 return NULL;
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
128 }
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
129 return ((Dowa_PHashEntry) value)->buffer;
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
130 }
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
131
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
132 int32 Dowa_HashMap_Push_Value_With_Type_NoCopy(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type)
2
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
133 {
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
134 int idx = Dowa_HashMap_GetPosition(p_hash_map, key);
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
135 Dowa_PHashEntry entry = p_hash_map->entries[idx];
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
136 if (entry)
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
137 free(entry->buffer);
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
138 else
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
139 {
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
140 entry = p_hash_map->p_arena ?
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
141 Dowa_Arena_Allocate(p_hash_map->p_arena, sizeof(Dowa_HashEntry)) :
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
142 malloc(sizeof(Dowa_HashEntry));
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
143 if (entry == NULL) { perror("malloc or arena alloc"); return -1; }
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
144
2
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
145 p_hash_map->entries[idx] = entry;
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
146 p_hash_map->current_capacity++;
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
147 entry->key = strdup(key);
2
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
148 }
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
149 entry->buffer = value;
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
150 entry->capacity = value_size;
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
151 entry->type = type;
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
152 return 0;
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
153 }
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
154
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
155 int32 Dowa_HashMap_Push_Value_With_Type(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type)
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
156 {
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
157 int idx = Dowa_HashMap_GetPosition(p_hash_map, key);
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
158 Dowa_PHashEntry entry = p_hash_map->entries[idx];
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
159 if (entry)
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
160 free(entry->buffer);
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
161 else
2
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
162 {
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
163 entry = p_hash_map->p_arena ?
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
164 Dowa_Arena_Allocate(p_hash_map->p_arena, sizeof(Dowa_HashEntry)) :
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
165 malloc(sizeof(Dowa_HashEntry));
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
166 if (entry == NULL) { perror("malloc or arena alloc"); return -1; }
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
167
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
168 p_hash_map->entries[idx] = entry;
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
169 p_hash_map->current_capacity++;
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
170 entry->key = strdup(key);
2
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
171 }
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
172 entry->buffer = p_hash_map->p_arena ?
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
173 Dowa_Arena_Allocate(p_hash_map->p_arena, value_size) :
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
174 malloc(value_size);
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
175
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
176 if (entry->buffer == NULL) { perror("malloc or arena alloc"); return -1; }
2
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
177 entry->capacity = value_size;
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
178 entry->type = type;
2
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
179 memcpy(entry->buffer, value, value_size);
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
180 return 0;
2
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
181 }
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
182
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
183 void Dowa_HashMap_Push_Value(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size)
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
184 {
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
185 Dowa_HashMap_Push_Value_With_Type(p_hash_map, key, value, value_size, DOWA_HASH_MAP_TYPE_BUFFER);
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
186 }
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
187
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
188 void Dowa_HashMap_Pop_Key(Dowa_PHashMap p_hash_map, char *key)
2
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
189 {
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
190 int idx = Dowa_HashMap_GetPosition(p_hash_map, key);
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
191 Dowa_PHashEntry entry = p_hash_map->entries[idx];
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
192 if (entry)
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
193 {
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
194 free(entry->key);
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
195 free(entry->buffer);
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
196 free(entry);
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
197 }
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
198 p_hash_map->entries[idx] = NULL;
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
199 if (p_hash_map->current_capacity > 0)
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
200 {
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
201 p_hash_map->current_capacity--;
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
202 }
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
203 }
8a43dedbe530 [Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
204
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
205 void Dowa_HashMap_Print(Dowa_PHashMap map)
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
206 {
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
207 printf("\n-----------\n\n");
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
208 for (size_t i = 0; i < map->capacity; ++i)
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
209 {
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
210 Dowa_PHashEntry e = map->entries[i];
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
211 if (!e) continue;
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
212 printf("%s: ", e->key);
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
213 switch (e->type)
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
214 {
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
215 case DOWA_HASH_MAP_TYPE_BUFFER:
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
216 {
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
217 unsigned char *p = e->buffer;
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
218 for (size_t j = 0; j < e->capacity; ++j)
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
219 {
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
220 printf("%02x", p[j]);
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
221 }
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
222 printf("\n");
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
223 }
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
224 break;
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
225 case DOWA_HASH_MAP_TYPE_STRING:
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
226 {
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
227 printf("%s\n", (char*)e->buffer);
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
228 }
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
229 break;
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
230 case DOWA_HASH_MAP_TYPE_HASHMAP:
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
231 {
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
232 printf("This is a hashmap with size of %zu, and pointer %p\n", (Dowa_PHashMap)e->capacity, (void *)e);
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
233 }
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
234 break;
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
235 case DOWA_HASH_MAP_TYPE_INT:
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
236 {
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
237 printf("%d\n", *(int32*)e->buffer);
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
238 }
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
239 break;
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
240 default:
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
241 {
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
242 printf("<unknown type>\n");
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
243 }
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
244 }
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
245 }
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
246 printf("-----------\n");
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
247 }
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
248
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
249 int Dowa_HashMap_Cache_Folder(Dowa_PHashMap p_hash_map, const char *folder_path)
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
250 {
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
251 DIR *dir = opendir(folder_path);
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
252 if (!dir) { perror("opendir"); return -1; }
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
253
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
254 struct dirent *entry;
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
255 while ((entry = readdir(dir)))
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
256 {
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
257 // skip "." and ".."
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
258 if (entry->d_name[0] == '.' &&
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
259 (entry->d_name[1] == '\0' ||
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
260 (entry->d_name[1] == '.' && entry->d_name[2] == '\0')))
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
261 continue;
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
262
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
263 char fullpath[PATH_MAX];
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
264 snprintf(fullpath, sizeof fullpath, "%s/%s", folder_path, entry->d_name);
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
265
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
266 struct stat st;
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
267 if (stat(fullpath, &st) < 0) { perror("stat"); continue; }
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
268
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
269 if (S_ISREG(st.st_mode))
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
270 {
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
271 size_t size = (size_t)st.st_size;
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
272 FILE *f = fopen(fullpath, "rb");
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
273 if (!f) { perror("fopen"); continue; }
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
274
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
275 void *buf = p_hash_map->p_arena ?
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
276 Dowa_Arena_Allocate(p_hash_map->p_arena, value_size) :
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
277 malloc(value_size);
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
278 if (!buf) { perror("malloc"); fclose(f); closedir(dir); return -1; }
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
279
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
280 if (fread(buf, 1, size, f) != size)
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
281 {
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
282 perror("fread");
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
283 free(buf);
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
284 fclose(f);
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
285 continue;
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
286 }
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
287 fclose(f);
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
288
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
289 Dowa_HashMap_PushValueWithType(p_hash_map, entry->d_name, buf, size, DOWA_HASH_MAP_TYPE_STRING);
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
290 free(buf); // Dowa_HashMap_PushValue made its own copy
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
291 }
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
292 else if (S_ISDIR(st.st_mode))
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
293 {
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
294 Dowa_PHashMap p_child_map = Dowa_HashMap_Create(100, p_hash_map->p_arena);
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
295 if (!p_child_map)
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
296 {
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
297 perror("Dowa_HashMap_Create");
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
298 return -1;
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
299 }
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
300 if (Dowa_HashMap_Cache_Folder(p_child_map, fullpath) == -1)
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
301 {
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
302 perror("Dowa_HashMap_Cache_Folder");
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
303 return -1;
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
304 }
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
305
6
1e61008b9980 [Seobeo] Updated seobeo so that API is more opinionated. Added my webpage./build.sh
June Park <parkjune1995@gmail.com>
parents: 5
diff changeset
306 // Should not copy as we malloced already.
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
307 if (Dowa_HashMap_Push_Value_With_Type_NoCopy(p_hash_map, entry->d_name, p_child_map,
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
308 sizeof(p_child_map), DOWA_HASH_MAP_TYPE_HASHMAP) == -1)
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
309 {
20
0a9e67c7039a [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
310 Dowa_HashMap_Destory(p_child_map);
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
311 return -1;
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
312 }
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
313 }
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
314 }
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
315 closedir(dir);
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
316 return 0;
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 2
diff changeset
317 }