Subversion Repositories spk

Rev

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