StringTools.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * @file StringTools.cpp
  3. // refactor-nice.pl: check this substitution
  4. // old: * @brief some string handling routines
  5. * @brief some std::string handling routines
  6. * @author Erik Rodner
  7. * @date 19.09.2007
  8. */
  9. #include <iostream>
  10. #include <vector>
  11. #include <string>
  12. #include <sstream>
  13. #include <assert.h>
  14. #include "StringTools.h"
  15. #define USE_REGEX_LIB
  16. #ifdef USE_REGEX_LIB
  17. #include <regex.h>
  18. #endif
  19. #include "ossettings.h"
  20. using namespace NICE;
  21. using namespace std;
  22. /** return base of a filename */
  23. std::string StringTools::baseName(const std::string& str,bool extension)
  24. {
  25. size_t begin = 0;
  26. size_t end = str.length() - 1;
  27. for (size_t i=0; i<str.length(); ++i) {
  28. if (str[i] == FILESEP) begin = i+1;
  29. }
  30. //leave extension iff true
  31. if (!extension)
  32. {
  33. for (size_t i=begin; i<str.length(); ++i) {
  34. if (str[i] == '.') end = i-1;
  35. }
  36. }
  37. return str.substr(begin,end - begin + 1);
  38. }
  39. void StringTools::normalize_string ( std::string & s )
  40. {
  41. size_t j = 0;
  42. while ( (j < s.length()) &&
  43. ((s[j] == ' ') || (s[j] == '\t') ) ) j++;
  44. int jj = s.length()-1;
  45. while ( (jj >= 0) &&
  46. ((s[jj] == ' ') || (s[jj] == ';') ||
  47. (s[jj] == '\t')) ) jj--;
  48. std::string news = "";
  49. for ( int k = j ; k <= jj ; k++ )
  50. {
  51. if ( s[k] != '\t' )
  52. news.push_back (s[k]);
  53. }
  54. s = news;
  55. }
  56. void StringTools::split ( const std::string & s, char splitChar, vector<string> & list )
  57. {
  58. size_t pos = 0;
  59. size_t oldpos = 0;
  60. while ( (pos = s.find(splitChar,oldpos)) != string::npos )
  61. {
  62. list.push_back ( s.substr(oldpos, pos-oldpos) );
  63. oldpos = pos+1;
  64. if ( oldpos >= s.length() ) break;
  65. }
  66. if ( oldpos < s.length() )
  67. list.push_back ( s.substr(oldpos) );
  68. for ( vector<string>::iterator i = list.begin(); i != list.end() ; i++ )
  69. normalize_string ( *i );
  70. }
  71. void StringTools::splitVector ( const std::string & s, char splitChar, NICE::Vector & x )
  72. {
  73. size_t pos = 0;
  74. size_t oldpos = 0;
  75. vector<double> xdyn;
  76. double f;
  77. while ( (pos = s.find(splitChar,oldpos)) != string::npos )
  78. {
  79. stringstream sstr;
  80. sstr << s.substr(oldpos, pos-oldpos);
  81. sstr >> f;
  82. xdyn.push_back ( f );
  83. oldpos = pos+1;
  84. if ( oldpos >= s.length() ) break;
  85. }
  86. if ( oldpos < s.length() )
  87. {
  88. stringstream sstr;
  89. sstr << s.substr(oldpos);
  90. sstr >> f;
  91. xdyn.push_back(f);
  92. }
  93. x = NICE::Vector(xdyn);
  94. }
  95. void StringTools::trimbounds ( std::string & value, char trimChar )
  96. {
  97. // remove "
  98. size_t pos1 = value.find(trimChar);
  99. size_t pos2 = value.rfind(trimChar);
  100. if ( (pos1 != pos2) &&
  101. (pos1 != string::npos) &&
  102. (pos2 != string::npos) )
  103. {
  104. value.erase( pos1, 1 );
  105. value.erase( pos2-1, 1 );
  106. }
  107. }
  108. /** @author Matti Bickel */
  109. std::string StringTools::trim(string s, const std::string& drop)
  110. {
  111. size_t findLastIndex = s.find_last_not_of(drop);
  112. if ( findLastIndex < s.size() )
  113. {
  114. std::string r=s.erase(findLastIndex+1);
  115. size_t findFirstIndex = r.find_first_not_of(drop);
  116. if ( findFirstIndex > 0 )
  117. return r.erase(0,findFirstIndex);
  118. else
  119. return r;
  120. } else
  121. {
  122. size_t findFirstIndex = s.find_first_not_of(drop);
  123. if ( findFirstIndex > 0 )
  124. return s.erase(0,findFirstIndex);
  125. else
  126. return s;
  127. }
  128. }
  129. std::string StringTools::chomp(string s)
  130. {
  131. return trim(trim(s, "\r"), "\n");
  132. }
  133. bool StringTools::regexSubstitute ( std::string & s, const std::string & regex, const std::string & subst )
  134. {
  135. #ifdef USE_REGEX_LIB
  136. vector<string> submatches;
  137. std::ostringstream os_regex;
  138. os_regex << "(.*)" << regex << "(.*)";
  139. bool ok = regexMatch ( s, os_regex.str(), submatches );
  140. if ( ! ok ) return false;
  141. assert ( submatches.size() >= 3 );
  142. std::string substc = subst;
  143. for ( uint k = 2 ; k < submatches.size()-1 ; k++ )
  144. {
  145. uint index = k - 1;
  146. std::ostringstream os;
  147. os << "\\" << index;
  148. size_t pos = subst.find(os.str());
  149. assert( pos != string::npos );
  150. substc.replace ( pos, 2, submatches[k] );
  151. }
  152. s = submatches[1] + substc + submatches[submatches.size()-1];
  153. return true;
  154. #else
  155. fthrow ( Exception, "You have to install the regex lib to use this function!");
  156. #endif
  157. }
  158. bool StringTools::regexMatch ( const string & s, const string & regex )
  159. {
  160. vector<string> submatches;
  161. return regexMatch ( s, regex, submatches );
  162. }
  163. bool StringTools::regexMatch ( const string & s, const string & regex,
  164. vector<string> & submatches )
  165. {
  166. submatches.clear();
  167. int status;
  168. regex_t re;
  169. const int max_num_matches = 100;
  170. regmatch_t pmatch[max_num_matches];
  171. if (regcomp(&re, regex.c_str(), REG_EXTENDED) != 0) {
  172. fthrow ( Exception, "The regular expression <" << regex << "> is invalid.");
  173. }
  174. status = regexec(&re, s.c_str(), max_num_matches, pmatch, 0);
  175. if (status != 0) {
  176. regfree(&re);
  177. return false;
  178. }
  179. for (uint i = 0; !status && i <= re.re_nsub; i++)
  180. {
  181. char submatch[1024];
  182. strncpy (submatch, s.c_str() + pmatch[i].rm_so,
  183. pmatch[i].rm_eo - pmatch[i].rm_so);
  184. submatch[pmatch[i].rm_eo - pmatch[i].rm_so] = '\0';
  185. submatches.push_back(submatch);
  186. }
  187. regfree(&re);
  188. return true;
  189. }