Dumb-ways-to-memorize
2D game
Enumerations | Functions
mystrings.c File Reference
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <stdio.h>
#include <math.h>
#include "globals.h"
#include "mystrings.h"
#include "parseobject.h"
+ Include dependency graph for mystrings.c:

Go to the source code of this file.

Enumerations

enum  FreeVar {
  PARSER = 0x1,
  STRING = 0x2,
  TOKEN = 0x4
}
 

Functions

jsmntok_tFindKey (jsmntok_t *token, char *key, char *g_str)
 
char * JsmnToString (jsmntok_t *token, char *g_str)
 
char * FindValue (struct object_s *obj, char *key, char *g_str)
 
void JsmnToInt (jsmntok_t *token, char *str, int *dst)
 
int StrToInt (char *str)
 
int CharToInt (char c)
 
char * TypeFromJSON (jsmntype_t Type)
 
char * FileToString (char *fileName)
 
int ConvertFileToUseable (char *fileName, jsmn_parser *parser, char **stringStorage, jsmntok_t **jsmnStorage)
 
GameState StrToGameState (char *str)
 
int StrToHazard (char *str)
 
collision_type_t StrToCollisionType (char *str)
 
entity_state_t StrToEntityState (char *str)
 
vec2_tParseToVec2 (object_t *object, char *str)
 
char ** ParseToStringArray (object_t *object, char *str)
 

Enumeration Type Documentation

enum FreeVar
Enumerator
PARSER 
STRING 
TOKEN 

Definition at line 185 of file mystrings.c.

186 {
187  PARSER= 0x1,
188  STRING= 0x2,
189  TOKEN= 0x4
190 };

Function Documentation

int CharToInt ( char  c)

Character to int. A cheap switch statement.

Parameters
cThe character.
Returns
An int.
Author
Anthony Rios
Date
2/28/2016

Definition at line 121 of file mystrings.c.

Referenced by StrToInt().

122 {
123  switch(c)
124  {
125  case '0': return 0;
126  case '1': return 1;
127  case '2': return 2;
128  case '3': return 3;
129  case '4': return 4;
130  case '5': return 5;
131  case '6': return 6;
132  case '7': return 7;
133  case '8': return 8;
134  case '9': return 9;
135  default:
136  return 0;
137  }
138 }
int ConvertFileToUseable ( char *  fileName,
jsmn_parser parser,
char **  stringStorage,
jsmntok_t **  jsmnStorage 
)

Convert file to useable, by setting the storage for string and jsmntokens, give a filename.

Parameters
[in,out]fileNameIf non-null, filename of the file.
[in,out]parserIf non-null, the parser.
[in,out]stringStorageIf non-null, the string storage.
[in,out]jsmnStorageIf non-null, the jsmn storage.
Returns
The file converted to useable.
Author
Anthony Rios
Date
3/29/2016

Definition at line 192 of file mystrings.c.

References FileToString(), jsmn_init(), jsmn_parse(), PARSER, STRING, and TOKEN.

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

