Subversion Repositories spk

Rev

Rev 285 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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;
186 cycrow 101
 
102
	// casting operators
285 cycrow 103
	inline operator const wchar_t *() const			{ return _data.c_str(); }
104
	inline operator wchar_t *() const				{ return (wchar_t *)_data.c_str(); }
105
	inline operator const std::wstring &() const	{ return _data; }
284 cycrow 106
	inline operator const int () const				{ return static_cast<int>(this->toLong()); }
186 cycrow 107
	inline operator const long () const				{ return this->toLong(); }
288 cycrow 108
	inline operator const long long () const		{ return this->toLong64(); }
186 cycrow 109
	inline operator const double () const			{ return this->toDouble(); }
110
	inline operator const float () const			{ return this->toFloat(); }
111
	inline operator const bool () const				{ return (this->empty() ? false : ((this->toLong()) ? true : false)); }
112
 
113
	// assignment operators
114
	const WString &operator= ( const WString &str );
115
	const WString &operator= ( const std::wstring &str );
116
	const WString &operator= ( const wchar_t *str );
117
	const WString &operator= ( wchar_t c );
118
	const WString &operator= ( long l );
119
	const WString &operator= ( unsigned long l );
120
	const WString &operator= ( float l );
121
	const WString &operator= ( double l );
122
 
123
	// subscript operators
285 cycrow 124
#ifdef _WIN32
125
	const wchar_t operator[] (int num) const;
126
	wchar_t& operator[] (int num);
127
#else
284 cycrow 128
	const wchar_t operator[] (size_t num) const;
285 cycrow 129
	wchar_t& operator[] (size_t num);
130
#endif
131
	const wchar_t& at(size_t num) const { return _data.at(num); }
186 cycrow 132
 
133
	// addition operators
189 cycrow 134
	WString operator+ (const wchar_t* str) const;
135
	WString operator+ (const char* str) const;
186 cycrow 136
	WString operator+ ( const std::wstring &str ) const;
137
	WString operator+ ( const WString &str ) const;
138
	WString operator+ ( const wchar_t c ) const;
139
	WString operator+ ( const long l ) const;
140
	WString operator+ ( const unsigned long l ) const;
141
	WString operator+ ( const float f ) const;
142
	WString operator+ ( const double f ) const;
143
 
144
	// compound operators
145
	const WString &operator+= ( const wchar_t *str );
146
	const WString &operator+= ( const std::wstring &str );
147
	const WString &operator+= ( const WString &str );
148
	const WString &operator+= ( const wchar_t c );
149
	const WString &operator+= ( const long l );
150
	const WString &operator+= ( const unsigned long l );
151
	const WString &operator+= ( const float f );
152
	const WString &operator+= ( const double f );
153
 
154
	// comparison operators
155
	bool operator== ( const wchar_t *str ) const;
156
	bool operator== ( const std::wstring &str ) const;
157
	bool operator== ( const WString &str ) const;
158
 
159
	bool operator!= ( const wchar_t *str ) const;
160
	bool operator!= ( const std::wstring &str ) const;
161
	bool operator!= ( const WString &str ) const;
162
 
163
	bool operator !() const;
164
 
165
	bool Compare(const WString &str, bool bCaseSensative = false) const;
166
	bool Compare(const wchar_t *str, bool bCaseSensative = false) const;
167
 
285 cycrow 168
	//std::wstring functions
169
	bool empty() const;
170
	void clear();
171
	WString &erase(size_t offset, size_t count);
172
	size_t length() const;
173
	WString substr(size_t offset, size_t count) const;
174
	WString substr(size_t offset) const;
175
	wchar_t back() const;
176
 
186 cycrow 177
	// file handling
178
	wchar_t *readToEndOfLine(wchar_t *data);
179
	const WString &readToEndOfLine(FILE *id, int *line, bool upper);
180
 
181
	// tokens
182
	int countToken(const wchar_t *token) const;
183
	WString token(const wchar_t *token, int tok) const;
184
	WString tokens(const wchar_t *token, int from, int to = 0) const;
185
	WString *tokenise(const wchar_t *token, int *max) const;
284 cycrow 186
	size_t tokenise(const wchar_t* token, std::vector<WString>& str) const;
187
	std::vector<std::wstring> tokenise(const wchar_t* token) const;
186 cycrow 188
	WString replaceToken(const wchar_t *token, int from, const WString &replace) const;
189
	WString remToken(const wchar_t* token, int from) const;
190
	WString remTokens(const wchar_t* token, int from, int to = -1) const;
191
	WString word(int word) const;
192
	WString words(int from, int to = 0) const;
193
	WString addToken(const wchar_t *token, const Utils::WString &str) const;
194
 
195
	// find/replacement
273 cycrow 196
	long findPos(const WString &find, int iStartPos = 0) const;
186 cycrow 197
	WString findReplace(const WString &find, const WString &replace ) const;
198
	WString remove(wchar_t c) const;
199
	const WString &removeChar(wchar_t c);
200
	const WString &removeChar(const wchar_t *cs);
201
	WString findRemove(const WString &find) const;
202
	WString stripHtml() const;
203
	WString removeIf(int iChar, wchar_t c) const;
204
 
205
	bool containsAny(const WString &str, bool bCaseSensative = false) const;
206
	bool contains(const WString &str, bool bCaseSensative = false) const;
207
	bool contains(wchar_t c, bool bCaseSensative = false) const;
208
	bool isin(const WString &str, bool bCaseSensative = false) const;
209
	bool isin(wchar_t c, bool bCaseSensative = false) const;
210
	int compareVersion(const Utils::WString &v) const;
211
	bool match(const Utils::WString &pattern) const;
285 cycrow 212
	bool startsWith(const WString& prefix) const;
213
	bool endsWith(const WString& prefix) const;
186 cycrow 214
 
204 cycrow 215
	WString& prepend(const WString &str);
216
 
186 cycrow 217
	// sub string
218
	WString left(long num) const;
219
	WString right(int num) const;
220
	WString mid(int start, int end) const;
221
	WString between(const WString &before, const WString &after) const;
222
 
223
	bool isNumber(bool integer = false) const;
224
	bool isCharNumber(int c) const;
225
	const WString &removeFirstSpace();
226
	const WString &removeEndSpace();
227
	const WString &truncate(int iNum);
228
	const WString &padNumber(int iNum);
229
	const WString &pad(int iAmoumt, wchar_t cWith);
230
 
231
	WString lower() const;
232
	WString upper() const;
233
	const WString &toLower();
234
	const WString &toUpper();
235
 
236
	const WString &toFilename();
237
	WString asFilename() const;
238
 
239
private:
285 cycrow 240
	void _assign(const wchar_t* str) { _data.assign(str); }
241
	void _assign(const wchar_t* str, size_t len) { _data.assign(str, len); }
186 cycrow 242
	bool _isCharNumber(wchar_t c) const;
285 cycrow 243
	std::wstring::size_type _token_nextPos(const wchar_t *token, std::wstring::size_type curPos) const;
186 cycrow 244
};
245
 
246
SPKEXPORT WString operator+(const wchar_t* str1, const WString& str2);
189 cycrow 247
SPKEXPORT WString operator+(const char* str1, const WString& str2);
186 cycrow 248
 
249
#pragma warning( pop )
250
 
251
}