annotate dowa/d_string.c @ 261:b401627fc49e

Add JRPG mock flows and interactive previews Add scripted mock SSE commands, custom event forwarding, animated chat turns, full-height message navigation, and a cyberpunk resume dossier. Co-authored-by: Copilot <[email protected]>
author MrJuneJune <mrjunejune@users.noreply.github.com>
date Wed, 05 Aug 2026 20:38:32 -0700
parents 1f9877b637e9
children 04fee26ecce0
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
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
3 #ifndef MAX_STR_BUFFER
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
4 #define MAX_STR_BUFFER 1024
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
5 #endif
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
6
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
7 char *Dowa_String_Slice(char *from, size_t start, size_t end, Dowa_Arena *p_arena)
1
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
8 {
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
9 char *buffer = p_arena == NULL ?
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
10 malloc(sizeof(char) * end-start+1) : Dowa_Arena_Allocate(p_arena, end-start+1);
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
11 if (!buffer)
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
12 return NULL;
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
13
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
14 for (int32 i = 0; i < (end - start); i++)
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
15 buffer[i] = from[start + i];
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
16
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
17 buffer[end - start] = '\0';
1
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
18 return buffer;
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
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
21 char **Dowa_String_Split(char *from, char *token, int32 from_length, int32 token_length, Dowa_Arena *p_arena)
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
22 {
112
d6d578b49a19 [PostDog] Got CRUD working.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
23 if (!from)
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
24 return NULL;
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
25
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
26 int32 *token_pos_arr = NULL;
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
27 for (int32 i = 0; i < from_length; i++)
63
fff1b048dda6 [Postdog] Fixed a problem where string did not wrap.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
28 {
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
29 if (from[i] == token[0])
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
30 {
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
31 int32 curr_token_pointer = 0;
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
32 while (curr_token_pointer < token_length && (i + curr_token_pointer) < from_length)
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
33 {
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
34 if (from[i + curr_token_pointer] != token[curr_token_pointer])
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
35 break;
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
36 curr_token_pointer++;
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
37 }
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
38
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
39 if (curr_token_pointer == token_length)
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
40 Dowa_Array_Push(token_pos_arr, i);
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
41 }
63
fff1b048dda6 [Postdog] Fixed a problem where string did not wrap.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
42 }
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
43
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
44 char **splitted_strings = NULL;
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
45 int32 start = 0;
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
46 int32 num_tokens = Dowa_Array_Length(token_pos_arr);
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
47
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
48 for (int32 i = 0; i <= num_tokens; i++)
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
49 {
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
50 int32 end = (i < num_tokens) ? token_pos_arr[i] : from_length;
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
51
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
52 char *val = Dowa_String_Slice(from, start, end, p_arena);
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
53
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
54 if (p_arena)
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
55 Dowa_Array_Push_Arena(splitted_strings, val, p_arena);
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
56 else
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
57 Dowa_Array_Push(splitted_strings, val);
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
58
112
d6d578b49a19 [PostDog] Got CRUD working.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
59 if (i < num_tokens)
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
60 start = token_pos_arr[i] + token_length;
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
61 }
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
62
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
63 Dowa_Array_Free(token_pos_arr);
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
64 return splitted_strings;
63
fff1b048dda6 [Postdog] Fixed a problem where string did not wrap.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
65 }
fff1b048dda6 [Postdog] Fixed a problem where string did not wrap.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
66
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
67 int32 Dowa_String_Pos_Find(const char *from, const char *value, const size_t from_length, const size_t value_length)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
68 {
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
69 if (value == NULL || from == NULL)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
70 return -1;
63
fff1b048dda6 [Postdog] Fixed a problem where string did not wrap.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
71
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
72 for (int32 i = 0; i < from_length - value_length; i++)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
73 {
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
74 if (from[i] == value[0])
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
75 {
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
76 int32 j = 0;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
77 while (j < value_length && value[j] == from[i+j])
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
78 j++;
63
fff1b048dda6 [Postdog] Fixed a problem where string did not wrap.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
79
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
80 if (j == value_length)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
81 return i;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
82 }
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
83 }
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
84 return -1;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
85 }
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
86
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
87 char *Dowa_String_Find(const char *from, const char *value, const size_t from_length, const size_t value_length)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
88 {
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
89 if (value == NULL || from == NULL)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
90 return NULL;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
91
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
92 for (int32 i = 0; i < from_length - value_length; i++)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
93 {
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
94 if (from[i] == value[0])
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
95 {
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
96 int32 j = 0;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
97 while (j < value_length && value[j] == from[i+j])
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
98 j++;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
99
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
100 if (j == value_length)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
101 return &from[i];
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
102 }
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
103 }
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
104 return NULL;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
105 }
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
106
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
107 int32 Dowa_String_Pos_Find_Char(const char *from, int c, int32 from_length)
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
108 {
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
109 if (!from || from_length == 0)
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
110 return -1;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
111
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
112 for (int32 i = 0; i < from_length; i++)
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
113 {
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
114 if ((int)from[i] == c)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
115 return i;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
116 }
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
117 return -1;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
118 }
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
119
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
120 char *Dowa_String_Find_Char(const char *from, int c, int32 from_length)
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
121 {
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
122 if (!from || from_length == 0)
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
123 return NULL;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
124
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
125 for (int32 i = 0; i < from_length; i++)
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
126 {
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
127 if ((int)from[i] == c)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
128 return &from[i];
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
129 }
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
130 return NULL ;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
131 }
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 63
diff changeset
132
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
133 char *Dowa_String_Copy_Arena(char *from, Dowa_Arena *p_arena)
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
134 {
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
135 char *buffer = Dowa_Arena_Allocate(p_arena, sizeof(char*) * strlen(from) + 1);
181
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
136 if (buffer)
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
137 memcpy(buffer, from, strlen(from));
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
138 buffer[strlen(from)] = '\0';
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
139 return buffer;
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
140 }
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
141
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
142 char *Dowa_String_UUID(uint32 seed, void *buffer)
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
143 {
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
144 char *res = buffer ? buffer : malloc(sizeof(char)*37);
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
145 if (!res)
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
146 return NULL;
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
147
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
148 const char *POOL = "abcdef0123456789";
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
149 int32 i = 0;
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
150 while (i < 37)
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
151 {
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
152 if (i == 8 || i == 13 || i == 18 || i == 23)
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
153 {
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
154 res[i++] = '-';
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
155 continue;
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
156 }
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
157 seed = Dowa_Math_Random_Uint32(seed);
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
158 res[i++] = POOL[seed % 16];
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
159 }
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
160 res[36] = '\0';
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
161 return res;
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
162 }
181
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
163
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
164 // --- JSON Parser --- //
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
165
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
166 static void json_skip_ws(const char *json, int32 *pos, int32 len)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
167 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
168 while (*pos < len && (json[*pos] == ' ' || json[*pos] == '\t' ||
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
169 json[*pos] == '\n' || json[*pos] == '\r'))
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
170 (*pos)++;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
171 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
172
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
173 static char *json_parse_str(const char *json, int32 *pos, int32 len, Dowa_Arena *arena)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
174 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
175 if (*pos >= len || json[*pos] != '"')
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
176 return NULL;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
177
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
178 (*pos)++;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
179 int32 start = *pos;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
180
260
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
181 while (*pos < len)
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
182 {
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
183 if (json[*pos] == '\\' && *pos + 1 < len)
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
184 {
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
185 *pos += 2;
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
186 continue;
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
187 }
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
188 if (json[*pos] == '"')
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
189 break;
181
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
190 (*pos)++;
260
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
191 }
181
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
192
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
193 int32 slen = *pos - start;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
194 char *str = arena ? Dowa_Arena_Allocate(arena, slen + 1) : malloc(slen + 1);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
195 if (!str) return NULL;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
196
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
197 int32 j = 0;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
198 for (int32 i = start; i < *pos; i++)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
199 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
200 if (json[i] == '\\' && i + 1 < *pos)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
201 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
202 i++;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
203 switch (json[i])
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
204 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
205 case 'n': str[j++] = '\n'; break;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
206 case 't': str[j++] = '\t'; break;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
207 case 'r': str[j++] = '\r'; break;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
208 case '\\': str[j++] = '\\'; break;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
209 case '"': str[j++] = '"'; break;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
210 default: str[j++] = json[i]; break;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
211 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
212 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
213 else
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
214 str[j++] = json[i];
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
215 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
216 str[j] = '\0';
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
217
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
218 if (*pos < len && json[*pos] == '"')
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
219 (*pos)++;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
220
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
221 return str;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
222 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
223
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
224 // Forward declaration for recursion
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
225 static Dowa_JSON_Value json_parse_val(const char *json, int32 *pos, int32 len, Dowa_Arena *arena);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
226
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
227 static Dowa_JSON_Entry *json_parse_obj(const char *json, int32 *pos, int32 len, Dowa_Arena *arena)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
228 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
229 if (*pos >= len || json[*pos] != '{')
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
230 return NULL;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
231
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
232 (*pos)++;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
233 Dowa_JSON_Entry *map = NULL;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
234
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
235 while (*pos < len)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
236 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
237 json_skip_ws(json, pos, len);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
238
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
239 if (*pos >= len) break;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
240 if (json[*pos] == '}') { (*pos)++; break; }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
241 if (json[*pos] == ',') { (*pos)++; continue; }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
242
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
243 char *key = json_parse_str(json, pos, len, arena);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
244 if (!key) break;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
245
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
246 json_skip_ws(json, pos, len);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
247 if (*pos >= len || json[*pos] != ':') break;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
248 (*pos)++;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
249
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
250 Dowa_JSON_Value val = json_parse_val(json, pos, len, arena);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
251
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
252 if (arena)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
253 Dowa_HashMap_Push_Arena(map, key, val, arena);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
254 else
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
255 Dowa_HashMap_Push(map, key, val);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
256 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
257
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
258 return map;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
259 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
260
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
261 static Dowa_JSON_Value *json_parse_arr(const char *json, int32 *pos, int32 len, Dowa_Arena *arena)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
262 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
263 if (*pos >= len || json[*pos] != '[')
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
264 return NULL;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
265
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
266 (*pos)++;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
267 Dowa_JSON_Value *arr = NULL;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
268
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
269 while (*pos < len)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
270 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
271 json_skip_ws(json, pos, len);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
272
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
273 if (*pos >= len) break;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
274 if (json[*pos] == ']') { (*pos)++; break; }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
275 if (json[*pos] == ',') { (*pos)++; continue; }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
276
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
277 Dowa_JSON_Value val = json_parse_val(json, pos, len, arena);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
278
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
279 if (arena)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
280 Dowa_Array_Push_Arena(arr, val, arena);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
281 else
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
282 Dowa_Array_Push(arr, val);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
283 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
284
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
285 return arr;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
286 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
287
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
288 static Dowa_JSON_Value json_parse_val(const char *json, int32 *pos, int32 len, Dowa_Arena *arena)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
289 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
290 Dowa_JSON_Value val = {0};
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
291 json_skip_ws(json, pos, len);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
292
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
293 if (*pos >= len)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
294 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
295 val.type = DOWA_JSON_NULL;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
296 return val;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
297 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
298
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
299 char c = json[*pos];
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
300
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
301 // String
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
302 if (c == '"')
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
303 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
304 val.type = DOWA_JSON_STRING;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
305 val.str_val = json_parse_str(json, pos, len, arena);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
306 return val;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
307 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
308
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
309 // Object
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
310 if (c == '{')
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
311 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
312 val.type = DOWA_JSON_OBJECT;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
313 val.object_val = json_parse_obj(json, pos, len, arena);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
314 return val;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
315 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
316
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
317 // Array
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
318 if (c == '[')
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
319 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
320 val.type = DOWA_JSON_ARRAY;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
321 val.array_val = json_parse_arr(json, pos, len, arena);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
322 return val;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
323 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
324
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
325 // Number
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
326 if (c == '-' || (c >= '0' && c <= '9'))
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
327 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
328 val.type = DOWA_JSON_NUMBER;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
329 int32 start = *pos;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
330 while (*pos < len && (json[*pos] == '-' || json[*pos] == '+' ||
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
331 json[*pos] == '.' || json[*pos] == 'e' || json[*pos] == 'E' ||
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
332 (json[*pos] >= '0' && json[*pos] <= '9')))
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
333 (*pos)++;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
334
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
335 char tmp[64];
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
336 int32 nlen = *pos - start;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
337 if (nlen >= 64) nlen = 63;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
338 memcpy(tmp, &json[start], nlen);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
339 tmp[nlen] = '\0';
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
340 val.num_val = atof(tmp);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
341 return val;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
342 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
343
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
344 // true
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
345 if (c == 't' && *pos + 4 <= len && memcmp(&json[*pos], "true", 4) == 0)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
346 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
347 val.type = DOWA_JSON_BOOL;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
348 val.bool_val = TRUE;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
349 *pos += 4;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
350 return val;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
351 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
352
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
353 // false
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
354 if (c == 'f' && *pos + 5 <= len && memcmp(&json[*pos], "false", 5) == 0)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
355 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
356 val.type = DOWA_JSON_BOOL;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
357 val.bool_val = FALSE;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
358 *pos += 5;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
359 return val;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
360 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
361
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
362 // null
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
363 if (c == 'n' && *pos + 4 <= len && memcmp(&json[*pos], "null", 4) == 0)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
364 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
365 val.type = DOWA_JSON_NULL;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
366 *pos += 4;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
367 return val;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
368 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
369
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
370 val.type = DOWA_JSON_NULL;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
371 return val;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
372 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
373
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
374 Dowa_JSON_Value Dowa_JSON_Parse(const char *json, int32 length, Dowa_Arena *p_arena)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
375 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
376 Dowa_JSON_Value val = {0};
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
377 if (!json || length <= 0)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
378 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
379 val.type = DOWA_JSON_NULL;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
380 return val;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
381 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
382
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
383 int32 pos = 0;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
384 return json_parse_val(json, &pos, length, p_arena);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
385 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
386
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
387 Dowa_JSON_Value *Dowa_JSON_Get(Dowa_JSON_Entry *map, const char *key)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
388 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
389 if (!map || !key)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
390 return NULL;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
391
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
392 void *kv = Dowa_HashMap_Get_Ptr(map, key);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
393 if (!kv)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
394 return NULL;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
395
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
396 return &((Dowa_JSON_Entry *)kv)->value;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
397 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
398
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
399 char *Dowa_JSON_Get_String(Dowa_JSON_Entry *map, const char *key)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
400 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
401 Dowa_JSON_Value *val = Dowa_JSON_Get(map, key);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
402 if (!val || val->type != DOWA_JSON_STRING)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
403 return NULL;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
404 return val->str_val;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
405 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
406
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
407 double Dowa_JSON_Get_Number(Dowa_JSON_Entry *map, const char *key)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
408 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
409 Dowa_JSON_Value *val = Dowa_JSON_Get(map, key);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
410 if (!val || val->type != DOWA_JSON_NUMBER)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
411 return 0.0;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
412 return val->num_val;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
413 }
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
414
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
415 boolean Dowa_JSON_Get_Bool(Dowa_JSON_Entry *map, const char *key)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
416 {
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
417 Dowa_JSON_Value *val = Dowa_JSON_Get(map, key);
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
418 if (!val || val->type != DOWA_JSON_BOOL)
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
419 return FALSE;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
420 return val->bool_val;
a2720eac50ce [NPC] Adding JSON RPC protocol for MPC endpoints
June Park <parkjune1995@gmail.com>
parents: 112
diff changeset
421 }
260
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
422
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
423 char *Dowa_JSON_Escape_String(
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
424 const char *value,
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
425 size_t length,
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
426 Dowa_Arena *p_arena)
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
427 {
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
428 if (!value || !p_arena)
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
429 return NULL;
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
430 if (length == 0)
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
431 length = strlen(value);
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
432
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
433 if (length > (((size_t)-1) - 1) / 6)
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
434 return NULL;
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
435 char *escaped = Dowa_Arena_Allocate(p_arena, length * 6 + 1);
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
436 if (!escaped)
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
437 return NULL;
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
438
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
439 static const char hex[] = "0123456789abcdef";
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
440 size_t output = 0;
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
441 for (size_t i = 0; i < length; i++)
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
442 {
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
443 uint8 c = (uint8)value[i];
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
444 switch (c)
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
445 {
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
446 case '"': escaped[output++] = '\\'; escaped[output++] = '"'; break;
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
447 case '\\': escaped[output++] = '\\'; escaped[output++] = '\\'; break;
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
448 case '\b': escaped[output++] = '\\'; escaped[output++] = 'b'; break;
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
449 case '\f': escaped[output++] = '\\'; escaped[output++] = 'f'; break;
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
450 case '\n': escaped[output++] = '\\'; escaped[output++] = 'n'; break;
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
451 case '\r': escaped[output++] = '\\'; escaped[output++] = 'r'; break;
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
452 case '\t': escaped[output++] = '\\'; escaped[output++] = 't'; break;
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
453 default:
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
454 if (c < 0x20)
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
455 {
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
456 escaped[output++] = '\\';
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
457 escaped[output++] = 'u';
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
458 escaped[output++] = '0';
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
459 escaped[output++] = '0';
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
460 escaped[output++] = hex[c >> 4];
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
461 escaped[output++] = hex[c & 0x0f];
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
462 }
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
463 else
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
464 {
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
465 escaped[output++] = (char)c;
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
466 }
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
467 break;
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
468 }
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
469 }
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
470 escaped[output] = '\0';
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
471 return escaped;
1f9877b637e9 Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents: 181
diff changeset
472 }