193 {
194  int num_tokens, varsToFree = 0;
195  jsmn_parser *tempPars;
196  char **tempStr;
197  jsmntok_t **tempJsmn;
198  if(!fileName)
199  {
200  printf("Convert File given NULL filename");
201  return -1;
202  }
203 
204  //Init parser, if not given
205  if(!parser)
206  {
207  tempPars = (jsmn_parser*) malloc(sizeof(parser));
208  if(!tempPars) return -1;
209  parser = tempPars;
210  varsToFree |= PARSER;
211  }
212  jsmn_init(parser);
213 
214  //Init stringStorage if not given
215  if(!stringStorage)
216  {
217  tempStr = (char**) malloc(sizeof(char*));
218  if(!tempStr) return -1;
219  stringStorage = tempStr;
220  varsToFree |= STRING;
221  }
222  *stringStorage = FileToString(fileName);
223  if(!*stringStorage)
224  {
225  return -1;
226  }
227 
228  //Init jsmnStorage if not given
229  if(!jsmnStorage)
230  {
231  tempJsmn = (jsmntok_t**) malloc(sizeof(jsmntok_t*));
232  if(!tempJsmn) return -1;
233  jsmnStorage = tempJsmn;
234  varsToFree |= TOKEN;
235  }
236 
237  //Actual Parsing
238  num_tokens = jsmn_parse(parser, *stringStorage, strlen(*stringStorage), NULL, 0);
239  if(num_tokens < 1)
240  {
241  printf("Jsmn Parse Eror: %d", num_tokens);
242  return -1;
243  }
244  *jsmnStorage = (jsmntok_t*) malloc(sizeof(jsmntok_t)*(num_tokens+1));
245  if(!*jsmnStorage)
246  {
247  return -1;
248  }
249  jsmn_init(parser); //Reset parser
250  num_tokens = jsmn_parse(parser, *stringStorage, strlen(*stringStorage), *jsmnStorage, num_tokens);
251  printf("Jsmn returned : %d\n", num_tokens);
252 
253  memset( &(*jsmnStorage)[num_tokens], 0, sizeof(jsmntok_t));
254 
255  //Freeing if necessary
256  if(varsToFree & PARSER) ;
257  if(varsToFree & STRING) free(*stringStorage);
258  if(varsToFree & TOKEN) free(*jsmnStorage);
259 
260  return num_tokens;
261 }
Definition: jsmn.h:40
char * FileToString(char *fileName)
Definition: mystrings.c:159
int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens)
Definition: jsmn.c:151
void jsmn_init(jsmn_parser *parser)
Definition: jsmn.c:306
char* FileToString ( char *  file)

Loads string data from a file.

Parameters
[in,out]fileIf non-null, filename of the file.
Returns
null if it fails, else a char*.
Author
Anthony Rios
Date
1/30/2016

Definition at line 159 of file mystrings.c.

References ALLOC_STR.

Referenced by ConvertFileToUseable().

160 {
161  char *string;
162  int size;
163  struct stat st;
164  FILE *file;
165  stat(fileName, &st);
166  file = fopen(fileName, "r");
167  if(!file)
168  {
169  return NULL;
170  }
171  size = st.st_size;
172  string = ALLOC_STR(size);
173  if(!string)
174  {
175  printf("Could not allocate memory");
176  exit(-1);
177  }
178  size = fread(string, sizeof(unsigned char), size, file);
179  string[size] = '\0';
180  fclose(file);
181  return string;
182 }
#define ALLOC_STR(X)
Definition: mystrings.h:4
jsmntok_t* FindKey ( jsmntok_t token,
char *  key,
char *  g_str 
)

Searches for the first key that matches given gPlayerName, through through tokens & g_str.

Parameters
[in,out]tokenThe token.
[in,out]keyThe key.
[in,out]g_strThe string to get from.
Returns
null if it fails, else the found key.
Author
Junji
Date
1/30/2016

Definition at line 10 of file mystrings.c.

References CountMem(), JsmnToString(), and jsmntok_t::size.

Referenced by LoadEntityData(), LoadLevel(), LoadLevelData(), ParseAI(), ParsePresetAI(), and ParseToEntity().

11 {
12  char *str;
13  jsmntok_t *local_token = token;
14  jsmntok_t* endPtr = token + CountMem(token, sizeof(jsmntok_t));
15  if(!local_token || !g_str || !key)
16  return local_token;
17  while(local_token < endPtr)
18  {
19  if(local_token->size == 1)
20  {
21  str = JsmnToString(local_token, g_str);
22  if(!strcmp(str, key))
23  {
24  if(str) free(str);
25  return local_token;
26  }
27  if(str) free(str);
28  }
29  local_token++;
30  }
31  return NULL;
32 }
Definition: jsmn.h:40
int CountMem(void *src, int size_type)
Definition: mymath.c:51
char * JsmnToString(jsmntok_t *token, char *g_str)
Definition: mystrings.c:34
int size
Definition: jsmn.h:44
char* FindValue ( struct object_s obj,
char *  key,
char *  g_str 
)

Searches for the a value in the object that matches the key string given. Recursive.

