Mon, 15 Apr 2013 02:56:42 +0300
Picking improvements
30
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
1 | /* |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
2 | * LDForge: LDraw parts authoring CAD |
104 | 3 | * Copyright (C) 2013 Santeri Piippo |
30
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
4 | * |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
5 | * This program is free software: you can redistribute it and/or modify |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
6 | * it under the terms of the GNU General Public License as published by |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
7 | * the Free Software Foundation, either version 3 of the License, or |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
8 | * (at your option) any later version. |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
9 | * |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
10 | * This program is distributed in the hope that it will be useful, |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
13 | * GNU General Public License for more details. |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
14 | * |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
15 | * You should have received a copy of the GNU General Public License |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
17 | */ |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
18 | |
0 | 19 | #ifndef __STR_H__ |
20 | #define __STR_H__ | |
21 | ||
22 | #include <string.h> | |
23 | #include <stdlib.h> | |
24 | #include <stdarg.h> | |
25 | // #include <initializer_list> | |
26 | #include <vector> | |
25
c74bb88f537d
Deleted scanner.cpp (don't need it), merged model.cpp into io.cpp. Renamed LDForgeWindow to just ForgeWindow since I want the LD* prefix only be given to LDObject derivatives.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
18
diff
changeset
|
27 | #include <QString> |
0 | 28 | |
29 | char* vdynformat (const char* csFormat, va_list vArgs, long int lSize); | |
30 | ||
18
a6732098fed8
Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
31 | class vertex; |
a6732098fed8
Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
32 | |
0 | 33 | // Dynamic string object, allocates memory when needed and |
34 | // features a good bunch of manipulation methods | |
35 | class str { | |
36 | private: | |
37 | // The actual message | |
38 | char* text; | |
39 | ||
40 | // Where will append() place new characters? | |
41 | unsigned short curs; | |
42 | ||
43 | // Allocated length | |
44 | unsigned short alloclen; | |
45 | ||
46 | // Resize the text buffer to len characters | |
47 | void resize (unsigned int len); | |
48 | ||
49 | public: | |
50 | // ====================================================================== | |
51 | str (); | |
52 | str (const char* c); | |
53 | str (char c); | |
29
55406ce7446e
Added LDraw path setting dialog
Santeri Piippo <crimsondusk64@gmail.com>
parents:
25
diff
changeset
|
54 | str (QString c); |
0 | 55 | ~str (); |
56 | ||
57 | static str mkfmt (const char* fmt, ...) { | |
58 | va_list va; | |
59 | char* buf; | |
60 | ||
61 | va_start (va, fmt); | |
62 | buf = vdynformat (fmt, va, 256); | |
63 | va_end (va); | |
64 | ||
65 | str val = buf; | |
66 | delete[] buf; | |
67 | return val; | |
68 | } | |
69 | ||
70 | // ====================================================================== | |
71 | // METHODS | |
72 | ||
73 | // Empty the string | |
74 | void clear (); | |
75 | ||
76 | // Length of the string | |
77 | size_t len () { | |
78 | return strlen (text); | |
79 | } | |
80 | ||
81 | // The char* form of the string | |
82 | char* chars (); | |
83 | ||
84 | // Dumps the character table of the string | |
85 | void dump (); | |
86 | ||
87 | // Appends text to the string | |
25
c74bb88f537d
Deleted scanner.cpp (don't need it), merged model.cpp into io.cpp. Renamed LDForgeWindow to just ForgeWindow since I want the LD* prefix only be given to LDObject derivatives.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
18
diff
changeset
|
88 | void append (const char c); |
0 | 89 | void append (const char* c); |
90 | void append (str c); | |
25
c74bb88f537d
Deleted scanner.cpp (don't need it), merged model.cpp into io.cpp. Renamed LDForgeWindow to just ForgeWindow since I want the LD* prefix only be given to LDObject derivatives.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
18
diff
changeset
|
91 | void append (QString c); |
0 | 92 | |
93 | // Formats text to the string. | |
94 | void format (const char* fmt, ...); | |
95 | str format (...); | |
96 | ||
97 | // Appends formatted text to the string. | |
98 | void appendformat (const char* c, ...); | |
99 | ||
100 | // Returns the first occurrence of c in the string, optionally starting | |
101 | // from a certain position rather than the start. | |
102 | int first (const char* c, unsigned int a = 0); | |
103 | ||
104 | // Returns the last occurrence of c in the string, optionally starting | |
105 | // from a certain position rather than the end. | |
106 | int last (const char* c, int a = -1); | |
107 | ||
108 | // Returns a substring of the string, from a to b. | |
109 | str substr (unsigned int a, unsigned int b); | |
110 | ||
111 | // Replace a substring with another substring. | |
112 | void replace (const char* o, const char* n, unsigned int a = 0); | |
113 | ||
114 | // Removes a given index from the string, optionally more characters than just 1. | |
115 | void remove (unsigned int idx, unsigned int dellen=1); | |
116 | ||
25
c74bb88f537d
Deleted scanner.cpp (don't need it), merged model.cpp into io.cpp. Renamed LDForgeWindow to just ForgeWindow since I want the LD* prefix only be given to LDObject derivatives.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
18
diff
changeset
|
117 | // Trims the given amount of characters. If negative, the characters |
c74bb88f537d
Deleted scanner.cpp (don't need it), merged model.cpp into io.cpp. Renamed LDForgeWindow to just ForgeWindow since I want the LD* prefix only be given to LDObject derivatives.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
18
diff
changeset
|
118 | // are removed from the beginning of the string, if positive, from the |
c74bb88f537d
Deleted scanner.cpp (don't need it), merged model.cpp into io.cpp. Renamed LDForgeWindow to just ForgeWindow since I want the LD* prefix only be given to LDObject derivatives.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
18
diff
changeset
|
119 | // end of the string. |
0 | 120 | str trim (int dellen); |
121 | ||
122 | // Inserts a substring into a certain position. | |
123 | void insert (char* c, unsigned int pos); | |
124 | ||
125 | // Reverses the string. | |
126 | str reverse (); | |
127 | ||
128 | // Repeats the string a given amount of times. | |
129 | str repeat (int n); | |
130 | ||
131 | // Is the string a number? | |
132 | bool isnumber (); | |
133 | ||
134 | // Is the string a word, i.e consists only of alphabetic letters? | |
135 | bool isword (); | |
136 | ||
137 | // Convert string to lower case | |
138 | str tolower (); | |
139 | ||
140 | // Convert string to upper case | |
141 | str toupper (); | |
142 | ||
143 | // Compare this string with another | |
144 | int compare (const char* c); | |
145 | int compare (str c); | |
146 | int icompare (str c); | |
147 | int icompare (const char* c); | |
148 | ||
149 | // Counts the amount of substrings in the string | |
150 | unsigned int count (char* s); | |
151 | unsigned int count (char s); | |
152 | ||
153 | // Counts where the given substring is seen for the nth time | |
154 | int instanceof (const char* s, unsigned n); | |
155 | ||
156 | char subscript (uint pos) { | |
157 | return operator[] (pos); | |
158 | } | |
159 | ||
60
961663d05463
Parsing stability, finally figured that dumb crash
Santeri Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
160 | std::vector<str> split (str del, bool bNoBlanks = false); |
0 | 161 | |
162 | void strip (char c); | |
163 | void strip (std::initializer_list<char> unwanted); | |
164 | ||
165 | // ====================================================================== | |
166 | // OPERATORS | |
167 | str operator+ (str& c) { | |
168 | append (c); | |
169 | return *this; | |
170 | } | |
171 | ||
172 | str& operator+= (char c) { | |
173 | append (c); | |
174 | return *this; | |
175 | } | |
176 | ||
177 | str& operator+= (const char* c) { | |
178 | append (c); | |
179 | return *this; | |
180 | } | |
181 | ||
182 | str& operator+= (const str c) { | |
183 | append (c); | |
184 | return *this; | |
185 | } | |
186 | ||
25
c74bb88f537d
Deleted scanner.cpp (don't need it), merged model.cpp into io.cpp. Renamed LDForgeWindow to just ForgeWindow since I want the LD* prefix only be given to LDObject derivatives.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
18
diff
changeset
|
187 | str& operator+= (const QString c) { |
c74bb88f537d
Deleted scanner.cpp (don't need it), merged model.cpp into io.cpp. Renamed LDForgeWindow to just ForgeWindow since I want the LD* prefix only be given to LDObject derivatives.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
18
diff
changeset
|
188 | append (c); |
c74bb88f537d
Deleted scanner.cpp (don't need it), merged model.cpp into io.cpp. Renamed LDForgeWindow to just ForgeWindow since I want the LD* prefix only be given to LDObject derivatives.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
18
diff
changeset
|
189 | return *this; |
c74bb88f537d
Deleted scanner.cpp (don't need it), merged model.cpp into io.cpp. Renamed LDForgeWindow to just ForgeWindow since I want the LD* prefix only be given to LDObject derivatives.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
18
diff
changeset
|
190 | } |
c74bb88f537d
Deleted scanner.cpp (don't need it), merged model.cpp into io.cpp. Renamed LDForgeWindow to just ForgeWindow since I want the LD* prefix only be given to LDObject derivatives.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
18
diff
changeset
|
191 | |
18
a6732098fed8
Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
192 | str& operator+= (vertex vrt); |
a6732098fed8
Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
193 | |
0 | 194 | str operator* (const int repcount) { |
195 | repeat (repcount); | |
196 | return *this; | |
197 | } | |
198 | ||
199 | str& operator*= (const int repcount) { | |
200 | str other = repeat (repcount); | |
201 | clear (); | |
202 | append (other); | |
203 | return *this; | |
204 | } | |
205 | ||
206 | str operator- (const int trimcount) { | |
207 | return trim (trimcount); | |
208 | } | |
209 | ||
210 | str& operator-= (const int trimcount) { | |
211 | str other = trim (trimcount); | |
212 | clear (); | |
213 | append (other); | |
214 | return *this; | |
215 | } | |
216 | ||
217 | std::vector<str> operator/ (str splitstring); | |
218 | std::vector<str> operator/ (char* splitstring); | |
219 | std::vector<str> operator/ (const char* splitstring); | |
220 | ||
221 | int operator% (str splitstring) { | |
222 | return count (splitstring.chars()); | |
223 | } | |
224 | ||
225 | int operator% (char* splitstring) { | |
226 | return count (splitstring); | |
227 | } | |
228 | ||
229 | int operator% (const char* splitstring) { | |
230 | return count (str (splitstring).chars()); | |
231 | } | |
232 | ||
233 | str operator+ () { | |
234 | return toupper (); | |
235 | } | |
236 | ||
237 | str operator- () { | |
238 | return tolower (); | |
239 | } | |
240 | ||
241 | str operator! () { | |
242 | return reverse (); | |
243 | } | |
244 | ||
245 | size_t operator~ () { | |
246 | return len (); | |
247 | } | |
248 | ||
249 | #define DEFINE_OPERATOR_TYPE(OPER, TYPE) \ | |
250 | bool operator OPER (TYPE other) {return compare(other) OPER 0;} | |
251 | #define DEFINE_OPERATOR(OPER) \ | |
252 | DEFINE_OPERATOR_TYPE (OPER, str) \ | |
253 | DEFINE_OPERATOR_TYPE (OPER, char*) \ | |
254 | DEFINE_OPERATOR_TYPE (OPER, const char*) | |
255 | ||
256 | DEFINE_OPERATOR (==) | |
257 | DEFINE_OPERATOR (!=) | |
258 | DEFINE_OPERATOR (>) | |
259 | DEFINE_OPERATOR (<) | |
260 | DEFINE_OPERATOR (>=) | |
261 | DEFINE_OPERATOR (<=) | |
262 | ||
263 | char& operator[] (int pos) { | |
264 | return text[pos]; | |
265 | } | |
266 | ||
267 | operator char* () const { | |
268 | return text; | |
269 | } | |
270 | ||
271 | operator QString () const { | |
272 | return text; | |
273 | } | |
274 | ||
275 | operator int () const { | |
276 | return atoi (text); | |
277 | } | |
278 | ||
279 | operator uint () const { | |
280 | return operator int(); | |
281 | } | |
282 | }; | |
283 | ||
284 | #endif // __STR_H__ |