Simple JSON  0.1
A free simple json library for C
simple_json_string.c
Go to the documentation of this file.
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "simple_json.h"
5 #include "simple_json_string.h"
6 #include "simple_json_error.h"
7 
9 {
11  string = (SJString *)malloc(sizeof(SJString));
12  if (!string)
13  {
14  sj_set_error("failed to allocate space for a string");
15  return NULL;
16  }
17  memset(string,0,sizeof(SJString));
18  return string;
19 }
20 
27 {
29  unsigned int l;
30  if (!s)
31  {
32  sj_set_error("sj_string_new_text: no string provided");
33  return sj_string_new();
34  }
35  string = sj_string_new();
36  if (!string)return NULL;
37  l = strlen(s)+1;
38  string->text = (char *)malloc(sizeof(char)*l);
39  if (!string->text)
40  {
41  sj_string_free(string);
42  sj_set_error("failed to allocate space for string!");
43  return NULL;
44  }
45  strncpy(string->text,s,l);
46  string->size = l;
47  return string;
48 }
49 
51 {
52  char buffer[128];
53  sprintf(buffer,"%i",i);
54  return sj_string_new_text(buffer);
55 }
56 
58 {
59  char buffer[128];
60  sprintf(buffer,"%f",f);
61  return sj_string_new_text(buffer);
62 }
63 
65 {
66  if (i)return sj_string_new_text("true");
67  return sj_string_new_text("false");
68 }
69 
70 void sj_string_free(SJString *string)
71 {
72  if (!string)return;
73  if (string->text != NULL)
74  {
75  free(string->text);
76  }
77  free(string);
78 }
79 
80 int sj_string_cmp(SJString *string,char *s)
81 {
82  if (!string)
83  {
84  sj_set_error("sj_string_cmp: no string provided");
85  return -1;
86  }
87  if (!s)
88  {
89  sj_set_error("sj_string_cmp: no character array provided");
90  return 1;
91  }
92  return strncmp(string->text,s,string->size);
93 }
94 
95 void sj_string_set(SJString *string,char *s)
96 {
97  unsigned int l;
98  if (!string)
99  {
100  sj_set_error("sj_string_set: no string provided");
101  return;
102  }
103  if (!s)
104  {
105  sj_set_error("sj_string_set: no character array provided");
106  return;
107  }
108  l = strlen(s);
109  if (l >= string->size)
110  {
111  if (string->text)free(string->text);
112  string->text = (char*)malloc(sizeof(char)*l);
113  if (!string->text)
114  {
115  sj_set_error("sj_string_set: failed to allocate space for resized string");
116  return;
117  }
118  string->size = l;
119  }
120  strncpy(string->text,s,string->size);
121 }
122 
123 void sj_string_set_limit(SJString *string,char *s,unsigned long l)
124 {
125  if (!string)
126  {
127  sj_set_error("sj_string_set: no string provided");
128  return;
129  }
130  if (!s)
131  {
132  sj_set_error("sj_string_set: no character array provided");
133  return;
134  }
135  if (l >= string->size)
136  {
137  if (string->text)free(string->text);
138  string->text = (char*)malloc(sizeof(char)*l);
139  if (!string->text)
140  {
141  sj_set_error("sj_string_set: failed to allocate space for resized string");
142  return;
143  }
144  string->size = l;
145  }
146  strncpy(string->text,s,l);
147  string->text[l] = '\0';
148 }
149 
151 {
152  if (!json)return;
153  sj_string_free(json->v.string);
154  free(json);
155 }
156 
158 {
159  if (!json)return NULL;
160  return json->v.string->text;
161 }
162 
164 {
165  if (!json)return NULL;
166  if (json->sjtype != SJVT_String)return NULL;
167  return sj_new_str(json->v.string->text);
168 }
169 
171 {
172  SJson *json;
173  if (!string)return NULL;
174  json = sj_new();
175  if (!json)return NULL;
176  json->v.string = string;
177  json->sjtype = SJVT_String;
180  json->copy = sj_string_copy;
181  return json;
182 }
183 
185 {
186  if (!string)return NULL;
187  return string->text;
188 }
189 
190 void sj_string_concat(SJString *string1,SJString *string2)
191 {
192  char *newtext;
193  size_t size;
194  if ((!string1) || (!string2))return;// no op
195  size = string1->size + string2->size;
196  if (size <= 0)return;
197  newtext=(char *)malloc(sizeof(char)*size);
198  if (!newtext)
199  {
200  sj_set_error("sj_string_concat:failed to allocate space for new string\n");
201  return;
202  }
203  memset(newtext,0,sizeof(char)*size);
204  snprintf(newtext,size,"%s%s",string1->text,string2->text);
205  if (string1->text)
206  {
207  free(string1->text);
208  }
209  string1->text = newtext;
210  string1->size = size;
211 }
212 
213 void sj_string_append(SJString *string,char *buffer)
214 {
215  char *newtext;
216  size_t size;
217  if ((!string) || (!buffer))return;// no op
218  size = string->size + strlen(buffer);
219  if (size <= 0)return;
220  newtext=(char *)malloc(sizeof(char)*size);
221  if (!newtext)
222  {
223  sj_set_error("sj_string_append:failed to allocate space for new string\n");
224  return;
225  }
226  memset(newtext,0,sizeof(char)*size);
227  snprintf(newtext,size,"%s%s",string->text,buffer);
228  if (string->text)
229  {
230  free(string->text);
231  }
232  string->text = newtext;
233  string->size = size;
234 }
235 
237 {
238  SJString *json;
239  if (!string)return NULL;
240  if (string->sjtype != SJVT_String)
241  {
242  sj_set_error("sj_string_to_json_string: input string not a string type");
243  return NULL;
244  }
245  json = sj_string_new_text("\"");
246  if (!json)return NULL;
247  sj_string_concat(json,string->v.string);
248  sj_string_append(json,"\"");
249  return json;
250 }
251 
252 int sj_string_as_integer(SJString *string,int *output)
253 {
254  int value;
255  if (!string)return 0;
256  if (!string->text)return 0;
257  value = atoi(string->text);
258  if (!value)// if we have a zero, make sure the string itself is not just zero
259  {
260  if (string->text[0] != '0')return 0;
261  }
262  if (output)
263  {
264  *output = value;
265  }
266  return 1;
267 }
268 
269 int sj_string_as_float(SJString *string,float *output)
270 {
271  double value;
272  if (!string)return 0;
273  if (!string->text)return 0;
274  value = atoi(string->text);
275  if (value == 0.0)// if we have a zero, make sure the string itself is not just zero
276  {
277  if (string->text[0] != '0')return 0;
278  }
279  if (output)
280  {
281  *output = (float)value;
282  }
283  return 1;
284 }
285 
286 int sj_string_as_bool(SJString *string,short int *output)
287 {
288  if (!string)return 0;
289  if (!string->text)return 0;
290  if ((strcmp(string->text,"true")==0) ||
291  (strcmp(string->text,"TRUE")==0) ||
292  (strcmp(string->text,"True")==0) ||
293  (strcmp(string->text,"1\0")==0))
294  {
295  if (output)*output = 1;
296  return 1;
297  }
298  if ((strcmp(string->text,"false")==0) ||
299  (strcmp(string->text,"FALSE")==0) ||
300  (strcmp(string->text,"False")==0) ||
301  (strcmp(string->text,"0\0")==0))
302  {
303  if (output)*output = 0;
304  return 1;
305  }
306  return 0;
307 }
308 
309 /*eol@eof*/
SJString * sj_string_new_float(float f)
void sj_string_concat(SJString *string1, SJString *string2)
append the contents of string2 to string1
SJString * sj_string_to_json_string(SJson *string)
get the contents of the string back formatted and escaped for json
this is the abstract container structure for all json data This structure may be an object...
SJson * sj_new()
allocate a new empty json object
Definition: simple_json.c:96
int sj_string_as_integer(SJString *string, int *output)
get the integer value if the string is a numer
void sj_string_set(SJString *string, char *s)
set the value of string to s
int sj_string_as_float(SJString *string, float *output)
get the float value if the string is a numer
SJString * sj_string_new_bool(int i)
void sj_string_set_limit(SJString *string, char *s, unsigned long l)
set the value of string to s but only length of s
char * sj_string_get_text(SJString *string)
get the text back from the string
SJValueTypes sjtype
SJString * sj_string_new()
make a new empty string
void sj_string_value_free(SJson *json)
char * sj_string_value_get_string(SJson *json)
void(* json_free)(struct SJson_S *json)
SJson * sj_string_to_value(SJString *string)
make a jason value object out of a string
union SJson_S::@0 v
void sj_set_error(char *er)
used internally to se the error message
SJson * sj_string_copy(SJson *json)
a basic structure that keeps track of a string and its length Automatically grows to accomodate longe...
SJString * string
SJString * sj_string_new_integer(int i)
int sj_string_as_bool(SJString *string, short int *output)
get the bool value if the string is a bool
SJson * sj_new_str(char *str)
make a new json value that is a string
Definition: simple_json.c:9
void sj_string_free(SJString *string)
free an SJString
struct SJson_S *(* copy)(struct SJson_S *json)
int sj_string_cmp(SJString *string, char *s)
performs a strcmp on the string with the provided character array
void sj_string_append(SJString *string, char *buffer)
append the contents of buffer to the string
SJString * sj_string_new_text(char *s)
make a new string with the characters from array s
SJString *(* get_string)(struct SJson_S *json)