Parameters
[in,out]objIf non-null, the object.
[in,out]keyIf non-null, the key.
[in,out]g_strIf non-null, the string.
Returns
null if it fails, else the found value.
Author
Anthony Rios
Date
3/25/2016

Definition at line 53 of file mystrings.c.

References object_t::children, CountMem(), FindValue(), JsmnToString(), object_t::keys, and object_t::values.

Referenced by FindValue(), LoadLevel(), LoadMenu(), LoadMenuData(), ParseAI(), ParsePresetAI(), ParseToPowerUp(), and Update().

54 {
55  int i, keys, objects;
56  char *temp_str;
57  keys = CountMem(obj->keys, sizeof(jsmntok_t));
58  objects = CountMem(obj->children, sizeof(object_t));
59  //Iterate through keys
60  for(i = 0; i < keys; i++)
61  {
62  temp_str = JsmnToString(&obj->keys[i], g_str);
63  if(!temp_str)
64  continue;
65  if(!strcmp(temp_str, key))
66  {
67  if(temp_str) free(temp_str);
68  return JsmnToString(&obj->values[i], g_str);
69  }
70  if(temp_str) free(temp_str);
71  }
72  //Iterate through children
73  for(i = 0; i < objects; i++)
74  {
75  if( (temp_str = FindValue(&obj->children[i], key, g_str)) != NULL)
76  {
77  return temp_str;
78  }
79  if(temp_str) free(temp_str);
80  }
81  return NULL;
82 }
Definition: jsmn.h:40
int CountMem(void *src, int size_type)
Definition: mymath.c:51
char * JsmnToString(jsmntok_t *token, char *g_str)
Definition: mystrings.c:34
jsmntok_t * values
Definition: parseobject.h:23
jsmntok_t * keys
Definition: parseobject.h:22
object_t * children
Definition: parseobject.h:21
char * FindValue(struct object_s *obj, char *key, char *g_str)
Definition: mystrings.c:53
void JsmnToInt ( jsmntok_t token,
char *  str,
int *  dst 
)

Jsmn to int.

Parameters
[in,out]tokenIf non-null, the token.
[in,out]strIf non-null, the string.
[in,out]dstIf non-null, destination for the value.
Author
Anthony Rios
Date
2/28/2016

Definition at line 84 of file mystrings.c.

References JsmnToString(), and StrToInt().

Referenced by LoadLevelData(), and ParseToEntity().

85 {
86  char *temp;
87  temp = JsmnToString(token, str);
88  *dst = StrToInt(temp);
89  if(temp) free(temp);
90 }
char * JsmnToString(jsmntok_t *token, char *g_str)
Definition: mystrings.c:34
int StrToInt(char *str)
Definition: mystrings.c:92
char* JsmnToString ( jsmntok_t token,
char *  g_str 
)

Jsmn to string.

Parameters
[in,out]tokenIf non-null, the token.
[in,out]g_strIf non-null, the global string.
Returns
null if it fails, else a char*.
Author
Anthony Rios
Date
1/30/2016

Definition at line 34 of file mystrings.c.

References ALLOC_STR, jsmntok_t::end, and jsmntok_t::start.

Referenced by FindKey(), FindValue(), JsmnToInt(), LoadGameData(), LoadLevel(), LoadLevelData(), LoadMenuData(), LoadPowerUpData(), ParseAI(), ParseComplexMember(), ParsePresetAI(), ParseToEntity(), ParseToObject(), ParseToStringArray(), ParseToVec2(), PrintObject(), SetAI_Action(), and UpdatePowerUpMenu().

35 {
36  int size = token->end - token->start;
37  char *retVal;
38  if(size < 0)
39  {
40  return NULL;
41  }
42  retVal = ALLOC_STR(size+1);
43  if(!retVal)
44  {
45  printf("Could not allocate memory");
46  exit(-1);
47  }
48  strncpy( retVal, &g_str[token->start], size);
49  retVal[size] = 0;
50  return retVal;
51 }
#define ALLOC_STR(X)
Definition: mystrings.h:4
int start
Definition: jsmn.h:42
int end
Definition: jsmn.h:43
char** ParseToStringArray ( object_t *  object,
char *  str 
)

