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 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__ |
… | |
… | |
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); |
33 | |
34 | |
34 | template<int size> |
35 | template<int size> |
35 | struct shstr_vec |
36 | struct shstr_vec |
36 | { |
37 | { |
|
|
38 | uint32_t hash; |
37 | uint32_t len; |
39 | uint32_t len; |
38 | uint32_t refcnt; |
40 | uint32_t refcnt; |
|
|
41 | // pointer points here |
39 | char string[size]; |
42 | char string [size]; |
40 | }; |
43 | }; |
41 | |
44 | |
42 | // this class is a non-refcounted shared string |
45 | // this class is a non-refcounted shared string |
43 | // it cannot be used to create or store shared strings, but |
46 | // 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 |
47 | // 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. |
48 | // 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. |
49 | // they are only valid as temporary values within the same server tick. |
47 | struct shstr_tmp |
50 | struct shstr_tmp |
48 | { |
51 | { |
49 | static shstr_vec<sizeof ("(null)")> nullvec; |
52 | static shstr_vec<sizeof ("(null)")> nullvec; |
50 | static const char *null () { return nullvec.string; } // this is the null pointer value |
53 | static const char *null () { return nullvec.string; } // this is the null pointer value |
51 | |
54 | |
52 | const char *s; |
55 | const char *s; |
53 | |
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 | } |
|
|
66 | |
54 | static unsigned int &length (const char *s) |
67 | static unsigned int &length (const char *s) |
55 | { |
68 | { |
56 | return *((unsigned int *)s - 2); |
69 | return *((unsigned int *)s - 2); |
57 | } |
70 | } |
58 | |
71 | |
… | |
… | |
68 | int plen = strlen (prefix); |
81 | int plen = strlen (prefix); |
69 | |
82 | |
70 | return length () >= plen && !strncasecmp (s, prefix, plen); |
83 | return length () >= plen && !strncasecmp (s, prefix, plen); |
71 | } |
84 | } |
72 | |
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. |
73 | bool contains (const char *substring) const |
89 | bool contains (const char *substring) const; |
74 | { |
|
|
75 | return strstr (s, substring); |
|
|
76 | } |
|
|
77 | |
90 | |
78 | //TODO: case sensitive should be eradicated |
91 | //TODO: case sensitive should be eradicated |
79 | bool eq_nc (const char *otherstring) const |
92 | bool eq_nc (const char *otherstring) const |
80 | { |
93 | { |
81 | return !strcasecmp (s, otherstring); |
94 | return !strcasecmp (s, otherstring); |
… | |
… | |
119 | inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b) |
132 | inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b) |
120 | { |
133 | { |
121 | return a.s != b.s; |
134 | return a.s != b.s; |
122 | } |
135 | } |
123 | |
136 | |
124 | inline int strlen (const shstr_tmp &sh) |
137 | inline int strlen (shstr_tmp sh) |
125 | { |
138 | { |
126 | return sh.length (); |
139 | return sh.length (); |
127 | } |
140 | } |
128 | |
141 | |
129 | static std::ostream &operator <<(std::ostream &o, shstr_tmp sh) |
142 | static inline std::ostream &operator <<(std::ostream &o, shstr_tmp sh) |
130 | { |
143 | { |
131 | o.write (sh.s, sh.length ()); |
144 | o.write (sh.s, sh.length ()); |
132 | |
145 | |
133 | return o; |
146 | return o; |
134 | } |
147 | } |
… | |
… | |
241 | inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) |
254 | inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) |
242 | { |
255 | { |
243 | return a.s == b.s; |
256 | return a.s == b.s; |
244 | } |
257 | } |
245 | |
258 | |
246 | #define def2(id,str) extern const shstr shstr_ ## id; |
259 | #define def2(id,str) extern const shstr id; |
247 | #define def(id) def2(id, # id) |
260 | #define def(id) def2(shstr_ ## id, # id) |
248 | # include "shstrinc.h" |
261 | # include "shstrinc.h" |
249 | #undef def |
262 | #undef def |
250 | #undef def2 |
263 | #undef def2 |
251 | |
264 | |
252 | // undefined external reference to catch people using str* functions when they shouldn't |
265 | // undefined external reference to catch people using str* functions when they shouldn't |
253 | //template<class any> void strcmp (const shstr_tmp &a, any b); |
266 | //template<class any> void strcmp (const shstr_tmp &a, any b); |
254 | //template<class any> void strstr (const shstr_tmp &a, any b); |
267 | template<class any> void strstr (const shstr_tmp &a, any b); |
255 | |
268 | |
256 | #endif |
269 | #endif |
257 | |
270 | |