util.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include <malloc.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <functional>
  8. #include <algorithm>
  9. #include "util.h"
  10. //////////////////////////////////////////////////////////////////////////////////
  11. // strucase
  12. std::string & strucase(std::string &s)
  13. {
  14. std::transform(s.begin(), s.end(), s.begin(), ::toupper);
  15. return s;
  16. }
  17. //////////////////////////////////////////////////////////////////////////////////
  18. // strlcase
  19. std::string & strlcase(std::string &s)
  20. {
  21. std::transform(s.begin(), s.end(), s.begin(), ::tolower);
  22. return s;
  23. }
  24. //////////////////////////////////////////////////////////////////////////////////
  25. // trim from start (in place)
  26. std::string & ltrim(std::string &s)
  27. {
  28. s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
  29. return s;
  30. }
  31. //////////////////////////////////////////////////////////////////////////////////
  32. // trim from end (in place)
  33. std::string & rtrim(std::string &s)
  34. {
  35. s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
  36. return s;
  37. }
  38. //////////////////////////////////////////////////////////////////////////////////
  39. // trim from both ends (in place)
  40. std::string & trim(std::string &s)
  41. {
  42. return ltrim(rtrim(s));
  43. }
  44. std::string trim(const std::string &s)
  45. {
  46. std::string t(s);
  47. return ltrim(rtrim(t));
  48. }
  49. //////////////////////////////////////////////////////////////////////////////////
  50. // SplitString
  51. size_t SplitString(const std::string &str, const std::regex &reg, std::vector<std::string> &vec)
  52. {
  53. const std::sregex_token_iterator end;
  54. for(std::sregex_token_iterator iter(str.begin(), str.end(), reg); iter != end; ++iter)
  55. vec.push_back(*iter);
  56. return vec.size();
  57. }
  58. size_t SplitString(const std::string &str, const char *regxsplit, std::vector<std::string> &vec)
  59. {
  60. const std::regex reg(regxsplit);
  61. return SplitString(str, reg, vec);
  62. /* const std::sregex_token_iterator end;
  63. for(std::sregex_token_iterator iter(str.begin(), str.end(), reg); iter != end; ++iter)
  64. vec.push_back(*iter);*/
  65. }
  66. std::string JoinStringArray(const std::vector<std::string> &vec, int nPosStart, int nPosEnd, const char *sep)
  67. {
  68. auto vSize = vec.size();
  69. if( vSize > 0 &&
  70. nPosStart < (int)vSize &&
  71. nPosEnd < (int)vSize)
  72. {
  73. std::ostringstream os;
  74. auto itBegin = (nPosStart <= 0) ? vec.begin() : vec.begin() + nPosStart;
  75. auto itEnd = (nPosEnd < 0) ? vec.end() : vec.begin() + (nPosEnd + 1);
  76. if(itBegin != itEnd)
  77. os << *itBegin++;
  78. while(itBegin != itEnd)
  79. {
  80. os << sep;
  81. os << *itBegin++;
  82. }
  83. return os.str();
  84. }
  85. return "";
  86. }
  87. //////////////////////////////////////////////////////////////////////////////////
  88. ssize_t ReadFile(const char *pszPath, void **ppBuf, size_t nBlockSize)
  89. {
  90. if(!ppBuf)
  91. return -1;
  92. *ppBuf = NULL;
  93. if(!nBlockSize)
  94. nBlockSize = sysconf(_SC_PAGESIZE);
  95. int fd;
  96. ssize_t l, nRead = 0;
  97. if((fd = open(pszPath, O_RDONLY)) == -1)
  98. return -1;
  99. // off_t size = lseek(fd, 0, SEEK_END);
  100. // perror("lseek - ");
  101. char *p = (char*)malloc(nBlockSize);
  102. while((l = read(fd, p + nRead, nBlockSize)) == (ssize_t)nBlockSize)
  103. {
  104. nRead += l;
  105. p = (char*)realloc(p, nRead + nBlockSize);
  106. }
  107. close(fd);
  108. if(l == -1)
  109. {
  110. free(p);
  111. return -1;
  112. }
  113. nRead += l;
  114. p[nRead] = '\n';
  115. *ppBuf = p;
  116. return nRead;
  117. }
  118. //////////////////////////////////////////////////////////////////////////////////
  119. ssize_t ReadFile(const char *pszPath, std::string &str)
  120. {
  121. char *pszBuf;
  122. ssize_t nRet = ReadFile(pszPath, (void**)&pszBuf, 0);
  123. if(nRet > 0)
  124. str = pszBuf;
  125. if(pszBuf)
  126. free(pszBuf);
  127. return nRet;
  128. }
  129. //////////////////////////////////////////////////////////////////////////////////
  130. int FindStringInList(const std::string &str, const char *pszList[], size_t nCntList)
  131. {
  132. for(size_t i = 0; i < nCntList; i++)
  133. {
  134. if(!str.compare(pszList[i]))
  135. return i;
  136. }
  137. return -1;
  138. }
  139. //////////////////////////////////////////////////////////////////////////////////
  140. int RemoveDoubleStringsFromVector(std::vector<std::string> &vec)
  141. {
  142. int nRet = 0;
  143. for(auto it1 = vec.begin(); it1 != vec.end(); ++it1)
  144. {
  145. const std::string &str1 = *it1;
  146. for(auto it2 = it1 + 1; it2 != vec.end(); )
  147. {
  148. const std::string &str2 = *it2;
  149. if(!str1.compare(str2))
  150. {
  151. it2 = vec.erase(it2);
  152. nRet++;
  153. }
  154. else
  155. ++it2;
  156. }
  157. }
  158. return nRet;
  159. }
  160. //////////////////////////////////////////////////////////////////////////////////
  161. int RemoveStringFromVector(const std::string &str, std::vector<std::string> &vec)
  162. {
  163. int nRet = 0;
  164. for(auto it = vec.begin(); it != vec.end(); )
  165. {
  166. const std::string &str2 = *it;
  167. if(!str.compare(str2))
  168. {
  169. it = vec.erase(it);
  170. nRet++;
  171. }
  172. else
  173. ++it;
  174. }
  175. return nRet;
  176. }
  177. //////////////////////////////////////////////////////////////////////////////////
  178. std::vector<std::string>::iterator FindString(const std::string &str, std::vector<std::string> &vec)
  179. {
  180. auto it = vec.begin();
  181. for(; it != vec.end(); it++)
  182. {
  183. if(!str.compare(*it))
  184. break;
  185. }
  186. return it;
  187. }