Parser.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 $Header$
00004 
00005 */
00006 
00007 #include <string.h>
00008 #include <ctype.h>
00009 
00010 #include "Error.h"
00011 
00012 #include "Parser.h"
00013 
00014 // Public methods.
00015 
00016 int Parser::getValue(const String &str, const String &key, String &value, const CaseSensitivity caseSensitive,
00017                      const unsigned int maxValueSize)
00018 {
00019   int strLen, keyLen;
00020   const char *p, *s = CSTRING(str);
00021   bool foundValue = false, quot = false;
00022 
00023   value = "";
00024 
00025   if (str.empty() || key.empty())
00026     return OK;
00027 
00028   strLen = str.length();
00029   keyLen = key.length();
00030 
00031   if (keyLen >= strLen)
00032     return OK;
00033 
00034   p = caseSensitive == CASE_SENSITIVE ? strstr(s, key.c_str()) : strcasestr(s, key.c_str());
00035   if (!p)
00036     return OK;
00037 
00038   p += keyLen;
00039   while (p < s + strLen) {
00040     if (!quot && strchr(PARSER_DELIMITERS, *p) != NULL) {
00041       if (foundValue)
00042         break;
00043     } else {
00044         if (!foundValue) {
00045           if (*p == PARSER_QUOT) {
00046             quot = 1;
00047             foundValue = 1;
00048             ++p;
00049             continue;
00050           }
00051         } else
00052             if (quot && *p == PARSER_QUOT) {
00053               quot = 0;
00054               ++p;
00055               continue;
00056             }
00057         foundValue = true;
00058         value += *p;
00059         if (maxValueSize && value.length() >= maxValueSize)
00060           break;
00061       }
00062     ++p;
00063   }
00064 
00065   return OK;
00066 }
00067 #include "Utils.h"
00068 int Parser::split(const String &str, StringList &values, const unsigned int maxValuesNum,
00069                   const String &delimiters, const bool combineDelimiters)
00070 {
00071   unsigned int i;
00072   String token;
00073   const char *d = CSTRING(delimiters);
00074   unsigned int strLen = str.length();
00075 
00076   values.clear();
00077 
00078   for (i = 0; i < strLen; ++i)
00079     if (strchr(d, str[i]) != NULL) {
00080       if (!combineDelimiters || !token.empty()) {
00081         if (maxValuesNum > 0 && values.size() >= maxValuesNum - 1) {
00082           values.push_back(token + str.substr(i));
00083           return OK;
00084         }
00085         values.push_back(token);
00086         token.clear();
00087       }
00088     } else
00089         token += str[i];
00090 
00091   if (!token.empty())
00092     values.push_back(token);
00093 
00094   return OK;
00095 }
00096 
00097 int Parser::split(char *str, char **values, unsigned int maxValuesNum, unsigned int &valuesNum, const char *delimiters)
00098 {
00099   int strLen;
00100   unsigned int i = 0;
00101   char *p;
00102   int foundValue = 0;
00103 
00104   if (!str || !values || !delimiters)
00105     return ERROR(MSG_NULL_POINTER);
00106 
00107   valuesNum = 0;
00108 
00109   if ((strLen = strlen(str)) == 0)
00110     return OK;
00111 
00112   p = str;
00113   while (p < str + strLen) {
00114     if (strchr(delimiters, *p) != NULL) {
00115       if (foundValue) {
00116         *p = '\0';
00117         ++i;
00118         foundValue = 0;
00119         if (i >= maxValuesNum)
00120           break;
00121       }
00122     } else {
00123         if (!foundValue)
00124           values[i] = p;
00125         foundValue = 1;
00126       }
00127     ++p;
00128   }
00129   if (foundValue)
00130     ++i;
00131   valuesNum = i;
00132 
00133   return OK;
00134 }
00135 
00136 String Parser::join(const StringList &values, const String &delimiter)
00137 {
00138   String result;
00139   unsigned int i;
00140   unsigned int size = values.size();
00141 
00142   for (i = 0; i < size; ++i)
00143     result += values[i] + (i == size - 1 ? "" : delimiter);
00144 
00145   return result;
00146 }
00147 
00148 String Parser::join(const char *tokens[], const unsigned int tokensNum, const char *delimiter)
00149 {
00150   if (tokensNum == 0 || !tokens || !delimiter)
00151     return "";
00152 
00153   String result;
00154   unsigned int i;
00155 
00156   for (i = 0; i < tokensNum; ++i)
00157     result += String(tokens[i]) + (i == tokensNum - 1 ? "" : delimiter);
00158 
00159   return result;
00160 }
00161 
00162 int Parser::parseAddress(const String &str, String &ip, String &port)
00163 {
00164   StringList values;
00165   int res;
00166 
00167   if (FAILED(res = split(str, values, 0, PARSER_ADDRESS_DELIMITERS)))
00168     return ERROR_BACKTRACE(res);
00169 
00170   if (values.size() == 0 || values.size() > 2)
00171     return ERROR(ERROR_PARSER_GENERAL, str.c_str());
00172 
00173   ip = values[0];
00174   port = values.size() > 1 ? values[1] : "";
00175 
00176   return OK;
00177 }

Generated on Thu Sep 6 20:11:25 2007 for Pylon Application Platform by  doxygen 1.5.1