Subversion Repositories spk

Rev

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