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__ |
24 | |
26 | |
25 | #include <cstring> |
27 | #include <cstring> |
26 | #include <sstream> |
28 | #include <sstream> |
27 | |
29 | |
28 | #include "util.h" |
30 | #include "traits.h" |
29 | |
31 | |
30 | extern size_t shstr_alloc; |
32 | extern size_t shstr_alloc; |
31 | |
33 | |
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); |
|
|
35 | |
|
|
36 | template<int size> |
|
|
37 | struct shstr_vec |
|
|
38 | { |
|
|
39 | uint32_t hash; |
|
|
40 | uint32_t len; |
|
|
41 | uint32_t refcnt; |
|
|
42 | // pointer points here |
|
|
43 | char string [size]; |
|
|
44 | }; |
33 | |
45 | |
34 | // this class is a non-refcounted shared string |
46 | // this class is a non-refcounted shared string |
35 | // it cannot be used to create or store shared strings, but |
47 | // 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 |
48 | // 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. |
49 | // 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. |
50 | // they are only valid as temporary values within the same server tick. |
39 | struct shstr_tmp |
51 | struct shstr_tmp |
40 | { |
52 | { |
41 | static const char *null; |
53 | static shstr_vec<sizeof ("(null)")> nullvec; |
|
|
54 | static const char *null () { return nullvec.string; } // this is the null pointer value |
42 | |
55 | |
43 | const char *s; |
56 | const char *s; |
|
|
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 | } |
44 | |
67 | |
45 | static unsigned int &length (const char *s) |
68 | static unsigned int &length (const char *s) |
46 | { |
69 | { |
47 | return *((unsigned int *)s - 2); |
70 | return *((unsigned int *)s - 2); |
48 | } |
71 | } |
… | |
… | |
59 | int plen = strlen (prefix); |
82 | int plen = strlen (prefix); |
60 | |
83 | |
61 | return length () >= plen && !strncasecmp (s, prefix, plen); |
84 | return length () >= plen && !strncasecmp (s, prefix, plen); |
62 | } |
85 | } |
63 | |
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. |
64 | 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 |
65 | { |
94 | { |
66 | return strstr (s, substring); |
95 | return !strcasecmp (s, otherstring); |
67 | } |
96 | } |
68 | |
97 | |
69 | shstr_tmp () |
98 | shstr_tmp () |
70 | : s (null) |
99 | : s (null ()) |
71 | { |
100 | { |
72 | } |
101 | } |
73 | |
102 | |
74 | shstr_tmp (const shstr_tmp &sh) |
103 | shstr_tmp (const shstr_tmp &sh) |
75 | : s (sh.s) |
104 | : s (sh.s) |
… | |
… | |
84 | } |
113 | } |
85 | |
114 | |
86 | // this is used for informational messages and the like |
115 | // this is used for informational messages and the like |
87 | const char *operator &() const { return s; } |
116 | const char *operator &() const { return s; } |
88 | |
117 | |
89 | operator const char *() const { return s == null ? 0 : s; } |
118 | operator const char *() const { return s == null () ? 0 : s; } |
|
|
119 | |
|
|
120 | protected: |
|
|
121 | // dummy is there so it isn't used as type converter accidentally |
|
|
122 | shstr_tmp (int dummy, const char *s) |
|
|
123 | : s(s) |
|
|
124 | { |
|
|
125 | } |
90 | }; |
126 | }; |
91 | |
127 | |
92 | inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b) |
128 | inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b) |
93 | { |
129 | { |
94 | return a.s == b.s; |
130 | return a.s == b.s; |
… | |
… | |
97 | inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b) |
133 | inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b) |
98 | { |
134 | { |
99 | return a.s != b.s; |
135 | return a.s != b.s; |
100 | } |
136 | } |
101 | |
137 | |
102 | inline int strlen (const shstr_tmp &sh) |
138 | inline int strlen (shstr_tmp sh) |
103 | { |
139 | { |
104 | return sh.length (); |
140 | return sh.length (); |
105 | } |
141 | } |
106 | |
142 | |
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) |
143 | static inline std::ostream &operator <<(std::ostream &o, shstr_tmp sh) |
111 | { |
144 | { |
112 | o.write (sh.s, sh.length ()); |
145 | o.write (sh.s, sh.length ()); |
113 | |
146 | |
114 | return o; |
147 | return o; |
115 | } |
148 | } |
… | |
… | |
146 | { |
179 | { |
147 | ++refcnt (); |
180 | ++refcnt (); |
148 | } |
181 | } |
149 | |
182 | |
150 | explicit shstr (const char *str) |
183 | explicit shstr (const char *str) |
151 | { |
|
|
152 | s = is_constant (str) && !str ? null : intern (str); |
184 | : shstr_tmp (0, ecb_is_constant (str) && !str ? null () : intern (str)) |
|
|
185 | { |
153 | } |
186 | } |
154 | |
187 | |
155 | ~shstr () |
188 | ~shstr () |
156 | { |
189 | { |
157 | --refcnt (); |
190 | --refcnt (); |
… | |
… | |
178 | |
211 | |
179 | // shstr_tmp doesn't have this one |
212 | // shstr_tmp doesn't have this one |
180 | shstr &operator =(const char *str) |
213 | shstr &operator =(const char *str) |
181 | { |
214 | { |
182 | --refcnt (); |
215 | --refcnt (); |
183 | s = is_constant (str) && !str ? null : intern (str); |
216 | s = ecb_is_constant (str) && !str ? null () : intern (str); |
184 | |
217 | |
185 | return *this; |
218 | return *this; |
186 | } |
219 | } |
187 | }; |
220 | }; |
188 | |
221 | |
… | |
… | |
190 | // temporary passing, basically a non-refcounted shstr |
223 | // temporary passing, basically a non-refcounted shstr |
191 | struct shstr_cmp |
224 | struct shstr_cmp |
192 | { |
225 | { |
193 | const char *s; |
226 | const char *s; |
194 | |
227 | |
|
|
228 | // initialies to the non-matching string (as opposed to the null string) |
|
|
229 | shstr_cmp () |
|
|
230 | { |
|
|
231 | s = 0; |
|
|
232 | } |
|
|
233 | |
195 | explicit shstr_cmp (const char *str) |
234 | shstr_cmp (const char *str) |
196 | : s (shstr::find (str)) |
235 | : s (shstr::find (str)) |
197 | { |
236 | { |
198 | } |
237 | } |
199 | |
238 | |
200 | shstr_cmp (const shstr_cmp &sh) |
239 | shstr_cmp (shstr_tmp sh) |
201 | : s (sh.s) |
|
|
202 | { |
|
|
203 | } |
|
|
204 | |
|
|
205 | shstr_cmp (const shstr &sh) |
|
|
206 | : s (sh.s) |
240 | : s (sh.s) |
207 | { |
241 | { |
208 | } |
242 | } |
209 | |
243 | |
210 | // this is used for informational messages and the like |
244 | // this is used for informational messages and the like |
211 | const char *operator &() const { return s; } |
245 | const char *operator &() const { return s; } |
212 | |
246 | |
213 | shstr_cmp operator =(const shstr_cmp sh) { s = sh.s; return *this; } |
|
|
214 | operator const char *() const { return s; } |
247 | operator const char *() const { return s; } |
215 | }; |
248 | }; |
216 | |
249 | |
217 | inline bool operator ==(const shstr_cmp &a, const shstr_tmp &b) |
250 | inline bool operator ==(const shstr_cmp &a, const shstr_tmp &b) |
218 | { |
251 | { |
… | |
… | |
222 | inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) |
255 | inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) |
223 | { |
256 | { |
224 | return a.s == b.s; |
257 | return a.s == b.s; |
225 | } |
258 | } |
226 | |
259 | |
227 | extern const shstr shstr_null; |
|
|
228 | |
|
|
229 | #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) |
230 | # include "shstrinc.h" |
262 | # include "shstrinc.h" |
231 | #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); |
232 | |
269 | |
233 | #endif |
270 | #endif |
234 | |
271 | |