/***************************************************************************** * num_words_in_string * Return the number of words in a string * * Parameters * --------- * - a string * * Returns * ------- * - number of words * * Notes * ----- * Get the number of whitespace delimited words in a string. * this assumes that all the cntrl character (eg "\n", "\r" etc..) * have been stripped from str already ******************************************************************************/ int num_words_in_string(char *str) { int num=0; int j=0; int ok=TRUE; int len; if (str == NULL) return (0); len = string_len(str); /* skip through leading whitespace */ /* note is_white_space('\0') should eval to false */ while( is_white_space(str[j]) ) j++; if (str[j] == '\0') return(0); /* apparently we are at a word */ while (ok) { while( !is_white_space(str[j]) && (j<len) ) j++; num++; while ( is_white_space(str[j]) && (j<len) ) j++; if( (str[j] == '\0') || ( j>= len) ) ok=FALSE; } return num; }
static char * s_get_tag_name(Doc *doc, const char *s, int len) { int ii; int sp; int size; char *return_value = NULL; /* ignore space. */ for (ii = 0; ii < len; ii++) { if (is_white_space(s[ii])) continue; break; } sp = ii; for (;ii<len; ii++) { if (is_white_space(s[ii])) break; } size = ii-sp; return_value = (char *)apr_palloc(doc->pool, size+1); memset(return_value, 0, size+1); memcpy(return_value, &s[sp], size); QX_LOGGER_DEBUG(return_value); return return_value; }
/****************************************************************************** * strip_white_space() * Strip white space from a string * * Parameters * --------- * - a string * * Returns * ------- * - a new string * * Notes * ----- * This function returns a ptr to the first non-whitespace char of a string * and insert a '\0' after the last non-whitespace char of string * This function creates a new string. * ******************************************************************************/ char *strip_white_space (char *in_string) { char *s, *t, *string, *ret_str; if(in_string == NULL) return(NULL); string = copy_string(in_string); for (s = string; is_white_space(*s); s++); if (*s == 0) return (NULL); t = s + string_len(s) - 1; while (t > s && is_white_space (*t)) t--; *++t = '\0'; ret_str = copy_string(s); free(string); return (ret_str); }
//---------------------------------------------------------------------------- size_t skip_space_forward(const std::string & src, size_t pos, const size_t size) { while ((pos < size) && is_white_space(src[pos])) ++pos; return pos; }
void option_db_t::parse_text( const std::string& text ) { // Split a chunk of text into lines to parse. std::string::size_type first = 0; while ( true ) { while ( first < text.size() && is_white_space( text[ first ] ) ) ++first; if ( first >= text.size() ) break; std::string::size_type last = text.find( '\n', first ); if ( false ) { std::cerr << "first = " << first << ", last = " << last << " [" << text.substr( first, last - first ) << ']' << std::endl; } if ( text[ first ] != '#' ) { parse_line( text.substr( first, last - first ) ); } first = last; } }
static void skip_white_spc(port *in) { int ch; while ((ch = get_next_char(in)) != EOF && is_white_space(ch)) ; if (ch != EOF) push_back_char(in, ch); }
//---------------------------------------------------------------------------- size_t skip_space_backward(const std::string & src, size_t pos) { while (is_white_space(src[pos])) { if (pos == 0) break; --pos; } return pos; }
//replace all instances of to_replace in filter which are bounded by one of the characters in white_space with replacement //returns 0 on normal, else on error int word_replace(char *filter, char *white_space, char *string_to_replace, char *string_replacement) { //some basic validation if (strlen(string_replacement) != strlen(string_to_replace)) { return 1; } if (strlen(string_to_replace) < 1) { return 1; } char *filter_current_pos; char *filter_current_match; int i; filter_current_pos = filter; while((filter_current_match = strstr(filter_current_pos,string_to_replace))) { //check to see if previous char is white space //special case for match at first char if ((is_white_space(white_space, filter[strlen(filter)-strlen(filter_current_match)-1])) || (filter_current_match == filter)) { //now try for white space after hit //special case for hit that contains last car if( (is_white_space(white_space, filter_current_match[strlen(string_to_replace)])) || (strlen(filter_current_match) == strlen(string_to_replace)) ) { //the word is surrounded by white space, replace it for (i = 0; i < strlen(string_to_replace); i++) { filter[strlen(filter)-strlen(filter_current_match)+i] = string_replacement[i]; } } } filter_current_pos = &filter_current_match[1]; } return 0; }
void cmd_build_cmdline(queue_t *head, ui_cmdline_t *cmd) { ui_token_t *t; ui_token_t *next; memset(cmd, 0, sizeof(ui_cmdline_t)); t = (ui_token_t *) q_deqnext(head); while (t != NULL) { if (is_white_space(t)) { /* do nothing */ } else if (t->token != '-') { if(cmd->argc < MAX_TOKENS){ cmd->argv[cmd->argc] = cmd_eat_quoted_arg(head,t); cmd->argc++; } /* Token is a switch */ } else { if (cmd->swc < MAX_SWITCHES) { cmd->swv[cmd->swc].swname = lib_strdup(&(t->token)); if (t->qb.q_next != head) { /* more tokens */ next = (ui_token_t *) t->qb.q_next; if (next->token == '=') { /* switch has value */ KFREE(t); /* Free switch name */ t = (ui_token_t *) q_deqnext(head); /* eat equal sign */ KFREE(t); /* and free it */ t = (ui_token_t *) q_deqnext(head); /* now have value */ if (t != NULL) { cmd->swv[cmd->swc].swvalue = cmd_eat_quoted_arg(head,t); } } else { /* no value */ cmd->swv[cmd->swc].swvalue = NULL; } } /* * swidx is the index of the argument that this * switch precedes. So, if you have "foo -d bar", * swidx for "-d" would be 1. */ cmd->swv[cmd->swc].swidx = cmd->argc; cmd->swc++; } } KFREE(t); t = (ui_token_t *) q_deqnext(head); } }
/** Check if the line is a comment or empty space. If it is, consume the leading whitespace from line. */ bool is_space_or_comment(char *&line) { char *tmp = line; while (is_white_space(tmp)) tmp++; if (*tmp == '#' || *tmp == 0) { line = tmp; return true; } return false; }
static void cmd_eat_leading_white(queue_t *head) { ui_token_t *t; while (!q_isempty(head)) { t = (ui_token_t *) q_getfirst(head); if (is_white_space(t)) { q_dequeue(&(t->qb)); KFREE(t); } else break; } }
void TBParser::OnCompactLine(char *line, TBParserTarget *target) { target->Enter(); while (*line) { // consume any whitespace while (is_white_space(line)) line++; // Find token char *token = line; while (*line != ':' && *line != 0) line++; if (!*line) break; // Syntax error, expected token *line++ = 0; // consume any whitespace while (is_white_space(line)) line++; TBValue v; ConsumeValue(v, line); if (pending_multiline) { // The value wrapped to the next line, so we should remember the token and continue. multi_line_token.Set(token); // Since we need to call target->Leave when the multiline is ready, set multi_line_sub_level. multi_line_sub_level = 1; return; } // Ready target->OnToken(current_line_nr, token, v); } target->Leave(); }
/****************************************************************************** * get_word_from_string * Get a specific word from a srting * * Parameters * --------- * - a string * - which word * * Returns * ------- * - a new string * * Notes * ----- * Get the "which" word from a string, where the words are space * delimited. note this starts with 1, ie which = 1 is the first word * in the string, the returned string should have NO whitespace * Note this rets a new string. ******************************************************************************/ char *get_word_from_string(char *str, int which) { int num_words = 0; int num=0; int j=0; int idx_l, idx_h; int cont; int len; if (str == NULL) return(NULL); len = string_len(str); num_words = num_words_in_string(str); /* if the last word is only a single char then it misses that in the count ??*/ if (which > num_words) return NULL; /* skip through leading whitespace */ while( is_white_space(str[j]) ) j++; if (str[j] == '\0') return NULL; /* apparently we are at a word */ cont = TRUE; while (cont) { idx_l = j; while( !is_white_space(str[j]) && (j<len) ) j++; num++; idx_h = j-1; if (num == which){ cont = FALSE; } else { while (is_white_space(str[j]) && (j < len)) j++; if( (str[j] == '\n') || (str[j] == '\0') || ((j+1)>len) ) cont=FALSE; // this last line seems wrong but it works????? } } return (sub_string(str,idx_l,idx_h)); }
static char *get_char_until_delim(port *in) { int i = 0, ch; char str[MAX_SYM_NUM_LEN], *dst; str[0] = '\0'; while ((ch = get_next_char(in)) != EOF && !is_white_space(ch) && ch != '(' && ch != ')') str[i++] = ch; str[i] = '\0'; memcpy((dst = alloc_strseg(i + 1)), str, i + 1); if (ch != EOF) push_back_char(in, ch); return dst; }
void TBParser::ConsumeValue(TBValue &dst_value, char *&line) { // Find value (As quoted string, or as auto) char *value = line; if (*line == '\"' || *line == '\'') { const char quote_type = *line; // Consume starting quote line++; value++; // Find ending quote or end while (!IsEndQuote(value, line, quote_type) && *line != 0) line++; // Terminate away the quote if (*line == quote_type) *line++ = 0; // consume any whitespace while (is_white_space(line)) line++; // consume any comma if (*line == ',') line++; UnescapeString(value); dst_value.SetString(value, TBValue::SET_AS_STATIC); } else { // Find next comma or end while (*line != ',' && *line != 0) line++; // Terminate away the comma if (*line == ',') *line++ = 0; UnescapeString(value); dst_value.SetFromStringAuto(value, TBValue::SET_AS_STATIC); } // Check if we still have pending value data on the following line and set pending_multiline. bool continuing_multiline = pending_multiline; pending_multiline = is_pending_multiline(line); // Append the multi line value to the buffer. if (continuing_multiline || pending_multiline) multi_line_value.AppendString(dst_value.GetString()); }
BOOL is_valid_nonalphabetic_char( char ch, unsigned short input_type) { if( (inputtype_numeric & input_type) && is_numeric( ch)) return TRUE; if( (inputtype_white_spaces& input_type) && is_white_space( ch)) return TRUE; if( (inputtype_punctuation & input_type) && is_punctuation( ch)) return TRUE; if( (inputtype_symbols & input_type) && is_symbol( ch)) return TRUE; return FALSE; }
int cmd_addcmd(char *command, int (*func)(ui_cmdline_t *,int argc,char *argv[]), void *ref, char *help, char *usage, char *switches) { cmdtab_t **list = &cmd_root; cmdtab_t *cmd = NULL; queue_t tokens; queue_t *cur; ui_token_t *t; cmd_build_list(&tokens,command); cur = tokens.q_next; while (cur != &tokens) { t = (ui_token_t *) cur; if (!is_white_space(t)) { cmd = cmd_findword(*list,&(t->token)); if (!cmd) { cmd = KMALLOC(sizeof(cmdtab_t)+strlen(&(t->token))+1,0); memset(cmd,0,sizeof(cmdtab_t)); cmd->cmdword = (char *) (cmd+1); strcpy(cmd->cmdword,&(t->token)); cmd->sibling = *list; *list = cmd; } list = &(cmd->child); } cur = cur->q_next; } cmd_free_tokens(&tokens); if (!cmd) return -1; cmd->func = func; cmd->usage = usage; cmd->ref = ref; cmd->help = help; cmd->switches = switches; return 0; }
void litehtml::el_text::draw( uint_ptr hdc, int x, int y, const position* clip ) { if(is_white_space() && !m_draw_spaces) { return; } position pos = m_pos; pos.x += x; pos.y += y; if(pos.does_intersect(clip)) { uint_ptr font = m_parent->get_font(); litehtml::web_color color = m_parent->get_color(_t("color"), true, m_doc->get_def_color()); m_doc->container()->draw_text(hdc, m_use_transformed ? m_transformed_text.c_str() : m_text.c_str(), font, color, pos); } }
std::shared_ptr<xml_node_t> xml_node_t::create_node( sim_t* sim, const std::string& input, std::string::size_type& index ) { char c = input[ index ]; if ( c == '?' ) index++; std::string name_str; parse_name( name_str, input, index ); assert( ! name_str.empty() ); std::shared_ptr<xml_node_t> node( new xml_node_t( name_str ) ); if ( ! node ) return node; while ( is_white_space( input[ index ] ) ) { node -> create_parameter( input, ++index ); } c = input[ index ]; if ( c == '/' || c == '?' ) { index += 2; } else if ( c == '>' ) { node -> create_children( sim, input, ++index ); } else { if ( sim ) { int start = std::min( 0, ( ( int ) index - 32 ) ); sim -> errorf( "Unexpected character '%c' at index=%d node=%s context=%s\n", c, ( int ) index, node -> name(), input.substr( start, index - start ).c_str() ); sim -> out_std.raw() << input << "\n"; sim -> cancel(); } return std::shared_ptr<xml_node_t>(); } return node; }
void litehtml::el_text::parse_styles(bool is_reparse) { m_text_transform = (text_transform) value_index(get_style_property(_t("text-transform"), true, _t("none")), text_transform_strings, text_transform_none); if(m_text_transform != text_transform_none) { m_transformed_text = m_text; m_use_transformed = true; m_doc->container()->transform_text(m_transformed_text, m_text_transform); } if(is_white_space()) { m_transformed_text = _t(" "); m_use_transformed = true; } else { if(m_text == _t("\t")) { m_transformed_text = _t(" "); m_use_transformed = true; } if(m_text == _t("\n") || m_text == _t("\r")) { m_transformed_text = _t(""); m_use_transformed = true; } } font_metrics fm; uint_ptr font = m_parent->get_font(&fm); if(is_break()) { m_size.height = 0; m_size.width = 0; } else { m_size.height = fm.height; m_size.width = m_doc->container()->text_width(m_use_transformed ? m_transformed_text.c_str() : m_text.c_str(), font); } m_draw_spaces = fm.draw_spaces; }
char getsymbol (FILE* file, char* s) { register char c; int comm = 0; while ((c = getc (file)) != EOF) { if (is_comment(c)) { comm = 1; continue; } if (comm == 1) { if(c == '\n') comm = 0; continue; } if (is_white_space(c)) continue; if (is_delimiter(c)) break; *s++ = c; } *s = '\0'; return(c); }
void TBParser::OnMultiline(char *line, TBParserTarget *target) { // consume any whitespace while (is_white_space(line)) line++; TBValue value; ConsumeValue(value, line); if (!pending_multiline) { // Ready with all lines value.SetString(multi_line_value.GetData(), TBValue::SET_AS_STATIC); target->OnToken(current_line_nr, multi_line_token, value); if (multi_line_sub_level) target->Leave(); // Reset multi_line_value.SetAppendPos(0); multi_line_sub_level = 0; } }
void read_file(void) { int fd; char buf[1]; char address[1024]; char name[1280]; int j, k; enum t_state {out, addr, sym_name, type} state; char *endp; long final_addr = 0; fd = open("/proc/kallsyms", O_RDONLY); state = out; j = 0; k = 0; while(read(fd, buf, 1) > 0) { char c = buf[0]; if((state == out || state == addr) && ((c >= '0' && c <= '9' ) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) { state = addr; address[j++] = c; } else if(c == ' ' && state == addr) { state = type; name[j] = '\0'; } else if(c == ' ' && state == type) { state = sym_name; } else if(state == type) { continue; } else if(state == sym_name && c != ' ' && c != '\n' && c != '\r' && c != '\t') { name[k++] = c; } else if(is_white_space(c) && state == sym_name) { state = out; name[k] = '\0'; k = 0; j = 0; if(strcmp(name, "kallsyms_lookup_name") == 0) { final_addr = strtol(address, &endp, 16); goto out; } } } out: printf("Found address: %lu\n", final_addr); close(fd); }
unsigned int is_printable(unsigned char ch) { return (is_digit(ch) || is_letter(ch) || is_symbol(ch) || is_white_space(ch)); }
extern void html_ignore_while_space(HTMLScanner *_this) { while(_this->current_position < _this->length &&is_white_space(_this->string[_this->current_position]) ) _this->current_position++; }
// Unified function for loading strings.tbl and tstrings.tbl (and their modular versions). // The "external" parameter controls which format to load: true for tstrings.tbl, false for strings.tbl void parse_stringstbl_common(const char *filename, const bool external) { char chr, buf[4096]; char language_tag[512]; int z, index; char *p_offset = NULL; int offset_lo = 0, offset_hi = 0; read_file_text(filename, CF_TYPE_TABLES); reset_parse(); // move down to the proper section memset(language_tag, 0, sizeof(language_tag)); strcpy_s(language_tag, "#"); if (external && Lcl_current_lang == FS2_OPEN_DEFAULT_LANGUAGE){ strcat_s(language_tag, "default"); } else { strcat_s(language_tag, Lcl_languages[Lcl_current_lang].lang_name); } if ( skip_to_string(language_tag) != 1 ) { mprintf(("Current language not found in %s\n", filename)); return; } // parse all the strings in this section of the table while ( !check_for_string("#") ) { int num_offsets_on_this_line = 0; stuff_int(&index); if (external) { ignore_white_space(); get_string(buf, sizeof(buf)); drop_trailing_white_space(buf); } else { stuff_string(buf, F_RAW, sizeof(buf)); } if (external && (index < 0 || index >= LCL_MAX_STRINGS)) { error_display(0, "Invalid tstrings table index specified (%i). Please increment LCL_MAX_STRINGS in localize.cpp.", index); return; } else if (!external && (index < 0 || index >= XSTR_SIZE)) { Error(LOCATION, "Invalid strings table index specified (%i)", index); } if (!external) { size_t i = strlen(buf); while (i--) { if ( !isspace(buf[i]) ) break; } // trim unnecessary end of string // Assert(buf[i] == '"'); if (buf[i] != '"') { // probably an offset on this entry // drop down a null terminator (prolly unnecessary) buf[i+1] = 0; // back up over the potential offset while ( !is_white_space(buf[i]) ) i--; // now back up over intervening spaces while ( is_white_space(buf[i]) ) i--; num_offsets_on_this_line = 1; if (buf[i] != '"') { // could have a 2nd offset value (one for 640, one for 1024) // so back up again while ( !is_white_space(buf[i]) ) i--; // now back up over intervening spaces while ( is_white_space(buf[i]) ) i--; num_offsets_on_this_line = 2; } p_offset = &buf[i+1]; // get ptr to string section with offset in it if (buf[i] != '"') Error(LOCATION, "%s is corrupt", filename); // now its an error } buf[i] = 0; // copy string into buf z = 0; for (i = 1; buf[i]; i++) { chr = buf[i]; if (chr == '\\') { chr = buf[++i]; if (chr == 'n') chr = '\n'; else if (chr == 'r') chr = '\r'; } buf[z++] = chr; } // null terminator on buf buf[z] = 0; } // write into Xstr_table (for strings.tbl) or Lcl_ext_str (for tstrings.tbl) if (Parsing_modular_table) { if ( external && (Lcl_ext_str[index] != NULL) ) { vm_free((void *) Lcl_ext_str[index]); Lcl_ext_str[index] = NULL; } else if ( !external && (Xstr_table[index].str != NULL) ) { vm_free((void *) Xstr_table[index].str); Xstr_table[index].str = NULL; } } if (external && (Lcl_ext_str[index] != NULL)) { Warning(LOCATION, "Tstrings table index %d used more than once", index); } else if (!external && (Xstr_table[index].str != NULL)) { Warning(LOCATION, "Strings table index %d used more than once", index); } if (external) { Lcl_ext_str[index] = vm_strdup(buf); } else { Xstr_table[index].str = vm_strdup(buf); } // the rest of this loop applies only to strings.tbl, // so we can move on to the next line if we're reading from tstrings.tbl if (external) { continue; } // read offset information, assume 0 if nonexistant if (p_offset != NULL) { if (sscanf(p_offset, "%d%d", &offset_lo, &offset_hi) < num_offsets_on_this_line) { // whatever is in the file ain't a proper offset Error(LOCATION, "%s is corrupt", filename); } } Xstr_table[index].offset_x = offset_lo; if (num_offsets_on_this_line == 2) Xstr_table[index].offset_x_hi = offset_hi; else Xstr_table[index].offset_x_hi = offset_lo; // clear out our vars p_offset = NULL; offset_lo = 0; offset_hi = 0; } }
// given a valid XSTR() tag piece of text, extract the id# portion, return the value in out, nonzero on success int lcl_ext_get_id(const SCP_string &xstr, int *out) { char id_buf[10]; size_t p, pnext; // find the first quote p = xstr.find('\"'); if (p == SCP_string::npos) { error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr.c_str()); return 0; } p++; // continue searching until we find the close quote while(1) { pnext = xstr.find('\"', p); if (pnext == SCP_string::npos) { error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr.c_str()); return 0; } // if the previous char is a \, we know its not the "end-of-string" quote if (xstr[pnext - 1] != '\\') { p = pnext; break; } // continue p = pnext; } // search until we find a , pnext = xstr.find(',', p); if (pnext == SCP_string::npos) { error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr.c_str()); return 0; } pnext++; // find the close parenthesis p = pnext; pnext = xstr.find(')', p); if (pnext == SCP_string::npos) { error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr.c_str()); return 0; } pnext--; // get only the number while (is_white_space(xstr[p]) && p <= pnext) p++; while (is_white_space(xstr[pnext]) && p <= pnext) pnext--; if (p > pnext) { error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr.c_str()); return 0; } // now get the id string if ((pnext - p + 1) > 9) { error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr.c_str()); return 0; } memset(id_buf, 0, 10); xstr.copy(id_buf, pnext - p + 1, p); // get the value and we're done *out = atoi(id_buf); // success return 1; }
/* * Parse succesive blocks of XML data, generating events for the * handlers/callbacks as we go. State is maintained in the * simple_xml_parser object. * If the top level XML document ends before the last character, * the "read" parameter indicates how much input was consumed. */ hcerr_t xml_parse(xml_parser *formal_parser, char s[], hc_long_t size, hc_long_t *read){ simple_xml_parser *parser = (simple_xml_parser *) formal_parser; int i = 0; if (DEBUG == TRUE){ print_state(parser->state, parser->depth); printf ("in parser with " LL_FORMAT " %s\n", size, s); fflush(stdout); } while (i < size){ switch(parser->state){ case OUTSIDE_ELEMENT: if (is_white_space(s[i])){ /*skip_white_space */ break; } if (s[i] == '<'){ parser->start_tag = TRUE; change_state(&parser->state, EXPECTING_OPEN_OR_CLOSE_TAG, parser->depth); } else { HC_ERR_LOG(("Expected '<', read %c at %d %s\n", s[i], i, s)); return HCERR_XML_EXPECTED_LT; } break; case DOCUMENT_ELEMENT: /* discard document element */ if (s[i] != '>'){ if (DEBUG == TRUE) printf("discarding %c", s[i]); break; } else{ parser->state = OUTSIDE_ELEMENT; break; } case EXPECTING_OPEN_OR_CLOSE_TAG: if (is_white_space(s[i])){ /*skip_white_space */ break; } if (s[i] == '/'){ if (DEBUG) printf("parser->start_tag = FALSE\n"); parser->start_tag = FALSE; break; } case EXPECTING_TAG: if (is_name_first_char(s[i]) == TRUE){ change_state(&parser->state, SCANNING_TAG, parser->depth); require_ok(token_append(&parser->buffer_list, s[i])); break; } /* Discard document element */ else if (s[i] == '?' && parser->depth == 0){ parser->state = DOCUMENT_ELEMENT; break; } else{ HC_ERR_LOG(("Invalid first character for element name : %c %d %s\n", s[i], i, s)); return HCERR_XML_INVALID_ELEMENT_TAG; } // FALLTHRU INTENTIONAL??? /* Start tag is terminated by whitespace, /, or > End tag is terminated by whitespace or > */ case SCANNING_TAG: /* Still reading token */ if (is_name_char(s[i]) == TRUE){ require_ok(token_append(&parser->buffer_list, s[i])); break; } else if (is_white_space(s[i]) == TRUE) { parser->current_tag = token_finish(&parser->buffer_list); if (parser->start_tag == TRUE){ /*printf("Start element: %s\n", parser->current_tag);*/ change_state(&parser->state, SCANNING_ATTRIBUTES, parser->depth); break; } else{ change_state(&parser->state, SCANNING_CLOSE_TAG, parser->depth); break; } } else if (s[i] == '>') { if (DEBUG == TRUE) printf("parser->depth: %d\n", parser->depth); require_ok(close_tag(&i, parser)); if (DEBUG == TRUE) printf("parser->depth: %d\n", parser->depth); if (parser->depth == 0){ *read = i + 1; return HCERR_OK; } } /* <element/> */ else if (s[i] == '/' && parser->start_tag == TRUE) { if (DEBUG == TRUE){ printf("Start element: %s\n", parser->current_tag); printf("End element: %s\n", parser->current_tag); } change_state(&parser->state, EXPECTING_RIGHT_BRACKET, parser->depth); break; } else { HC_ERR_LOG(("Invalid character '%c' in tag. %i %s\n", s[i], i, s)); return HCERR_XML_INVALID_ELEMENT_TAG; } break; case EXPECTING_RIGHT_BRACKET: if (s[i] != '>') { HC_ERR_LOG(("Unexpected character %c after close element. %d %s", s[i], i, s)); return HCERR_XML_MALFORMED_START_ELEMENT; } if (parser->depth == 0){ *read = i + 1; return HCERR_OK; } change_state(&parser->state, OUTSIDE_ELEMENT, parser->depth); break; case SCANNING_CLOSE_TAG: if (is_white_space(s[i])) { break; } if (DEBUG == TRUE) fprintf(stdout, "End element: %s\n", parser->current_tag); if (s[i] != '>') { HC_ERR_LOG(("Unexpected character %c after close element. %d %s", s[i], i, s)); return HCERR_XML_MALFORMED_END_ELEMENT; } require_ok((*parser->end_element_callback)(parser->current_tag, parser->data)); parser->depth--; if (parser->depth == 0){ *read = i + 1; return HCERR_OK; } change_state(&parser->state, OUTSIDE_ELEMENT, parser->depth); break; /* Expected tokens: * attribute_name * '/' * > */ case SCANNING_ATTRIBUTES: if (is_white_space(s[i])){ /*skip_white_space */ break; } if (is_name_first_char(s[i]) == TRUE) { change_state(&parser->state, SCANNING_ATTRIBUTE_NAME, parser->depth); require_ok(token_append(&parser->buffer_list, s[i])); } else if (s[i] == '/' && parser->start_tag == TRUE) { if (DEBUG == TRUE){ int j = 0; printf("SA Start element: %s\n", parser->current_tag); fprintf(stdout, "Start element: %s %d\n", parser->current_tag, parser->current_attribute); for (j = 0; j < parser->current_attribute; j++){ printf(" %s=\"%s\"", *(parser->attribute_names + j), *(parser->attribute_values + j)); } fprintf(stdout, "End element: %s\n", parser->current_tag); fflush(stdout); } require_ok((*parser->start_element_callback)(parser->current_tag, parser->data, parser->attribute_names, parser->attribute_values, parser->current_attribute)); require_ok((*parser->end_element_callback)(parser->current_tag, parser->data)); parser->current_attribute = 0; change_state(&parser->state, EXPECTING_RIGHT_BRACKET, parser->depth); } else if (s[i] == '>') { if (DEBUG == TRUE){ int j = 0; fprintf(stdout, "Start element event: %s %d\n", parser->current_tag, parser->current_attribute); for (j = 0; j < parser->current_attribute; j++){ printf(" %s=\"%s\"", *(parser->attribute_names + j), *(parser->attribute_values + j)); } } require_ok((*parser->start_element_callback)(parser->current_tag, parser->data, parser->attribute_names, parser->attribute_values, parser->current_attribute)); parser->current_attribute = 0; parser->depth++; change_state(&parser->state, OUTSIDE_ELEMENT, parser->depth); } else{ HC_ERR_LOG(("Unexpected character %c after close element. %d %s", s[i], i, s)); return HCERR_XML_MALFORMED_START_ELEMENT; } break; case SCANNING_ATTRIBUTE_NAME: if (s[i] == '='){ if (parser->current_attribute == parser->attribute_arrays_size){ require_ok(grow_attribute_arrays(parser)); } parser->attribute_names[parser->current_attribute] = token_finish(&parser->buffer_list); change_state(&parser->state, SCANNING_START_ATTRIBUTE_VALUE, parser->depth); } else if (is_name_char(s[i]) == TRUE) { require_ok(token_append(&parser->buffer_list, s[i])); } else{ HC_ERR_LOG(("Illegal char %c in attribute name. %i <<%s>>\n", s[i], i, s)); return HCERR_XML_BAD_ATTRIBUTE_NAME; } break; case SCANNING_START_ATTRIBUTE_VALUE: if (is_white_space(s[i])){ break; } else if (s[i] != '"'){ HC_ERR_LOG(("Attribute value does not begin with quote: '%c'. %i %s\n", s[i], i, s)); return HCERR_XML_BAD_ATTRIBUTE_NAME; } change_state(&parser->state, SCANNING_ATTRIBUTE_VALUE, parser->depth); break; case SCANNING_ATTRIBUTE_VALUE: if (s[i] == '\\') { if (parser->backslash == TRUE){ parser->backslash = FALSE; } else{ parser->backslash = TRUE; } } else if (s[i] == '"' && parser->backslash == FALSE) { parser->attribute_values[parser->current_attribute++] = token_finish(&parser->buffer_list); change_state(&parser->state, SCANNING_ATTRIBUTES, parser->depth); break; } require_ok(token_append(&parser->buffer_list, s[i])); break; } i++; } return HCERR_OK; }
Attr * qs_parse_attr(Doc *doc, const char *s, int len, int *pos) { int ii; int start_pos; int size; int novalue; char *name; char *value; Attr *attr; int use_quote_sq; int use_quote_dq; int backslash; if (! doc) { QX_LOGGER_FATAL("runtime exception: qs_parse_attr(): doc is null"); return NULL; } if (! doc->pool) { QX_LOGGER_FATAL("runtime exception: qs_parse_attr(): doc->pool is null"); return NULL; } if (! s) return NULL; use_quote_sq = 0; use_quote_dq = 0; backslash = 0; QX_LOGGER_DEBUG("start qs_parse_attr()"); /* ignore space */ ii = start_pos = qs_ignore_sp_and_quote(doc, s, len); QX_LOGGER_DEBUG_INT("len",len); /* get attr name */ for (;ii<len; ii++) { if (is_white_space(s[ii])) break; if (s[ii] == '=') break; } size = ii - start_pos; QX_LOGGER_DEBUG_INT("size", size); /* * not found */ if (size == 0) { *pos = ii; return NULL; } name = (char *)apr_palloc(doc->pool,size+1); memset(name, 0, size+1); memcpy(name, &s[start_pos], size); QX_LOGGER_DEBUG((char *)name); novalue = 0; /* find '=' */ for (;ii<len; ii++) { if (is_white_space(s[ii])) /* ignore */ continue; if (s[ii] == '=') ii++; else /* novalue */ novalue = 1; break; } if (ii == len) novalue = 1; size = 0; if (!novalue) { /* * ignore space */ ii += qs_ignore_sp(doc, &s[ii], len-ii); backslash = 0; for (;ii<len; ii++) { if (s[ii] == '\\') { backslash = 1; break; } if (s[ii] == '\'') { use_quote_sq = 1; ii++; break; } if (s[ii] == '"') { use_quote_dq = 1; ii++; break; } if (!is_white_space(s[ii])) break; } start_pos = ii; if (backslash && ii + 2 < len) ii+=2; backslash = 0; /* * get attr value */ for (;ii<len; ii++) { if (is_sjis_kanji(s[ii])) { ii++; continue; } if (is_sjis_kana(s[ii])) continue; if (is_white_space(s[ii])) { if (! use_quote_sq && ! use_quote_dq) break; } if (s[ii] == '\\') { ii++; continue; } if (s[ii] == '"' && use_quote_dq) break; if (s[ii] == '\'' && use_quote_sq) break; } size = ii - start_pos; QX_LOGGER_DEBUG_INT("size",size); } value = (char *)apr_palloc(doc->pool, size+1); memset(value, 0, size+1); if (size != 0) memcpy(value, &s[start_pos], size); attr = qs_new_attr(doc); attr->name = name; attr->value = value; QX_LOGGER_DEBUG("end qs_parse_attr()"); *pos = ii; return attr; }
// initialize the xstr table void lcl_xstr_init() { char chr, buf[4096]; char language_tag[512]; int i, z, index, rval; char *p_offset = NULL; int offset_lo = 0, offset_hi = 0; for (i=0; i<XSTR_SIZE; i++){ Xstr_table[i].str = NULL; } if ((rval = setjmp(parse_abort)) != 0) { mprintf(("Error parsing 'strings.tbl'\nError code = %i.\n", rval)); } else { // make sure localization is NOT running lcl_ext_close(); read_file_text("strings.tbl"); reset_parse(); // move down to the proper section memset(language_tag, 0, 512); strcpy(language_tag, "#"); strcat(language_tag, Lcl_languages[Lcl_current_lang].lang_name); if(skip_to_string(language_tag) != 1){ Error(LOCATION, NOX("Strings.tbl is corrupt")); } // parse all the strings in this section of the table while (!check_for_string("#")) { int num_offsets_on_this_line = 0; stuff_int(&index); stuff_string(buf, F_NAME, NULL, 4096); i = strlen(buf); while (i--) { if (!isspace(buf[i])) { break; } } // trim unneccesary end of string if (i >= 0) { // Assert(buf[i] == '"'); if (buf[i] != '"') { // probably an offset on this entry buf[i+1] = 0; // drop down a null terminator (prolly unnecessary) while(!is_white_space(buf[i])) i--; // back up over the potential offset while(is_white_space(buf[i])) i--; // now back up over intervening spaces num_offsets_on_this_line = 1; if (buf[i] != '"') { // could have a 2nd offset value (one for 640, one for 1024) // so back up again while(!is_white_space(buf[i])) i--; // back up over the 2nd offset while(is_white_space(buf[i])) i--; // now back up over intervening spaces num_offsets_on_this_line = 2; } p_offset = &buf[i+1]; // get ptr to string section with offset in it if (buf[i] != '"') { Error(LOCATION, NOX("Strings.tbl is corrupt")); // now its an error } } buf[i] = 0; } // copy string into buf z = 0; for (i=1; buf[i]; i++) { chr = buf[i]; if (chr == '\\') { chr = buf[++i]; if (chr == 'n') { chr = '\n'; } else if (chr == 'r') { chr = '\r'; } } buf[z++] = chr; } // null terminator on buf buf[z] = 0; // write into Xstr_table if (index >= 0 && index < XSTR_SIZE) { if (Xstr_table[index].str != NULL) { Warning(LOCATION, "Strings table index %d used more than once", index); } Xstr_table[index].str = strdup(buf); } // read offset information, assume 0 if nonexistant if (p_offset != NULL) { if (sscanf(p_offset, "%d%d", &offset_lo, &offset_hi) < num_offsets_on_this_line) { // whatever is in the file ain't a proper offset Error(LOCATION, NOX("Strings.tbl is corrupt")); } } Xstr_table[index].offset_x = offset_lo; if (num_offsets_on_this_line == 2) { Xstr_table[index].offset_x_hi = offset_hi; } else { Xstr_table[index].offset_x_hi = offset_lo; } // clear out our vars p_offset = NULL; offset_lo = 0; offset_hi = 0; num_offsets_on_this_line = 0; } } Xstr_inited = 1; }