Wed, 19 Dec 2012 22:01:42 +0200
So it turns out that the functions I thought were taking float are actually taking int. So, with the only reason for float removed, the float type is removed as well. I'd rather have fixed point anyway.
0 | 1 | /* |
2 | * botc source code | |
3 | * Copyright (C) 2012 Santeri `Dusk` Piippo | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright notice, | |
10 | * this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
3 | 14 | * 3. Neither the name of the developer nor the names of its contributors may |
15 | * be used to endorse or promote products derived from this software without | |
16 | * specific prior written permission. | |
0 | 17 | * 4. Redistributions in any form must be accompanied by information on how to |
18 | * obtain complete source code for the software and any accompanying | |
19 | * software that uses the software. The source code must either be included | |
20 | * in the distribution or be available for no more than the cost of | |
21 | * distribution plus a nominal fee, and must be freely redistributable | |
22 | * under reasonable conditions. For an executable file, complete source | |
23 | * code means the source code for all modules it contains. It does not | |
24 | * include source code for modules or files that typically accompany the | |
25 | * major components of the operating system on which the executable file | |
26 | * runs. | |
27 | * | |
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
34 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
35 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
36 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
37 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
38 | * POSSIBILITY OF SUCH DAMAGE. | |
39 | */ | |
40 | ||
41 | #ifndef __STR_H__ | |
42 | #define __STR_H__ | |
43 | ||
66
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
44 | template<class T> class array; |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
45 | |
0 | 46 | char* vdynformat (const char* c, va_list v, unsigned int size); |
47 | ||
48 | #define SCCF_NUMBER 1<<0 | |
49 | #define SCCF_WORD 1<<1 | |
50 | ||
51 | // Dynamic string object, allocates memory when needed and | |
52 | // features a good bunch of manipulation methods | |
53 | class str { | |
54 | private: | |
55 | // The actual message | |
56 | char* text; | |
57 | ||
58 | // Where will append() place new characters? | |
59 | unsigned int curs; | |
60 | ||
61 | // Allocated length | |
62 | unsigned int alloclen; | |
63 | ||
64 | // Resize the text buffer to len characters | |
65 | void resize (unsigned int len); | |
66 | public: | |
67 | // ====================================================================== | |
68 | // CONSTRUCTORS | |
69 | str (); | |
70 | str (const char* c); | |
71 | str (char c); | |
72 | ||
73 | // ====================================================================== | |
74 | // METHODS | |
75 | ||
76 | // Empty the string | |
77 | void clear (); | |
78 | ||
79 | // Length of the string | |
80 | unsigned int len (); | |
81 | ||
82 | // The char* form of the string | |
83 | char* chars (); | |
84 | ||
85 | // Dumps the character table of the string | |
86 | void dump (); | |
87 | ||
88 | // Appends text to the string | |
89 | void append (char c); | |
90 | void append (const char* c); | |
91 | void append (str c); | |
92 | ||
93 | // Appends formatted text to the string. | |
94 | void appendformat (const char* c, ...); | |
95 | void appendformat (str c, ...); | |
96 | ||
97 | // Returns the first occurrence of c in the string, optionally starting | |
98 | // from a certain position rather than the start. | |
99 | unsigned int first (const char* c, unsigned int a = 0); | |
100 | ||
101 | // Returns the last occurrence of c in the string, optionally starting | |
102 | // from a certain position rather than the end. | |
103 | unsigned int last (const char* c, int a = -1); | |
104 | ||
105 | // Returns a substring of the string, from a to b. | |
106 | str substr (unsigned int a, unsigned int b); | |
107 | ||
108 | // Replace a substring with another substring. | |
109 | void replace (const char* o, const char* n, unsigned int a = 0); | |
110 | ||
111 | // Removes a given index from the string, optionally more characters than just 1. | |
112 | void remove (unsigned int idx, unsigned int dellen=1); | |
113 | ||
114 | void trim (int dellen); | |
115 | ||
116 | // Inserts a substring into a certain position. | |
117 | void insert (char* c, unsigned int pos); | |
118 | ||
119 | // Reverses the string. | |
120 | void reverse (); | |
121 | ||
122 | // Repeats the string a given amount of times. | |
123 | void repeat (unsigned int n); | |
124 | ||
125 | // Is the string a number? | |
126 | bool isnumber (); | |
127 | ||
128 | // Is the string a word, i.e consists only of alphabetic letters? | |
129 | bool isword (); | |
130 | ||
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
131 | // Convert string to lower case |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
132 | str tolower (); |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
133 | |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
134 | // Convert string to upper case |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
135 | str toupper (); |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
136 | |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
137 | // Compare this string with another |
0 | 138 | int compare (const char* c); |
139 | int compare (str c); | |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
140 | int icompare (str c); |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
141 | int icompare (const char* c); |
0 | 142 | |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
143 | // Counts the amount of substrings in the string |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
144 | unsigned int count (char* s); |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
145 | |
66
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
146 | array<str> split (str del); |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
147 | |
0 | 148 | // ====================================================================== |
149 | // OPERATORS | |
66
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
150 | str operator+ (str& c) { |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
151 | append (c); |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
152 | return *this; |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
153 | } |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
154 | |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
155 | str& operator+= (char c) { |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
156 | append (c); |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
157 | return *this; |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
158 | } |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
159 | |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
160 | str& operator+= (const char* c) { |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
161 | append (c); |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
162 | return *this; |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
163 | } |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
164 | |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
165 | str& operator+= (const str c) { |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
166 | append (c); |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
167 | return *this; |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
168 | } |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
169 | |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
170 | str operator* (const int repcount) { |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
171 | repeat (repcount); |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
172 | return *this; |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
173 | } |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
174 | |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
175 | str& operator*= (const int repcount) { |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
176 | repeat (repcount); |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
177 | return *this; |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
178 | } |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
179 | |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
180 | str operator- (const int trimcount) { |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
181 | trim (trimcount); |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
182 | return *this; |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
183 | } |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
184 | |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
185 | str& operator-= (const int trimcount) { |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
186 | trim (trimcount); |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
187 | return *this; |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
188 | } |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
189 | |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
190 | array<str> operator/ (str splitstring); |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
191 | array<str> operator/ (char* splitstring); |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
192 | array<str> operator/ (const char* splitstring); |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
193 | |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
194 | str operator+ () { |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
195 | return toupper (); |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
196 | } |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
197 | |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
198 | str operator- () { |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
199 | return tolower (); |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
200 | } |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
201 | |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
202 | str operator! () { |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
203 | reverse (); |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
204 | return *this; |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
205 | } |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
206 | |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
207 | #define DEFINE_OPERATOR_TYPE(OPER, TYPE) \ |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
208 | bool operator OPER (TYPE other) {return compare(other) OPER 0;} |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
209 | #define DEFINE_OPERATOR(OPER) \ |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
210 | DEFINE_OPERATOR_TYPE (OPER, str) \ |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
211 | DEFINE_OPERATOR_TYPE (OPER, char*) \ |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
212 | DEFINE_OPERATOR_TYPE (OPER, const char*) |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
213 | |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
214 | DEFINE_OPERATOR (==) |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
215 | DEFINE_OPERATOR (!=) |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
216 | DEFINE_OPERATOR (>) |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
217 | DEFINE_OPERATOR (<) |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
218 | DEFINE_OPERATOR (>=) |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
219 | DEFINE_OPERATOR (<=) |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
220 | |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
221 | char operator [] (unsigned int pos) { |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
222 | return text[pos]; |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
223 | } |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
224 | |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
225 | operator char* () const { |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
226 | return text; |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
227 | } |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
228 | |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
229 | operator int () const {return atoi(text);} |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
230 | operator unsigned int () const {return atoi(text);} |
0 | 231 | }; |
232 | ||
233 | #endif // __STR_H__ |