- #ifndef UTILITYPARSER_H__
- #define UTILITYPARSER_H__
- #include <string>
- #include <vector>
- class UtilityParser {
- void InitParser(void)
- {
- SetDelimiters(",t ");
- m_check_quotes = 0;
- m_strip = 0;
- m_max_args = 0;
- m_check_single_quotes = 0;
- m_include_quotes = 0;
- m_keep_args = 0;
- m_arg_index = 0;
- }
- protected:
- std::vector<char> m_delimiters;
- std::vector<std::string> m_data;
- int m_check_quotes;
- int m_check_single_quotes;
- int m_strip;
- int m_max_args;
- int m_include_quotes;
- int m_keep_args;
- int m_arg_index;
- public:
- UtilityParser(void){ InitParser();}
- UtilityParser(const char * delimiters)
- {
- InitParser();
- SetDelimiters(delimiters);
- }
- UtilityParser(const char * delimiters, const char * str)
- {
- InitParser();
- SetDelimiters(delimiters);
- ParseString(str);
- }
- ~UtilityParser(void){}
- std::vector<std::string> * GetData(void){ return &m_data;}
- int StripSpaces(int i = -1)
- {
- if (i == 0)
- m_strip = 0;
- else if (i == 1)
- m_strip = 1;
- return m_strip;
- }
- void CheckQuotes(void){ m_check_quotes = 1;}
- void CheckSingleQuotes(void){ m_check_single_quotes = 1;}
- void IncludeQuotes(void){ m_include_quotes = 1;}
- void ExcludeQuotes(void){ m_include_quotes = 0;}
- void KeepAllArguments(void){ m_keep_args = 1;}
- void KeepNonZeroArguments(void){ m_keep_args = 0;}
- void IncrementArgumentIndex(int incr=1){ m_arg_index += incr;}
- int GetArgument(int i, std::string& s)
- {
- int result = 0;
- if (i < m_data.size())
- {
- result++;
- s = m_data[i];
- }
- return result;
- }
- int GetSize(void){ return m_data.size();}
- void AppendDelimiter(const char c)
- {
- m_delimiters.push_back(c);
- }
- void SetDelimiters(const char * s)
- {
- if (s)
- {
- int size = strlen(s);
- if (size > 0)
- {
- m_delimiters.clear();
- for (int i=0;i<size;i++){
- m_delimiters.push_back(s[i]);
- }
- }
- }
- }
- int IsDelimiter(char c)
- {
- int result = 0;
- char tempc;
- int size = m_delimiters.size();
- for (int i=0;i<size;i++){
- tempc = m_delimiters[i];
- if (tempc == c)
- {
- result++;
- break;
- }
- }
- return result;
- }
- int IsQuote(char c)
- {
- int result = 0;
- if (c == (char)34)
- {
- result++;
- }
- return result;
- }
- int IsSingleQuote(char c)
- {
- int result = 0;
- if (c == (char)39)
- {
- result++;
- }
- return result;
- }
- void ParseString(const char * s)
- {
- if (s)
- {
- std::string temp = s;
- ParseString(temp);
- }
- }
- void SetMaxArgs(int args){ m_max_args = args;}
- void ParseString(const std::string& s)
- {
- m_data.clear();
- m_arg_index = 0;
- std::string token = "";
- char c;
- int i;
- int inside_quotes = 0;
- int length = s.length();
- for (i=0;i<length;i++){
- c = s.c_str()[i];
- if (m_check_quotes && IsQuote(c))
- {
- if (m_include_quotes)
- {
- if (inside_quotes)
- {
- token += c;
- m_data.push_back(token);
- token = "";
- }
- else
- {
- if (!m_keep_args)
- {
- if (token.length() > 0)
- {
- m_data.push_back(token);
- }
- }
- else
- {
- m_data.push_back(token);
- }
- token = c;
- }
- }
- else
- {
- if (!m_keep_args)
- {
- if (token.length() > 0)
- {
- m_data.push_back(token);
- }
- }
- else
- {
- m_data.push_back(token);
- }
- token = "";
- }
- inside_quotes = !inside_quotes;
- }
- else if (m_check_single_quotes && IsSingleQuote(c))
- {
- if (m_include_quotes)
- {
- if (inside_quotes)
- {
- token += c;
- m_data.push_back(token);
- token = "";
- }
- else
- {
- if (!m_keep_args)
- {
- if (token.length() > 0)
- {
- m_data.push_back(token);
- }
- }
- else
- {
- m_data.push_back(token);
- }
- token = "";
- token += c;
- }
- }
- else
- {
- if (!m_keep_args)
- {
- if (token.length() > 0)
- {
- m_data.push_back(token);
- }
- }
- else
- {
- m_data.push_back(token);
- }
- token = "";
- }
- inside_quotes = !inside_quotes;
- }
- else if ((m_check_quotes||m_check_single_quotes) && inside_quotes)
- {
- token += c;
- }
- else if (IsDelimiter(c) || (c<32))
- {
- if (!m_keep_args)
- {
- if (token.length() > 0)
- {
- m_data.push_back(token);
- }
- }
- else
- {
- m_data.push_back(token);
- }
- token = "";
- // max arg check
- if (m_max_args && m_data.size() >= m_max_args)
- break;
- }
- else
- {
- token += c;
- }
- }
- if (!m_keep_args)
- {
- if (token.length() > 0)
- m_data.push_back(token);
- }
- else
- {
- m_data.push_back(token);
- }
- }
- bool IsNumber(std::string& s)
- {
- return IsNumber(s.c_str());
- }
- bool IsNumber(const char * s)
- {
- bool result = (IsIntNumber(s) || IsFloatNumber(s));
- return result;
- }
- bool IsFloatNumber(const char * s)
- {
- bool result = false;
- if (s)
- {
- std::string us = s;
- return IsFloatNumber(us);
- }
- return result;
- }
- bool IsFloatNumber(std::string& s)
- {
- bool result = false;
- char c;
- int size = s.length();
- bool got_decimal = false;
- for (int i=0;i<size;i++){
- c = s.c_str()[i];
- if (i == 0)
- {
- if (c == '.')
- {
- result = true;
- got_decimal = true;
- }
- else if (c == '-')
- {
- result = true;
- }
- else if ((c>=48) && (c<=57))
- {
- result = true;
- }
- else
- {
- break;
- }
- }
- else
- {
- if (c == '.' && !got_decimal)
- {
- got_decimal = true;
- }
- else if ((c>=48 && c<=57))
- {
- }
- else
- {
- result = false;
- break;
- }
- }
- }
- return result;
- }
- bool IsIntNumber(const char * s)
- {
- bool result = false;
- if (s)
- {
- std::string us = s;
- result = IsIntNumber(us);
- }
- return result;
- }
- bool IsIntNumber(std::string& s)
- {
- bool result = false;
- char c;
- int size = s.length();
- for (int i=0;i<size;i++){
- c = s.c_str()[i];
- if (i == 0)
- {
- if (c == '-')
- {
- result = true;
- }
- else if ((c>=48) && (c<=57))
- {
- result = true;
- }
- else
- {
- break;
- }
- }
- else
- {
- if ((c>=48 && c<=57))
- {
- }
- else
- {
- result = false;
- break;
- }
- }
- }
- return result;
- }
- };
- #endif