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

Go to the source code of this file.

Macros

#define ALLOC_STR(X)   ( (char *) malloc(sizeof(char)*X) )
 
#define GAME_STATE_SPLASH_STR   "Splash"
 
#define GAME_STATE_START_STR   "Start"
 
#define GAME_STATE_GUESS_STR   "Guess"
 
#define GAME_STATE_CHOOSE_STR   "Choose"
 
#define GAME_STATE_PLAYING_STR   "Playing"
 
#define GAME_STATE_END_STR   "End"
 

Functions

char * FileToString (char *file)
 
char * TypeFromJSON (jsmntype_t Type)
 
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)
 
jsmntok_tFindKey (jsmntok_t *token, char *key, char *g_str)
 
int ConvertFileToUseable (char *fileName, jsmn_parser *parser, char **stringStorage, jsmntok_t **jsmnStorage)
 

Macro Definition Documentation

#define ALLOC_STR (   X)    ( (char *) malloc(sizeof(char)*X) )

Definition at line 4 of file mystrings.h.

Referenced by FileToString(), and JsmnToString().

#define GAME_STATE_CHOOSE_STR   "Choose"

Definition at line 12 of file mystrings.h.

Referenced by StrToGameState().

#define GAME_STATE_END_STR   "End"

Definition at line 14 of file mystrings.h.

Referenced by StrToGameState().

#define GAME_STATE_GUESS_STR   "Guess"

Definition at line 11 of file mystrings.h.

Referenced by StrToGameState().

#define GAME_STATE_PLAYING_STR   "Playing"

Definition at line 13 of file mystrings.h.

Referenced by StrToGameState().

#define GAME_STATE_SPLASH_STR   "Splash"

Definition at line 9 of file mystrings.h.

Referenced by StrToGameState().

#define GAME_STATE_START_STR   "Start"

Definition at line 10 of file mystrings.h.

Referenced by StrToGameState().

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
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 }