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