ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/shstr.h
Revision: 1.15
Committed: Mon May 14 21:52:32 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_1
Changes since 1.14: +8 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.2 #ifndef SHSTR_H__
2     #define SHSTR_H__
3    
4 root 1.13 #include <sstream>
5    
6 root 1.8 #include "util.h"
7    
8     extern int buf_overflow (const char *buf1, const char *buf2, int bufsize);
9 root 1.5
10     struct shstr
11     {
12 root 1.8 static const char *null;
13    
14 root 1.5 const char *s;
15 root 1.4
16 root 1.7 static int &refcnt (const char *s)
17 root 1.5 {
18     return *((int *)s - 1);
19     }
20    
21 root 1.7 static int &length (const char *s)
22     {
23     return *((int *)s - 2);
24     }
25    
26     int &refcnt () const
27     {
28     return refcnt (s);
29     }
30    
31     int length () const
32     {
33 root 1.8 return length (s);
34 root 1.7 }
35    
36 root 1.15 // returns wether this shared string begins with the given prefix,
37     // used mainly for searched when users give only the start of a name.
38     bool begins_with (const char *prefix) const
39     {
40     int plen = strlen (prefix);
41     return !strncasecmp (s, prefix, plen) && length () >= plen;
42     }
43    
44 root 1.5 static const char *find (const char *s);
45     static const char *intern (const char *s);
46    
47     static void gc (); // garbage collect a few strings
48    
49 root 1.6 // this is used for informational messages and the like
50 root 1.8 const char *operator &() const { return s; }
51 root 1.5
52 root 1.6 const char &operator [](int i) const { return s[i]; }
53 root 1.8 operator const char *() const { return s == null ? 0 : s; }
54 root 1.5
55     shstr ()
56 root 1.8 : s (null)
57 root 1.5 {
58     }
59    
60 root 1.6 shstr (const shstr &sh)
61 root 1.5 : s (sh.s)
62     {
63 root 1.8 ++refcnt ();
64 root 1.5 }
65    
66     explicit shstr (const char *s)
67     : s (intern (s))
68     {
69     }
70    
71     ~shstr ()
72     {
73 root 1.8 --refcnt ();
74 root 1.5 }
75    
76     const shstr &operator =(const shstr &sh)
77     {
78 root 1.8 --refcnt ();
79 root 1.5 s = sh.s;
80 root 1.8 ++refcnt ();
81 root 1.5
82     return *this;
83     }
84    
85     const shstr &operator =(const char *str)
86     {
87 root 1.8 --refcnt ();
88    
89     // this optimises the important case of str == constant 0
90     if (is_constant (str))
91     s = str ? intern (str) : null;
92     else
93     s = intern (str);
94 root 1.1
95 root 1.5 return *this;
96     }
97 root 1.10
98     bool operator ==(const shstr &b)
99     {
100     return s == b.s;
101     }
102    
103     bool operator !=(const shstr &b)
104     {
105     return !(*this == b);
106     }
107 root 1.5 };
108 root 1.1
109 root 1.5 inline int strlen (const shstr &sh)
110     {
111     return sh.length ();
112     }
113 root 1.2
114 root 1.12 inline int strcmp (const shstr &a, const shstr &b)
115     {
116     // TODO: use this to find all the occurences of people using strcmp
117     // all uses should be bogus, as we should be never interested in
118     // comparing shstr's alphabetically
119     #if 0
120     extern void do_not_use_strcmp_to_compare_shstr_values ();
121     do_not_use_strcmp_to_compare_shstr_values ();
122     #endif
123     return a != b;
124     }
125    
126 root 1.14 static std::ostream &operator <<(std::ostream &o, const shstr &sh)
127 root 1.13 {
128     o.write (sh.s, sh.length ());
129     return o;
130     }
131    
132 root 1.10 // only good for mass comparisons to shstr objects
133     struct shstr_cmp
134     {
135     const char *s;
136    
137     explicit shstr_cmp (const char *s)
138     : s (shstr::find (s))
139     {
140     }
141    
142 root 1.11 shstr_cmp (const shstr_cmp &sh)
143     : s (sh.s)
144     {
145     }
146    
147     shstr_cmp &operator =(const shstr_cmp sh) { s = sh.s; return *this; }
148 root 1.10 operator const char *() const { return s; }
149     };
150    
151     inline bool operator ==(const shstr_cmp &a, const shstr &b)
152 root 1.5 {
153     return a.s == b.s;
154     }
155 root 1.4
156 root 1.10 inline bool operator ==(const shstr &a, const shstr_cmp &b)
157 root 1.4 {
158 root 1.10 return b == a;
159 root 1.5 }
160 root 1.4
161 root 1.9 extern const shstr undead_name; /* Used in hit_player() in main.c */
162    
163 root 1.2 #endif
164