Dumb-ways-to-memorize
2D game
Data Structures | Functions
parseobject.h File Reference
#include <jsmn.h>
+ Include dependency graph for parseobject.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  object_t
 

Functions

object_t * ParseToObject (jsmntok_t *token, char *g_str)
 
object_t * FindObject (object_t *obj, char *name)
 
int CountObjectMembers (object_t *obj, char *g_str)
 
int CountObjectChildren (object_t *obj)
 
void PrintObject (object_t *obj, char *g_str)
 
int CopyObjectToObjectArray (object_t **dst, object_t *src, int size)
 

Function Documentation

int CopyObjectToObjectArray ( object_t **  dst,
object_t *  src,
int  size 
)

Definition at line 237 of file parseobject.c.

References AllocateDynamic(), CopyObjectToObjectArray(), and CountMem().

Referenced by CopyObjectToObjectArray(), and ParseToObject().

238 {
239  int count, i;
240  if(!dst || !src)
241  {
242  return -1;
243  }
244  *dst = (object_t*) realloc(*dst, sizeof(object_t)*(size+1));
245  if(!*dst)
246  {
247  return -1;
248  }
249  if(size == 1)
250  {
251  memset(*dst, 0, sizeof(object_t));
252  }
253  ((*dst)[size-1]).name = src->name;
254  ((*dst)[size-1]).parent = src->parent;
255  if(src->keys)
256  {
257  count = CountMem(src->keys, sizeof(jsmntok_t));
258  for(i = 0; i < count+1; i++)
259  {
260  if( AllocateDynamic( &((*dst)[size-1]).keys, &src->keys[i], sizeof(jsmntok_t), i+1) == -1)
261  {
262  return -1;
263  }
264  }
265 
266  }
267  if(src->values)
268  {
269  count = CountMem(src->values, sizeof(jsmntok_t));
270  for(i = 0; i < count+1; i++)
271  {
272  if( AllocateDynamic( &((*dst)[size-1]).values, &src->values[i], sizeof(jsmntok_t), i+1) == -1)
273  {
274  return -1;
275  }
276  }
277  }
278  if(src->children)
279  {
280  count = CountMem(src->children, sizeof(object_t ));
281  for(i = 0; i < count+1; i++)
282  {
283  if( CopyObjectToObjectArray( &((*dst)[size-1]).children, &src->children[i], i+1) == -1)
284  {
285  return -1;
286  }
287  }
288  }
289  memset(&(*dst)[size], 0, sizeof(object_t));
290  return 0;
291 }
Definition: jsmn.h:40
int CopyObjectToObjectArray(object_t **dst, object_t *src, int size)
Definition: parseobject.c:237
int CountMem(void *src, int size_type)
Definition: mymath.c:51
int AllocateDynamic(void **dst, void *src, int size_type, int size)
Definition: mymath.c:66
int CountObjectChildren ( object_t *  obj)

Definition at line 170 of file parseobject.c.

References CountMem(), and CountObjectChildren().

Referenced by CountObjectChildren(), and LoadMenu().

171 {
172  int objects, temp, children;
173  objects = CountMem(obj->children, sizeof(object_t));
174  temp = 0;
175  children = 0;
176  while (temp < objects)
177  {
178  if(obj->children[temp].children)
179  {
180  children += CountObjectChildren(obj->children);
181  }
182  children++;
183  temp++;
184  }
185  return children;
186 }
int CountMem(void *src, int size_type)
Definition: mymath.c:51
int CountObjectChildren(object_t *obj)
Definition: parseobject.c:170
int CountObjectMembers ( object_t *  obj,
char *  g_str 
)

Definition at line 144 of file parseobject.c.

References CountMem(), and CountObjectMembers().

Referenced by CountObjectMembers(), ParseAI(), ParsePresetAI(), and ParseToObject().

