Mercurial
annotate dowa/dowa.h @ 21:09def63429b9
[Dowa] Updated the naming scheme and tests.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Mon, 06 Oct 2025 10:57:30 -0700 |
| parents | 0a9e67c7039a |
| children | 947b81010aba |
| rev | line source |
|---|---|
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
1 #ifndef DOWA |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
2 #define DOWA |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
3 |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
4 #include <stdio.h> |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
5 #include <string.h> // stdup |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
6 #include <stdlib.h> // only for malloc, free, stuff |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
7 #include <assert.h> // mostly for TODO |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
8 #include <dirent.h> // some functions loop through files |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
9 |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
10 #include <sys/stat.h> |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
11 #include <limits.h> |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
12 |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
13 #include "dowa_internal.h" |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
14 |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
15 #define HASH_KEY_NUMBER 5381 // DJD hash number |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
16 #define ONE_MEGA_BYTE 1048576 |
|
18
fa2b8af609d9
[Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
17 #define TRUE 1 |
|
fa2b8af609d9
[Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
18 #define FALSE 0 |
|
fa2b8af609d9
[Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
19 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
20 // Fixed-width integer types |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
21 typedef unsigned int uint32; // 32-bit unsigned integer |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
22 typedef int int32; // 32-bit signed integer |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
23 typedef unsigned short uint16; // 16-bit unsigned integer |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
24 typedef short int16; // 16-bit signed integer |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
25 typedef unsigned char uint8; // 8-bit unsigned integer |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
26 typedef char int8; // 8-bit signed integer |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
27 typedef char boolean; // Boolean type (0 = false, nonzero = true) |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
28 |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
29 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
30 // --- Miscellaneous --- // |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
31 /* Just use atoid lmao*/ |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
32 char *Dowa_Int32ToString(uint32 value, char *buffer); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
33 |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
34 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
35 // --- Arena Allocator --- // |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
36 typedef struct { |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
37 char *buffer; |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
38 size_t offset; |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
39 size_t capacity; |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
40 } Dowa_Arena, *Dowa_PArena; |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
41 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
42 /* Creates a new arena with the specified capacity (in bytes). Returns a pointer to the arena, or NULL on failure. */ |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
43 extern Dowa_PArena Dowa_Arena_Create(size_t capacity); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
44 /* Allocates a block of memory of the given size from the arena. Returns pointer to the allocated memory, or NULL if there is insufficient space. */ |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
45 extern void *Dowa_Arena_Allocate(Dowa_PArena arena, size_t size); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
46 /* Destroys the arena and frees its underlying memory block.*/ |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
47 extern void Dowa_Arena_Destroy(Dowa_PArena arena); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
48 |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
49 |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
50 // --- HashMap --- // |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
51 typedef enum { |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
52 DOWA_HASH_MAP_TYPE_BUFFER, // Raw byte buffer |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
53 DOWA_HASH_MAP_TYPE_STRING, // Null-terminated string |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
54 DOWA_HASH_MAP_TYPE_HASHMAP, // Nested hashmap |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
55 DOWA_HASH_MAP_TYPE_INT // Integer value |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
56 } Dowa_HashMap_ValueType; |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
57 |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
58 typedef struct { |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
59 char *key; |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
60 void *buffer; |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
61 size_t capacity; |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
62 Dowa_HashMap_ValueType type; |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
63 } Dowa_HashEntry, *Dowa_PHashEntry; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
64 |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
65 typedef struct { |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
66 Dowa_PHashEntry *entries; |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
67 size_t capacity; |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
68 uint32 current_capacity; |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
69 Dowa_PArena p_arena; |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
70 } Dowa_HashMap, *Dowa_PHashMap; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
71 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
72 /* Creates a new hashmap with the given initial capacity. Returns pointer to the hashmap, or NULL on failure. */ |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
73 extern Dowa_PHashMap Dowa_HashMap_Create(size_t capacity); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
74 /* Creates a new hashmap with the given initial capacity, using the provided arena for all internal allocations. Returns pointer to the hashmap, or NULL on failure. */ |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
75 extern Dowa_PHashMap Dowa_HashMap_Create_With_Arena(size_t capacity, Dowa_PArena arena); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
76 /* Destroys the hashmap and frees all associated memory */ |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
77 extern void Dowa_HashMap_Destroy(Dowa_PHashMap p_hash_map); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
78 /* Returns the index of the entry for the specified key, or -1 if the key is not found. */ |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
79 extern int32 Dowa_HashMap_Get_Position(Dowa_PHashMap p_hash_map, const char *key); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
80 /* Retrieves the value buffer for the specified key, or NULL if the key does not exist. */ |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
81 extern void *Dowa_HashMap_Get(Dowa_PHashMap p_hash_map, const char *key); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
82 /* Inserts a copy of the given value into the hashmap under the specified key. Uses DOWA_HASH_MAP_TYPE_BUFFER as the entry type. */ |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
83 extern void Dowa_HashMap_Push_Value(Dowa_PHashMap p_hash_map, const char *key, void *value, size_t value_size); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
84 /* Inserts a copy of the given value with the specified type into the hashmap under the key. Returns the index of the new entry, or -1 on failure. */ |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
85 extern int32 Dowa_HashMap_Push_Value_With_Type(Dowa_PHashMap p_hash_map, const char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
86 /* Inserts a value pointer into the hashmap under the specified key without copying data. The caller retains ownership of the pointer. Returns the entry index, or -1 on failure. */ |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
87 extern int32 Dowa_HashMap_Push_Value_With_Type_NoCopy(Dowa_PHashMap p_hash_map, const char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
88 /* Removes the entry with the specified key from the hashmap and frees its data if owned. */ |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
89 extern void Dowa_HashMap_Pop_Key(Dowa_PHashMap p_hash_map, const char *key); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
90 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
91 // --- Utility Functions --- // |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
92 /* Prints all entries in the hashmap to stdout for debugging purposes. */ |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
93 extern void Dowa_HashMap_Print(Dowa_PHashMap map); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
94 /* Loads all files from the specified folder into the hashmap. Uses file names as keys and file contents as values. Returns 0 on success, or -1 on failure. */ |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
95 extern int Dowa_HashMap_Cache_Folder(Dowa_PHashMap 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
|
96 |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
97 |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
98 #endif |