StringTools.cpp 4.8 KB

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