mirror of
https://github.com/smartcmd/MinecraftConsoles.git
synced 2026-04-24 07:56:30 +00:00
158 lines
3.8 KiB
C++
158 lines
3.8 KiB
C++
#include "stdafx.h"
|
|
|
|
wstring toLower(const wstring& a)
|
|
{
|
|
wstring out = wstring(a);
|
|
std::transform(out.begin(), out.end(), out.begin(), ::tolower);
|
|
return out;
|
|
}
|
|
|
|
wstring trimString(const wstring& a)
|
|
{
|
|
wstring b;
|
|
size_t start = a.find_first_not_of(L" \t\n\r");
|
|
size_t end = a.find_last_not_of(L" \t\n\r");
|
|
if( start == wstring::npos ) start = 0;
|
|
if( end == wstring::npos ) end = a.size() - 1;
|
|
b = a.substr(start,(end-start)+1);
|
|
return b;
|
|
}
|
|
|
|
wstring replaceAll(const wstring& in, const wstring& replace, const wstring& with)
|
|
{
|
|
wstring out = in;
|
|
size_t pos = 0;
|
|
while( ( pos = out.find(replace, pos) ) != wstring::npos )
|
|
{
|
|
out.replace( pos, replace.length(), with );
|
|
pos++;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
bool equalsIgnoreCase(const wstring& a, const wstring& b)
|
|
{
|
|
bool out;
|
|
wstring c = toLower(a);
|
|
wstring d = toLower(b);
|
|
out = c.compare(d) == 0;
|
|
return out;
|
|
}
|
|
|
|
wstring convStringToWstring(const string& converting)
|
|
{
|
|
wstring converted(converting.length(), L' ');
|
|
copy(converting.begin(), converting.end(), converted.begin());
|
|
return converted;
|
|
}
|
|
|
|
const std::string wstringtofilename(const wstring& name)
|
|
{
|
|
assert(name.length()<256);
|
|
std::string buf;
|
|
buf.reserve(name.length());
|
|
for(unsigned int i = 0; i < name.length(); i++ )
|
|
{
|
|
wchar_t c = name[i];
|
|
#if defined __PS3__ || defined __ORBIS__
|
|
if(c=='\\') c='/';
|
|
#else
|
|
if(c=='/') c='\\';
|
|
#endif
|
|
assert(c<128); // Will we have to do any conversion of non-ASCII characters in filenames?
|
|
buf += static_cast<char>(c);
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
const std::string wstringtostring(const wstring& name)
|
|
{
|
|
assert(name.length() < 256);
|
|
std::string buf;
|
|
buf.reserve(name.length());
|
|
for (unsigned int i = 0; i < name.length(); i++)
|
|
{
|
|
wchar_t c = name[i];
|
|
assert(c < 128); // Will we have to do any conversion of non-ASCII characters in filenames?
|
|
buf += static_cast<char>(c);
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
const vector<char> wstringtochararray(const wstring& name)
|
|
{
|
|
assert(name.length()<256);
|
|
vector<char> buf;
|
|
buf.reserve(name.length());
|
|
for(unsigned int i = 0; i < name.length(); i++ )
|
|
{
|
|
wchar_t c = name[i];
|
|
assert(c<128); // Will we have to do any conversion of non-ASCII characters in filenames?
|
|
buf[i] = static_cast<char>(c);
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
wstring filenametowstring(const char *name)
|
|
{
|
|
return convStringToWstring(name);
|
|
}
|
|
|
|
std::vector<std::wstring> &stringSplit(const std::wstring &s, wchar_t delim, std::vector<std::wstring> &elems)
|
|
{
|
|
std::wstringstream ss(s);
|
|
std::wstring item;
|
|
while(std::getline(ss, item, delim))
|
|
{
|
|
elems.push_back(item);
|
|
}
|
|
return elems;
|
|
}
|
|
|
|
|
|
std::vector<std::wstring> stringSplit(const std::wstring &s, wchar_t delim)
|
|
{
|
|
std::vector<std::wstring> elems;
|
|
return stringSplit(s, delim, elems);
|
|
}
|
|
|
|
bool BothAreSpaces(wchar_t lhs, wchar_t rhs) { return (lhs == rhs) && (lhs == L' '); }
|
|
|
|
void stripWhitespaceForHtml(wstring &string, bool bRemoveNewline)
|
|
{
|
|
// Strip newline chars
|
|
if(bRemoveNewline)
|
|
{
|
|
string.erase(std::remove(string.begin(), string.end(), '\n'), string.end());
|
|
string.erase(std::remove(string.begin(), string.end(), '\r'), string.end());
|
|
}
|
|
|
|
string.erase(std::remove(string.begin(), string.end(), '\t'), string.end());
|
|
|
|
// Strip duplicate spaces
|
|
string.erase(std::unique(string.begin(), string.end(), BothAreSpaces), string.end());
|
|
|
|
string = trimString(string);
|
|
}
|
|
|
|
wstring escapeXML(const wstring &in)
|
|
{
|
|
wstring out = in;
|
|
out = replaceAll(out, L"&", L"&");
|
|
//out = replaceAll(out, L"\"", L""");
|
|
//out = replaceAll(out, L"'", L"'");
|
|
out = replaceAll(out, L"<", L"<");
|
|
out = replaceAll(out, L">", L">");
|
|
return out;
|
|
}
|
|
|
|
wstring parseXMLSpecials(const wstring &in)
|
|
{
|
|
wstring out = in;
|
|
out = replaceAll(out, L"&", L"&");
|
|
//out = replaceAll(out, L"\"", L""");
|
|
//out = replaceAll(out, L"'", L"'");
|
|
out = replaceAll(out, L"<", L"<");
|
|
out = replaceAll(out, L">", L">");
|
|
return out;
|
|
} |