Skip to content

Commit df3d3f7

Browse files
committed
Renamed TS_.. functions to GS_.. to match TypeStack renaming to GenericStack. Continued string parser function implementation
1 parent f3ff6d5 commit df3d3f7

File tree

3 files changed

+65
-44
lines changed

3 files changed

+65
-44
lines changed

inc/cJSON_GenericStack.h

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @return false if GenericStack is not empty
2525
*
2626
*/
27-
#define TS_IS_EMPTY(TS) ((TS).isEmpty)
27+
#define GS_IS_EMPTY(TS) ((TS).isEmpty)
2828
/**
2929
* @brief Macro used to check if a cJSON_GenericStack_t pointer's stack is empty.
3030
*
@@ -34,7 +34,7 @@
3434
* @return false if GenericStack is not empty
3535
*
3636
*/
37-
#define TS_PTR_IS_EMPTY(TS) ((TS)->isEmpty)
37+
#define GS_PTR_IS_EMPTY(TS) ((TS)->isEmpty)
3838

3939
/**
4040
* @brief Macro used to check if a cJSON_GenericStack_t stack is full.
@@ -45,7 +45,7 @@
4545
* @return false if GenericStack is not full
4646
*
4747
*/
48-
#define TS_IS_FULL(TS) ((TS).index == ((TS).stackSize - 1))
48+
#define GS_IS_FULL(TS) ((TS).index == ((TS).stackSize - 1))
4949
/**
5050
* @brief Macro used to check if a cJSON_GenericStack_t pointer's stack is full.
5151
*
@@ -55,22 +55,22 @@
5555
* @return false if GenericStack is not full
5656
*
5757
*/
58-
#define TS_PTR_IS_FULL(TS) ((TS)->index == ((TS)->stackSize - 1))
58+
#define GS_PTR_IS_FULL(TS) ((TS)->index == ((TS)->stackSize - 1))
5959

6060
/**
6161
* @brief Unsafe macro used to get the top item from a cJSON_GenericStack_t stack.
6262
*
6363
* @param TS cJSON_GenericStack_t struct.
6464
*
6565
*/
66-
#define TS_TOP(TS) ((TS).stack[(TS).index])
66+
#define GS_TOP(TS) ((TS).stack[(TS).index])
6767
/**
6868
* @brief Unsafe macro used to get the top item from a cJSON_GenericStack_t pointer's stack.
6969
*
7070
* @param TS cJSON_GenericStack_t struct pointer.
7171
*
7272
*/
73-
#define TS_PTR_TOP(TS) ((TS)->stack[(TS)->index])
73+
#define GS_PTR_TOP(TS) ((TS)->stack[(TS)->index])
7474

7575
#pragma endregion
7676

@@ -91,22 +91,22 @@ typedef enum cJSON_GenericStack_Result
9191
* @brief Default result.
9292
*
9393
*/
94-
TS_Ok,
94+
GS_Ok,
9595
/**
96-
* @brief Returned on TS_Push operation of a full stack.
96+
* @brief Returned on GS_Push operation of a full stack.
9797
*
9898
*/
99-
TS_StackOverflow_Error,
99+
GS_StackOverflow_Error,
100100
/**
101-
* @brief Returned on TS_Pop operation of an empty stack.
101+
* @brief Returned on GS_Pop operation of an empty stack.
102102
*
103103
*/
104-
TS_StackUnderflow_Error,
104+
GS_StackUnderflow_Error,
105105
/**
106-
* @brief Returned on TS_Top of an empty stack.
106+
* @brief Returned on GS_Top of an empty stack.
107107
*
108108
*/
109-
TS_StackIsEmpty
109+
GS_StackIsEmpty
110110
} cJSON_GenericStack_Result_t;
111111

112112
#pragma endregion
@@ -157,52 +157,52 @@ typedef struct cJSON_GenericStack
157157
* @param stackSize Size of the stack in number of items.
158158
* @return cJSON_GenericStack_t Struct with allocated stack memory.
159159
*/
160-
cJSON_GenericStack_t TS_Create(cJSON_depth_t stackSize);
160+
cJSON_GenericStack_t GS_Create(cJSON_depth_t stackSize);
161161
/**
162162
* @brief Resets and frees stack memory of a cJSON_GenericStack_t struct. Needs to be used on discarding of the struct to avoid memory leaks.
163163
*
164164
* @param TSptr Pointer to a cJSON_GenericStack_t struct.
165165
*/
166-
void TS_Delete(cJSON_GenericStack_t *TSptr);
166+
void GS_Delete(cJSON_GenericStack_t *TSptr);
167167