145 {
146  int objects, objCount, retVal, i;
147  if(!obj || !g_str)
148  {
149  return 0;
150  }
151  objects = 1 + CountMem(obj->children, sizeof(object_t));
152  objCount = 0;
153  retVal = 0;
154  while(objCount < objects)
155  {
156  if(objCount == 0)
157  {
158  retVal += CountMem(obj->keys, sizeof(jsmntok_t));
159  retVal += CountMem(obj->values, sizeof(jsmntok_t));
160  } else
161  {
162  retVal += CountObjectMembers(&obj->children[objCount-1], g_str) + 1;
163  }
164  objCount++;
165  }
166 
167  return retVal;
168 }
Definition: jsmn.h:40
int CountMem(void *src, int size_type)
Definition: mymath.c:51
int CountObjectMembers(object_t *obj, char *g_str)
Definition: parseobject.c:144
object_t* FindObject ( object_t *  obj,
char *  name 
)

Searches for the first object in object list that matches name.

Parameters
[in,out]objIf non-null, the object.
[in,out]nameIf non-null, the name.
Returns
null if it fails, else the found object.
Author
Anthony Rios
Date
2/27/2016

Definition at line 120 of file parseobject.c.

References FindObject().

Referenced by FindObject(), LoadEntityData(), LoadGameData(), LoadLevel(), LoadLevelData(), LoadMenu(), LoadMenuData(), LoadPowerUpData(), ParseAI(), ParsePresetAI(), ParseToEntity(), and UpdatePowerUpMenu().

121 {
122  int i;
123  object_t *retVal, *temp;
124  retVal = NULL;
125  if(obj == NULL || name == NULL)
126  return retVal;
127  temp = obj;
128  for(i = 0; temp[i].name != NULL; i++)
129  {
130  if(!temp[i].name)
131  continue;
132  if(!strcmp(temp[i].name, name) )
133  {
134  return &temp[i];
135  }
136  }
137  if(temp->children)
138  {
139  return FindObject(temp->children, name);
140  }
141  return retVal;
142 }
object_t * FindObject(object_t *obj, char *name)
Definition: parseobject.c:120
object_t* ParseToObject ( jsmntok_t token,
char *  g_str 
)

Parse to object, this object has no parent.

Parameters
[in,out]tokenIf non-null, the token that begins with object.
[in,out]g_strIf non-null, the global string data of given tokens.
Returns
null if it fails, else an object_t*.
Author
Anthony Rios
Date
2/1/2016

Definition at line 8 of file parseobject.c.

References AllocateDynamic(), CopyObjectToObjectArray(), CountObjectMembers(), jsmntok_t::end, JSMN_ARRAY, JSMN_OBJECT, JSMN_STRING, JsmnToString(), ParseToObject(), and jsmntok_t::size.

Referenced by LoadEntityData(), LoadGameData(), LoadLevel(), LoadMenuData(), LoadPowerUpData(), LoadSelectedLevel(), ParseToObject(), SetAI_Check(), and UpdatePowerUpMenu().

