library/src/javascript/JSON/BP-LibJSMN.cc
/**
 * Fills next token with JSON string.
 */
Fills next token with JSON string.
Fills next token with JSON string .
static int jsmn_parse_string
(
	P_BP_JSON    json,
	jsmn_parser *parser,
	const char * js,
	size_t       len,
	jsmntok_t *  tokens,
	size_t       num_tokens
)
{
	jsmntok_t *token;
	int start = parser->pos;
	parser->pos++;
	char tmp_err_buff[512];
	BP_ZERO_STACK(tmp_err_buff);
	for
	(
		;
		parser->pos < len && js[parser->pos] != '\0';
		parser->pos++
	)
	{
		char c = js[parser->pos];
		if (c == '\"')
		{
			if (!tokens)
				return 0;
			token = jsmn_alloc_token(json, parser, tokens, num_tokens);
			if (token == NULL)
			{
				parser->pos = start;
				BP_JSONParseAddErrorMsg(json, NULL, "Parser indicates that jsmn_alloc_token() has failed (potentially out of memory).");
				return JSMN_ERROR_NOMEM;
			}
			jsmn_fill_token(json, token, JSMN_STRING, start+1, parser->pos);
			token->parent = parser->toksuper;
			return 0;
		}
		if (c == '\\' && parser->pos + 1 < len)
		{
			int i;
			parser->pos++;
			switch (js[parser->pos])
			{
				case '\"':
				case '/' :
				case '\\' :
				case 'b' :
				case 'f' :
				case 'r' :
				case 'n'  :
				case 't' :
					break;
				case 'u':
					parser->pos++;
					for
					(
						i = 0;
						i < 4 && parser->pos < len && js[parser->pos] != '\0';
						i++
					)
					{
						if
						(
							!(
								(js[parser->pos] >= 48 && js[parser->pos] <= 57) || 
								(js[parser->pos] >= 65 && js[parser->pos] <= 70) || 
								(js[parser->pos] >= 97 && js[parser->pos] <= 102)   
							)
						)
						{
							parser->pos = start;
							BP_ZERO_STACK(tmp_err_buff);
							sprintf((char *) &tmp_err_buff, "jsmn_parse_string() reports that an invalid character \\x%.02x (%c) was found within the json buffer at byte-position: %u.", js[parser->pos], js[parser->pos], parser->pos);
							BP_JSONParseAddErrorMsg(json, NULL, (char *) &tmp_err_buff);
							return JSMN_ERROR_INVAL;
						}
						parser->pos++;
					}
					parser->pos--;
					break;
				default:
					parser->pos = start;
					BP_ZERO_STACK(tmp_err_buff);
					sprintf((char *) &tmp_err_buff, "jsmn_parse_string() reports that an invalid character \\x%.02x (%c) was found within the json buffer at byte-position: %u.", js[parser->pos], js[parser->pos], parser->pos);
					BP_JSONParseAddErrorMsg(json, NULL, (char *) &tmp_err_buff);
					return JSMN_ERROR_INVAL;
			}
		}
	}
	parser->pos = start;
	BP_JSONParseAddErrorMsg(json, NULL, "jsmn_parse_string() indicates that we have recieved only partial data.  In short, we were parsing fine, but then we unexpectedly ran out of data to parse.");
	return JSMN_ERROR_PART;
}
static int jsmn_parse_string ( P_BP_JSON json , jsmn_parser * parser , const char * js , size_t len , jsmntok_t * tokens , size_t num_tokens ) { jsmntok_t * token ; int start = parser -> pos ; parser -> pos ++ ; char tmp_err_buff [ 512 ] ; BP_ZERO_STACK ( tmp_err_buff ) ; for ( ; parser -> pos < len && js [ parser -> pos ] != ' \0 ' ; parser -> pos ++ ) { char c = js [ parser -> pos ] ; if ( c == ' \" ' ) { if ( ! tokens ) return 0 ; token = jsmn_alloc_token ( json , parser , tokens , num_tokens ) ; if ( token == NULL ) { parser -> pos = start ; BP_JSONParseAddErrorMsg ( json , NULL , " " ) ; return JSMN_ERROR_NOMEM ; } jsmn_fill_token ( json , token , JSMN_STRING , start + 1 , parser -> pos ) ; token -> parent = parser -> toksuper ; return 0 ; } if ( c == ' \\ ' && parser -> pos + 1 < len ) { int i ; parser -> pos ++ ; switch ( js [ parser -> pos ] ) { case ' \" ' : case ' ' : case ' \\ ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : break ; case ' ' : parser -> pos ++ ; for ( i = 0 ; i < 4 && parser -> pos < len && js [ parser -> pos ] != ' \0 ' ; i ++ ) { if ( ! ( ( js [ parser -> pos ] >= 48 && js [ parser -> pos ] <= 57 ) || ( js [ parser -> pos ] >= 65 && js [ parser -> pos ] <= 70 ) || ( js [ parser -> pos ] >= 97 && js [ parser -> pos ] <= 102 ) ) ) { parser -> pos = start ; BP_ZERO_STACK ( tmp_err_buff ) ; sprintf ( ( char * ) & tmp_err_buff , " \\ " , js [ parser -> pos ] , js [ parser -> pos ] , parser -> pos ) ; BP_JSONParseAddErrorMsg ( json , NULL , ( char * ) & tmp_err_buff ) ; return JSMN_ERROR_INVAL ; } parser -> pos ++ ; } parser -> pos -- ; break ; default : parser -> pos = start ; BP_ZERO_STACK ( tmp_err_buff ) ; sprintf ( ( char * ) & tmp_err_buff , " \\ " , js [ parser -> pos ] , js [ parser -> pos ] , parser -> pos ) ; BP_JSONParseAddErrorMsg ( json , NULL , ( char * ) & tmp_err_buff ) ; return JSMN_ERROR_INVAL ; } } } parser -> pos = start ; BP_JSONParseAddErrorMsg ( json , NULL , " " ) ; return JSMN_ERROR_PART ; }
Fills next token with JSON string.
Fills next token with JSON string .
// temporary error buffer /* Skip starting quote */ // set reference to current character /* Quote: end of string */ // if tokens is unset, exit immediately /* Backslash: Quoted symbol expected */ /* Allowed escaped symbols */ /* Allows escaped symbol \uXXXX */ // inner search loop for \uXXXX chars /* If it isn't a hex character we have an error */ /* 0-9 */ /* A-F */ /* a-f */ // create a small temporary stack buffer // convert values for output // add the error message /* Unexpected symbol */ // create a small temporary stack buffer // convert values for output // add the error message