/* (C) 2003 XDA Developers * Author: Willem Jan Hengeveld * Web: http://www.xda-developers.com/ * * $Header$ */ #include #include "stringutils.h" #include "debug.h" #include using namespace std; string ToString(TCHAR* tbuf) { char *buf= new char[_tcslen(tbuf)+1]; _snprintf(buf, _tcslen(tbuf), "%ls", tbuf); string str(buf); delete buf; return str; } wstring ToWString(TCHAR* tbuf) { return wstring(tbuf); } string ToString(char* buf) { return string(buf); } wstring ToWString(char* buf) { TCHAR *tbuf= new TCHAR[strlen(buf)+1]; _sntprintf(tbuf, strlen(buf), L"%hs", buf); wstring wstr(tbuf); delete tbuf; return wstr; } string ToString(const wstring& wstr) { return ToString((TCHAR*)wstr.c_str()); } wstring ToWString(const string& str) { return ToWString((char*)str.c_str()); } // removes cr, lf, whitespace from end of string void chomp(char *str) { char *p= str+strlen(str)-1; while (p>=str && isspace(*p)) { *p--= 0; } } void chomp(string& str) { for (string::size_type i=str.size()-1 ; i>=0 && isspace(str.at(i)) ; --i) { str.erase(i); } } bool SplitString(const string& str, StringList& strlist) { string::const_iterator pos= str.begin(); bool bQuoted= false; bool bEscaped= false; string current; while (pos != str.end()) { if (bEscaped) { current += *pos++; bEscaped= false; } else if (bQuoted) { switch(*pos) { case '\\': ++pos; // skip escaped char break; case '"': bQuoted= false; strlist.push_back(string(current)); //debug("added %hs\n", current.c_str()); current.clear(); ++pos; break; default: current += *pos++; } } else { switch(*pos) { case ' ': case '\t': ++pos; if (!current.empty()) { strlist.push_back(string(current)); //debug("added %hs\n", current.c_str()); current.clear(); } break; case '\\': ++pos; // skip escaped char break; case '"': bQuoted=true; ++pos; break; default: current += *pos++; } } } if (!current.empty()) { strlist.push_back(string(current)); //debug("added %hs\n", current.c_str()); current.clear(); } if (bQuoted || bEscaped) { debug("ERROR: Unterminated commandline\n"); return false; } return true; } string stringformat(char *fmt, ...) { char buf[1024]; va_list ap; va_start(ap, fmt); _vsnprintf(buf, 1024, fmt, ap); va_end(ap); return string(buf); }