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