StringTools.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. std::string r=s.erase(s.find_last_not_of(drop)+1);
  111. return r.erase(0,r.find_first_not_of(drop));
  112. }
  113. std::string StringTools::chomp(string s)
  114. {
  115. return trim(trim(s, "\r"), "\n");
  116. }
  117. bool StringTools::regexSubstitute ( std::string & s, const std::string & regex, const std::string & subst )
  118. {
  119. #ifdef NICE_USELIB_REGEX
  120. vector<string> submatches;
  121. std::ostringstream os_regex;
  122. os_regex << "(.*)" << regex << "(.*)";
  123. bool ok = regexMatch ( s, os_regex.str(), submatches );
  124. if ( ! ok ) return false;
  125. assert ( submatches.size() >= 3 );
  126. std::string substc = subst;
  127. for ( uint k = 2 ; k < submatches.size()-1 ; k++ )
  128. {
  129. uint index = k - 1;
  130. std::ostringstream os;
  131. os << "\\" << index;
  132. size_t pos = subst.find(os.str());
  133. assert( pos != string::npos );
  134. substc.replace ( pos, 2, submatches[k] );
  135. }
  136. s = submatches[1] + substc + submatches[submatches.size()-1];
  137. return true;
  138. #else
  139. fthrow ( Exception, "You have to install the regex lib to use this function!");
  140. #endif
  141. }
  142. bool StringTools::regexMatch ( const string & s, const string & regex )
  143. {
  144. #ifdef NICE_USELIB_REGEX
  145. vector<string> submatches;
  146. return regexMatch ( s, regex, submatches );
  147. #else
  148. fthrow ( Exception, "You have to install the regex lib to use this function!");
  149. #endif
  150. }
  151. bool StringTools::regexMatch ( const string & s, const string & regex,
  152. vector<string> & submatches )
  153. {
  154. #ifdef NICE_USELIB_REGEX
  155. submatches.clear();
  156. int status;
  157. regex_t re;
  158. const int max_num_matches = 100;
  159. regmatch_t pmatch[max_num_matches];
  160. if (regcomp(&re, regex.c_str(), REG_EXTENDED) != 0) {
  161. fthrow ( Exception, "The regular expression <" << regex << "> is invalid.");
  162. }
  163. status = regexec(&re, s.c_str(), max_num_matches, pmatch, 0);
  164. if (status != 0) {
  165. regfree(&re);
  166. return false;
  167. }
  168. for (uint i = 0; !status && i <= re.re_nsub; i++)
  169. {
  170. char submatch[1024];
  171. strncpy (submatch, s.c_str() + pmatch[i].rm_so,
  172. pmatch[i].rm_eo - pmatch[i].rm_so);
  173. submatch[pmatch[i].rm_eo - pmatch[i].rm_so] = '\0';
  174. submatches.push_back(submatch);
  175. }
  176. regfree(&re);
  177. return true;
  178. #else
  179. fthrow ( Exception, "You have to install the regex lib to use this function!");
  180. #endif
  181. }