Definition at line 402 of file mystrings.c.

References CountMem(), and JsmnToString().

403 {
404  int i, size;
405  char *temp;
406  char **retVal;
407  if(!object || !str)
408  return NULL;
409  size = CountMem(object->values, sizeof(jsmntok_t));
410  retVal = (char**) malloc(sizeof(char*)*(size+1));
411  for(i = 0; i < size; i++)
412  {
413  temp = JsmnToString(&object->values[i], str);
414  retVal[i] = temp;
415  }
416  retVal[size+1] = 0;
417  return retVal;
418 }
Definition: jsmn.h:40
int CountMem(void *src, int size_type)
Definition: mymath.c:51
char * JsmnToString(jsmntok_t *token, char *g_str)
Definition: mystrings.c:34
vec2_t* ParseToVec2 ( object_t *  object,
char *  str 
)

Definition at line 385 of file mystrings.c.

References JsmnToString(), StrToInt(), vec2_t::x, and vec2_t::y.

386 {
387  vec2_t *retVal;
388  char *temp1, *temp2;
389  retVal = (vec2_t*) malloc(sizeof(vec2_t));
390  if(!retVal || !object || !str)
391  {
392  return NULL;
393  }
394  temp1 = JsmnToString(&object->values[0], str);
395  temp2 = JsmnToString(&object->values[1], str);
396  retVal->x = StrToInt(temp1);
397  retVal->y = StrToInt(temp2);
398  free(temp1); free(temp2);
399  return retVal;
400 }
int y
Definition: globals.h:22
char * JsmnToString(jsmntok_t *token, char *g_str)
Definition: mystrings.c:34
int StrToInt(char *str)
Definition: mystrings.c:92
int x
Definition: globals.h:21
Definition: globals.h:19
collision_type_t StrToCollisionType ( char *  str)

Converts a str to a collision type.

Parameters
[in,out]strIf non-null, the string.
Returns
str as a collision_type_t.
Author
Anthony Rios
Date
3/29/2016

Definition at line 344 of file mystrings.c.

References COLLISION_TYPE_CLIP, and Collisions_str.

Referenced by ParseComplexMember().

345 {
346  int i;
347  if(!str)
348  return 0;
349  for(i = 0; i <= COLLISION_TYPE_CLIP; i++)
350  {
351  if(!strcmp(Collisions_str[i], str))
352  {
353  return (collision_type_t)i;
354  }
355  }
356  return 0;
357 }
collision_type_t
Definition: globals.h:53
char * Collisions_str[]
Definition: entity.c:14
entity_state_t StrToEntityState ( char *  str)

Converts a str to an entity state.

Parameters
[in,out]strIf non-null, the string.
Returns
str as an entity_state_t.
Author
Anthony Rios
Date
3/29/2016

Definition at line 370 of file mystrings.c.

References COLLISION_TYPE_CLIP, and EntityStates_str.

Referenced by ParseComplexMember().

371 {
372  int i;
373  if(!str)
374  return 0;
375  for(i = 0; i <= COLLISION_TYPE_CLIP; i++)
376  {
377  if(!strcmp(EntityStates_str[i], str))
378  {
379  return (entity_state_t)i;
380  }
381  }
382  return 0;
383 }
entity_state_t
Definition: globals.h:45
char * EntityStates_str[]
Definition: entity.c:15
GameState StrToGameState ( char *  str)

Converts a str to a game state.

Parameters
[in,out]strIf non-null, the string.
Returns
str as a GameState.
Author
Anthony Rios
Date
3/29/2016

Definition at line 274 of file mystrings.c.

References CHOOSE, END, GAME_STATE_CHOOSE_STR, GAME_STATE_END_STR, GAME_STATE_GUESS_STR, GAME_STATE_PLAYING_STR, GAME_STATE_SPLASH_STR, GAME_STATE_START_STR, GUESS, PLAYING, SPLASH, and START.

Referenced by LoadMenu(), and LoadMenuData().

