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 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__ |
… | |
… | |
68 | int plen = strlen (prefix); |
69 | int plen = strlen (prefix); |
69 | |
70 | |
70 | return length () >= plen && !strncasecmp (s, prefix, plen); |
71 | return length () >= plen && !strncasecmp (s, prefix, plen); |
71 | } |
72 | } |
72 | |
73 | |
|
|
74 | // returns true if the substring is contained in the shstr |
|
|
75 | // if the shstr is 0, then this always returns false. |
|
|
76 | // the shstr is (theoretically) treated as a comma/colon/space etc. separated list. |
73 | bool contains (const char *substring) const |
77 | bool contains (const char *substring) const |
74 | { |
78 | { |
75 | return strstr (s, substring); |
79 | return s != null () && strstr (s, substring); |
|
|
80 | } |
|
|
81 | |
|
|
82 | //TODO: case sensitive should be eradicated |
|
|
83 | bool eq_nc (const char *otherstring) const |
|
|
84 | { |
|
|
85 | return !strcasecmp (s, otherstring); |
76 | } |
86 | } |
77 | |
87 | |
78 | shstr_tmp () |
88 | shstr_tmp () |
79 | : s (null ()) |
89 | : s (null ()) |
80 | { |
90 | { |
… | |
… | |
94 | |
104 | |
95 | // this is used for informational messages and the like |
105 | // this is used for informational messages and the like |
96 | const char *operator &() const { return s; } |
106 | const char *operator &() const { return s; } |
97 | |
107 | |
98 | operator const char *() const { return s == null () ? 0 : s; } |
108 | operator const char *() const { return s == null () ? 0 : s; } |
|
|
109 | |
|
|
110 | protected: |
|
|
111 | // dummy is there so it isn't used as type converter accidentally |
|
|
112 | shstr_tmp (int dummy, const char *s) |
|
|
113 | : s(s) |
|
|
114 | { |
|
|
115 | } |
99 | }; |
116 | }; |
100 | |
117 | |
101 | inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b) |
118 | inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b) |
102 | { |
119 | { |
103 | return a.s == b.s; |
120 | return a.s == b.s; |
… | |
… | |
106 | inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b) |
123 | inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b) |
107 | { |
124 | { |
108 | return a.s != b.s; |
125 | return a.s != b.s; |
109 | } |
126 | } |
110 | |
127 | |
111 | inline int strlen (const shstr_tmp &sh) |
128 | inline int strlen (shstr_tmp sh) |
112 | { |
129 | { |
113 | return sh.length (); |
130 | return sh.length (); |
114 | } |
131 | } |
115 | |
|
|
116 | // undefined external reference to catch people using strcmp when they shouldn't |
|
|
117 | int strcmp (const shstr_tmp &a, const shstr_tmp &b); |
|
|
118 | |
132 | |
119 | static std::ostream &operator <<(std::ostream &o, shstr_tmp sh) |
133 | static std::ostream &operator <<(std::ostream &o, shstr_tmp sh) |
120 | { |
134 | { |
121 | o.write (sh.s, sh.length ()); |
135 | o.write (sh.s, sh.length ()); |
122 | |
136 | |
… | |
… | |
155 | { |
169 | { |
156 | ++refcnt (); |
170 | ++refcnt (); |
157 | } |
171 | } |
158 | |
172 | |
159 | explicit shstr (const char *str) |
173 | explicit shstr (const char *str) |
160 | { |
|
|
161 | s = is_constant (str) && !str ? null () : intern (str); |
174 | : shstr_tmp (0, is_constant (str) && !str ? null () : intern (str)) |
|
|
175 | { |
162 | } |
176 | } |
163 | |
177 | |
164 | ~shstr () |
178 | ~shstr () |
165 | { |
179 | { |
166 | --refcnt (); |
180 | --refcnt (); |
… | |
… | |
199 | // temporary passing, basically a non-refcounted shstr |
213 | // temporary passing, basically a non-refcounted shstr |
200 | struct shstr_cmp |
214 | struct shstr_cmp |
201 | { |
215 | { |
202 | const char *s; |
216 | const char *s; |
203 | |
217 | |
|
|
218 | // initialies to the non-matching string (as opposed to the null string) |
|
|
219 | shstr_cmp () |
|
|
220 | { |
|
|
221 | s = 0; |
|
|
222 | } |
|
|
223 | |
204 | explicit shstr_cmp (const char *str) |
224 | shstr_cmp (const char *str) |
205 | : s (shstr::find (str)) |
225 | : s (shstr::find (str)) |
206 | { |
226 | { |
207 | } |
227 | } |
208 | |
228 | |
209 | shstr_cmp (shstr_tmp sh) |
229 | shstr_cmp (shstr_tmp sh) |
… | |
… | |
225 | inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) |
245 | inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) |
226 | { |
246 | { |
227 | return a.s == b.s; |
247 | return a.s == b.s; |
228 | } |
248 | } |
229 | |
249 | |
230 | #define def(str) extern const shstr shstr_ ## str; |
250 | #define def2(id,str) extern const shstr id; |
|
|
251 | #define def(id) def2(shstr_ ## id, # id) |
231 | # include "shstrinc.h" |
252 | # include "shstrinc.h" |
232 | #undef def |
253 | #undef def |
|
|
254 | #undef def2 |
|
|
255 | |
|
|
256 | // undefined external reference to catch people using str* functions when they shouldn't |
|
|
257 | //template<class any> void strcmp (const shstr_tmp &a, any b); |
|
|
258 | template<class any> void strstr (const shstr_tmp &a, any b); |
233 | |
259 | |
234 | #endif |
260 | #endif |
235 | |
261 | |