Sat, 16 Mar 2013 00:35:36 +0200
Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
0 | 1 | #ifndef __STR_H__ |
2 | #define __STR_H__ | |
3 | ||
4 | #include <string.h> | |
5 | #include <stdlib.h> | |
6 | #include <stdarg.h> | |
7 | // #include <initializer_list> | |
8 | #include <vector> | |
9 | ||
10 | #ifdef QT_VERSION | |
11 | #include <QString> | |
12 | #endif // QT_VERSION | |
13 | ||
14 | char* vdynformat (const char* csFormat, va_list vArgs, long int lSize); | |
15 | ||
16 | // Dynamic string object, allocates memory when needed and | |
17 | // features a good bunch of manipulation methods | |
18 | class str { | |
19 | private: | |
20 | // The actual message | |
21 | char* text; | |
22 | ||
23 | // Where will append() place new characters? | |
24 | unsigned short curs; | |
25 | ||
26 | // Allocated length | |
27 | unsigned short alloclen; | |
28 | ||
29 | // Resize the text buffer to len characters | |
30 | void resize (unsigned int len); | |
31 | ||
32 | public: | |
33 | // ====================================================================== | |
34 | str (); | |
35 | str (const char* c); | |
36 | str (char c); | |
37 | ~str (); | |
38 | ||
39 | static str mkfmt (const char* fmt, ...) { | |
40 | va_list va; | |
41 | char* buf; | |
42 | ||
43 | va_start (va, fmt); | |
44 | buf = vdynformat (fmt, va, 256); | |
45 | va_end (va); | |
46 | ||
47 | str val = buf; | |
48 | delete[] buf; | |
49 | return val; | |
50 | } | |
51 | ||
52 | // ====================================================================== | |
53 | // METHODS | |
54 | ||
55 | // Empty the string | |
56 | void clear (); | |
57 | ||
58 | // Length of the string | |
59 | size_t len () { | |
60 | return strlen (text); | |
61 | } | |
62 | ||
63 | // The char* form of the string | |
64 | char* chars (); | |
65 | ||
66 | // Dumps the character table of the string | |
67 | void dump (); | |
68 | ||
69 | // Appends text to the string | |
70 | void append (char c); | |
71 | void append (const char* c); | |
72 | void append (str c); | |
73 | ||
74 | // Formats text to the string. | |
75 | void format (const char* fmt, ...); | |
76 | str format (...); | |
77 | ||
78 | // Appends formatted text to the string. | |
79 | void appendformat (const char* c, ...); | |
80 | ||
81 | // Returns the first occurrence of c in the string, optionally starting | |
82 | // from a certain position rather than the start. | |
83 | int first (const char* c, unsigned int a = 0); | |
84 | ||
85 | // Returns the last occurrence of c in the string, optionally starting | |
86 | // from a certain position rather than the end. | |
87 | int last (const char* c, int a = -1); | |
88 | ||
89 | // Returns a substring of the string, from a to b. | |
90 | str substr (unsigned int a, unsigned int b); | |
91 | ||
92 | // Replace a substring with another substring. | |
93 | void replace (const char* o, const char* n, unsigned int a = 0); | |
94 | ||
95 | // Removes a given index from the string, optionally more characters than just 1. | |
96 | void remove (unsigned int idx, unsigned int dellen=1); | |
97 | ||
98 | str trim (int dellen); | |
99 | ||
100 | // Inserts a substring into a certain position. | |
101 | void insert (char* c, unsigned int pos); | |
102 | ||
103 | // Reverses the string. | |
104 | str reverse (); | |
105 | ||
106 | // Repeats the string a given amount of times. | |
107 | str repeat (int n); | |
108 | ||
109 | // Is the string a number? | |
110 | bool isnumber (); | |
111 | ||
112 | // Is the string a word, i.e consists only of alphabetic letters? | |
113 | bool isword (); | |
114 | ||
115 | // Convert string to lower case | |
116 | str tolower (); | |
117 | ||
118 | // Convert string to upper case | |
119 | str toupper (); | |
120 | ||
121 | // Compare this string with another | |
122 | int compare (const char* c); | |
123 | int compare (str c); | |
124 | int icompare (str c); | |
125 | int icompare (const char* c); | |
126 | ||
127 | // Counts the amount of substrings in the string | |
128 | unsigned int count (char* s); | |
129 | unsigned int count (char s); | |
130 | ||
131 | // Counts where the given substring is seen for the nth time | |
132 | int instanceof (const char* s, unsigned n); | |
133 | ||
134 | char subscript (uint pos) { | |
135 | return operator[] (pos); | |
136 | } | |
137 | ||
138 | std::vector<str> split (str del); | |
139 | ||
140 | /* | |
141 | void strip (char c); | |
142 | void strip (std::initializer_list<char> unwanted); | |
143 | */ | |
144 | ||
145 | // ====================================================================== | |
146 | // OPERATORS | |
147 | str operator+ (str& c) { | |
148 | append (c); | |
149 | return *this; | |
150 | } | |
151 | ||
152 | str& operator+= (char c) { | |
153 | append (c); | |
154 | return *this; | |
155 | } | |
156 | ||
157 | str& operator+= (const char* c) { | |
158 | append (c); | |
159 | return *this; | |
160 | } | |
161 | ||
162 | str& operator+= (const str c) { | |
163 | append (c); | |
164 | return *this; | |
165 | } | |
166 | ||
167 | str operator* (const int repcount) { | |
168 | repeat (repcount); | |
169 | return *this; | |
170 | } | |
171 | ||
172 | str& operator*= (const int repcount) { | |
173 | str other = repeat (repcount); | |
174 | clear (); | |
175 | append (other); | |
176 | return *this; | |
177 | } | |
178 | ||
179 | str operator- (const int trimcount) { | |
180 | return trim (trimcount); | |
181 | } | |
182 | ||
183 | str& operator-= (const int trimcount) { | |
184 | str other = trim (trimcount); | |
185 | clear (); | |
186 | append (other); | |
187 | return *this; | |
188 | } | |
189 | ||
190 | std::vector<str> operator/ (str splitstring); | |
191 | std::vector<str> operator/ (char* splitstring); | |
192 | std::vector<str> operator/ (const char* splitstring); | |
193 | ||
194 | int operator% (str splitstring) { | |
195 | return count (splitstring.chars()); | |
196 | } | |
197 | ||
198 | int operator% (char* splitstring) { | |
199 | return count (splitstring); | |
200 | } | |
201 | ||
202 | int operator% (const char* splitstring) { | |
203 | return count (str (splitstring).chars()); | |
204 | } | |
205 | ||
206 | str operator+ () { | |
207 | return toupper (); | |
208 | } | |
209 | ||
210 | str operator- () { | |
211 | return tolower (); | |
212 | } | |
213 | ||
214 | str operator! () { | |
215 | return reverse (); | |
216 | } | |
217 | ||
218 | size_t operator~ () { | |
219 | return len (); | |
220 | } | |
221 | ||
222 | #define DEFINE_OPERATOR_TYPE(OPER, TYPE) \ | |
223 | bool operator OPER (TYPE other) {return compare(other) OPER 0;} | |
224 | #define DEFINE_OPERATOR(OPER) \ | |
225 | DEFINE_OPERATOR_TYPE (OPER, str) \ | |
226 | DEFINE_OPERATOR_TYPE (OPER, char*) \ | |
227 | DEFINE_OPERATOR_TYPE (OPER, const char*) | |
228 | ||
229 | DEFINE_OPERATOR (==) | |
230 | DEFINE_OPERATOR (!=) | |
231 | DEFINE_OPERATOR (>) | |
232 | DEFINE_OPERATOR (<) | |
233 | DEFINE_OPERATOR (>=) | |
234 | DEFINE_OPERATOR (<=) | |
235 | ||
236 | char& operator[] (int pos) { | |
237 | return text[pos]; | |
238 | } | |
239 | ||
240 | operator char* () const { | |
241 | return text; | |
242 | } | |
243 | ||
244 | #ifdef QT_VERSION | |
245 | operator QString () const { | |
246 | return text; | |
247 | } | |
248 | #endif // QT_VERSION | |
249 | ||
250 | operator int () const { | |
251 | return atoi (text); | |
252 | } | |
253 | ||
254 | operator uint () const { | |
255 | return operator int(); | |
256 | } | |
257 | }; | |
258 | ||
259 | #endif // __STR_H__ |