예제 #1
0
파일: load.c 프로젝트: cryptobiote/jansson
static int lex_scan(lex_t *lex, json_error_t *error)
{
    int c;

    strbuffer_clear(&lex->saved_text);

    if(lex->token == TOKEN_STRING) {
        jsonp_free(lex->value.string);
        lex->value.string = NULL;
    }

    c = lex_get(lex, error);
    while(c == ' ' || c == '\t' || c == '\n' || c == '\r')
        c = lex_get(lex, error);

    if(c == STREAM_STATE_EOF) {
        lex->token = TOKEN_EOF;
        goto out;
    }

    if(c == STREAM_STATE_ERROR) {
        lex->token = TOKEN_INVALID;
        goto out;
    }

    lex_save(lex, c);

    if(c == '{' || c == '}' || c == '[' || c == ']' || c == ':' || c == ',')
        lex->token = c;

    else if(c == '"')
        lex_scan_string(lex, error);

    else if(l_isdigit(c) || c == '-') {
        if(lex_scan_number(lex, c, error))
            goto out;
    }

    else if(l_isalpha(c)) {
        /* eat up the whole identifier for clearer error messages */
        const char *saved_text;

        c = lex_get_save(lex, error);
        while(l_isalpha(c))
            c = lex_get_save(lex, error);
        lex_unget_unsave(lex, c);

        saved_text = strbuffer_value(&lex->saved_text);

        if(strcmp(saved_text, "true") == 0)
            lex->token = TOKEN_TRUE;
        else if(strcmp(saved_text, "false") == 0)
            lex->token = TOKEN_FALSE;
        else if(strcmp(saved_text, "null") == 0)
            lex->token = TOKEN_NULL;
        else
            lex->token = TOKEN_INVALID;
    }

    else {
        /* save the rest of the input UTF-8 sequence to get an error
           message of valid UTF-8 */
        lex_save_cached(lex);
        lex->token = TOKEN_INVALID;
    }

out:
    return lex->token;
}
예제 #2
0
파일: ecma48.c 프로젝트: else/xelix
static char console_filter_ecma48_writeCallback(char c, console_info_t *info, console_driver_t *output)
{
	bool discard = 0;
	bool complete = 0;

	if (buffer_dictionary == NULL)
		buffer_dictionary = dict_new();

	strbuffer_t *buffer = dict_get(buffer_dictionary, info);
	if (buffer == (void *)-1)
	{
		buffer = strbuffer_new(0);
		dict_set(buffer_dictionary, info, buffer);
	}

	if (buffer->length == 0)
	{
		if (c == 27)
			strbuffer_append(buffer, c);
		else if (c == 0x9b)
		{
			strbuffer_append(buffer, 27);
			strbuffer_append(buffer, '[');
		}
		else
			discard = 1;
	}
	else if (strbuffer_last(buffer) == 27)
	{
		if (c == '[')
			strbuffer_append(buffer, c);
		else
			discard = 1;
	}
	else
	{
		if ((c >= '0' && c <= '9') || c == ';')
			strbuffer_append(buffer, c);
		else if (c == 'm' || c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'H' || c == 'J')
		{
			strbuffer_append(buffer, c);
			complete = 1;
		}
		else
			discard = 1;
	}

	if (complete == 1)
	{
		processControlSequence(info, buffer, output);
		strbuffer_clear(buffer);
		return 0;
	}

	if (discard == 0)
		return 0;

	if (complete == 0)
	{
		int i = 0;
		while ( i < buffer->length )
		{
			output->write(info, strbuffer_chr(buffer, i++));
		}
	}

	strbuffer_clear(buffer);

	return c;
}