summaryrefslogtreecommitdiff
path: root/cpp11addition.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp11addition.cpp')
-rwxr-xr-x[-rw-r--r--]cpp11addition.cpp165
1 files changed, 160 insertions, 5 deletions
diff --git a/cpp11addition.cpp b/cpp11addition.cpp
index 41f3cc1..0375ea3 100644..100755
--- a/cpp11addition.cpp
+++ b/cpp11addition.cpp
@@ -214,7 +214,7 @@ std::string binarytoHexa(const std::vector<char> &data, bool *ok)
std::string binarytoHexa(const void * const data, const uint32_t &size, bool *ok)
{
- return binarytoHexa(reinterpret_cast<const char * const>(data),size,ok);
+ return binarytoHexa(reinterpret_cast<const char *>(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<std::wstring> parts=stringsplit(newstring,'/');
+
+ #ifndef _WIN32
+ unsigned int index=1;
+ #else
+ unsigned int index=2;
+ #endif
+ while(index<parts.size())
+ {
+ if(parts.at(index)==L"..")
+ {
+ parts.erase(parts.begin()+index);
+ #ifndef _WIN32
+ if(index>0 && (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<std::wstring> 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::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
@@ -501,16 +564,22 @@ std::vector<std::string> stringtostringlist(const std::string &string)
std::vector<std::string> 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<start_pos || start_pos<string.size());
return returnedVar;
}
@@ -529,3 +598,89 @@ std::string stringlisttostring(const std::vector<std::string> &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<std::wstring> stringsplit(const std::wstring &s, wchar_t delim)
+{
+ std::vector<std::wstring> 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;
+ }
+}