Wed, 20 Mar 2013 02:44:21 +0200
Added code for parsing vertices
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 |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
3 | * Copyright (C) 2013 Santeri `arezey` Piippo |
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 | ||
160 | std::vector<str> split (str del); | |
161 | ||
162 | /* | |
163 | void strip (char c); | |
164 | void strip (std::initializer_list<char> unwanted); | |
165 | */ | |
166 | ||
167 | // ====================================================================== | |
168 | // OPERATORS | |
169 | str operator+ (str& c) { | |
170 | append (c); | |
171 | return *this; | |
172 | } | |
173 | ||
174 | str& operator+= (char c) { | |
175 | append (c); | |
176 | return *this; | |
177 | } | |
178 | ||
179 | str& operator+= (const char* c) { | |
180 | append (c); | |
181 | return *this; | |
182 | } | |
183 | ||
184 | str& operator+= (const str c) { | |
185 | append (c); | |
186 | return *this; | |
187 | } | |
188 | ||
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
|
189 | 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
|
190 | 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
|
191 | 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
|
192 | } |
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
|
193 | |
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
|
194 | 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
|
195 | |
0 | 196 | str operator* (const int repcount) { |
197 | repeat (repcount); | |
198 | return *this; | |
199 | } | |
200 | ||
201 | str& operator*= (const int repcount) { | |
202 | str other = repeat (repcount); | |
203 | clear (); | |
204 | append (other); | |
205 | return *this; | |
206 | } | |
207 | ||
208 | str operator- (const int trimcount) { | |
209 | return trim (trimcount); | |
210 | } | |
211 | ||
212 | str& operator-= (const int trimcount) { | |
213 | str other = trim (trimcount); | |
214 | clear (); | |
215 | append (other); | |
216 | return *this; | |
217 | } | |
218 | ||
219 | std::vector<str> operator/ (str splitstring); | |
220 | std::vector<str> operator/ (char* splitstring); | |
221 | std::vector<str> operator/ (const char* splitstring); | |
222 | ||
223 | int operator% (str splitstring) { | |
224 | return count (splitstring.chars()); | |
225 | } | |
226 | ||
227 | int operator% (char* splitstring) { | |
228 | return count (splitstring); | |
229 | } | |
230 | ||
231 | int operator% (const char* splitstring) { | |
232 | return count (str (splitstring).chars()); | |
233 | } | |
234 | ||
235 | str operator+ () { | |
236 | return toupper (); | |
237 | } | |
238 | ||
239 | str operator- () { | |
240 | return tolower (); | |
241 | } | |
242 | ||
243 | str operator! () { | |
244 | return reverse (); | |
245 | } | |
246 | ||
247 | size_t operator~ () { | |
248 | return len (); | |
249 | } | |
250 | ||
251 | #define DEFINE_OPERATOR_TYPE(OPER, TYPE) \ | |
252 | bool operator OPER (TYPE other) {return compare(other) OPER 0;} | |
253 | #define DEFINE_OPERATOR(OPER) \ | |
254 | DEFINE_OPERATOR_TYPE (OPER, str) \ | |
255 | DEFINE_OPERATOR_TYPE (OPER, char*) \ | |
256 | DEFINE_OPERATOR_TYPE (OPER, const char*) | |
257 | ||
258 | DEFINE_OPERATOR (==) | |
259 | DEFINE_OPERATOR (!=) | |
260 | DEFINE_OPERATOR (>) | |
261 | DEFINE_OPERATOR (<) | |
262 | DEFINE_OPERATOR (>=) | |
263 | DEFINE_OPERATOR (<=) | |
264 | ||
265 | char& operator[] (int pos) { | |
266 | return text[pos]; | |
267 | } | |
268 | ||
269 | operator char* () const { | |
270 | return text; | |
271 | } | |
272 | ||
273 | operator QString () const { | |
274 | return text; | |
275 | } | |
276 | ||
277 | operator int () const { | |
278 | return atoi (text); | |
279 | } | |
280 | ||
281 | operator uint () const { | |
282 | return operator int(); | |
283 | } | |
284 | }; | |
285 | ||
286 | #endif // __STR_H__ |