9 {
10  int objects, keys, values, size, i;
11  char *tempStr;
12  object_t *currentChild; jsmntok_t *currentKey, *currentValue;
13  object_t *children_array; jsmntok_t *keys_array, *values_array;
14  object_t *retVal = (object_t*) malloc(sizeof(object_t)*2);
15 
16  //Err Check
17  if(!g_str || !token)
18  {
19  printf("Parse Obj given NULL token or Str");
20  return NULL;
21  }
22 
23  //Init Vars
24  retVal->name = NULL;
25  retVal->parent = NULL;
26  size = token->end; i = 0; objects = 0; keys = 0; values = 0;
27  currentChild = NULL; currentKey = NULL; currentValue = NULL;
28  children_array = NULL; keys_array = NULL; values_array = NULL;
29 
30  //Parse
31  do
32  {
33  if(token[i].type == JSMN_OBJECT)
34  {
35  if(i == 0)
36  {
37  i++;
38  continue;
39  }
40  currentChild = ParseToObject(&token[i], g_str);
41  currentChild->parent = retVal;
42  objects++;
43  } else if(token[i].type == JSMN_STRING)
44  {
45  if(token[i].size > 0)
46  {
47  if(token[i+1].type == JSMN_ARRAY || token[i+1].type == JSMN_OBJECT)
48  {
49  currentChild = ParseToObject(&token[i+1], g_str);
50  currentChild->name = JsmnToString(&token[i], g_str);
51  currentChild->parent = retVal;
52  i += token[i].size;
53  objects++;
54  } else
55  {
56  currentKey = &token[i];
57  keys++;
58  }
59  } else
60  {
61  currentValue = &token[i];
62  values++;
63  }
64  } else if(token[i].type == JSMN_ARRAY)
65  {
66  if(i == 0)
67  {
68  i++;
69  continue;
70  }
71  currentChild = ParseToObject(&token[i], g_str);
72  currentChild->parent = retVal;
73  objects++;
74  } else
75  {
76  currentValue = &token[i];
77  values++;
78  }
79 
80  //Doing & to pass reference instead of value
81  if(currentChild)
82  {
83  if ( CopyObjectToObjectArray(&children_array, currentChild, objects) == -1 )
84  {
85  printf("Dynam Alloc error : parseobj \n");
86  }
87  //tempStr = JsmnToString(&token[i], g_str);
88  //printf("Token %d Size : %d CountObjectMems : %d \n %s \n", i, token[i].size, CountObjectMembers(currentChild, g_str), tempStr);
89  i += CountObjectMembers(currentChild, g_str);
90  currentChild = NULL;
91  } else if(currentKey)
92  {
93  if( AllocateDynamic(&keys_array, currentKey, sizeof(jsmntok_t), keys) == -1 )
94  {
95  printf("Dynam Alloc eror : parseobj \n");
96  }
97  currentKey = NULL;
98  } else if (currentValue)
99  {
100  if (AllocateDynamic(&values_array, currentValue, sizeof(jsmntok_t), values) == -1 )
101  {
102  printf("Dynam Alloc eror : parseobj \n");
103  }
104  currentValue = NULL;
105  } else
106  {
107  printf("ParseObject Error: idk");
108  break;
109  }
110  i++;
111  }while( (size > token[i].start) && (token[i].type > 0) );
112  retVal->values = values_array;
113  retVal->keys = keys_array;
114  retVal->children = children_array;
115  memset(&retVal[1], 0, sizeof(object_t));
116  return retVal;
117  }
Definition: jsmn.h:40
int CopyObjectToObjectArray(object_t **dst, object_t *src, int size)
Definition: parseobject.c:237
char * JsmnToString(jsmntok_t *token, char *g_str)
Definition: mystrings.c:34
int CountObjectMembers(object_t *obj, char *g_str)
Definition: parseobject.c:144
int AllocateDynamic(void **dst, void *src, int size_type, int size)
Definition: mymath.c:66
int end
Definition: jsmn.h:43
int size
Definition: jsmn.h:44
object_t * ParseToObject(jsmntok_t *token, char *g_str)
Definition: parseobject.c:8
void PrintObject ( object_t *  obj,
char *  g_str 
)

Definition at line 188 of file parseobject.c.

References CountMem(), JsmnToString(), and PrintObject().

Referenced by LoadEntityData(), LoadGameData(), LoadMenuData(), and PrintObject().

189 {
190  int objects, tempInt, i;
191  if(!obj || !g_str)
192  {
193  return;
194  }
195  objects = 1 + CountMem(obj->children, sizeof(object_t));
196  tempInt = 0;
197  while(tempInt < objects)
198  {
199  if(tempInt == 0)
200  {
201  if(obj->name)
202  {
203  printf("%s ", obj->name);
204  }
205  printf("{ \n");
206  if(obj->keys && obj->values)
207  {
208  i = 0;
209  while(obj->keys[i].type && obj->values[i].type)
210  {
211  printf("Key : %s \n", JsmnToString(&obj->keys[i], g_str));
212  printf("Value : %s \n", JsmnToString(&obj->values[i], g_str));
213  i++;
214  }
215  } else if(obj->values)
216  {
217  i = 0;
218  while(obj->values[i].type)
219  {
220  printf("Value : %s \n", JsmnToString(&obj->values[i], g_str));
221  i++;
222  }
223  }
224  tempInt++;
225  continue;
226  }
227  if(&obj->children[tempInt-1] < &obj->children[objects])
228  {
229  PrintObject(&obj->children[tempInt-1], g_str);
230  }
231  tempInt++;
232  }
233  printf("} \n\n");
234 
235 }
int CountMem(void *src, int size_type)
Definition: mymath.c:51
char * JsmnToString(jsmntok_t *token, char *g_str)
Definition: mystrings.c:34
void PrintObject(object_t *obj, char *g_str)
Definition: parseobject.c:188