1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
5 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * it under the terms of the GNU General Public License as published by |
7 | * the terms of the Affero GNU General Public License as published by the |
8 | * the Free Software Foundation, either version 3 of the License, or |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * (at your option) any later version. |
9 | * option) any later version. |
10 | * |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
14 | * GNU General Public License for more details. |
15 | * |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
16 | * You should have received a copy of the Affero GNU General Public License |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | * and the GNU General Public License along with this program. If not, see |
|
|
18 | * <http://www.gnu.org/licenses/>. |
18 | * |
19 | * |
19 | * The authors can be reached via e-mail to <support@deliantra.net> |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
20 | */ |
21 | */ |
21 | |
22 | |
22 | #ifndef SHSTR_H__ |
23 | #ifndef SHSTR_H__ |
23 | #define SHSTR_H__ |
24 | #define SHSTR_H__ |
24 | |
25 | |
25 | #include <cstring> |
26 | #include <cstring> |
26 | #include <sstream> |
27 | #include <sstream> |
27 | |
28 | |
28 | #include "util.h" |
29 | #include "traits.h" |
29 | |
30 | |
30 | extern size_t shstr_alloc; |
31 | extern size_t shstr_alloc; |
31 | |
32 | |
32 | extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); |
33 | extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); |
|
|
34 | |
|
|
35 | template<int size> |
|
|
36 | struct shstr_vec |
|
|
37 | { |
|
|
38 | uint32_t hash; |
|
|
39 | uint32_t len; |
|
|
40 | uint32_t refcnt; |
|
|
41 | // pointer points here |
|
|
42 | char string [size]; |
|
|
43 | }; |
33 | |
44 | |
34 | // this class is a non-refcounted shared string |
45 | // this class is a non-refcounted shared string |
35 | // it cannot be used to create or store shared strings, but |
46 | // it cannot be used to create or store shared strings, but |
36 | // it can be used to apss shared strings around, i.e. as function arguments |
47 | // it can be used to pass shared strings around, i.e. as function arguments |
37 | // or return values. their lifetime must not span a gc () call, i.e. |
48 | // or return values. their lifetime must not span a gc () call, i.e. |
38 | // they are only valid as temporary values within the same server tick. |
49 | // they are only valid as temporary values within the same server tick. |
39 | struct shstr_tmp |
50 | struct shstr_tmp |
40 | { |
51 | { |
41 | static const char *null; |
52 | static shstr_vec<sizeof ("(null)")> nullvec; |
|
|
53 | static const char *null () { return nullvec.string; } // this is the null pointer value |
42 | |
54 | |
43 | const char *s; |
55 | const char *s; |
|
|
56 | |
|
|
57 | static unsigned int &hash (const char *s) |
|
|
58 | { |
|
|
59 | return *((unsigned int *)s - 3); |
|
|
60 | } |
|
|
61 | |
|
|
62 | int hash () const |
|
|
63 | { |
|
|
64 | return hash (s); |
|
|
65 | } |
44 | |
66 | |
45 | static unsigned int &length (const char *s) |
67 | static unsigned int &length (const char *s) |
46 | { |
68 | { |
47 | return *((unsigned int *)s - 2); |
69 | return *((unsigned int *)s - 2); |
48 | } |
70 | } |
… | |
… | |
59 | int plen = strlen (prefix); |
81 | int plen = strlen (prefix); |
60 | |
82 | |
61 | return length () >= plen && !strncasecmp (s, prefix, plen); |
83 | return length () >= plen && !strncasecmp (s, prefix, plen); |
62 | } |
84 | } |
63 | |
85 | |
|
|
86 | // returns true if the substring is contained in the shstr |
|
|
87 | // if the shstr is 0, then this always returns false. |
|
|
88 | // the shstr is (theoretically) treated as a comma/colon/space etc. separated list. |
64 | bool contains (const char *substring) const |
89 | bool contains (const char *substring) const; |
|
|
90 | |
|
|
91 | //TODO: case sensitive should be eradicated |
|
|
92 | bool eq_nc (const char *otherstring) const |
65 | { |
93 | { |
66 | return strstr (s, substring); |
94 | return !strcasecmp (s, otherstring); |
67 | } |
95 | } |
68 | |
96 | |
69 | shstr_tmp () |
97 | shstr_tmp () |
70 | : s (null) |
98 | : s (null ()) |
71 | { |
99 | { |
72 | } |
100 | } |
73 | |
101 | |
74 | shstr_tmp (const shstr_tmp &sh) |
102 | shstr_tmp (const shstr_tmp &sh) |
75 | : s (sh.s) |
103 | : s (sh.s) |
… | |
… | |
84 | } |
112 | } |
85 | |
113 | |
86 | // this is used for informational messages and the like |
114 | // this is used for informational messages and the like |
87 | const char *operator &() const { return s; } |
115 | const char *operator &() const { return s; } |
88 | |
116 | |
89 | operator const char *() const { return s == null ? 0 : s; } |
117 | operator const char *() const { return s == null () ? 0 : s; } |
|
|
118 | |
|
|
119 | protected: |
|
|
120 | // dummy is there so it isn't used as type converter accidentally |
|
|
121 | shstr_tmp (int dummy, const char *s) |
|
|
122 | : s(s) |
|
|
123 | { |
|
|
124 | } |
90 | }; |
125 | }; |
91 | |
126 | |
92 | inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b) |
127 | inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b) |
93 | { |
128 | { |
94 | return a.s == b.s; |
129 | return a.s == b.s; |
… | |
… | |
97 | inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b) |
132 | inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b) |
98 | { |
133 | { |
99 | return a.s != b.s; |
134 | return a.s != b.s; |
100 | } |
135 | } |
101 | |
136 | |
102 | inline int strlen (const shstr_tmp &sh) |
137 | inline int strlen (shstr_tmp sh) |
103 | { |
138 | { |
104 | return sh.length (); |
139 | return sh.length (); |
105 | } |
140 | } |
106 | |
141 | |
107 | // undefined external reference to catch people using strcmp when they shouldn't |
|
|
108 | int strcmp (const shstr_tmp &a, const shstr_tmp &b); |
|
|
109 | |
|
|
110 | static std::ostream &operator <<(std::ostream &o, shstr_tmp sh) |
142 | static inline std::ostream &operator <<(std::ostream &o, shstr_tmp sh) |
111 | { |
143 | { |
112 | o.write (sh.s, sh.length ()); |
144 | o.write (sh.s, sh.length ()); |
113 | |
145 | |
114 | return o; |
146 | return o; |
115 | } |
147 | } |
… | |
… | |
146 | { |
178 | { |
147 | ++refcnt (); |
179 | ++refcnt (); |
148 | } |
180 | } |
149 | |
181 | |
150 | explicit shstr (const char *str) |
182 | explicit shstr (const char *str) |
151 | { |
|
|
152 | s = is_constant (str) && !str ? null : intern (str); |
183 | : shstr_tmp (0, is_constant (str) && !str ? null () : intern (str)) |
|
|
184 | { |
153 | } |
185 | } |
154 | |
186 | |
155 | ~shstr () |
187 | ~shstr () |
156 | { |
188 | { |
157 | --refcnt (); |
189 | --refcnt (); |
… | |
… | |
178 | |
210 | |
179 | // shstr_tmp doesn't have this one |
211 | // shstr_tmp doesn't have this one |
180 | shstr &operator =(const char *str) |
212 | shstr &operator =(const char *str) |
181 | { |
213 | { |
182 | --refcnt (); |
214 | --refcnt (); |
183 | s = is_constant (str) && !str ? null : intern (str); |
215 | s = is_constant (str) && !str ? null () : intern (str); |
184 | |
216 | |
185 | return *this; |
217 | return *this; |
186 | } |
218 | } |
187 | }; |
219 | }; |
188 | |
220 | |
… | |
… | |
190 | // temporary passing, basically a non-refcounted shstr |
222 | // temporary passing, basically a non-refcounted shstr |
191 | struct shstr_cmp |
223 | struct shstr_cmp |
192 | { |
224 | { |
193 | const char *s; |
225 | const char *s; |
194 | |
226 | |
|
|
227 | // initialies to the non-matching string (as opposed to the null string) |
|
|
228 | shstr_cmp () |
|
|
229 | { |
|
|
230 | s = 0; |
|
|
231 | } |
|
|
232 | |
195 | explicit shstr_cmp (const char *str) |
233 | shstr_cmp (const char *str) |
196 | : s (shstr::find (str)) |
234 | : s (shstr::find (str)) |
197 | { |
235 | { |
198 | } |
236 | } |
199 | |
237 | |
200 | shstr_cmp (const shstr_cmp &sh) |
238 | shstr_cmp (shstr_tmp sh) |
201 | : s (sh.s) |
|
|
202 | { |
|
|
203 | } |
|
|
204 | |
|
|
205 | shstr_cmp (const shstr &sh) |
|
|
206 | : s (sh.s) |
239 | : s (sh.s) |
207 | { |
240 | { |
208 | } |
241 | } |
209 | |
242 | |
210 | // this is used for informational messages and the like |
243 | // this is used for informational messages and the like |
211 | const char *operator &() const { return s; } |
244 | const char *operator &() const { return s; } |
212 | |
245 | |
213 | shstr_cmp operator =(const shstr_cmp sh) { s = sh.s; return *this; } |
|
|
214 | operator const char *() const { return s; } |
246 | operator const char *() const { return s; } |
215 | }; |
247 | }; |
216 | |
248 | |
217 | inline bool operator ==(const shstr_cmp &a, const shstr_tmp &b) |
249 | inline bool operator ==(const shstr_cmp &a, const shstr_tmp &b) |
218 | { |
250 | { |
… | |
… | |
222 | inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) |
254 | inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) |
223 | { |
255 | { |
224 | return a.s == b.s; |
256 | return a.s == b.s; |
225 | } |
257 | } |
226 | |
258 | |
227 | extern const shstr shstr_null; |
|
|
228 | |
|
|
229 | #define def(str) extern const shstr shstr_ ## str; |
259 | #define def2(id,str) extern const shstr id; |
|
|
260 | #define def(id) def2(shstr_ ## id, # id) |
230 | # include "shstrinc.h" |
261 | # include "shstrinc.h" |
231 | #undef def |
262 | #undef def |
|
|
263 | #undef def2 |
|
|
264 | |
|
|
265 | // undefined external reference to catch people using str* functions when they shouldn't |
|
|
266 | //template<class any> void strcmp (const shstr_tmp &a, any b); |
|
|
267 | template<class any> void strstr (const shstr_tmp &a, any b); |
232 | |
268 | |
233 | #endif |
269 | #endif |
234 | |
270 | |