/************************************************************************* * UrBackup - Client/Server backup system * Copyright (C) 2011-2015 Martin Raiber * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . **************************************************************************/ #include #include #include #include #include #include "utf8/utf8.h" #include "stringtools.h" #ifndef _WIN32 #include #include #endif using namespace std; typedef int s32; typedef unsigned int u32; typedef float f32; //-------------------------------------------------------------------- /** * liefert einen teil des strings nach dem gelieferten teilstring */ string getafterinc(const std::string &str,const std::string &data) { size_t pos=data.find(str); if(pos!=std::string::npos) { return data.substr(pos); } else { return ""; } } wstring getafterinc(const std::wstring &str,const std::wstring &data) { size_t pos=data.find(str); if(pos!=std::wstring::npos) { return data.substr(pos); } else { return L""; } } string getafter(const std::string &str,const std::string &data) { std::string ret=getafterinc(str,data); ret.erase(0,str.size() ); return ret; } wstring getafter(const std::wstring &str,const std::wstring &data) { std::wstring ret=getafterinc(str,data); ret.erase(0,str.size() ); return ret; } //-------------------------------------------------------------------- /** * liefert einen teil des strings zwischen zwei teilstrings */ string getbetween(string s1,string s2,string data) { size_t off1=data.find(s1); if(off1==-1)return ""; off1+=s1.size(); size_t off2=data.find(s2,off1); if(s2=="\n") { size_t off3=data.find("\r\n",off1); if(off30?(lines+1):0; } //-------------------------------------------------------------------- /** * text datei auslesen */ std::string getFile(std::string filename) { std::fstream FileBin; FileBin.open(filename.c_str(), std::ios::in|std::ios::binary); if(FileBin.is_open()==false) { return ""; } FileBin.seekg(0, std::ios::end); unsigned long FileSize = (unsigned int)std::streamoff(FileBin.tellg()); FileBin.seekg(0, std::ios::beg); char* buffer=new char[FileSize+1]; FileBin.read(buffer, FileSize); buffer[FileSize]='\0'; std::string ret=buffer; FileBin.close(); delete [] buffer; return ret; } #ifdef _WIN32 std::string getFile(std::wstring filename) { std::fstream FileBin; FileBin.open(filename.c_str(), std::ios::in | std::ios::binary); if (FileBin.is_open() == false) { return ""; } FileBin.seekg(0, std::ios::end); unsigned long FileSize = (unsigned int)std::streamoff(FileBin.tellg()); FileBin.seekg(0, std::ios::beg); char* buffer = new char[FileSize + 1]; FileBin.read(buffer, FileSize); buffer[FileSize] = '\0'; std::string ret = buffer; FileBin.close(); delete[] buffer; return ret; } #endif std::wstring widen(std::string tw); std::wstring getFileUTF8(string filename) { return L""; } //-------------------------------------------------------------------- /** * string in großbuchstaben umwandeln */ void strupper(char *string) { for(; *string != '\0'; string++ ) if(*string >= 'a' && *string <='z') *string -= 32; /* siehe ASCII-Tabelle */ } //-------------------------------------------------------------------- /** * string in kleinbuchstaben umwandeln */ void strlower(char *string) { for(; *string != '\0'; string++ ) if(*string >= 'A' && *string <='Z') *string += 32; } std::string strlower(const std::string &str) { std::string ret=str; for(size_t i=0; i= 'A' && str[i] <='Z') ret[i] += 32; return ret; } std::wstring strlower(const std::wstring &str) { std::wstring ret=str; for(size_t i=0; i= 'A' && str[i] <='Z') ret[i] += 32; return ret; } //-------------------------------------------------------------------- /** * string in großbuchstaben umwandeln */ void strupper(std::string *pStr) { for(size_t i=0;isize();++i) { (*pStr)[i]=toupper((*pStr)[i] ); } } //-------------------------------------------------------------------- /** * string in großbuchstaben umwandeln */ void strupper(std::wstring *pStr) { for(size_t i=0;isize();++i) { (*pStr)[i]=toupper((*pStr)[i] ); } } //-------------------------------------------------------------------- /** * dateiname aus pfadangabe extrahieren */ string ExtractFileName(string fulln) { string filename; s32 off=0; for(s32 i=(s32)fulln.length()-1;i>-1;i--) { if(fulln[i]=='/'||fulln[i]=='\\') { if(i<(s32)fulln.length()-2) break; } if(fulln[i]!=0&&fulln[i]!='/') filename=fulln[i]+filename; } return filename; } wstring ExtractFileName(wstring fulln) { wstring filename; s32 off=0; for(s32 i=(s32)fulln.length()-1;i>-1;i--) { if(fulln[i]=='/'||fulln[i]=='\\') { if(i<(s32)fulln.length()-2) break; } if(fulln[i]!=0&&fulln[i]!='/') filename=fulln[i]+filename; } return filename; } //-------------------------------------------------------------------- /** * pfad aus pfadangabe (inkl. dateiname) extrahieren */ string ExtractFilePath(string fulln) { bool in=false; string path; for(s32 i=(s32)fulln.length()-2;i>-1;i--) { if((fulln[i]=='/'||fulln[i]=='\\')&&in==false) { in=true; continue; } if(in==true) { path=fulln[i]+path; } } return path; } //-------------------------------------------------------------------- /** * pfad aus pfadangabe (inkl. dateiname) extrahieren */ wstring ExtractFilePath(wstring fulln) { bool in=false; wstring path; for(s32 i=(s32)fulln.length()-2;i>-1;i--) { if((fulln[i]=='/'||fulln[i]=='\\')&&in==false) { in=true; continue; } if(in==true) { path=fulln[i]+path; } } return path; } //-------------------------------------------------------------------- /** * bool in wide string (true/false) konvertieren */ std::wstring convert(bool pBool) { if(pBool==true) return L"true"; else return L"false"; } //-------------------------------------------------------------------- /** * integer in wide character */ std::wstring convert(s32 i){ wostringstream ss; ss << i; return ss.str(); } //-------------------------------------------------------------------- /** * f32 in wide character */ std::wstring convert(f32 f){ wostringstream ss; ss << f; return ss.str(); } //-------------------------------------------------------------------- /** * bool in string (true/false) konvertieren */ std::string nconvert(bool pBool) { if(pBool==true) return "true"; else return "false"; } //-------------------------------------------------------------------- /** * integer in string */ std::string nconvert(s32 i){ ostringstream ss; ss << i; return ss.str(); } std::string nconvert(int long i) { ostringstream ss; ss << i; return ss.str(); } #if defined(_WIN64) || defined(_LP64) std::string nconvert(unsigned int i){ ostringstream ss; ss << i; return ss.str(); } std::wstring convert(unsigned int i) { wostringstream ss; ss << i; return ss.str(); } #endif //-------------------------------------------------------------------- /** * integer in string */ std::string nconvert(long long int i){ ostringstream ss; ss << i; return ss.str(); } //-------------------------------------------------------------------- /** * integer in string */ std::wstring convert(long long int i){ wostringstream ss; ss << i; return ss.str(); } //-------------------------------------------------------------------- /** * size_t in string */ std::string nconvert(size_t i){ ostringstream ss; ss << i; return ss.str(); } //-------------------------------------------------------------------- /** * size_t in string */ std::wstring convert(size_t i){ wostringstream ss; ss << i; return ss.str(); } #if defined(_LP64) //-------------------------------------------------------------------- /** * integer in string */ std::string nconvert(unsigned long long int i){ ostringstream ss; ss << i; return ss.str(); } //-------------------------------------------------------------------- /** * integer in string */ std::wstring convert(unsigned long long int i){ wostringstream ss; ss << i; return ss.str(); } #endif //_WIN64 //-------------------------------------------------------------------- /** * f32 in string */ std::string nconvert(f32 f){ ostringstream ss; ss << f; return ss.str(); } //-------------------------------------------------------------------- /** * datei-endung finden */ std::string findextension(const std::string& pString) { std::string retv; std::string temp; for(s32 i=(s32)pString.size()-1; i>=0; i--) if( pString[i] != '.' ) temp.push_back(pString[i]); else break; for(s32 i=(s32)temp.size()-1; i>=0; i--) retv.push_back(temp[i]); return retv; } //-------------------------------------------------------------------- /** * string in wide string */ std::wstring widen(std::string tw) { std::wstring out; out.resize(tw.size() ); for(size_t i=0;i &tokens, std::string seps) { // one-space line for storing blank lines in the file std::string blankLine = " "; // pos0 and pos1 store the scope of the current turn, i stores // the position of the symbol \". s32 pos0 = 0, pos1 = 0, i = 0; while(true) { // find the next seperator pos1 = (s32)str.find_first_of(seps.c_str(), pos0); // find the next \" i = (s32)str.find_first_of("\"", pos0); // if the end is reached.. if(pos1 == std::string::npos) { // ..push back the string to the end tokens.push_back(str.substr(pos0, str.size())); break; } // if a string \" was found before the next seperator... if(( i 0 )) { // .. find the end of the string and push it back, strings // are treated like a single token pos1 = (s32)str.find_first_of("\"", i+1); tokens.push_back(str.substr(pos0, pos1-pos0+1)); } // if two seperators are found in a row, the file has a blank // line, in this case the one-space string is pushed as a token else if( pos1==pos0 ) { tokens.push_back(blankLine); } else // if no match is found, we have a simple token with the range // stored in pos0/1 tokens.push_back(str.substr(pos0, (pos1 - pos0))); // equalize pos pos0=pos1; // increase ++pos1; // added for ini-file! // increase by length of seps pos0++; } // loop through all tokens and check for empty tokens which may result // as garbage through the process for(s32 i=0;i<(s32)tokens.size();i++) if(tokens[i]=="") tokens.erase(tokens.begin()+(i--)); } void Tokenize(const std::wstring& str, std::vector &tokens, std::wstring seps) { // one-space line for storing blank lines in the file std::wstring blankLine = L" "; // pos0 and pos1 store the scope of the current turn, i stores // the position of the symbol \". s32 pos0 = 0, pos1 = 0, i = 0; while(true) { // find the next seperator pos1 = (s32)str.find_first_of(seps.c_str(), pos0); // find the next \" i = (s32)str.find_first_of(L"\"", pos0); // if the end is reached.. if(pos1 == std::string::npos) { // ..push back the string to the end tokens.push_back(str.substr(pos0, str.size())); break; } // if a string \" was found before the next seperator... if(( i 0 )) { // .. find the end of the string and push it back, strings // are treated like a single token pos1 = (s32)str.find_first_of(L"\"", i+1); tokens.push_back(str.substr(pos0, pos1-pos0+1)); } // if two seperators are found in a row, the file has a blank // line, in this case the one-space string is pushed as a token else if( pos1==pos0 ) { tokens.push_back(blankLine); } else // if no match is found, we have a simple token with the range // stored in pos0/1 tokens.push_back(str.substr(pos0, (pos1 - pos0))); // equalize pos pos0=pos1; // increase ++pos1; // added for ini-file! // increase by length of seps pos0++; } // loop through all tokens and check for empty tokens which may result // as garbage through the process for(s32 i=0;i<(s32)tokens.size();i++) if(tokens[i].empty()) tokens.erase(tokens.begin()+(i--)); } void TokenizeMail(const std::string& str, std::vector &tokens, std::string seps) { // one-space line for storing blank lines in the file std::string blankLine = ""; // pos0 and pos1 store the scope of the current turn, i stores // the position of the symbol \". s32 pos0 = 0, pos1 = 0; while(true) { // find the next seperator pos1 = (s32)str.find_first_of(seps.c_str(), pos0); // find the next \" // if the end is reached.. if(pos1 == std::string::npos) { // ..push back the string to the end std::string nt=str.substr(pos0, str.size()); if( nt!="" ) tokens.push_back(nt); break; } // if two seperators are found in a row, the file has a blank // line, in this case the one-space string is pushed as a token else if( pos1==pos0 ) { tokens.push_back(blankLine); } else // if no match is found, we have a simple token with the range // stored in pos0/1 tokens.push_back(str.substr(pos0, (pos1 - pos0))); // equalize pos pos0=pos1; // increase ++pos1; // added for ini-file! // increase by length of seps pos0++; } } //-------------------------------------------------------------------- /** */ bool isnumber(char ch) { if( ch>=48 && ch <=57 ) return true; else return false; } //-------------------------------------------------------------------- /** */ bool isletter(char ch) { ch=toupper(ch); if( ch<=90 && ch>=65) return true; else return false; } //-------------------------------------------------------------------- /** */ bool isnumber(wchar_t ch) { if( ch>=48 && ch <=57 ) return true; else return false; } //-------------------------------------------------------------------- /** */ bool isletter(wchar_t ch) { ch=toupper(ch); if( ch<=90 && ch>=65) return true; else return false; } bool next(const std::string &pData, const size_t & doff, const std::string &pStr) { for(size_t i=0;i=pData.size() ) return false; if( pData[doff+i]!=pStr[i] ) return false; } return true; } bool next(const std::wstring &pData, const size_t & doff, const std::wstring &pStr) { for(size_t i=0;i=pData.size() ) return false; if( pData[doff+i]!=pStr[i] ) return false; } return true; } std::string greplace(std::string tor, std::string tin, std::string data) { for(size_t i=0;i *pMap) { std::string key; std::string value; int pos = 0; for (size_t i = 0; iinsert(std::pair(key, wv)); key.clear(); value.clear(); } else if (pos == 0) { key += ch; } else if (pos == 1) { value += ch; } } if (value.size()>0 || key.size()>0) { std::string wv = htmldecode(value, false); pMap->insert(std::pair(key, wv)); } } std::string FormatTime(int timeins) { float t=(float)timeins; int h; int m; int s; h=int(t/3600.0f); m=int(t/60)-h*60; s=int(t-h*3600-m*60); std::string sh,sm,ss; sh=nconvert(h); sm=nconvert(m); ss=nconvert(s); if( sm.size()==1 && h>0 ) sm="0"+sm; if( ss.size()==1 ) ss="0"+ss; std::string ret=sm+":"+ss; if(h>0) ret=sh+":"+ret; return ret; } //-------------------HTML DECODE----------------- const char hex_array[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; bool IsHex(const std::string &str) { bool hex=true; for(size_t i=0;i(data.data()), data.size()); } string htmldecode(string str, bool html, char xc) { std::string ret; for (size_t i = 0; i') return false; } return true; } std::string nl2br(std::string str) { for(size_t i=0;i"); } else if( next(str,i," ")==true ) { str.erase(i,5); str.insert(i,"
"); } } return str; } bool FileExists(std::string pFile) { fstream in(pFile.c_str(), ios::in); if( in.is_open()==false ) return false; in.close(); return true; } bool checkStringHTML(const std::string &str) { for(size_t i=0;i=48 && ch <=57) ok=true; else if( ch >=65 && ch <=90 ) ok=true; else if( ch >=97 && ch <=122 ) ok=true; else if(ch==95 || ch==46 || ch==45 ) ok=true; if( ok==false ) return false; } return true; } std::string ReplaceChar(std::string str, char tr, char ch) { for(size_t i=0;i' ) in=false; else if(in==false ) { ret+=html[i]; } } return ret; } static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; static inline bool is_base64(unsigned char c) { return (isalnum(c) || (c == '+') || (c == '/')); } std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) { std::string ret; int i = 0; int j = 0; unsigned char char_array_3[3]; unsigned char char_array_4[4]; while (in_len--) { char_array_3[i++] = *(bytes_to_encode++); if (i == 3) { char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); char_array_4[3] = char_array_3[2] & 0x3f; for(i = 0; (i <4) ; i++) ret += base64_chars[char_array_4[i]]; i = 0; } } if (i) { for(j = i; j < 3; j++) char_array_3[j] = '\0'; char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); char_array_4[3] = char_array_3[2] & 0x3f; for (j = 0; (j < i + 1); j++) ret += base64_chars[char_array_4[j]]; while((i++ < 3)) ret += '='; } return ret; } std::string base64_decode(std::string const& encoded_string) { int in_len = (int)encoded_string.size(); int i = 0; int j = 0; int in_ = 0; unsigned char char_array_4[4], char_array_3[3]; std::string ret; while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { char_array_4[i++] = encoded_string[in_]; in_++; if (i ==4) { for (i = 0; i <4; i++) char_array_4[i] = (unsigned char)base64_chars.find(char_array_4[i]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (i = 0; (i < 3); i++) ret += char_array_3[i]; i = 0; } } if (i) { for (j = i; j <4; j++) char_array_4[j] = 0; for (j = 0; j <4; j++) char_array_4[j] = (unsigned char)base64_chars.find(char_array_4[j]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; } return ret; } bool CheckForIllegalChars(const std::string &str) { for(size_t i=0;i' ) fn[i]='_'; } return fn; } std::string base64_encode_dash( const std::string& data ) { std::string ret = base64_encode(reinterpret_cast(data.c_str()), static_cast(data.size())); for(size_t i=0;i