186 |
cycrow |
1 |
#pragma once
|
|
|
2 |
|
|
|
3 |
#include <string>
|
196 |
cycrow |
4 |
#include <vector>
|
186 |
cycrow |
5 |
#include "../spkdll.h"
|
|
|
6 |
#include "../spkdef.h"
|
|
|
7 |
|
187 |
cycrow |
8 |
enum WStringCompare {
|
|
|
9 |
Same = 0,
|
|
|
10 |
Newer = 1,
|
|
|
11 |
Older = -1,
|
186 |
cycrow |
12 |
};
|
|
|
13 |
|
|
|
14 |
#pragma warning( push )
|
|
|
15 |
#pragma warning( disable : 4251)
|
|
|
16 |
|
|
|
17 |
namespace Utils {
|
|
|
18 |
|
188 |
cycrow |
19 |
class SString;
|
198 |
cycrow |
20 |
class WStringList;
|
186 |
cycrow |
21 |
/**
|
|
|
22 |
* String wrapper class
|
|
|
23 |
*/
|
285 |
cycrow |
24 |
class SPKEXPORT WString
|
186 |
cycrow |
25 |
{
|
|
|
26 |
public:
|
|
|
27 |
static std::string ws2s(const std::wstring& wstr);
|
|
|
28 |
static std::wstring s2ws(const std::string& str);
|
243 |
cycrow |
29 |
static WString ConvertTime(time_t whattime, bool shortdesc = false);
|
186 |
cycrow |
30 |
|
285 |
cycrow |
31 |
private:
|
|
|
32 |
std::wstring _data;
|
|
|
33 |
|
186 |
cycrow |
34 |
public:
|
|
|
35 |
// constructors
|
|
|
36 |
WString(void);
|
|
|
37 |
WString(const char* str);
|
|
|
38 |
WString(const wchar_t* str);
|
|
|
39 |
WString(const std::string& str);
|
|
|
40 |
WString(const std::wstring& str);
|
|
|
41 |
WString(const char c);
|
|
|
42 |
WString(const unsigned char c);
|
|
|
43 |
WString(const wchar_t c);
|
|
|
44 |
WString(const WString &str);
|
288 |
cycrow |
45 |
WString(long long l);
|
|
|
46 |
WString(unsigned long long l);
|
186 |
cycrow |
47 |
WString(long l);
|
|
|
48 |
WString(unsigned long l);
|
|
|
49 |
WString(float f);
|
|
|
50 |
WString(double f);
|
|
|
51 |
WString(wchar_t* str, int len = -1)
|
|
|
52 |
{
|
|
|
53 |
if(len > 0)
|
|
|
54 |
this->_assign(str, len);
|
|
|
55 |
else
|
|
|
56 |
this->_assign(str);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
virtual ~WString(void);
|
|
|
60 |
|
|
|
61 |
// conversion functions
|
|
|
62 |
void fromLong(long l);
|
288 |
cycrow |
63 |
void fromLong64(long long l);
|
213 |
cycrow |
64 |
const WString& fromFloat(float f, int dp = -1);
|
|
|
65 |
const WString& fromString(const char* str);
|
|
|
66 |
const WString& fromString(const unsigned char* str);
|
186 |
cycrow |
67 |
void fromDouble(double f);
|
|
|
68 |
const WString &fromDouble(double f, int dp);
|
|
|
69 |
const WString &format(const wchar_t *sFormat, ...);
|
198 |
cycrow |
70 |
const WString args(const WString* args, int max) const;
|
|
|
71 |
const WString args(const WStringList& args) const;
|
|
|
72 |
const WString args(const std::vector<Utils::WString>& args) const;
|
186 |
cycrow |
73 |
const WString &arg(const WString &s1);
|
|
|
74 |
const WString &arg(const WString &s1, const WString &s2);
|
|
|
75 |
const WString &arg(const WString &s1, const WString &s2, const WString &s3);
|
|
|
76 |
const WString &arg(const WString &s1, const WString &s2, const WString &s3, const WString &s4);
|
|
|
77 |
const WString &arg(const WString &s1, const WString &s2, const WString &s3, const WString &s4, const WString &s5);
|
243 |
cycrow |
78 |
const WString& convertTime(time_t whattime, bool shortdesc = false);
|
186 |
cycrow |
79 |
|
|
|
80 |
static WString PadNumber(long iNum, int iAmount);
|
|
|
81 |
static WString Number(long l) { return WString(l); }
|
|
|
82 |
static WString Format(const wchar_t *sFormat, ...);
|
|
|
83 |
static WString Null();
|
|
|
84 |
static WString FromFloat(float f, int dp = -1);
|
213 |
cycrow |
85 |
static WString FromString(const char* data);
|
|
|
86 |
static WString FromString(const unsigned char* data);
|
186 |
cycrow |
87 |
|
|
|
88 |
bool toBool() const;
|
|
|
89 |
long toLong() const;
|
288 |
cycrow |
90 |
long long toLong64() const;
|
186 |
cycrow |
91 |
int toInt() const;
|
|
|
92 |
double toDouble() const;
|
|
|
93 |
float toFloat() const;
|
285 |
cycrow |
94 |
const wchar_t* toWChar() const;
|
|
|
95 |
const wchar_t* c_str() const;
|
|
|
96 |
std::string toUTF8() const;
|
|
|
97 |
std::string toFileUTF8() const;
|
|
|
98 |
std::wstring toStdWString() const { return _data; }
|
186 |
cycrow |
99 |
|
188 |
cycrow |
100 |
const SString toString() const;
|
291 |
cycrow |
101 |
const std::string toStdString() const;
|
186 |
cycrow |
102 |
|
|
|
103 |
// casting operators
|
285 |
cycrow |
104 |
inline operator const wchar_t *() const { return _data.c_str(); }
|
|
|
105 |
inline operator wchar_t *() const { return (wchar_t *)_data.c_str(); }
|
|
|
106 |
inline operator const std::wstring &() const { return _data; }
|
284 |
cycrow |
107 |
inline operator const int () const { return static_cast<int>(this->toLong()); }
|
186 |
cycrow |
108 |
inline operator const long () const { return this->toLong(); }
|
288 |
cycrow |
109 |
inline operator const long long () const { return this->toLong64(); }
|
186 |
cycrow |
110 |
inline operator const double () const { return this->toDouble(); }
|
|
|
111 |
inline operator const float () const { return this->toFloat(); }
|
|
|
112 |
inline operator const bool () const { return (this->empty() ? false : ((this->toLong()) ? true : false)); }
|
|
|
113 |
|
|
|
114 |
// assignment operators
|
|
|
115 |
const WString &operator= ( const WString &str );
|
|
|
116 |
const WString &operator= ( const std::wstring &str );
|
|
|
117 |
const WString &operator= ( const wchar_t *str );
|
|
|
118 |
const WString &operator= ( wchar_t c );
|
|
|
119 |
const WString &operator= ( long l );
|
|
|
120 |
const WString &operator= ( unsigned long l );
|
|
|
121 |
const WString &operator= ( float l );
|
|
|
122 |
const WString &operator= ( double l );
|
|
|
123 |
|
|
|
124 |
// subscript operators
|
285 |
cycrow |
125 |
#ifdef _WIN32
|
|
|
126 |
const wchar_t operator[] (int num) const;
|
|
|
127 |
wchar_t& operator[] (int num);
|
|
|
128 |
#else
|
284 |
cycrow |
129 |
const wchar_t operator[] (size_t num) const;
|
285 |
cycrow |
130 |
wchar_t& operator[] (size_t num);
|
|
|
131 |
#endif
|
|
|
132 |
const wchar_t& at(size_t num) const { return _data.at(num); }
|
186 |
cycrow |
133 |
|
|
|
134 |
// addition operators
|
189 |
cycrow |
135 |
WString operator+ (const wchar_t* str) const;
|
|
|
136 |
WString operator+ (const char* str) const;
|
186 |
cycrow |
137 |
WString operator+ ( const std::wstring &str ) const;
|
|
|
138 |
WString operator+ ( const WString &str ) const;
|
|
|
139 |
WString operator+ ( const wchar_t c ) const;
|
|
|
140 |
WString operator+ ( const long l ) const;
|
|
|
141 |
WString operator+ ( const unsigned long l ) const;
|
|
|
142 |
WString operator+ ( const float f ) const;
|
|
|
143 |
WString operator+ ( const double f ) const;
|
|
|
144 |
|
|
|
145 |
// compound operators
|
|
|
146 |
const WString &operator+= ( const wchar_t *str );
|
|
|
147 |
const WString &operator+= ( const std::wstring &str );
|
|
|
148 |
const WString &operator+= ( const WString &str );
|
|
|
149 |
const WString &operator+= ( const wchar_t c );
|
|
|
150 |
const WString &operator+= ( const long l );
|
|
|
151 |
const WString &operator+= ( const unsigned long l );
|
|
|
152 |
const WString &operator+= ( const float f );
|
|
|
153 |
const WString &operator+= ( const double f );
|
|
|
154 |
|
|
|
155 |
// comparison operators
|
|
|
156 |
bool operator== ( const wchar_t *str ) const;
|
|
|
157 |
bool operator== ( const std::wstring &str ) const;
|
|
|
158 |
bool operator== ( const WString &str ) const;
|
|
|
159 |
|
|
|
160 |
bool operator!= ( const wchar_t *str ) const;
|
|
|
161 |
bool operator!= ( const std::wstring &str ) const;
|
|
|
162 |
bool operator!= ( const WString &str ) const;
|
|
|
163 |
|
|
|
164 |
bool operator !() const;
|
|
|
165 |
|
|
|
166 |
bool Compare(const WString &str, bool bCaseSensative = false) const;
|
|
|
167 |
bool Compare(const wchar_t *str, bool bCaseSensative = false) const;
|
|
|
168 |
|
285 |
cycrow |
169 |
//std::wstring functions
|
|
|
170 |
bool empty() const;
|
|
|
171 |
void clear();
|
|
|
172 |
WString &erase(size_t offset, size_t count);
|
|
|
173 |
size_t length() const;
|
|
|
174 |
WString substr(size_t offset, size_t count) const;
|
|
|
175 |
WString substr(size_t offset) const;
|
|
|
176 |
wchar_t back() const;
|
|
|
177 |
|
186 |
cycrow |
178 |
// file handling
|
|
|
179 |
wchar_t *readToEndOfLine(wchar_t *data);
|
|
|
180 |
const WString &readToEndOfLine(FILE *id, int *line, bool upper);
|
|
|
181 |
|
|
|
182 |
// tokens
|
|
|
183 |
int countToken(const wchar_t *token) const;
|
|
|
184 |
WString token(const wchar_t *token, int tok) const;
|
|
|
185 |
WString tokens(const wchar_t *token, int from, int to = 0) const;
|
|
|
186 |
WString *tokenise(const wchar_t *token, int *max) const;
|
284 |
cycrow |
187 |
size_t tokenise(const wchar_t* token, std::vector<WString>& str) const;
|
|
|
188 |
std::vector<std::wstring> tokenise(const wchar_t* token) const;
|
186 |
cycrow |
189 |
WString replaceToken(const wchar_t *token, int from, const WString &replace) const;
|
|
|
190 |
WString remToken(const wchar_t* token, int from) const;
|
|
|
191 |
WString remTokens(const wchar_t* token, int from, int to = -1) const;
|
|
|
192 |
WString word(int word) const;
|
|
|
193 |
WString words(int from, int to = 0) const;
|
|
|
194 |
WString addToken(const wchar_t *token, const Utils::WString &str) const;
|
|
|
195 |
|
|
|
196 |
// find/replacement
|
273 |
cycrow |
197 |
long findPos(const WString &find, int iStartPos = 0) const;
|
186 |
cycrow |
198 |
WString findReplace(const WString &find, const WString &replace ) const;
|
|
|
199 |
WString remove(wchar_t c) const;
|
|
|
200 |
const WString &removeChar(wchar_t c);
|
|
|
201 |
const WString &removeChar(const wchar_t *cs);
|
|
|
202 |
WString findRemove(const WString &find) const;
|
|
|
203 |
WString stripHtml() const;
|
|
|
204 |
WString removeIf(int iChar, wchar_t c) const;
|
|
|
205 |
|
|
|
206 |
bool containsAny(const WString &str, bool bCaseSensative = false) const;
|
|
|
207 |
bool contains(const WString &str, bool bCaseSensative = false) const;
|
|
|
208 |
bool contains(wchar_t c, bool bCaseSensative = false) const;
|
|
|
209 |
bool isin(const WString &str, bool bCaseSensative = false) const;
|
|
|
210 |
bool isin(wchar_t c, bool bCaseSensative = false) const;
|
|
|
211 |
int compareVersion(const Utils::WString &v) const;
|
|
|
212 |
bool match(const Utils::WString &pattern) const;
|
285 |
cycrow |
213 |
bool startsWith(const WString& prefix) const;
|
|
|
214 |
bool endsWith(const WString& prefix) const;
|
186 |
cycrow |
215 |
|
204 |
cycrow |
216 |
WString& prepend(const WString &str);
|
|
|
217 |
|
186 |
cycrow |
218 |
// sub string
|
|
|
219 |
WString left(long num) const;
|
|
|
220 |
WString right(int num) const;
|
|
|
221 |
WString mid(int start, int end) const;
|
|
|
222 |
WString between(const WString &before, const WString &after) const;
|
|
|
223 |
|
|
|
224 |
bool isNumber(bool integer = false) const;
|
|
|
225 |
bool isCharNumber(int c) const;
|
|
|
226 |
const WString &removeFirstSpace();
|
|
|
227 |
const WString &removeEndSpace();
|
|
|
228 |
const WString &truncate(int iNum);
|
|
|
229 |
const WString &padNumber(int iNum);
|
|
|
230 |
const WString &pad(int iAmoumt, wchar_t cWith);
|
|
|
231 |
|
|
|
232 |
WString lower() const;
|
|
|
233 |
WString upper() const;
|
|
|
234 |
const WString &toLower();
|
|
|
235 |
const WString &toUpper();
|
|
|
236 |
|
|
|
237 |
const WString &toFilename();
|
|
|
238 |
WString asFilename() const;
|
|
|
239 |
|
|
|
240 |
private:
|
285 |
cycrow |
241 |
void _assign(const wchar_t* str) { _data.assign(str); }
|
|
|
242 |
void _assign(const wchar_t* str, size_t len) { _data.assign(str, len); }
|
186 |
cycrow |
243 |
bool _isCharNumber(wchar_t c) const;
|
285 |
cycrow |
244 |
std::wstring::size_type _token_nextPos(const wchar_t *token, std::wstring::size_type curPos) const;
|
186 |
cycrow |
245 |
};
|
|
|
246 |
|
|
|
247 |
SPKEXPORT WString operator+(const wchar_t* str1, const WString& str2);
|
189 |
cycrow |
248 |
SPKEXPORT WString operator+(const char* str1, const WString& str2);
|
186 |
cycrow |
249 |
|
|
|
250 |
#pragma warning( pop )
|
|
|
251 |
|
|
|
252 |
}
|