ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/shstr.h
(Generate patch)

Comparing deliantra/server/include/shstr.h (file contents):
Revision 1.7 by root, Sun Sep 3 09:00:09 2006 UTC vs.
Revision 1.16 by root, Mon May 28 21:15:56 2007 UTC

1/*
2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game.
3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
5 *
6 * Crossfire TRT is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * The authors can be reached via e-mail to <crossfire@schmorp.de>
21 */
22
1#ifndef SHSTR_H__ 23#ifndef SHSTR_H__
2#define SHSTR_H__ 24#define SHSTR_H__
3 25
26#include <sstream>
27
28#include "util.h"
29
4extern int buf_overflow(const char *buf1, const char *buf2, int bufsize); 30extern int buf_overflow (const char *buf1, const char *buf2, int bufsize);
5 31
6struct shstr 32struct shstr
7{ 33{
34 static const char *null;
35
8 const char *s; 36 const char *s;
9 37
10 static int &refcnt (const char *s) 38 static int &refcnt (const char *s)
11 { 39 {
12 return *((int *)s - 1); 40 return *((int *)s - 1);
22 return refcnt (s); 50 return refcnt (s);
23 } 51 }
24 52
25 int length () const 53 int length () const
26 { 54 {
27 return s ? length (s) : 0; 55 return length (s);
56 }
57
58 // returns wether this shared string begins with the given prefix,
59 // used mainly for searched when users give only the start of a name.
60 bool begins_with (const char *prefix) const
61 {
62 int plen = strlen (prefix);
63 return !strncasecmp (s, prefix, plen) && length () >= plen;
28 } 64 }
29 65
30 static const char *find (const char *s); 66 static const char *find (const char *s);
31 static const char *intern (const char *s); 67 static const char *intern (const char *s);
32 68
33 static void gc (); // garbage collect a few strings 69 static void gc (); // garbage collect a few strings
34 70
35 // this is used for informational messages and the like 71 // this is used for informational messages and the like
36 const char *operator &() const { return s ? s : "<nil>"; } 72 const char *operator &() const { return s; }
37 73
38 const char &operator [](int i) const { return s[i]; } 74 const char &operator [](int i) const { return s[i]; }
39 operator const char *() const { return s; } 75 operator const char *() const { return s == null ? 0 : s; }
40 76
41 shstr () 77 shstr ()
42 : s (0) 78 : s (null)
43 { 79 {
44 } 80 }
45 81
46 shstr (const shstr &sh) 82 shstr (const shstr &sh)
47 : s (sh.s) 83 : s (sh.s)
48 { 84 {
49 if (s) ++refcnt (); 85 ++refcnt ();
50 } 86 }
51 87
52 explicit shstr (const char *s) 88 explicit shstr (const char *s)
53 : s (intern (s)) 89 : s (intern (s))
54 { 90 {
55 } 91 }
56 92
57 ~shstr () 93 ~shstr ()
58 { 94 {
59 if (s) --refcnt (); 95 --refcnt ();
60 } 96 }
61 97
62 const shstr &operator =(const shstr &sh) 98 const shstr &operator =(const shstr &sh)
63 { 99 {
64 if (s) --refcnt (); 100 --refcnt ();
65 s = sh.s; 101 s = sh.s;
66 if (s) ++refcnt (); 102 ++refcnt ();
67 103
68 return *this; 104 return *this;
69 } 105 }
70 106
71 const shstr &operator =(const char *str) 107 const shstr &operator =(const char *str)
72 { 108 {
73 if (s) --refcnt (); 109 --refcnt ();
110
111 // this optimises the important case of str == constant 0
112 if (is_constant (str))
113 s = str ? intern (str) : null;
114 else
74 s = intern (str); 115 s = intern (str);
75 116
76 return *this; 117 return *this;
118 }
119
120 bool operator ==(const shstr &b)
121 {
122 return s == b.s;
123 }
124
125 bool operator !=(const shstr &b)
126 {
127 return !(*this == b);
77 } 128 }
78}; 129};
79 130
80inline int strlen (const shstr &sh) 131inline int strlen (const shstr &sh)
81{ 132{
82 return sh.length (); 133 return sh.length ();
83} 134}
84 135
136inline int strcmp (const shstr &a, const shstr &b)
137{
138 // TODO: use this to find all the occurences of people using strcmp
139 // all uses should be bogus, as we should be never interested in
140 // comparing shstr's alphabetically
141#if 0
142 extern void do_not_use_strcmp_to_compare_shstr_values ();
143 do_not_use_strcmp_to_compare_shstr_values ();
144#endif
145 return a != b;
146}
147
148static std::ostream &operator <<(std::ostream &o, const shstr &sh)
149{
150 o.write (sh.s, sh.length ());
151 return o;
152}
153
154// only good for mass comparisons to shstr objects
155struct shstr_cmp
156{
157 const char *s;
158
159 explicit shstr_cmp (const char *s)
160 : s (shstr::find (s))
161 {
162 }
163
164 shstr_cmp (const shstr_cmp &sh)
165 : s (sh.s)
166 {
167 }
168
169 shstr_cmp &operator =(const shstr_cmp sh) { s = sh.s; return *this; }
170 operator const char *() const { return s; }
171};
172
85inline bool operator ==(const shstr &a, const shstr &b) 173inline bool operator ==(const shstr_cmp &a, const shstr &b)
86{ 174{
87 return a.s == b.s; 175 return a.s == b.s;
88} 176}
89 177
90inline bool operator !=(const shstr &a, const shstr &b) 178inline bool operator ==(const shstr &a, const shstr_cmp &b)
91{ 179{
92 return !(a == b); 180 return b == a;
93} 181}
182
183extern const shstr undead_name; /* Used in hit_player() in main.c */
94 184
95#endif 185#endif
96 186

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines