Subversion Repositories spk

Rev

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