/** * Trim left ' ', '\t', '\r', or '\n' */ char *ltrim(char *str) { if (str == NULL/* || *str == '\0'*/) return NULL; char c; /* ltrim */ for (c = *str; BLANK_CHAR(c); c = *(++str)) ; return str; }
/** * Trim right ' ', '\t', '\r', or '\n' */ char *rtrim(char *str) { if (str == NULL/* || *str == '\0'*/) return NULL; int len = strlen(str); if (len == 0) return str; char c; char *p = (char *) (str + len - 1); for (c = *p; BLANK_CHAR(c); c = *(--p)) ; *(p + 1) = '\0'; return str; }
void InfoFileReader::parseLine(const std::string& line, std::string& name, std::string& value) { logMsg(LOG_DEBUG, "Parsing info file line \'%s\'", line.c_str()); int state = STATE_INIT; name.erase(); value.erase(); for(std::string::size_type i = 0;i < line.length();i++) { const char c = line[i]; //Line beginning; if (state == STATE_INIT) { if (BLANK_CHAR(c)) continue; if (c == '#') return; if (validIdentChar(c)) { state = STATE_NAME; name += c; continue; } throw InfoFileSyntaxException(InfoFileSyntaxException::UnexpectedCharacter, m_currentLineNumber, line); }//Beginning; //Name; if (state == STATE_NAME) { if (validIdentChar(c)) { name += c; continue; } if (c == '=') { state = STATE_VALUE; continue; } if (BLANK_CHAR(c)) { state = STATE_BEFORE_EQUALS; continue; } if (c == '#') throw InfoFileSyntaxException(InfoFileSyntaxException::IncompleteLine, m_currentLineNumber, line); throw InfoFileSyntaxException(InfoFileSyntaxException::UnexpectedCharacter, m_currentLineNumber, line); }//Name; if (state == STATE_BEFORE_EQUALS) { if (BLANK_CHAR(c)) continue; if (c == '=') { state = STATE_VALUE; continue; } if (c == '#') throw InfoFileSyntaxException(InfoFileSyntaxException::IncompleteLine, m_currentLineNumber, line); throw InfoFileSyntaxException(InfoFileSyntaxException::UnexpectedCharacter, m_currentLineNumber, line); } //Before equals; if (state == STATE_VALUE) { if (c != '#' && c != '\\') { value += c; continue; } if (c == '#') return; assert(c == '\\'); if (i + 1>= line.length()) throw InfoFileSyntaxException(InfoFileSyntaxException::IncompleteLine, m_currentLineNumber, line); i++; if (line[i] != '#') throw InfoFileSyntaxException(InfoFileSyntaxException::UnexpectedCharacter, m_currentLineNumber, line); value += '#'; continue; }//Value; } if (state != STATE_INIT && state != STATE_VALUE) throw InfoFileSyntaxException(InfoFileSyntaxException::IncompleteLine, m_currentLineNumber, line); }