StringTools.cpp 5.4 KB

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