/* (C) 2003 XDA Developers itsme@xs4all.nl * * $Header$ */ #ifndef __STRINGUTILS_H_ // make sure everything is compiled with '_MT' defined. // otherwise this will not link with code that has any of the mfc // headers included. // without _MT this code compiles to non multithreaded templates, // with it compiles to templates with threading support. #include #include #include "vectorutils.h" #include "util/wintypes.h" #include typedef std::basic_string,std::Safe_Allocator > utf8string; typedef std::basic_string,std::Safe_Allocator > utf16string; typedef std::vector StringList; typedef std::vector WStringList; typedef std::vector TStringList; // preprocessor helper macro #define STR$(s) XSTR$(s) #define XSTR$(s) #s //------------------------------------------------------- // string conversion functions // std::string ToString(const WCHAR* tbuf); std::Wstring ToWString(const WCHAR* tbuf); std::tstring ToTString(const WCHAR* tbuf); template ByteVector ToByteVector(const T*buf) { return ByteVector((const uint8_t*)buf, (const uint8_t*)(buf+stringlength(buf)*sizeof(T))); } template ByteVector ToByteVector(const T*buf, size_t n) { return ByteVector((const uint8_t*)buf, (const uint8_t*)(buf+n*sizeof(T))); } template ByteVector ToByteVector(const T&buf) { return ByteVector((const uint8_t*)buf.c_str(), (const uint8_t*)(buf.c_str()+buf.size())); } std::string ToString(const char* buf); std::Wstring ToWString(const char* buf); std::tstring ToTString(const char* buf); std::string ToString(const std::string& str); std::Wstring ToWString(const std::string& str); std::tstring ToTString(const std::string& str); std::string ToString(const std::Wstring& wstr); std::Wstring ToWString(const std::Wstring& str); std::tstring ToTString(const std::Wstring& str); /* -- unfortunately TCHAR is not a type, just a define. std::string ToString(const TCHAR* tbuf); std::Wstring ToWString(const TCHAR* tbuf); std::tstring ToTString(const TCHAR* tbuf); std::string ToString(const std::tstring& str); std::Wstring ToWString(const std::tstring& str); //std::tstring ToTString(const std::tstring& str); */ //------------------------------------------------------- // string manipulation functions // removes cr, lf, whitespace from end of string template void chomp(T& str) { for (int i=str.size()-1 ; i>=0 && isspace(str.at(i)) ; --i) { str.erase(i); } } void chomp(char *str); bool SplitString(const std::string& str, StringList& strlist, bool bWithEscape= true, const std::string& separator=" \t"); bool SplitString(const std::Wstring& str, WStringList& strlist, bool bWithEscape= true, const std::Wstring& separator=(const WCHAR*)L" \t"); template STRING JoinStringList(const std::vector& strlist, const SEPSTRING& sep) { if (strlist.empty()) return STRING(); STRING sep2= sep; size_t size=sep2.size()*(strlist.size()-1); for (typename std::vector::const_iterator i=strlist.begin() ; i!=strlist.end() ; ++i) size += (*i).size(); STRING result; result.reserve(size); //debug("join(%d, '%hs')\n", strlist.size(), sep.c_str()); for (typename std::vector::const_iterator i=strlist.begin() ; i!=strlist.end() ; ++i) { if (!result.empty()) result += sep2; result += *i; //debug(" added %hs\n", (*i).c_str()); } return result; } std::string stringformat(const char *fmt, ...); std::string stringvformat(const char *fmt, va_list ap); //int stringicompare(const std::string& a, const std::string& b); template int charicompare(T a,T b) { a=(T)tolower(a); b=(T)tolower(b); if (ab) return 1; return 0; } template int stringicompare(const T& a, const T& b) { typename T::const_iterator pa= a.begin(); typename T::const_iterator pa_end= a.end(); typename T::const_iterator pb= b.begin(); typename T::const_iterator pb_end= b.end(); while (pa!=pa_end && pb!=pb_end && charicompare(*pa, *pb)==0) { pa++; pb++; } if (pa==pa_end && pb==pb_end) return 0; if (pa==pa_end) return -1; if (pb==pb_end) return 1; return charicompare(*pa, *pb); } std::string tolower(const std::string& str); #define stringptr(v) ((v).empty()?NULL:&(v)[0]) template std::string vhexdump(const T& buf) { return hexdump((const uint8_t*)&buf[0], buf.size(), sizeof(typename T::value_type)); } template std::string vhexdump(const T& buf, int nDumpUnitSize) { return hexdump((const uint8_t*)&buf[0], (sizeof(typename T::value_type)*buf.size())/nDumpUnitSize, nDumpUnitSize); } std::string hexstring(const uint8_t *buf, int nLength); std::string hexdump(const ByteVector& buf, int nDumpUnitSize=1); std::string hexdump(const uint8_t *buf, int nLength, int nDumpUnitSize=1); std::string hexdump(int64_t llOffset, const uint8_t *buf, int nLength, int nDumpUnitSize=1, int nMaxUnitsPerLine=16); std::string ascdump(const ByteVector& buf, const std::string& escaped= "", bool bBreakOnEol= false); std::string asciidump(const uint8_t *buf, size_t bytelen); std::string hash_as_string(const ByteVector& hash); std::string GuidToString(const GUID *guid); std::string base64_encode(const uint8_t *data, size_t n); std::string base64_encode(const ByteVector& data); ByteVector base64_decode(const std::string& str); template size_t stringlength(const T *str) { size_t len=0; while (*str++) len++; return len; } template int stringcompare(const T *a, const T *b) { while (*a && *b && *a == *b) { a++; b++; } if (*a<*b) return -1; if (*a>*b) return 1; return 0; } template void stringcopy(T *a, const T *b) { while (*a++ = *b++) ; } template int char2nyble(T c) { if (c<'0') return -1; if (c<='9') return c-'0'; if (c<'A') return -1; if (c<='F') return 10+c-'A'; if (c<'a') return -1; if (c<='f') return 10+c-'a'; return -1; } template void hex2binary(const S& hex, T& v) { v.reserve(hex.size()/2/sizeof(typename T::value_type)); int shift=0; typename T::value_type word=0; for (typename S::const_iterator i= hex.begin() ; i!=hex.end() ; i++) { int n= char2nyble(*i); if (n>=0) { if (shift==0) { word= typename T::value_type(n); shift+=4; } else { word<<=4; word|= n; shift+=4; } if (shift==sizeof(typename T::value_type)*8) { v.push_back(word); shift=0; } } } } void binary2hex(std::string &str, const uint8_t *buf, int nLength); template void hex2binary(const char*hex, T& v) { hex2binary(std::string(hex), v); } std::string utf8forchar(WCHAR c); #define __STRINGUTILS_H_ #endif