275 {
276  if(!str)
277  {
278  return SPLASH;
279  }
280  else if(!strcmp(str, GAME_STATE_SPLASH_STR))
281  {
282  return SPLASH;
283  }
284  else if(!strcmp(str, GAME_STATE_START_STR))
285  {
286  return START;
287  }
288  else if(!strcmp(str, GAME_STATE_GUESS_STR))
289  {
290  return GUESS;
291  } else if(!strcmp(str, GAME_STATE_CHOOSE_STR))
292  {
293  return CHOOSE;
294  }
295  else if(!strcmp(str, GAME_STATE_PLAYING_STR))
296  {
297  return PLAYING;
298  }
299  if(!strcmp(str, GAME_STATE_END_STR))
300  {
301  return END;
302  }
303  return SPLASH;
304 }
Definition: globals.h:87
#define GAME_STATE_CHOOSE_STR
Definition: mystrings.h:12
#define GAME_STATE_SPLASH_STR
Definition: mystrings.h:9
#define GAME_STATE_START_STR
Definition: mystrings.h:10
Definition: globals.h:88
#define GAME_STATE_END_STR
Definition: mystrings.h:14
#define GAME_STATE_PLAYING_STR
Definition: mystrings.h:13
Definition: globals.h:86
Definition: globals.h:85
#define GAME_STATE_GUESS_STR
Definition: mystrings.h:11
Definition: globals.h:90
int StrToHazard ( char *  str)

Converts a str to a hazard.

Parameters
[in,out]strIf non-null, the string.
Returns
str as an int.
Author
Anthony Rios
Date
3/29/2016

Definition at line 317 of file mystrings.c.

References CountMem(), and Hazards_str.

Referenced by ParseComplexMember().

318 {
319  int i, length;
320  if(!str)
321  return 0;
322  length = CountMem(Hazards_str, sizeof(char*));
323  for(i = 0; i < length; i++)
324  {
325  if(!strcmp(str, Hazards_str[i]))
326  {
327  return (1 << i);
328  }
329  }
330  return 0;
331 }
int CountMem(void *src, int size_type)
Definition: mymath.c:51
char ** Hazards_str
Definition: entity.c:13
int StrToInt ( char *  str)

String to int.

Parameters
[in,out]strIf non-null, the string.
Returns
An int.
Author
Anthony Rios
Date
2/28/2016

Definition at line 92 of file mystrings.c.

References CharToInt().

Referenced by JsmnToInt(), LoadLevel(), ParseAI(), ParseToVec2(), SetAI_Check(), and SetAI_Var().

93 {
94  int i, retVal, length, neg;
95  if(str == NULL)
96  {
97  return 0;
98  }
99  length = strlen(str);
100  retVal = 0;
101  neg = 0;
102  if(str[0] == '-')
103  {
104  i = 1;
105  neg = 1;
106  } else
107  {
108  i = 0;
109  }
110  for(i; i < length; i++)
111  {
112  retVal += CharToInt(str[i])*(powf( 10, (length-i-1) ) );
113  }
114  if(neg)
115  {
116  retVal *= -1;
117  }
118  return retVal;
119 }
int CharToInt(char c)
Definition: mystrings.c:121
char* TypeFromJSON ( jsmntype_t  Type)

Type string from JSON string.

Parameters
TypeThe type.
Returns
null if it fails, else a char*.
Author
Anthony Rios
Date
1/30/2016

Definition at line 140 of file mystrings.c.

References JSMN_ARRAY, JSMN_OBJECT, JSMN_PRIMITIVE, JSMN_STRING, and JSMN_UNDEFINED.

141 {
142  switch(Type)
143  {
144  case(JSMN_UNDEFINED):
145  return "UNDEFINED";
146  case(JSMN_OBJECT):
147  return "OBJECT";
148  case(JSMN_ARRAY):
149  return "ARRAY";
150  case(JSMN_STRING):
151  return "STRING";
152  case(JSMN_PRIMITIVE):
153  return "PRIMITIVE";
154  default:
155  return "UNKOWN";
156  }
157 }