123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- #include <malloc.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <functional>
- #include <algorithm>
- #include "util.h"
- //////////////////////////////////////////////////////////////////////////////////
- // strucase
- std::string & strucase(std::string &s)
- {
- std::transform(s.begin(), s.end(), s.begin(), ::toupper);
- return s;
- }
- //////////////////////////////////////////////////////////////////////////////////
- // strlcase
- std::string & strlcase(std::string &s)
- {
- std::transform(s.begin(), s.end(), s.begin(), ::tolower);
- return s;
- }
- //////////////////////////////////////////////////////////////////////////////////
- // trim from start (in place)
- std::string & ltrim(std::string &s)
- {
- s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
- return s;
- }
- //////////////////////////////////////////////////////////////////////////////////
- // trim from end (in place)
- std::string & rtrim(std::string &s)
- {
- s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
- return s;
- }
- //////////////////////////////////////////////////////////////////////////////////
- // trim from both ends (in place)
- std::string & trim(std::string &s)
- {
- return ltrim(rtrim(s));
- }
- std::string trim(const std::string &s)
- {
- std::string t(s);
- return ltrim(rtrim(t));
- }
- //////////////////////////////////////////////////////////////////////////////////
- // SplitString
- size_t SplitString(const std::string &str, const std::regex ®, std::vector<std::string> &vec)
- {
- const std::sregex_token_iterator end;
- for(std::sregex_token_iterator iter(str.begin(), str.end(), reg); iter != end; ++iter)
- vec.push_back(*iter);
- return vec.size();
- }
- size_t SplitString(const std::string &str, const char *regxsplit, std::vector<std::string> &vec)
- {
- const std::regex reg(regxsplit);
- return SplitString(str, reg, vec);
- /* const std::sregex_token_iterator end;
- for(std::sregex_token_iterator iter(str.begin(), str.end(), reg); iter != end; ++iter)
- vec.push_back(*iter);*/
- }
- std::string JoinStringArray(const std::vector<std::string> &vec, int nPosStart, int nPosEnd, const char *sep)
- {
- auto vSize = vec.size();
-
- if( vSize > 0 &&
- nPosStart < (int)vSize &&
- nPosEnd < (int)vSize)
- {
- std::ostringstream os;
- auto itBegin = (nPosStart <= 0) ? vec.begin() : vec.begin() + nPosStart;
- auto itEnd = (nPosEnd < 0) ? vec.end() : vec.begin() + (nPosEnd + 1);
- if(itBegin != itEnd)
- os << *itBegin++;
- while(itBegin != itEnd)
- {
- os << sep;
- os << *itBegin++;
- }
- return os.str();
- }
-
- return "";
- }
- //////////////////////////////////////////////////////////////////////////////////
- ssize_t ReadFile(const char *pszPath, void **ppBuf, size_t nBlockSize)
- {
- if(!ppBuf)
- return -1;
- *ppBuf = NULL;
- if(!nBlockSize)
- nBlockSize = sysconf(_SC_PAGESIZE);
- int fd;
- ssize_t l, nRead = 0;
- if((fd = open(pszPath, O_RDONLY)) == -1)
- return -1;
- // off_t size = lseek(fd, 0, SEEK_END);
- // perror("lseek - ");
- char *p = (char*)malloc(nBlockSize);
- while((l = read(fd, p + nRead, nBlockSize)) == (ssize_t)nBlockSize)
- {
- nRead += l;
- p = (char*)realloc(p, nRead + nBlockSize);
- }
- close(fd);
- if(l == -1)
- {
- free(p);
- return -1;
- }
- nRead += l;
- p[nRead] = '\n';
- *ppBuf = p;
- return nRead;
- }
- //////////////////////////////////////////////////////////////////////////////////
- ssize_t ReadFile(const char *pszPath, std::string &str)
- {
- char *pszBuf;
- ssize_t nRet = ReadFile(pszPath, (void**)&pszBuf, 0);
- if(nRet > 0)
- str = pszBuf;
- if(pszBuf)
- free(pszBuf);
- return nRet;
- }
- //////////////////////////////////////////////////////////////////////////////////
- int FindStringInList(const std::string &str, const char *pszList[], size_t nCntList)
- {
- for(size_t i = 0; i < nCntList; i++)
- {
- if(!str.compare(pszList[i]))
- return i;
- }
- return -1;
- }
- //////////////////////////////////////////////////////////////////////////////////
- int RemoveDoubleStringsFromVector(std::vector<std::string> &vec)
- {
- int nRet = 0;
- for(auto it1 = vec.begin(); it1 != vec.end(); ++it1)
- {
- const std::string &str1 = *it1;
- for(auto it2 = it1 + 1; it2 != vec.end(); )
- {
- const std::string &str2 = *it2;
- if(!str1.compare(str2))
- {
- it2 = vec.erase(it2);
- nRet++;
- }
- else
- ++it2;
- }
- }
- return nRet;
- }
- //////////////////////////////////////////////////////////////////////////////////
- int RemoveStringFromVector(const std::string &str, std::vector<std::string> &vec)
- {
- int nRet = 0;
- for(auto it = vec.begin(); it != vec.end(); )
- {
- const std::string &str2 = *it;
- if(!str.compare(str2))
- {
- it = vec.erase(it);
- nRet++;
- }
- else
- ++it;
- }
- return nRet;
- }
- //////////////////////////////////////////////////////////////////////////////////
- std::vector<std::string>::iterator FindString(const std::string &str, std::vector<std::string> &vec)
- {
- auto it = vec.begin();
- for(; it != vec.end(); it++)
- {
- if(!str.compare(*it))
- break;
- }
- return it;
- }
|