Wed, 24 Apr 2013 19:59:41 +0300
Area selection! This sure was a *pain* to add.
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 | // ====================================================================== | |
58 | // METHODS | |
59 | ||
60 | // Empty the string | |
61 | void clear (); | |
62 | ||
63 | // Length of the string | |
64 | size_t len () { | |
65 | return strlen (text); | |
66 | } | |
67 | ||
68 | // The char* form of the string | |
69 | char* chars (); | |
70 | ||
71 | // Dumps the character table of the string | |
72 | void dump (); | |
73 | ||
74 | // 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
|
75 | void append (const char c); |
0 | 76 | void append (const char* c); |
77 | 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
|
78 | void append (QString c); |
0 | 79 | |
80 | // Formats text to the string. | |
81 | void format (const char* fmt, ...); | |
82 | str format (...); | |
83 | ||
84 | // Appends formatted text to the string. | |
85 | void appendformat (const char* c, ...); | |
86 | ||
87 | // Returns the first occurrence of c in the string, optionally starting | |
88 | // from a certain position rather than the start. | |
89 | int first (const char* c, unsigned int a = 0); | |
90 | ||
91 | // Returns the last occurrence of c in the string, optionally starting | |
92 | // from a certain position rather than the end. | |
93 | int last (const char* c, int a = -1); | |
94 | ||
95 | // Returns a substring of the string, from a to b. | |
96 | str substr (unsigned int a, unsigned int b); | |
97 | ||
98 | // Replace a substring with another substring. | |
99 | void replace (const char* o, const char* n, unsigned int a = 0); | |
100 | ||
101 | // Removes a given index from the string, optionally more characters than just 1. | |
102 | void remove (unsigned int idx, unsigned int dellen=1); | |
103 | ||
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
|
104 | // 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
|
105 | // 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
|
106 | // end of the string. |
0 | 107 | str trim (int dellen); |
108 | ||
109 | // Inserts a substring into a certain position. | |
110 | void insert (char* c, unsigned int pos); | |
111 | ||
112 | // Reverses the string. | |
113 | str reverse (); | |
114 | ||
115 | // Repeats the string a given amount of times. | |
116 | str repeat (int n); | |
117 | ||
118 | // Is the string a number? | |
119 | bool isnumber (); | |
120 | ||
121 | // Is the string a word, i.e consists only of alphabetic letters? | |
122 | bool isword (); | |
123 | ||
124 | // Convert string to lower case | |
125 | str tolower (); | |
126 | ||
127 | // Convert string to upper case | |
128 | str toupper (); | |
129 | ||
130 | // Compare this string with another | |
131 | int compare (const char* c); | |
132 | int compare (str c); | |
133 | int icompare (str c); | |
134 | int icompare (const char* c); | |
135 | ||
136 | // Counts the amount of substrings in the string | |
137 | unsigned int count (char* s); | |
138 | unsigned int count (char s); | |
139 | ||
140 | // Counts where the given substring is seen for the nth time | |
141 | int instanceof (const char* s, unsigned n); | |
142 | ||
143 | char subscript (uint pos) { | |
144 | return operator[] (pos); | |
145 | } | |
146 | ||
60
961663d05463
Parsing stability, finally figured that dumb crash
Santeri Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
147 | std::vector<str> split (str del, bool bNoBlanks = false); |
0 | 148 | |
113
bbaa40226ec9
Radial saving and reading from files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
149 | str strip (char c); |
bbaa40226ec9
Radial saving and reading from files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
150 | str strip (std::initializer_list<char> unwanted); |
0 | 151 | |
152 | // ====================================================================== | |
153 | // OPERATORS | |
154 | str operator+ (str& c) { | |
155 | append (c); | |
156 | return *this; | |
157 | } | |
158 | ||
159 | str& operator+= (char c) { | |
160 | append (c); | |
161 | return *this; | |
162 | } | |
163 | ||
164 | str& operator+= (const char* c) { | |
165 | append (c); | |
166 | return *this; | |
167 | } | |
168 | ||
169 | str& operator+= (const str c) { | |
170 | append (c); | |
171 | return *this; | |
172 | } | |
173 | ||
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
|
174 | 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
|
175 | 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
|
176 | 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
|
177 | } |
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
|
178 | |
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
|
179 | 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
|
180 | |
0 | 181 | str operator* (const int repcount) { |
182 | repeat (repcount); | |
183 | return *this; | |
184 | } | |
185 | ||
186 | str& operator*= (const int repcount) { | |
187 | str other = repeat (repcount); | |
188 | clear (); | |
189 | append (other); | |
190 | return *this; | |
191 | } | |
192 | ||
193 | str operator- (const int trimcount) { | |
194 | return trim (trimcount); | |
195 | } | |
196 | ||
197 | str& operator-= (const int trimcount) { | |
198 | str other = trim (trimcount); | |
199 | clear (); | |
200 | append (other); | |
201 | return *this; | |
202 | } | |
203 | ||
204 | std::vector<str> operator/ (str splitstring); | |
205 | std::vector<str> operator/ (char* splitstring); | |
206 | std::vector<str> operator/ (const char* splitstring); | |
207 | ||
208 | int operator% (str splitstring) { | |
209 | return count (splitstring.chars()); | |
210 | } | |
211 | ||
212 | int operator% (char* splitstring) { | |
213 | return count (splitstring); | |
214 | } | |
215 | ||
216 | int operator% (const char* splitstring) { | |
217 | return count (str (splitstring).chars()); | |
218 | } | |
219 | ||
220 | str operator+ () { | |
221 | return toupper (); | |
222 | } | |
223 | ||
224 | str operator- () { | |
225 | return tolower (); | |
226 | } | |
227 | ||
228 | str operator! () { | |
229 | return reverse (); | |
230 | } | |
231 | ||
232 | size_t operator~ () { | |
233 | return len (); | |
234 | } | |
235 | ||
236 | #define DEFINE_OPERATOR_TYPE(OPER, TYPE) \ | |
237 | bool operator OPER (TYPE other) {return compare(other) OPER 0;} | |
238 | #define DEFINE_OPERATOR(OPER) \ | |
239 | DEFINE_OPERATOR_TYPE (OPER, str) \ | |
240 | DEFINE_OPERATOR_TYPE (OPER, char*) \ | |
241 | DEFINE_OPERATOR_TYPE (OPER, const char*) | |
242 | ||
243 | DEFINE_OPERATOR (==) | |
244 | DEFINE_OPERATOR (!=) | |
245 | DEFINE_OPERATOR (>) | |
246 | DEFINE_OPERATOR (<) | |
247 | DEFINE_OPERATOR (>=) | |
248 | DEFINE_OPERATOR (<=) | |
249 | ||
250 | char& operator[] (int pos) { | |
251 | return text[pos]; | |
252 | } | |
253 | ||
254 | operator char* () const { | |
255 | return text; | |
256 | } | |
257 | ||
258 | operator QString () const { | |
259 | return text; | |
260 | } | |
261 | ||
262 | operator int () const { | |
263 | return atoi (text); | |
264 | } | |
265 | ||
266 | operator uint () const { | |
267 | return operator int(); | |
268 | } | |
269 | }; | |
270 | ||
116 | 271 | str format (const char* fmt, ...); |
272 | ||
0 | 273 | #endif // __STR_H__ |