int current_strlen; /* XXX need to move this into the function params for thread support */
-jsonObject* jsonParseString( char* string, ... ) {
+jsonObject* jsonParseString( char* string) {
+ return json_parse_string( string );
+}
+
+jsonObject* jsonParseStringFmt( char* string, ... ) {
VA_LIST_TO_STRING(string);
return json_parse_string( VA_BUF );
}
+
+
//jsonObject* (*jsonParseString) (char* str) = &_jsonParseString;
jsonObject* json_parse_string(char* string) {
unsigned long index = 0;
- json_eat_ws(string, &index, 1); /* remove leading whitespace */
+ json_eat_ws(string, &index, 1, current_strlen); /* remove leading whitespace */
if(index == current_strlen) return NULL;
jsonObject* obj = jsonNewObject(NULL);
- int status = _json_parse_string(string, &index, obj);
+ int status = _json_parse_string(string, &index, obj, current_strlen);
if(!status) return obj;
if(status == -2) {
}
-int _json_parse_string(char* string, unsigned long* index, jsonObject* obj) {
+int _json_parse_string(char* string, unsigned long* index, jsonObject* obj, int current_strlen) {
if( !string || !index || *index >= current_strlen) return -2;
int status = 0; /* return code from parsing routines */
char* classname = NULL; /* object class hint */
- json_eat_ws(string, index, 1); /* remove leading whitespace */
+ json_eat_ws(string, index, 1, current_strlen); /* remove leading whitespace */
char c = string[*index];
while(1) {
(*index)++; /* move to second comment char */
- status = json_eat_comment(string, index, &classname, 1);
+ status = json_eat_comment(string, index, &classname, 1, current_strlen);
if(status) return status;
- json_eat_ws(string, index, 1);
+ json_eat_ws(string, index, 1, current_strlen);
c = string[*index];
if(c != '/')
break;
}
}
- json_eat_ws(string, index, 1); /* remove leading whitespace */
+ json_eat_ws(string, index, 1, current_strlen); /* remove leading whitespace */
if(*index >= current_strlen)
return -2;
/* json string */
case '"':
(*index)++;
- status = json_parse_json_string(string, index, obj);
- break;
+ status = json_parse_json_string(string, index, obj, current_strlen); break;
/* json array */
case '[':
(*index)++;
- status = json_parse_json_array(string, index, obj);
+ status = json_parse_json_array(string, index, obj, current_strlen);
break;
/* json object */
case '{':
(*index)++;
- status = json_parse_json_object(string, index, obj);
+ status = json_parse_json_object(string, index, obj, current_strlen);
break;
/* NULL */
case 'n':
case 'N':
- status = json_parse_json_null(string, index, obj);
+ status = json_parse_json_null(string, index, obj, current_strlen);
break;
case 'F':
case 't':
case 'T':
- status = json_parse_json_bool(string, index, obj);
+ status = json_parse_json_bool(string, index, obj, current_strlen);
break;
default:
if(is_number(c) || c == '.' || c == '-') { /* are we a number? */
- status = json_parse_json_number(string, index, obj);
+ status = json_parse_json_number(string, index, obj, current_strlen);
if(status) return status;
break;
}
if(status) return status;
- json_eat_ws(string, index, 1);
+ json_eat_ws(string, index, 1, current_strlen);
if( *index < current_strlen ) {
/* remove any trailing comments */
c = string[*index];
if( c == '/' ) {
(*index)++;
- status = json_eat_comment(string, index, NULL, 0);
+ status = json_eat_comment(string, index, NULL, 0, current_strlen);
if(status) return status;
}
}
}
-int json_parse_json_null(char* string, unsigned long* index, jsonObject* obj) {
+int json_parse_json_null(char* string, unsigned long* index, jsonObject* obj, int current_strlen) {
if(*index >= (current_strlen - 3)) {
return json_handle_error(string, index,
}
/* should be at the first character of the bool at this point */
-int json_parse_json_bool(char* string, unsigned long* index, jsonObject* obj) {
+int json_parse_json_bool(char* string, unsigned long* index, jsonObject* obj, int current_strlen) {
if( ! string || ! obj || *index >= current_strlen ) return -1;
char* ret = "json_parse_json_bool(): truncated bool";
/* expecting the first character of the number */
-int json_parse_json_number(char* string, unsigned long* index, jsonObject* obj) {
+int json_parse_json_number(char* string, unsigned long* index, jsonObject* obj, int current_strlen) {
if( ! string || ! obj || *index >= current_strlen ) return -1;
growing_buffer* buf = buffer_init(64);
/* index should point to the character directly following the '['. when done
* index will point to the character directly following the ']' character
*/
-int json_parse_json_array(char* string, unsigned long* index, jsonObject* obj) {
+int json_parse_json_array(char* string, unsigned long* index, jsonObject* obj, int current_strlen) {
if( ! string || ! obj || ! index || *index >= current_strlen ) return -1;
while(*index < current_strlen) {
- json_eat_ws(string, index, 1);
+ json_eat_ws(string, index, 1, current_strlen);
if(string[*index] == ']') {
(*index)++;
}
if(in_parse) {
- json_eat_ws(string, index, 1);
+ json_eat_ws(string, index, 1, current_strlen);
if(string[*index] != ',') {
return json_handle_error(string, index,
"json_parse_json_array(): array item not followed by a ','");
}
(*index)++;
- json_eat_ws(string, index, 1);
+ json_eat_ws(string, index, 1, current_strlen);
}
jsonObject* item = jsonNewObject(NULL);
set = 1;
}
}
- if(!set) status = _json_parse_string(string, index, item);
+ if(!set) status = _json_parse_string(string, index, item, current_strlen);
#else
- status = _json_parse_string(string, index, item);
+ status = _json_parse_string(string, index, item, current_strlen);
#endif
if(status) { jsonObjectFree(item); return status; }
/* index should point to the character directly following the '{'. when done
* index will point to the character directly following the '}'
*/
-int json_parse_json_object(char* string, unsigned long* index, jsonObject* obj) {
+int json_parse_json_object(char* string, unsigned long* index, jsonObject* obj, int current_strlen) {
if( ! string || !obj || ! index || *index >= current_strlen ) return -1;
obj->type = JSON_HASH;
while(*index < current_strlen) {
- json_eat_ws(string, index, 1);
+ json_eat_ws(string, index, 1, current_strlen);
if(string[*index] == '}') {
(*index)++;
"json_parse_json_object(): object missing ',' between elements" );
}
(*index)++;
- json_eat_ws(string, index, 1);
+ json_eat_ws(string, index, 1, current_strlen);
}
/* first we grab the hash key */
jsonObject* key_obj = jsonNewObject(NULL);
- status = _json_parse_string(string, index, key_obj);
+ status = _json_parse_string(string, index, key_obj, current_strlen);
if(status) return status;
if(key_obj->type != JSON_STRING) {
char* key = key_obj->value.s;
- json_eat_ws(string, index, 1);
+ json_eat_ws(string, index, 1, current_strlen);
if(string[*index] != ':') {
return json_handle_error(string, index,
(*index)++;
/* now grab the value object */
- json_eat_ws(string, index, 1);
+ json_eat_ws(string, index, 1, current_strlen);
jsonObject* value_obj = jsonNewObject(NULL);
#ifndef STRICT_JSON_READ
}
}
if(!set)
- status = _json_parse_string(string, index, value_obj);
+ status = _json_parse_string(string, index, value_obj, current_strlen);
#else
- status = _json_parse_string(string, index, value_obj);
+ status = _json_parse_string(string, index, value_obj, current_strlen);
#endif
if(status) return status;
/* when done, index will point to the character after the closing quote */
-int json_parse_json_string(char* string, unsigned long* index, jsonObject* obj) {
+int json_parse_json_string(char* string, unsigned long* index, jsonObject* obj, int current_strlen) {
if( ! string || ! index || *index >= current_strlen ) return -1;
int in_escape = 0;
}
-void json_eat_ws(char* string, unsigned long* index, int eat_all) {
+void json_eat_ws(char* string, unsigned long* index, int eat_all, int current_strlen) {
if( ! string || ! index ) return;
if(*index >= current_strlen)
return;
/* index should be at the '*' character at the beginning of the comment.
* when done, index will point to the first character after the final /
*/
-int json_eat_comment(char* string, unsigned long* index, char** buffer, int parse_class) {
+int json_eat_comment(char* string, unsigned long* index, char** buffer, int parse_class, int current_strlen) {
if( ! string || ! index || *index >= current_strlen ) return -1;
on_star = 0;
if(second_dash && !in_hint) {
(*index)++;
- json_eat_ws(string, index, 1);
+ json_eat_ws(string, index, 1, current_strlen);
(*index)--; /* this will get incremented at the bottom of the loop */
in_hint = 1;
break;
on_star = 0;
if(second_dash && !in_hint) {
(*index)++;
- json_eat_ws(string, index, 1);
+ json_eat_ws(string, index, 1, current_strlen);
(*index)--; /* this will get incremented at the bottom of the loop */
in_hint = 1;
break;
fprintf(stderr,
"\nError parsing json string at charracter %c "
- "(code %d) and index %ld\nMsg:\t%s\nNear:\t%s\n\n",
- string[*index], string[*index], *index, err_msg, buf );
+ "(code %d) and index %ld\nString length: %d\nMsg:\t%s\nNear:\t%s\n\nFull String:\n%s\n\n",
+ string[*index], string[*index], *index, current_strlen, err_msg, buf, string );
return -1;
}
jsonObject* json_parse_string(char* string);
-jsonObject* jsonParseString( char* string, ... );
+jsonObject* jsonParseString(char* string);
+jsonObject* jsonParseStringFmt( char* string, ... );
jsonObject* json_parse_file( const char* filename );
/* does the actual parsing work. returns 0 on success. -1 on error and
* -2 if there was no object to build (string was all comments)
*/
-int _json_parse_string(char* string, unsigned long* index, jsonObject* obj);
+int _json_parse_string(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
/* returns 0 on success and turns obj into a string object */
-int json_parse_json_string(char* string, unsigned long* index, jsonObject* obj);
+int json_parse_json_string(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
/* returns 0 on success and turns obj into a number or double object */
-int json_parse_json_number(char* string, unsigned long* index, jsonObject* obj);
+int json_parse_json_number(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
/* returns 0 on success and turns obj into an 'object' object */
-int json_parse_json_object(char* string, unsigned long* index, jsonObject* obj);
+int json_parse_json_object(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
/* returns 0 on success and turns object into an array object */
-int json_parse_json_array(char* string, unsigned long* index, jsonObject* obj);
+int json_parse_json_array(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
/* churns through whitespace and increments index as it goes.
* eat_all == true means we should eat newlines, tabs
*/
-void json_eat_ws(char* string, unsigned long* index, int eat_all);
+void json_eat_ws(char* string, unsigned long* index, int eat_all, int current_strlen);
-int json_parse_json_bool(char* string, unsigned long* index, jsonObject* obj);
+int json_parse_json_bool(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
/* removes comments from a json string. if the comment contains a class hint
* and class_hint isn't NULL, an allocated char* with the class name will be
* shoved into *class_hint. returns 0 on success, -1 on parse error.
* 'index' is assumed to be at the second character (*) of the comment
*/
-int json_eat_comment(char* string, unsigned long* index, char** class_hint, int parse_class);
+int json_eat_comment(char* string, unsigned long* index, char** class_hint, int parse_class, int current_strlen);
/* prints a useful error message to stderr. always returns -1 */
int json_handle_error(char* string, unsigned long* index, char* err_msg);
/* returns true if c is 0-9 */
int is_number(char c);
-int json_parse_json_null(char* string, unsigned long* index, jsonObject* obj);
+int json_parse_json_null(char* string, unsigned long* index, jsonObject* obj, int current_strlen);
#endif