the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 147 lines 3.8 kB view raw
1#include "stdafx.h" 2 3wstring toLower(const wstring& a) 4{ 5 wstring out = wstring(a); 6 std::transform(out.begin(), out.end(), out.begin(), ::tolower); 7 return out; 8} 9 10wstring trimString(const wstring& a) 11{ 12 wstring b; 13 int start = (int)a.find_first_not_of(L" \t\n\r"); 14 int end = (int)a.find_last_not_of(L" \t\n\r"); 15 if( start == wstring::npos ) start = 0; 16 if( end == wstring::npos ) end = (int)a.size()-1; 17 b = a.substr(start,(end-start)+1); 18 return b; 19} 20 21wstring replaceAll(const wstring& in, const wstring& replace, const wstring& with) 22{ 23 wstring out = in; 24 size_t pos = 0; 25 while( ( pos = out.find(replace, pos) ) != wstring::npos ) 26 { 27 out.replace( pos, replace.length(), with ); 28 pos++; 29 } 30 return out; 31} 32 33bool equalsIgnoreCase(const wstring& a, const wstring& b) 34{ 35 bool out; 36 wstring c = toLower(a); 37 wstring d = toLower(b); 38 out = c.compare(d) == 0; 39 return out; 40} 41 42wstring convStringToWstring(const string& converting) 43{ 44 wstring converted(converting.length(), L' '); 45 copy(converting.begin(), converting.end(), converted.begin()); 46 return converted; 47} 48 49// Convert for filename wstrings to a straight character pointer for Xbox APIs. The returned string is only valid until 50// this function is called again, and it isn't thread-safe etc. as I'm just storing the returned name in a local static 51// to save having to clear it up everywhere this is used. 52const char *wstringtofilename(const wstring& name) 53{ 54 static char buf[256]; 55 assert(name.length()<256); 56 for(unsigned int i = 0; i < name.length(); i++ ) 57 { 58 wchar_t c = name[i]; 59#if defined __PS3__ || defined __ORBIS__ 60 if(c=='\\') c='/'; 61#else 62 if(c=='/') c='\\'; 63#endif 64 assert(c<128); // Will we have to do any conversion of non-ASCII characters in filenames? 65 buf[i] = (char)c; 66 } 67 buf[name.length()] = 0; 68 return buf; 69} 70 71const char *wstringtochararray(const wstring& name) 72{ 73 static char buf[256]; 74 assert(name.length()<256); 75 for(unsigned int i = 0; i < name.length(); i++ ) 76 { 77 wchar_t c = name[i]; 78 assert(c<128); // Will we have to do any conversion of non-ASCII characters in filenames? 79 buf[i] = (char)c; 80 } 81 buf[name.length()] = 0; 82 return buf; 83} 84 85wstring filenametowstring(const char *name) 86{ 87 return convStringToWstring(name); 88} 89 90std::vector<std::wstring> &stringSplit(const std::wstring &s, wchar_t delim, std::vector<std::wstring> &elems) 91{ 92 std::wstringstream ss(s); 93 std::wstring item; 94 while(std::getline(ss, item, delim)) 95 { 96 elems.push_back(item); 97 } 98 return elems; 99} 100 101 102std::vector<std::wstring> stringSplit(const std::wstring &s, wchar_t delim) 103{ 104 std::vector<std::wstring> elems; 105 return stringSplit(s, delim, elems); 106} 107 108bool BothAreSpaces(wchar_t lhs, wchar_t rhs) { return (lhs == rhs) && (lhs == L' '); } 109 110void stripWhitespaceForHtml(wstring &string, bool bRemoveNewline) 111{ 112 // Strip newline chars 113 if(bRemoveNewline) 114 { 115 string.erase(std::remove(string.begin(), string.end(), '\n'), string.end()); 116 string.erase(std::remove(string.begin(), string.end(), '\r'), string.end()); 117 } 118 119 string.erase(std::remove(string.begin(), string.end(), '\t'), string.end()); 120 121 // Strip duplicate spaces 122 string.erase(std::unique(string.begin(), string.end(), BothAreSpaces), string.end()); 123 124 string = trimString(string); 125} 126 127wstring escapeXML(const wstring &in) 128{ 129 wstring out = in; 130 out = replaceAll(out, L"&", L"&amp;"); 131 //out = replaceAll(out, L"\"", L"&quot;"); 132 //out = replaceAll(out, L"'", L"&apos;"); 133 out = replaceAll(out, L"<", L"&lt;"); 134 out = replaceAll(out, L">", L"&gt;"); 135 return out; 136} 137 138wstring parseXMLSpecials(const wstring &in) 139{ 140 wstring out = in; 141 out = replaceAll(out, L"&amp;", L"&"); 142 //out = replaceAll(out, L"\"", L"&quot;"); 143 //out = replaceAll(out, L"'", L"&apos;"); 144 out = replaceAll(out, L"&lt;", L"<"); 145 out = replaceAll(out, L"&gt;", L">"); 146 return out; 147}