From 3ac113857071fc1f225b2e1b42547269e568c6b7 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Tue, 11 Aug 2020 22:35:12 +0100 Subject: New upstream version 2.2.4.4 --- cpp11addition.cpp | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 160 insertions(+), 5 deletions(-) mode change 100644 => 100755 cpp11addition.cpp (limited to 'cpp11addition.cpp') diff --git a/cpp11addition.cpp b/cpp11addition.cpp old mode 100644 new mode 100755 index 41f3cc1..0375ea3 --- a/cpp11addition.cpp +++ b/cpp11addition.cpp @@ -214,7 +214,7 @@ std::string binarytoHexa(const std::vector &data, bool *ok) std::string binarytoHexa(const void * const data, const uint32_t &size, bool *ok) { - return binarytoHexa(reinterpret_cast(data),size,ok); + return binarytoHexa(reinterpret_cast(data),size,ok); } std::string binarytoHexa(const char * const data, const uint32_t &size, bool *ok) @@ -476,6 +476,69 @@ std::string FSabsolutePath(const std::string &string) return tempFile; } +std::wstring FSabsoluteFilePath(const std::wstring &string) +{ + std::wstring newstring=string; + #ifdef _WIN32 + stringreplaceAll(newstring,L"\\",L"/"); + #endif + stringreplaceAll(newstring,L"//",L"/"); + std::vector parts=stringsplit(newstring,'/'); + + #ifndef _WIN32 + unsigned int index=1; + #else + unsigned int index=2; + #endif + while(index0 && (index>1 || !parts.at(index-1).empty())) + #else + if(index>1) + #endif + { + parts.erase(parts.begin()+index-1); + index--; + } + } + else + index++; + } + + #ifndef _WIN32 + if(parts.empty() || (parts.size()==1 && parts.at(0).empty())) + return L"/"; + #endif + + std::wstring newString; + std::vector copy=parts; + unsigned int count=0; + while(!copy.empty()) + { + if(count>0) + newString+=L'/'; + newString+=copy.front(); + copy.erase(copy.begin()); + ++count; + } + + return newString; +} + +std::wstring FSabsolutePath(const std::wstring &string) +{ + const std::wstring &tempFile=FSabsoluteFilePath(string); + const std::size_t &found=tempFile.find_last_of(L"/\\"); + if(found!=std::wstring::npos) + return tempFile.substr(0,found)+L'/'; + else + return tempFile; +} + uint64_t msFrom1970() { return std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); @@ -501,16 +564,22 @@ std::vector stringtostringlist(const std::string &string) std::vector returnedVar; size_t start_pos = 0; size_t firstChar = 0; - while((start_pos = string.find(',', start_pos)) != std::string::npos) { - if(start_pos==0 || string.at(start_pos-1)!=',') + do + { + size_t s=start_pos = string.find(',', start_pos); + if(s == std::string::npos) + start_pos=string.size(); + if(start_pos>=string.size() || string.at(start_pos+1)!=',') { - std::string tempString=string.substr(firstChar,start_pos-1); + std::string tempString=string.substr(firstChar,start_pos-firstChar); stringreplaceAll(tempString,",,",","); returnedVar.push_back(tempString); start_pos++; firstChar=start_pos; } - } + else + start_pos+=2; + } while(firstChar &stringlist) } return returnedString; } + +bool stringreplaceOne(std::wstring& str, const std::wstring& from, const std::wstring& to) +{ + const size_t start_pos = str.find(from); + if(start_pos == std::wstring::npos) + return false; + str.replace(start_pos, from.length(), to); + return true; +} + +uint8_t stringreplaceAll(std::wstring& str, const std::wstring& from, const std::wstring& to) +{ + if(from.empty()) + return 0; + size_t start_pos = 0; + uint8_t count=0; + while((start_pos = str.find(from, start_pos)) != std::wstring::npos) { + str.replace(start_pos, from.length(), to); + start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx' + count++; + } + return count; +} + +bool stringEndsWith(std::wstring const &fullString, std::wstring const &ending) +{ + if (fullString.length() >= ending.length()) { + return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending)); + } else { + return false; + } +} + +bool stringEndsWith(std::wstring const &fullString, char const &ending) +{ + if (fullString.length()>0) { + return fullString[fullString.size()-1]==ending; + } else { + return false; + } +} + +bool stringStartWith(std::wstring const &fullString, std::wstring const &starting) +{ + if (fullString.length() >= starting.length()) { + return (fullString.substr(0,starting.length())==starting); + } else { + return false; + } +} + +bool stringStartWith(std::wstring const &fullString, char const &starting) +{ + if (fullString.length()>0) { + return fullString[0]==starting; + } else { + return false; + } +} + +std::vector stringsplit(const std::wstring &s, wchar_t delim) +{ + std::vector elems; + + std::wstring::size_type i = 0; + std::wstring::size_type j = s.find(delim); + + if(j == std::wstring::npos) + { + if(!s.empty()) + elems.push_back(s); + return elems; + } + else + { + while (j != std::wstring::npos) { + elems.push_back(s.substr(i, j-i)); + i = ++j; + j = s.find(delim, j); + + if (j == std::wstring::npos) + elems.push_back(s.substr(i, s.length())); + } + return elems; + } +} -- cgit v1.2.3