src/shepp/SheppStrUtil.H

Go to the documentation of this file.
00001 /* ${copyright}$ */
00002 /* $Id: SheppStrUtil.H 744 2006-05-22 20:38:31Z eduardo $ */
00007 #ifndef __SHEPP_STR_UTIL_H__
00008 #define __SHEPP_STR_UTIL_H__
00009 
00010 #include <string>
00011 #include <vector>
00012 
00013 using std::string;
00014 using std::vector;
00015 
00017 class SheppStrUtil
00018 {
00019 public:
00021 
00025   static char *trim(char *line)
00026   {
00027     
00028     // remove leading white spaces by incrementing pointer to beginning
00029     // of line
00030     char *head;
00031     for (head = line; whitespace(*head); head++)
00032       ;
00033     
00034     // returns if head points to the end of line
00035     if (*head == '\0') {
00036       return head;
00037     }
00038     
00039     // remove ending white spaces
00040     char *tail;
00041     tail = head + strlen(head) - 1;
00042     while (tail > head && whitespace(*tail)) {
00043       tail--;
00044     }
00045     
00046     // ends the string at position where a white space was last seen
00047     *++tail = '\0';
00048     
00049     return head;
00050   }
00051   
00053 
00058   static vector<string> parse_line(char *line, bool loop = true)
00059   {
00060     vector<string> words;
00061     int from;
00062     int to;
00063     char word[1024];
00064     
00065     do {
00066       from = 0;
00067       to = 1;
00068       
00069       // ignore leading white spaces
00070       while (line[from] != '\0' && whitespace(line[from])) {
00071         line++;
00072       }
00073       
00074       // find end of this word
00075       while (line[to] != '\0' && !whitespace(line[to])) {
00076         to++;
00077       }
00078       
00079       if (line[to] == '\0') {
00080         // this is the last word
00081         loop = false;
00082       }
00083       
00084       // insert found word in vector
00085       strncpy(word, line, to - from);
00086       word[to - from] = '\0';
00087       if (strlen(word) > 0) {
00088         words.push_back((string) word);
00089       }
00090       
00091       line += to + 1;
00092     } while (loop);
00093     
00094     return words;
00095   }
00096 
00106   static int split(string input, string &first, string &second,
00107                    string splitter, bool relaxed = false)
00108   {
00109     int split_pos = input.find(splitter, 0);
00110     
00111     // first can never be empty
00112     if (split_pos == 0) {
00113       return -1;
00114     }
00115     first = input.substr(0, split_pos);
00116     
00117     // second can be empty if relaxed is true
00118     if (split_pos == -1 ||
00119         split_pos == (int) (input.length() - 1)) {
00120       if (relaxed) {
00121         second = "";
00122       } else {
00123         return -1;
00124       }
00125     } else {
00126       second = input.substr(split_pos + 1);
00127     }
00128     
00129     return 0;  
00130   }
00131 
00133 
00138   static int quote_gathering(vector<string> &words, string &gather)
00139   {
00140     string tmp_str = gather.substr(0, 1);
00141 
00142     if (words.empty() && tmp_str == "\"") {
00143       return -1;
00144     }
00145 
00146     if (tmp_str == "\"") {
00147       gather = gather.substr(1, gather.length() - 1);
00148 
00149       tmp_str = gather.substr(gather.length() - 1);
00150       while (tmp_str != "\"") {
00151         if (words.empty()) {
00152           return -1;
00153         }
00154 
00155         gather += " " + words[0];
00156 
00157         words.erase(words.begin());
00158         tmp_str = gather.substr(gather.length() - 1);
00159       }
00160 
00161       //remove starting and ending quotes
00162       gather = gather.substr(0, gather.length() - 1);
00163     }
00164 
00165     return 0;
00166   }
00167 
00169 
00173   static string doc2id(const string &doc)
00174   {
00175     string numbers = doc;
00176 
00177     StrUtil::gsub(numbers, ".", "");
00178     StrUtil::gsub(numbers, "/", "");
00179     StrUtil::gsub(numbers, "-", "");
00180 
00181     return numbers;
00182   }
00183 };
00184 
00185 #endif //__SHEPP_STR_UTIL_H__

Generated on Thu Jun 8 17:40:01 2006 for libepp_nicbr by  doxygen 1.4.6