Subversion Repositories spk

Rev

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

Rev Author Line No. Line
8 cycrow 1
#pragma once
2
 
3
#include <string>
4
#include "../spkdll.h"
5
 
50 cycrow 6
enum {
7
	COMPARE_SAME		= 0,
8
	COMPARE_NEWER		= 1,
9
	COMPARE_OLDER		= -1,
10
};
11
 
8 cycrow 12
namespace Utils {
13
/**
14
 * String wrapper class
15
 */
16
class SPKEXPORT String : public std::string
17
{
18
public:
19
	// constructors
20
	String(void);
21
	String(const char *str);
22
	String(const unsigned char *str);
23
	String(const std::string &str);
14 cycrow 24
	String(const char c);
8 cycrow 25
	String(const unsigned char c);
26
	String(const String &str);
27
	String(long l);
28
	String(unsigned long l);
29
	String(float f);
30
	String(double f);
31
 
32
	virtual ~String(void);
33
 
34
	// conversion functions
35
	void fromLong(long l);
36
	void fromFloat(float f);
37
	void fromFloat(float f, int dp);
38
	void fromDouble(double f);
39
	void fromDouble(double f, int dp);
40
	const String &format(const char *sFormat, ...);
41
 
50 cycrow 42
	static String PadNumber(long iNum, int iAmount);
43
	static String Number(long l) { return String(l); }
44
	static String Format(const char *sFormat, ...);
45
 
8 cycrow 46
	long toLong() const;
47
	double toDouble() const;
48
	float toFloat() const;
49
 
50
	// casting operators
51
	inline operator const char *() const			{ return (const char *)this->c_str(); }
52
	inline operator char *() const					{ return (char *)this->c_str(); }
53
	inline operator const unsigned char *() const	{ return (const unsigned char *)this->c_str(); }
54
	inline operator unsigned char *() const			{ return (unsigned char *)this->c_str(); }
55
	inline operator const std::string &() const		{ return (const std::string &)*this; }
10 cycrow 56
	inline operator const int () const				{ return this->toLong(); }
8 cycrow 57
	inline operator const long () const				{ return this->toLong(); }
58
	inline operator const double () const			{ return this->toDouble(); }
59
	inline operator const float () const			{ return this->toFloat(); }
10 cycrow 60
	inline operator const bool () const				{ return (this->toLong()) ? true : false; }
8 cycrow 61
 
62
	// assignment operators
63
	const String &operator= ( const String &str );
64
	const String &operator= ( const std::string &str );
65
	const String &operator= ( const char *str );
66
	const String &operator= ( const unsigned char *str );
67
	const String &operator= ( unsigned char c );
68
	const String &operator= ( char c );
69
	const String &operator= ( long l );
70
	const String &operator= ( unsigned long l );
71
	const String &operator= ( float l );
72
	const String &operator= ( double l );
73
 
74
	// subscript operators
75
	const unsigned char operator[] ( int num ) const;
76
	unsigned char &operator[] ( int num );
77
 
78
	// addition operators
79
	String operator+ ( const char *str ) const;
80
	String operator+ ( const unsigned char *str ) const;
81
	String operator+ ( const std::string &str ) const;
82
	String operator+ ( const String &str ) const;
83
	String operator+ ( const char c ) const;
84
	String operator+ ( const unsigned char c ) const;
85
	String operator+ ( const long l ) const;
86
	String operator+ ( const unsigned long l ) const;
87
	String operator+ ( const float f ) const;
88
	String operator+ ( const double f ) const;
89
 
90
	// compound operators
91
	const String &operator+= ( const char *str );
92
	const String &operator+= ( const unsigned char *str );
93
	const String &operator+= ( const std::string &str );
94
	const String &operator+= ( const String &str );
95
	const String &operator+= ( const char c );
96
	const String &operator+= ( const unsigned char c );
97
	const String &operator+= ( const long l );
98
	const String &operator+= ( const unsigned long l );
99
	const String &operator+= ( const float f );
100
	const String &operator+= ( const double f );
101
 
102
	// comparison operators
103
	bool operator== ( const char *str ) const;
104
	bool operator== ( const unsigned char *str ) const;
105
	bool operator== ( const std::string &str ) const;
106
	bool operator== ( const String &str ) const;
107
 
108
	bool operator!= ( const char *str ) const;
109
	bool operator!= ( const unsigned char *str ) const;
110
	bool operator!= ( const std::string &str ) const;
111
	bool operator!= ( const String &str ) const;
112
 
113
	bool Compare(const String &str, bool bCaseSensative = false) const;
114
	bool Compare(const unsigned char *str, bool bCaseSensative = false) const;
115
	bool Compare(const char *str, bool bCaseSensative = false) const;
116
 
39 cycrow 117
	// file handling
8 cycrow 118
	unsigned char *readToEndOfLine(unsigned char *data);
119
	char *readToEndOfLine(char *data);
39 cycrow 120
	const String &readToEndOfLine(FILE *id, int *line, bool upper);
8 cycrow 121
 
122
	// tokens
123
	int countToken(const char *token) const;
124
	String token(const char *token, int tok) const;
125
	String tokens(const char *token, int from, int to = 0) const;
10 cycrow 126
	String *tokenise(const char *token, int *max) const;
39 cycrow 127
	String replaceToken(const char *token, int from, const String &replace) const;
128
	String remToken(const char *token, int t) const;
129
	String word(int word) const;
130
	String words(int from, int to = 0) const;
14 cycrow 131
 
39 cycrow 132
	// find/replacement
133
	int findPos(const String &find, int iStartPos = 0) const;
8 cycrow 134
	String findReplace(const String &find, const String &replace ) const;
39 cycrow 135
	String remove(char c) const;
136
	const String &removeChar(char c);
69 cycrow 137
	const String &removeChar(const char *cs);
48 cycrow 138
	String findRemove(const String &find) const;
139
	String stripHtml() const;
58 cycrow 140
	String removeIf(int iChar, char c) const;
8 cycrow 141
 
142
	bool isin(const String &str, bool bCaseSensative = false) const;
143
	bool isin(char c, bool bCaseSensative = false) const;
50 cycrow 144
	int compareVersion(const Utils::String &v) const;
8 cycrow 145
 
39 cycrow 146
	// sub string
8 cycrow 147
	String left(long num) const;
148
	String right(int num) const;
39 cycrow 149
	String mid(int start, int end) const;
56 cycrow 150
	String between(const String &before, const String &after) const;
8 cycrow 151
 
13 cycrow 152
	bool isNumber(bool integer = false) const;
153
	bool isCharNumber(int c) const;
14 cycrow 154
	const String &removeFirstSpace();
39 cycrow 155
	const String &removeEndSpace();
34 cycrow 156
	const String &truncate(int iNum);
50 cycrow 157
	const String &padNumber(int iNum);
158
	const String &pad(int iAmoumt, char cWith);
13 cycrow 159
 
43 cycrow 160
	String lower() const;
161
	String upper() const;
162
	const String &toLower();
163
	const String &toUpper();
164
 
8 cycrow 165
private:
13 cycrow 166
	bool _isCharNumber(char c) const;
8 cycrow 167
	std::string::size_type _token_nextPos(const char *token, std::string::size_type curPos) const;
168
};
169
 
170
String operator+(const char *str1, const String &str2);
171
}