168168
/**
169169
* @brief Used to push a cJSON_ContainerType_t value to the stack.
170170
*
171171
* @param TSptr Pointer to a cJSON_GenericStack_t struct.
172172
* @param type cJSON_ContainerType_t type that is to be pushed to the stack.
173-
* @return cJSON_GenericStack_Result_t Returns TS_Ok by default. Returns TS_StackOverflow_Error if stack was already full before the push operation (type not pushed to stack).
173+
* @return cJSON_GenericStack_Result_t Returns GS_Ok by default. Returns GS_StackOverflow_Error if stack was already full before the push operation (type not pushed to stack).
174174
*/
175-
cJSON_GenericStack_Result_t TS_Push(cJSON_GenericStack_t *TSptr, cJSON_ContainerType_t type);
175+
cJSON_GenericStack_Result_t GS_Push(cJSON_GenericStack_t *TSptr, cJSON_ContainerType_t type);
176176
/**
177177
* @brief Used to remove the top item from the stack.
178178
*
179179
* @param TSptr Pointer to a cJSON_GenericStack_t struct.
180-
* @return cJSON_GenericStack_Result_t Returns TS_Ok by default. Returns TS_StackUnderflow_Error if stack was already empty before the pop operation (nothing changed).
180+
* @return cJSON_GenericStack_Result_t Returns GS_Ok by default. Returns GS_StackUnderflow_Error if stack was already empty before the pop operation (nothing changed).
181181
*/
182-
cJSON_GenericStack_Result_t TS_Pop(cJSON_GenericStack_t *TSptr);
182+
cJSON_GenericStack_Result_t GS_Pop(cJSON_GenericStack_t *TSptr);
183183
/**
184184
* @brief Used to retreive the top value from the stack.
185185
*
186186
* @param TSptr Pointer to a cJSON_GenericStack_t struct.
187187
* @param type Pointer to a cJSON_ContainerType_t variable, the stack's top type is to be stored in.
188-
* @return cJSON_ContainerType_t Returns TS_Ok by default. Returns TS_StackIsEmpty if stack is empty (type pointer's contents are left untouched).
188+
* @return cJSON_ContainerType_t Returns GS_Ok by default. Returns GS_StackIsEmpty if stack is empty (type pointer's contents are left untouched).
189189
*/
190-
cJSON_GenericStack_Result_t TS_Top(cJSON_GenericStack_t *TSptr, cJSON_ContainerType_t *type);
190+
cJSON_GenericStack_Result_t GS_Top(cJSON_GenericStack_t *TSptr, cJSON_ContainerType_t *type);
191191
/**
192192
* @brief Function used to check if stack is empty.
193193
*
194194
* @param TSptr Pointer to a cJSON_GenericStack_t struct.
195195
* @return true if stack is empty.
196196
* @return false if stack is not empty.
197197
*/
198-
bool TS_IsEmpty(cJSON_GenericStack_t *TSptr);
198+
bool GS_IsEmpty(cJSON_GenericStack_t *TSptr);
199199
/**
200200
* @brief Used to check if stack is full.
201201
*
202202
* @param TSptr Pointer to a cJSON_GenericStack_t struct.
203203
* @return true if stack is full.
204204
* @return false if stack is not full.
205205
*/
206-
bool TS_IsFull(cJSON_GenericStack_t *TSptr);
206+
bool GS_IsFull(cJSON_GenericStack_t *TSptr);
207207

208208
#pragma endregion

src/cJSON.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,29 +92,44 @@ cJSON_Result_t cJSON_parseStr(cJSON_Generic_t *GObjPtr, const char *str)
9292
// Check if GObjPtr is allocated. If so delete old data structure
9393
if (GObjPtr->dataContainer != NULL) cJSON_delGenObj(*GObjPtr);
9494

95+
// Create object stack
96+
cJSON_GenericStack_t ObjectStack = GS_Create(CJSON_MAX_DEPTH);
9597

9698

97-
enum valueEnironmentType
99+
/*enum valueEnironmentType
98100
{
99101
None,
100102
StringValue,
101103
NumericValue
102-
} valEnvType;
104+
} valEnvType;*/
105+
106+
bool ObjectIsEmpty = true;
103107

104108
// Loop through string's contents
105109
while (*str)
106110
{
107-
if (0)
111+
if (ObjectIsEmpty)
108112
{
113+
if (*str == '{') *GObjPtr = mallocGenObj(Dictionary);
114+
else if (*str == '[') *GObjPtr = mallocGenObj(List);
115+
else
116+
{
117+
// Skip invalid characters
118+
str++;
119+
continue;
120+
}
109121

122+
GS_Push(&ObjectStack, *GObjPtr);
123+
ObjectIsEmpty = false;
110124
}
111125
else
112126
{
113127
switch (*str)
114128
{
115129
case ' ': // Ignore space character
130+
case '\t': // Ignore tab character
116131
case '\n': // Ignore newline character
117-
continue;
132+
break;
118133
case '{':
119134
// Start dictionary
120135
break;
@@ -134,12 +149,18 @@ cJSON_Result_t cJSON_parseStr(cJSON_Generic_t *GObjPtr, const char *str)
134149
case ',':
135150
// Next item
136151
break;
152+
default:
153+
// Unexpected character
154+
return cJSON_Structure_Error;
137155
}
138156
}
139157

140158
// Handle next character
141159
str++;
142160
}
161+
162+
// Delete object stack
163+
GS_Delete(&ObjectStack);
143164
}
144165

