1 | /* string.cc
|
---|
2 | Copyright (c) 1996 Eberhard Mattes
|
---|
3 |
|
---|
4 | This file is part of pmgdb.
|
---|
5 |
|
---|
6 | pmgdb is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | pmgdb is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with pmgdb; see the file COPYING. If not, write to
|
---|
18 | the Free Software Foundation, 59 Temple Place - Suite 330,
|
---|
19 | Boston, MA 02111-1307, USA. */
|
---|
20 |
|
---|
21 |
|
---|
22 | #include <stdlib.h>
|
---|
23 | #include <string.h>
|
---|
24 | #include "string.h"
|
---|
25 |
|
---|
26 |
|
---|
27 | bool bstring::operator == (const bstring &b) const
|
---|
28 | {
|
---|
29 | if (str == NULL && b.str == NULL)
|
---|
30 | return true;
|
---|
31 | if (str == NULL || b.str == NULL)
|
---|
32 | return false;
|
---|
33 | return strcmp (str, b.str) == 0;
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | void vstring::set (const char *s, int len)
|
---|
38 | {
|
---|
39 | if (len >= alloc)
|
---|
40 | set (len);
|
---|
41 | memcpy (str, s, len);
|
---|
42 | str[len] = 0;
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | void vstring::set (int len)
|
---|
47 | {
|
---|
48 | if (len >= alloc)
|
---|
49 | {
|
---|
50 | alloc = len + 1;
|
---|
51 | alloc = ((alloc + 31) / 32) * 32;
|
---|
52 | char *temp = new char[alloc];
|
---|
53 | if (str != NULL)
|
---|
54 | {
|
---|
55 | strcpy (temp, str);
|
---|
56 | delete[] str;
|
---|
57 | }
|
---|
58 | else
|
---|
59 | *temp = 0;
|
---|
60 | str = temp;
|
---|
61 | }
|
---|
62 | str[len] = 0;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | void vstring::set_null ()
|
---|
67 | {
|
---|
68 | delete[] str;
|
---|
69 | str = NULL;
|
---|
70 | alloc = 0;
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | vstring &vstring::operator = (const vstring &src)
|
---|
75 | {
|
---|
76 | if (src.str == NULL)
|
---|
77 | set_null ();
|
---|
78 | else
|
---|
79 | set (src.str);
|
---|
80 | return *this;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | void fstring::set (const char *s, int len)
|
---|
85 | {
|
---|
86 | delete[] str;
|
---|
87 | str = new char[len + 1];
|
---|
88 | memcpy (str, s, len);
|
---|
89 | str[len] = 0;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | // Note: Does not keep the contents of the string
|
---|
94 |
|
---|
95 | void fstring::set (int len)
|
---|
96 | {
|
---|
97 | delete[] str;
|
---|
98 | str = new char[len + 1];
|
---|
99 | *str = 0;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | void fstring::set_null ()
|
---|
104 | {
|
---|
105 | delete[] str;
|
---|
106 | str = NULL;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | fstring &fstring::operator = (const fstring &src)
|
---|
111 | {
|
---|
112 | if (src.str == NULL)
|
---|
113 | set_null ();
|
---|
114 | else
|
---|
115 | set (src.str);
|
---|
116 | return *this;
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | void astring::append (const char *s, int len)
|
---|
121 | {
|
---|
122 | if (cur_len + len >= alloc)
|
---|
123 | set (cur_len + len);
|
---|
124 | memcpy (str + cur_len, s, len);
|
---|
125 | cur_len += len;
|
---|
126 | str[cur_len] = 0;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | astring &astring::operator = (const astring &src)
|
---|
131 | {
|
---|
132 | if (src.str == NULL)
|
---|
133 | set_null ();
|
---|
134 | else
|
---|
135 | {
|
---|
136 | // TODO: no need to copy old string
|
---|
137 | parent::set (src.str);
|
---|
138 | cur_len = src.cur_len;
|
---|
139 | }
|
---|
140 | return *this;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | void astring::remove_trailing (char c)
|
---|
145 | {
|
---|
146 | while (cur_len > 0 && str[cur_len-1] == c)
|
---|
147 | --cur_len;
|
---|
148 | str[cur_len] = 0;
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | lstring::lstring (const char *s)
|
---|
153 | {
|
---|
154 | str = NULL; vec = NULL;
|
---|
155 | set (s);
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | lstring::lstring (const bstring &s)
|
---|
160 | {
|
---|
161 | str = NULL; vec = NULL;
|
---|
162 | set ((const char *)s);
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | lstring::lstring (const lstring &s)
|
---|
167 | {
|
---|
168 | str = NULL; vec = NULL;
|
---|
169 | assign (s);
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | lstring::~lstring ()
|
---|
174 | {
|
---|
175 | delete[] str;
|
---|
176 | delete[] vec;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | lstring &lstring::operator = (const lstring &s)
|
---|
181 | {
|
---|
182 | assign (s);
|
---|
183 | return *this;
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | void lstring::assign (const lstring &s)
|
---|
188 | {
|
---|
189 | delete[] str;
|
---|
190 | delete[] vec;
|
---|
191 | str_len = s.str_len;
|
---|
192 | str = new char[str_len+1];
|
---|
193 | memcpy (str, s.str, str_len+1);
|
---|
194 | n = s.n;
|
---|
195 | vec = new const char *[n];
|
---|
196 | const char *p = str;
|
---|
197 | for (int i = 0; i < n; ++i)
|
---|
198 | {
|
---|
199 | vec[i] = p;
|
---|
200 | p += strlen (p) + 1;
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | void lstring::set (const char *s, size_t len)
|
---|
206 | {
|
---|
207 | delete[] str;
|
---|
208 | delete[] vec;
|
---|
209 | str_len = len;
|
---|
210 | str = new char[len+1];
|
---|
211 | memcpy (str, s, len+1);
|
---|
212 | n = _memcount (str, '\n', len);
|
---|
213 | if (len != 0 && str[len-1] != '\n')
|
---|
214 | ++n;
|
---|
215 | vec = new const char *[n];
|
---|
216 | char *p = str;
|
---|
217 | int i = 0;
|
---|
218 | while (len != 0)
|
---|
219 | {
|
---|
220 | vec[i++] = p;
|
---|
221 | char *end = (char *)memchr (p, '\n', len);
|
---|
222 | if (end == NULL)
|
---|
223 | break;
|
---|
224 | *end = 0;
|
---|
225 | len -= (end + 1 - p);
|
---|
226 | p = end + 1;
|
---|
227 | }
|
---|
228 | if (i != n)
|
---|
229 | abort ();
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | static int (*lstring_compare_strings)(const char *, const char *);
|
---|
234 |
|
---|
235 |
|
---|
236 | static int default_compare (const char *s1, const char *s2)
|
---|
237 | {
|
---|
238 | return strcmp (s1, s2);
|
---|
239 | }
|
---|
240 |
|
---|
241 |
|
---|
242 | extern "C"
|
---|
243 | {
|
---|
244 | static int lstring_compare (const void *p1, const void *p2)
|
---|
245 | {
|
---|
246 | return lstring_compare_strings (*(const char **)p1, *(const char **)p2);
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | void lstring::sort ()
|
---|
251 | {
|
---|
252 | sort (default_compare);
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | // This is not thread-safe!
|
---|
257 |
|
---|
258 | void lstring::sort (int (*compare)(const char *, const char *))
|
---|
259 | {
|
---|
260 | lstring_compare_strings = compare;
|
---|
261 | qsort (vec, n, sizeof (*vec), lstring_compare);
|
---|
262 | }
|
---|