Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. // g2o - General Graph Optimization
  2. // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above copyright
  12. // notice, this list of conditions and the following disclaimer in the
  13. // documentation and/or other materials provided with the distribution.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  17. // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  18. // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  21. // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26.  
  27. #include "string_tools.h"
  28. #include "os_specific.h"
  29. #include "macros.h"
  30.  
  31. #include <cctype>
  32. #include <string>
  33. #include <cstdarg>
  34. #include <cstring>
  35. #include <algorithm>
  36. #include <cstdio>
  37. #include <iostream>
  38. #include <iterator>
  39.  
  40. #if (defined (UNIX) || defined(CYGWIN)) && !defined(ANDROID)
  41. #include <wordexp.h>
  42. #endif
  43.  
  44. //vasprintf
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #include <stdarg.h>
  48.  
  49. namespace g2o {
  50.  
  51. using namespace std;
  52.  
  53. std::string trim(const std::string& s)
  54. {
  55. if(s.length() == 0)
  56. return s;
  57. string::size_type b = s.find_first_not_of(" \t\n");
  58. string::size_type e = s.find_last_not_of(" \t\n");
  59. if(b == string::npos)
  60. return "";
  61. return std::string(s, b, e - b + 1);
  62. }
  63.  
  64. std::string trimLeft(const std::string& s)
  65. {
  66. if(s.length() == 0)
  67. return s;
  68. string::size_type b = s.find_first_not_of(" \t\n");
  69. string::size_type e = s.length() - 1;
  70. if(b == string::npos)
  71. return "";
  72. return std::string(s, b, e - b + 1);
  73. }
  74.  
  75. std::string trimRight(const std::string& s)
  76. {
  77. if(s.length() == 0)
  78. return s;
  79. string::size_type b = 0;
  80. string::size_type e = s.find_last_not_of(" \t\n");
  81. if(b == string::npos)
  82. return "";
  83. return std::string(s, b, e - b + 1);
  84. }
  85.  
  86. std::string strToLower(const std::string& s)
  87. {
  88. string ret;
  89. std::transform(s.begin(), s.end(), back_inserter(ret), (int(*)(int)) std::tolower);
  90. return ret;
  91. }
  92.  
  93. std::string strToUpper(const std::string& s)
  94. {
  95. string ret;
  96. std::transform(s.begin(), s.end(), back_inserter(ret), (int(*)(int)) std::toupper);
  97. return ret;
  98. }
  99.  
  100. std::string formatString(const char* fmt, ...)
  101. {
  102. char* auxPtr = NULL;
  103. va_list arg_list;
  104. va_start(arg_list, fmt);
  105. int len;
  106. char *buffer;
  107.  
  108. len = _vscprintf(fmt, arg_list) // _vscprintf doesn't count
  109. + 1; // terminating '\0'
  110.  
  111. buffer = (char*)malloc(len * sizeof(char));
  112.  
  113. //int numChar = vasprintf(&auxPtr, fmt, arg_list);
  114. int numChar = vsprintf(buffer, fmt, arg_list); // C4996
  115. va_end(arg_list);
  116. string retString;
  117. if (numChar != -1)
  118. retString = buffer;//auxPtr;
  119. else {
  120. cerr << __PRETTY_FUNCTION__ << ": Error while allocating memory" << endl;
  121. }
  122. //free(auxPtr);
  123. free(buffer);
  124. return retString;
  125. }
  126.  
  127. int strPrintf(std::string& str, const char* fmt, ...)
  128. {
  129. //char* auxPtr = NULL;
  130. va_list arg_list;
  131. va_start(arg_list, fmt);
  132. int len;
  133. char *buffer;
  134.  
  135. len = _vscprintf(fmt, arg_list) // _vscprintf doesn't count
  136. + 1; // terminating '\0'
  137.  
  138. buffer = (char*)malloc(len * sizeof(char));
  139. //int numChars = vasprintf(&auxPtr, fmt, arg_list);
  140. int numChars = vsprintf(buffer, fmt, arg_list); // C4996
  141. va_end(arg_list);
  142. str = buffer;//auxPtr;
  143. //free(auxPtr);
  144. free(buffer);
  145. return numChars;
  146. }
  147.  
  148. std::string strExpandFilename(const std::string& filename)
  149. {
  150. #if (defined (UNIX) || defined(CYGWIN)) && !defined(ANDROID)
  151. string result = filename;
  152. wordexp_t p;
  153.  
  154. wordexp(filename.c_str(), &p, 0);
  155. if(p.we_wordc > 0) {
  156. result = p.we_wordv[0];
  157. }
  158. wordfree(&p);
  159. return result;
  160. #else
  161. (void) filename;
  162. std::cerr << "WARNING: " << __PRETTY_FUNCTION__ << " not implemented" << std::endl;
  163. return std::string();
  164. #endif
  165. }
  166.  
  167. std::vector<std::string> strSplit(const std::string& str, const std::string& delimiters)
  168. {
  169. std::vector<std::string> tokens;
  170. string::size_type lastPos = 0;
  171. string::size_type pos = 0;
  172.  
  173. do {
  174. pos = str.find_first_of(delimiters, lastPos);
  175. tokens.push_back(str.substr(lastPos, pos - lastPos));
  176. lastPos = pos + 1;
  177. } while (string::npos != pos);
  178.  
  179. return tokens;
  180. }
  181.  
  182. bool strStartsWith(const std::string& s, const std::string& start)
  183. {
  184. if (s.size() < start.size())
  185. return false;
  186. return equal(start.begin(), start.end(), s.begin());
  187. }
  188.  
  189. bool strEndsWith(const std::string& s, const std::string& end)
  190. {
  191. if (s.size() < end.size())
  192. return false;
  193. return equal(end.rbegin(), end.rend(), s.rbegin());
  194. }
  195.  
  196. int readLine(std::istream& is, std::stringstream& currentLine)
  197. {
  198. if (is.eof())
  199. return -1;
  200. currentLine.str("");
  201. currentLine.clear();
  202. is.get(*currentLine.rdbuf());
  203. if (is.fail()) // fail is set on empty lines
  204. is.clear();
  205. G2O_FSKIP_LINE(is); // read \n not read by get()
  206. return static_cast<int>(currentLine.str().size());
  207. }
  208.  
  209. } // end namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement