1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#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;
}
// Convert for filename wstrings to a straight character pointer for Xbox APIs. The returned string is only valid until
// this function is called again, and it isn't thread-safe etc. as I'm just storing the returned name in a local static
// to save having to clear it up everywhere this is used.
const char *wstringtofilename(const wstring& name)
{
static char buf[256];
assert(name.length()<256);
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[i] = static_cast<char>(c);
}
buf[name.length()] = 0;
return buf;
}
const char *wstringtochararray(const wstring& name)
{
static char buf[256];
assert(name.length()<256);
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);
}
buf[name.length()] = 0;
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;
}
|