145166
#pragma endregion

src/cJSON_GenericStack.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// - GenericStack Function Implementations -
1515
#pragma region GenericStack Functions
1616

17-
cJSON_GenericStack_t TS_Create(cJSON_depth_t stackSize)
17+
cJSON_GenericStack_t GS_Create(cJSON_depth_t stackSize)
1818
{
1919
cJSON_GenericStack_t tempTS;
2020

@@ -26,7 +26,7 @@ cJSON_GenericStack_t TS_Create(cJSON_depth_t stackSize)
2626

2727
return tempTS;
2828
}
29-
void TS_Delete(cJSON_GenericStack_t *TSptr)
29+
void GS_Delete(cJSON_GenericStack_t *TSptr)
3030
{
3131
// Free stack memory
3232
free(TSptr->stack);
@@ -37,10 +37,10 @@ void TS_Delete(cJSON_GenericStack_t *TSptr)
3737
TSptr->isEmpty = true;
3838
}
3939

40-
cJSON_GenericStack_Result_t TS_Push(cJSON_GenericStack_t *TSptr, cJSON_ContainerType_t type)
40+
cJSON_GenericStack_Result_t GS_Push(cJSON_GenericStack_t *TSptr, cJSON_ContainerType_t type)
4141
{
42-
if (TS_PTR_IS_FULL(TSptr)) return TS_StackOverflow_Error;
43-
else if (TS_PTR_IS_EMPTY(TSptr))
42+
if (GS_PTR_IS_FULL(TSptr)) return GS_StackOverflow_Error;
43+
else if (GS_PTR_IS_EMPTY(TSptr))
4444
{
4545
// Push to empty stack
4646
TSptr->isEmpty = false;
@@ -52,11 +52,11 @@ cJSON_GenericStack_Result_t TS_Push(cJSON_GenericStack_t *TSptr, cJSON_Container
5252
TSptr->stack[++TSptr->index] = type;
5353
}
5454

55-
return TS_Ok;
55+
return GS_Ok;
5656
}
57-
cJSON_GenericStack_Result_t TS_Pop(cJSON_GenericStack_t *TSptr)
57+
cJSON_GenericStack_Result_t GS_Pop(cJSON_GenericStack_t *TSptr)
5858
{
59-
if (TSptr->isEmpty) return TS_StackUnderflow_Error;
59+
if (TSptr->isEmpty) return GS_StackUnderflow_Error;
6060
else if (TSptr->index == 0)
6161
{
6262
TSptr->isEmpty = true;
@@ -66,24 +66,24 @@ cJSON_GenericStack_Result_t TS_Pop(cJSON_GenericStack_t *TSptr)
6666
TSptr->index--;
6767
}
6868

69-
return TS_Ok;
69+
return GS_Ok;
7070
}
71-
cJSON_GenericStack_Result_t TS_Top(cJSON_GenericStack_t *TSptr, cJSON_ContainerType_t *type)
71+
cJSON_GenericStack_Result_t GS_Top(cJSON_GenericStack_t *TSptr, cJSON_ContainerType_t *type)
7272
{
73-
if (TSptr->isEmpty) return TS_StackIsEmpty;
73+
if (TSptr->isEmpty) return GS_StackIsEmpty;
7474
else
7575
{
76-
*type = TS_PTR_TOP(TSptr);
77-
return TS_Ok;
76+
*type = GS_PTR_TOP(TSptr);
77+
return GS_Ok;
7878
}
7979
}
80-
bool TS_IsEmpty(cJSON_GenericStack_t *TSptr)
80+
bool GS_IsEmpty(cJSON_GenericStack_t *TSptr)
8181
{
82-
return TS_PTR_IS_EMPTY(TSptr);
82+
return GS_PTR_IS_EMPTY(TSptr);
8383
}
84-
bool TS_IsFull(cJSON_GenericStack_t *TSptr)
84+
bool GS_IsFull(cJSON_GenericStack_t *TSptr)
8585
{
86-
return TS_PTR_IS_FULL(TSptr);
86+
return GS_PTR_IS_FULL(TSptr);
8787
}
8888

8989
#pragma endregion

0 commit comments

Comments
 (0)