unsigned int set_var(var_t *var, unsigned int id, unsigned long long mult, char *line) { static unsigned int lastid = 0; char buf0[strlen(line) + 1]; char buf1[strlen(line) + 1]; char buf2i[strlen(line) + 1]; char *buf2 = buf2i; unsigned int ret = 0; int n = 0; if (id <= lastid) return 0; lastid = id; sscanf(line, "%[^: ]:%n", buf0, &n); if (n < 1) return 0; line += n; ret += n; n = 0; if ((n = parse_quoted_string(line, buf1))) { ret += n; if (line[n] == ':') { n++; ret++; } else { n = 0; } } else { if (sscanf(line, "%[^: ]:%n", buf1, &n) < 1) return 0; ret += n; } if (n < 1) { buf2 = NULL; } else { line += n; n = 0; if (!(n = parse_quoted_string(line, buf2))) if (sscanf(line, "%s%n", buf2, &n) < 1) return 0; ret += n; } if (strcmp(buf0, "file") == 0) { add_file(mult, buf1); set_file_var(var, id, mult, buf1, buf2); } else if (strcmp(buf0, "date") == 0) { set_date_var(var, id, mult, buf1); } else { add_stat(mult, buf0); set_stat_var(var, id, mult, buf0, buf1, buf2); } return ret; }
istream& configpar< list<hostaddress> >::readValFromConfig(std::istream &in) { std::string tmp= parse_quoted_string(in); stringstream inputstr(tmp); bool success= false; while ( inputstr >> tmp ) { hostaddress addr(tmp.c_str(), &success); if ( success ) value.push_back(addr); else throw configParExceptionParseError("invalid host address `" + tmp + "'"); } return in; }
static errcode_t parse_std_line(char *line, struct parse_state *state) { char *cp, ch, *tag, *value; char *p; errcode_t retval; struct profile_node *node; int do_subsection = 0; void *iter = 0; if (*line == 0) return 0; cp = skip_over_blanks(line); if (cp[0] == ';' || cp[0] == '#') return 0; strip_line(cp); ch = *cp; if (ch == 0) return 0; if (ch == '[') { if (state->group_level > 0) return PROF_SECTION_NOTOP; cp++; p = strchr(cp, ']'); if (p == NULL) return PROF_SECTION_SYNTAX; *p = '\0'; retval = profile_find_node_subsection(state->root_section, cp, &iter, 0, &state->current_section); if (retval == PROF_NO_SECTION) { retval = profile_add_node(state->root_section, cp, 0, &state->current_section); if (retval) return retval; } else if (retval) return retval; /* * Finish off the rest of the line. */ cp = p+1; if (*cp == '*') { profile_make_node_final(state->current_section); cp++; } /* * A space after ']' should not be fatal */ cp = skip_over_blanks(cp); if (*cp) return PROF_SECTION_SYNTAX; return 0; } if (ch == '}') { if (state->group_level == 0) return PROF_EXTRA_CBRACE; if (*(cp+1) == '*') profile_make_node_final(state->current_section); retval = profile_get_node_parent(state->current_section, &state->current_section); if (retval) return retval; state->group_level--; return 0; } /* * Parse the relations */ tag = cp; cp = strchr(cp, '='); if (!cp) return PROF_RELATION_SYNTAX; if (cp == tag) return PROF_RELATION_SYNTAX; *cp = '\0'; p = tag; /* Look for whitespace on left-hand side. */ while (p < cp && !isspace((int)*p)) p++; if (p < cp) { /* Found some sort of whitespace. */ *p++ = 0; /* If we have more non-whitespace, it's an error. */ while (p < cp) { if (!isspace((int)*p)) return PROF_RELATION_SYNTAX; p++; } } cp = skip_over_blanks(cp+1); value = cp; if (value[0] == '"') { value++; parse_quoted_string(value); } else if (value[0] == 0) { do_subsection++; state->state = STATE_GET_OBRACE; } else if (value[0] == '{' && *(skip_over_blanks(value+1)) == 0) do_subsection++; else { cp = value + strlen(value) - 1; while ((cp > value) && isspace((int) (*cp))) *cp-- = 0; } if (do_subsection) { p = strchr(tag, '*'); if (p) *p = '\0'; retval = profile_add_node(state->current_section, tag, 0, &state->current_section); if (retval) return retval; if (p) profile_make_node_final(state->current_section); state->group_level++; return 0; } p = strchr(tag, '*'); if (p) *p = '\0'; profile_add_node(state->current_section, tag, value, &node); if (p) profile_make_node_final(node); return 0; }
static pj_json_elem* parse_elem_throw(struct parse_state *st, pj_json_elem *elem) { pj_str_t name = {NULL, 0}, value = {NULL, 0}; pj_str_t token; if (!elem) elem = pj_pool_alloc(st->pool, sizeof(*elem)); /* Parse name */ if (*st->scanner.curptr == '"') { pj_scan_get_char(&st->scanner); pj_scan_get_until_ch(&st->scanner, '"', &token); pj_scan_get_char(&st->scanner); if (*st->scanner.curptr == ':') { pj_scan_get_char(&st->scanner); name = token; } else { value = token; } } if (value.slen) { /* Element with string value and no name */ pj_json_elem_string(elem, &name, &value); return elem; } /* Parse value */ if (pj_cis_match(&st->float_spec, *st->scanner.curptr) || *st->scanner.curptr == '-') { float val; pj_bool_t neg = PJ_FALSE; if (*st->scanner.curptr == '-') { pj_scan_get_char(&st->scanner); neg = PJ_TRUE; } pj_scan_get(&st->scanner, &st->float_spec, &token); val = pj_strtof(&token); if (neg) val = -val; pj_json_elem_number(elem, &name, val); } else if (*st->scanner.curptr == '"') { unsigned err; char *start = st->scanner.curptr; err = parse_quoted_string(st, &token); if (err) { st->scanner.curptr = start + err; return NULL; } pj_json_elem_string(elem, &name, &token); } else if (pj_isalpha(*st->scanner.curptr)) { if (pj_scan_strcmp(&st->scanner, "false", 5)==0) { pj_json_elem_bool(elem, &name, PJ_FALSE); pj_scan_advance_n(&st->scanner, 5, PJ_TRUE); } else if (pj_scan_strcmp(&st->scanner, "true", 4)==0) { pj_json_elem_bool(elem, &name, PJ_TRUE); pj_scan_advance_n(&st->scanner, 4, PJ_TRUE); } else if (pj_scan_strcmp(&st->scanner, "null", 4)==0) { pj_json_elem_null(elem, &name); pj_scan_advance_n(&st->scanner, 4, PJ_TRUE); } else { return NULL; } } else if (*st->scanner.curptr == '[') { pj_json_elem_array(elem, &name); if (parse_children(st, elem) != PJ_SUCCESS) return NULL; } else if (*st->scanner.curptr == '{') { pj_json_elem_obj(elem, &name); if (parse_children(st, elem) != PJ_SUCCESS) return NULL; } else { return NULL; } return elem; }
token lexer::next_token() { skip(); if (current == end) { auto location = make_location(); return token(location, token_type::END_OF_INPUT, "end of file"); } char c = next_char(); auto start = make_location(); if (c == '<') { if (consume_next_if('<')) { return token(start, token_type::OPERATOR, "<<", operator_type::SHIFT_LEFT); } else if (consume_next_if('=')) { return token(start, token_type::OPERATOR, "<=", operator_type::LTE); } else if (consume_next_if('>')) { return token(start, token_type::OPERATOR, "<>", operator_type::NEQ); } } else if (c == '>') { if (consume_next_if('>')) { return token(start, token_type::OPERATOR, "<<", operator_type::SHIFT_RIGHT); } else if (consume_next_if('=')) { return token(start, token_type::OPERATOR, "<=", operator_type::GTE); } } else if (c == '=' && consume_next_if('=')) { return token(start, token_type::OPERATOR, "==", operator_type::EQ); } else if (c == '!' && consume_next_if('=')) { return token(start, token_type::OPERATOR, "!=", operator_type::NEQ); } else if (c == '&' && consume_next_if('&')) { return token(start, token_type::OPERATOR, "&&", operator_type::AND); } else if (c == '|' && consume_next_if('|')) { return token(start, token_type::OPERATOR, "||", operator_type::OR); } else if (c == '\'') { string quoted_string = parse_quoted_string(start, c, true); if (quoted_string.length() == 0) { logger.warning(start, "empty character literal; assuming null terminator"); return token(start, token_type::INTEGER, quoted_string, 0); } if (quoted_string.length() > 1) { logger.error(start, "multi-byte character literal"); } uint8_t value = quoted_string[0]; return token(start, token_type::INTEGER, quoted_string, value); } else if (c == '"') { string quoted_string = parse_quoted_string(start, c, true); return token(start, token_type::QUOTED_STRING, quoted_string, quote_type::DOUBLE_QUOTE); } else if (c == ':' && is_identifier_first_char(peek_char())) { return token(start, token_type::LABEL, append_while(next_char(), &lexer::is_identifier_char)); } else if (c == '$' && is_identifier_first_char(peek_char())) { return token(start, token_type::SYMBOL, append_while(next_char(), &lexer::is_identifier_char), symbol_type::EXPLICIT); } else if (is_identifier_first_char(c)) { string identifier = append_while(c, &lexer::is_identifier_char); if (consume_next_if(':')) { return token(start, token_type::LABEL, identifier); } else { return parse_identifier(start, identifier); } } else if (isdigit(c)) { return parse_number(start, append_while(c, &lexer::is_identifier_char)); } else if (c == '[') { iterator start_pos = current; uint32_t start_col = column; auto stack_op_token = parse_stack_operation(start); if (stack_op_token) { return *stack_op_token; } else { current = start_pos; column = start_col; } } else if (c == '\n') { next_line(); return token(start, token_type::NEWLINE, "newline"); } return token(start, token_type::CHARACTER, c); }
// value = token | quoted-string const char* parse_value(unsigned char** p) { if (!parse_token(p)) return NULL; return parse_quoted_string(p); }
static long parse_line(char *line, struct parse_state *state) { char *cp, ch, *tag, *value; char *p; long retval; struct profile_node *node; int do_subsection = 0; void *iter = 0; state->line_num++; if (state->state == STATE_GET_OBRACE) { cp = skip_over_blanks(line); if (*cp != '{') return PROF_MISSING_OBRACE; state->state = STATE_STD_LINE; return 0; } if (state->state == STATE_INIT_COMMENT) { if (line[0] != '[') return 0; state->state = STATE_STD_LINE; } if (*line == 0) return 0; strip_line(line); cp = skip_over_blanks(line); ch = *cp; if (end_or_comment(ch)) return 0; if (ch == '[') { if (state->group_level > 0) return PROF_SECTION_NOTOP; cp++; cp = skip_over_blanks(cp); p = strchr(cp, ']'); if (p == NULL) return PROF_SECTION_SYNTAX; if (*cp == '"') { cp++; parse_quoted_string(cp); } else { *p-- = '\0'; while (isspace(*p) && (p > cp)) *p-- = '\0'; if (*cp == 0) return PROF_SECTION_SYNTAX; } retval = profile_find_node(state->root_section, cp, 0, 1, &iter, &state->current_section); if (retval == PROF_NO_SECTION) { retval = profile_add_node(state->root_section, cp, 0, &state->current_section); if (retval) return retval; } else if (retval) return retval; /* * Finish off the rest of the line. */ cp = p+1; if (*cp == '*') { state->current_section->final = 1; cp++; } /* * Spaces or comments after ']' should not be fatal */ cp = skip_over_blanks(cp); if (!end_or_comment(*cp)) return PROF_SECTION_SYNTAX; return 0; }
istream& configpar< string >::readValFromConfig(std::istream &in) { value= parse_quoted_string(in); return in; }