2 |
7u83 |
1 |
/*
|
7 |
7u83 |
2 |
* Copyright (c) 2002-2005 The TenDRA Project <http://www.tendra.org/>.
|
|
|
3 |
* All rights reserved.
|
|
|
4 |
*
|
|
|
5 |
* Redistribution and use in source and binary forms, with or without
|
|
|
6 |
* modification, are permitted provided that the following conditions are met:
|
|
|
7 |
*
|
|
|
8 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
9 |
* this list of conditions and the following disclaimer.
|
|
|
10 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
11 |
* this list of conditions and the following disclaimer in the documentation
|
|
|
12 |
* and/or other materials provided with the distribution.
|
|
|
13 |
* 3. Neither the name of The TenDRA Project nor the names of its contributors
|
|
|
14 |
* may be used to endorse or promote products derived from this software
|
|
|
15 |
* without specific, prior written permission.
|
|
|
16 |
*
|
|
|
17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
|
|
|
18 |
* IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
19 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
20 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
|
|
|
21 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
22 |
* EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
23 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
24 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
25 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
26 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
27 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
28 |
*
|
|
|
29 |
* $Id$
|
|
|
30 |
*/
|
|
|
31 |
/*
|
2 |
7u83 |
32 |
Crown Copyright (c) 1997
|
7 |
7u83 |
33 |
|
2 |
7u83 |
34 |
This TenDRA(r) Computer Program is subject to Copyright
|
|
|
35 |
owned by the United Kingdom Secretary of State for Defence
|
|
|
36 |
acting through the Defence Evaluation and Research Agency
|
|
|
37 |
(DERA). It is made available to Recipients with a
|
|
|
38 |
royalty-free licence for its use, reproduction, transfer
|
|
|
39 |
to other parties and amendment for any purpose not excluding
|
|
|
40 |
product development provided that any such use et cetera
|
|
|
41 |
shall be deemed to be acceptance of the following conditions:-
|
7 |
7u83 |
42 |
|
2 |
7u83 |
43 |
(1) Its Recipients shall ensure that this Notice is
|
|
|
44 |
reproduced upon any copies or amended versions of it;
|
7 |
7u83 |
45 |
|
2 |
7u83 |
46 |
(2) Any amended version of it shall be clearly marked to
|
|
|
47 |
show both the nature of and the organisation responsible
|
|
|
48 |
for the relevant amendment or amendments;
|
7 |
7u83 |
49 |
|
2 |
7u83 |
50 |
(3) Its onward transfer from a recipient to another
|
|
|
51 |
party shall be deemed to be that party's acceptance of
|
|
|
52 |
these conditions;
|
7 |
7u83 |
53 |
|
2 |
7u83 |
54 |
(4) DERA gives no warranty or assurance as to its
|
|
|
55 |
quality or suitability for any purpose and DERA accepts
|
|
|
56 |
no liability whatsoever in relation to any use to which
|
|
|
57 |
it may be put.
|
|
|
58 |
*/
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
/**** dstring.h --- String manipulation.
|
|
|
62 |
*
|
|
|
63 |
** Author: Steve Folkes <smf@hermes.mod.uk>
|
|
|
64 |
*
|
|
|
65 |
**** Commentary:
|
|
|
66 |
*
|
|
|
67 |
***=== INTRODUCTION =========================================================
|
|
|
68 |
*
|
|
|
69 |
* This file specifies the interface to a string manipulation facility. There
|
|
|
70 |
* are two types of strings supported: nstrings (strings are stored as a
|
|
|
71 |
* length, and a vector of characters), and dstrings which are only of use
|
|
|
72 |
* when it is necessary to append characters to a string one at a time with
|
|
|
73 |
* reasonable efficiency.
|
|
|
74 |
*
|
|
|
75 |
* Null pointers are not valid as nstrings or dstrings. Passing a null pointer
|
|
|
76 |
* as the argument to a function will have an undefined effect (on many
|
|
|
77 |
* machines the program will abort, but this is not guaranteed).
|
|
|
78 |
*
|
|
|
79 |
***=== TYPES ================================================================
|
|
|
80 |
*
|
|
|
81 |
** Type: NStringT
|
|
|
82 |
** Type: NStringP
|
|
|
83 |
** Repr: <private>
|
|
|
84 |
*
|
|
|
85 |
* This is the nstring type. These strings may contain null characters.
|
|
|
86 |
*
|
|
|
87 |
** Type: DStringT
|
|
|
88 |
** Type: DStringP
|
|
|
89 |
** Repr: <private>
|
|
|
90 |
*
|
|
|
91 |
* This is the dstring type. It is only for appending characters and C
|
|
|
92 |
* strings to. Once it has been completely initialised, it should be
|
|
|
93 |
* converted to one of the other string types.
|
|
|
94 |
*
|
|
|
95 |
***=== FUNCTIONS ============================================================
|
|
|
96 |
*
|
|
|
97 |
** Function: void nstring_init
|
7 |
7u83 |
98 |
* (NStringP nstring)
|
2 |
7u83 |
99 |
** Exceptions:
|
|
|
100 |
*
|
|
|
101 |
* This function initialises the specified nstring to be an empty nstring.
|
|
|
102 |
*
|
|
|
103 |
** Function: void nstring_init_length
|
7 |
7u83 |
104 |
* (NStringP nstring, unsigned length)
|
2 |
7u83 |
105 |
** Exceptions: XX_dalloc_no_memory
|
|
|
106 |
*
|
|
|
107 |
* This function initialises the specified nstring to be an nstring of the
|
|
|
108 |
* specified length. The initial contents are unspecified.
|
|
|
109 |
*
|
|
|
110 |
** Function: void nstring_assign
|
7 |
7u83 |
111 |
* (NStringP to, NStringP from)
|
2 |
7u83 |
112 |
** Exceptions:
|
|
|
113 |
*
|
|
|
114 |
* This function assigns the from nstring to the to nstring. The from nstring
|
|
|
115 |
* should not be used afterwards, without reinitialising it.
|
|
|
116 |
*
|
|
|
117 |
** Function: void nstring_copy_cstring
|
7 |
7u83 |
118 |
* (NStringP nstring, CStringP cstring)
|
2 |
7u83 |
119 |
** Exceptions: XX_dalloc_no_memory
|
|
|
120 |
*
|
|
|
121 |
* This function initialises the specified nstring from the content of the
|
|
|
122 |
* specified cstring.
|
|
|
123 |
*
|
|
|
124 |
** Function: void nstring_insert_cstring
|
7 |
7u83 |
125 |
* (NStringP nstring, CStringP cstring)
|
2 |
7u83 |
126 |
** Exceptions:
|
|
|
127 |
*
|
|
|
128 |
* This function inserts the specified cstring into the specified nstring.
|
|
|
129 |
* Sufficient characters are copied to fill up the nstring.
|
|
|
130 |
*
|
|
|
131 |
** Function: void nstring_copy
|
7 |
7u83 |
132 |
* (NStringP to, NStringP from)
|
2 |
7u83 |
133 |
** Exceptions: XX_dalloc_no_memory
|
|
|
134 |
*
|
|
|
135 |
* This function copies the specified from nstring into the specified to
|
|
|
136 |
* nstring.
|
|
|
137 |
*
|
|
|
138 |
** Function: CStringP nstring_to_cstring
|
7 |
7u83 |
139 |
* (NStringP nstring)
|
2 |
7u83 |
140 |
** Exceptions: XX_dalloc_no_memory
|
|
|
141 |
*
|
|
|
142 |
* This function returns a dynamically allocated cstring copy of the specified
|
|
|
143 |
* nstring. If the nstring contains null characters, then the characters
|
|
|
144 |
* after the first null will be ignored in the cstring (although they will
|
|
|
145 |
* still be part of it).
|
|
|
146 |
*
|
|
|
147 |
** Function: unsigned nstring_hash_value
|
7 |
7u83 |
148 |
* (NStringP nstring)
|
2 |
7u83 |
149 |
** Exceptions:
|
|
|
150 |
*
|
|
|
151 |
* This function returns the hash value associated with the specified nstring.
|
|
|
152 |
* This value is guaranteed to be identical for all nstrings with the same
|
|
|
153 |
* content.
|
|
|
154 |
*
|
|
|
155 |
** Function: unsigned nstring_length
|
7 |
7u83 |
156 |
* (NStringP nstring)
|
2 |
7u83 |
157 |
** Exceptions:
|
|
|
158 |
*
|
|
|
159 |
* This function returns the length of the specified nstring.
|
|
|
160 |
*
|
|
|
161 |
** Function: CStringP nstring_contents
|
7 |
7u83 |
162 |
* (NStringP nstring)
|
2 |
7u83 |
163 |
** Exceptions:
|
|
|
164 |
*
|
|
|
165 |
* This function returns the contents of the specified nstring.
|
|
|
166 |
*
|
|
|
167 |
** Function: CmpT nstring_compare
|
7 |
7u83 |
168 |
* (NStringP nstring1, NStringP nstring2)
|
2 |
7u83 |
169 |
** Exceptions:
|
|
|
170 |
*
|
|
|
171 |
* This function returns ``CMP_LT'', ``CMP_EQ'', or ``CMP_GT'', depending on
|
|
|
172 |
* whether the content of nstring1 is lexicographically less than, equal to,
|
|
|
173 |
* or greater than the content of nstring2.
|
|
|
174 |
*
|
|
|
175 |
** Function: BoolT nstring_equal
|
7 |
7u83 |
176 |
* (NStringP nstring1, NStringP nstring2)
|
2 |
7u83 |
177 |
** Exceptions:
|
|
|
178 |
*
|
|
|
179 |
* This function returns true if the specified nstrings have the same content,
|
|
|
180 |
* and false otherwise.
|
|
|
181 |
*
|
|
|
182 |
** Function: BoolT nstring_ci_equal
|
7 |
7u83 |
183 |
* (NStringP nstring1, NStringP nstring2)
|
2 |
7u83 |
184 |
** Exceptions:
|
|
|
185 |
*
|
|
|
186 |
* This function returns true if the specified nstrings have the same content
|
|
|
187 |
* (ignoring differences in case), and false otherwise.
|
|
|
188 |
*
|
|
|
189 |
** Function: BoolT nstring_contains
|
7 |
7u83 |
190 |
* (NStringP nstring, char c)
|
2 |
7u83 |
191 |
** Exceptions:
|
|
|
192 |
*
|
|
|
193 |
* This function returns true if the specified nstring contains the specified
|
|
|
194 |
* character, and false otherwise.
|
|
|
195 |
*
|
|
|
196 |
** Function: BoolT nstring_is_prefix
|
7 |
7u83 |
197 |
* (NStringP nstring1, NStringP nstring2)
|
2 |
7u83 |
198 |
** Exceptions:
|
|
|
199 |
*
|
|
|
200 |
* This function returns true if the second nstring is a prefix of the first
|
|
|
201 |
* nstring, and false otherwise.
|
|
|
202 |
*
|
|
|
203 |
** Function: void nstring_destroy
|
7 |
7u83 |
204 |
* (NStringP nstring)
|
2 |
7u83 |
205 |
** Exceptions:
|
|
|
206 |
*
|
|
|
207 |
* This function deallocates the contents of the specified nstring.
|
|
|
208 |
*
|
|
|
209 |
** Function: void write_nstring
|
7 |
7u83 |
210 |
* (OStreamP stream, NStringP nstring)
|
2 |
7u83 |
211 |
** Exceptions: XX_dalloc_no_memory, XX_ostream_write_error
|
|
|
212 |
*
|
|
|
213 |
* This function writes the content of the specified nstring to the specified
|
|
|
214 |
* ostream.
|
|
|
215 |
*
|
|
|
216 |
** Function: void dstring_init
|
7 |
7u83 |
217 |
* (DStringP dstring)
|
2 |
7u83 |
218 |
** Exceptions: XX_dalloc_no_memory
|
|
|
219 |
*
|
|
|
220 |
* This function initialises the specified dstring to be an empty dstring.
|
|
|
221 |
*
|
|
|
222 |
** Function: unsigned dstring_length
|
7 |
7u83 |
223 |
* (DStringP dstring)
|
2 |
7u83 |
224 |
** Exceptions:
|
|
|
225 |
*
|
|
|
226 |
* This function returns the length of the specified dstring.
|
|
|
227 |
*
|
|
|
228 |
** Function: void dstring_append_char
|
7 |
7u83 |
229 |
* (DStringP dstring, char c)
|
2 |
7u83 |
230 |
** Exceptions: XX_dalloc_no_memory
|
|
|
231 |
*
|
|
|
232 |
* This function appends the specified character to the specified dstring.
|
|
|
233 |
*
|
|
|
234 |
** Function: void dstring_append_cstring
|
7 |
7u83 |
235 |
* (DStringP dstring, CStringP cstring)
|
2 |
7u83 |
236 |
** Exceptions: XX_dalloc_no_memory
|
|
|
237 |
*
|
|
|
238 |
* This function appends the content of the specified cstring to the specified
|
|
|
239 |
* dstring.
|
|
|
240 |
*
|
|
|
241 |
** Function: void dstring_append_nstring
|
7 |
7u83 |
242 |
* (DStringP dstring, NStringP nstring)
|
2 |
7u83 |
243 |
** Exceptions: XX_dalloc_no_memory
|
|
|
244 |
*
|
|
|
245 |
* This function appends the content of the specified nstring to the specified
|
|
|
246 |
* dstring.
|
|
|
247 |
*
|
|
|
248 |
** Function: BoolT dstring_last_char_equal
|
7 |
7u83 |
249 |
* (DStringP dstring, char c)
|
2 |
7u83 |
250 |
** Exceptions:
|
|
|
251 |
*
|
|
|
252 |
* This function returns true if the last character of the specified dstring
|
|
|
253 |
* is the same as the specified character, and false otherwise. If the
|
|
|
254 |
* dstring is empty, then false is always returned.
|
|
|
255 |
*
|
|
|
256 |
** Function: void dstring_to_nstring
|
7 |
7u83 |
257 |
* (DStringP dstring, NStringP nstring_ref)
|
2 |
7u83 |
258 |
** Exceptions: XX_dalloc_no_memory
|
|
|
259 |
*
|
|
|
260 |
* This function copies the content of the specified dstring into the
|
|
|
261 |
* specified nstring.
|
|
|
262 |
*
|
|
|
263 |
** Functions: CStringP dstring_to_cstring
|
7 |
7u83 |
264 |
* (DStringP dstring)
|
2 |
7u83 |
265 |
** Exceptions: XX_dalloc_no_memory
|
|
|
266 |
*
|
|
|
267 |
* This function copies the content of the specified dstring into a
|
|
|
268 |
* dynamically allocated cstring, and returns it.
|
|
|
269 |
*
|
|
|
270 |
** Function: CStringP dstring_destroy_to_cstring
|
7 |
7u83 |
271 |
* (DStringP dstring)
|
2 |
7u83 |
272 |
** Exceptions: XX_dalloc_no_memory
|
|
|
273 |
*
|
|
|
274 |
* This function does the equivalent of a call to ``dstring_to_cstring''
|
|
|
275 |
* followed by a call to ``dstring_destroy''. It returns a cstring that is
|
|
|
276 |
* normally the internal cstring of the dstring (if there isn't enough room
|
|
|
277 |
* for a null character at the end, then the cstring will need to be
|
|
|
278 |
* reallocated).
|
|
|
279 |
*
|
|
|
280 |
** Function: void dstring_destroy
|
7 |
7u83 |
281 |
* (DStringP dstring)
|
2 |
7u83 |
282 |
** Exceptions:
|
|
|
283 |
*
|
|
|
284 |
* This function deallocates the contents of the specified dstring.
|
|
|
285 |
*
|
|
|
286 |
**** Change log:
|
|
|
287 |
* $Log: dstring.h,v $
|
|
|
288 |
* Revision 1.1.1.1 1998/01/17 15:57:45 release
|
|
|
289 |
* First version to be checked into rolling release.
|
|
|
290 |
*
|
|
|
291 |
* Revision 1.2 1994/12/12 11:44:35 smf
|
|
|
292 |
* Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
|
|
|
293 |
* OSSG C Coding Standards.
|
|
|
294 |
*
|
|
|
295 |
* Revision 1.1.1.1 1994/07/25 16:05:49 smf
|
|
|
296 |
* Initial import of library shared files.
|
|
|
297 |
*
|
|
|
298 |
**/
|
|
|
299 |
|
|
|
300 |
/****************************************************************************/
|
|
|
301 |
|
|
|
302 |
#ifndef H_DSTRING
|
|
|
303 |
#define H_DSTRING
|
|
|
304 |
|
|
|
305 |
#include "os-interface.h"
|
|
|
306 |
#include "cstring.h"
|
|
|
307 |
#include "dalloc.h"
|
|
|
308 |
#include "ostream.h"
|
|
|
309 |
|
|
|
310 |
/*--------------------------------------------------------------------------*/
|
|
|
311 |
|
|
|
312 |
typedef struct NStringT {
|
|
|
313 |
unsigned length;
|
|
|
314 |
CStringP contents;
|
|
|
315 |
} NStringT, *NStringP;
|
|
|
316 |
|
|
|
317 |
typedef struct DStringT {
|
|
|
318 |
unsigned length;
|
|
|
319 |
unsigned max_length;
|
|
|
320 |
CStringP contents;
|
|
|
321 |
} DStringT, *DStringP;
|
|
|
322 |
|
|
|
323 |
/*--------------------------------------------------------------------------*/
|
|
|
324 |
|
7 |
7u83 |
325 |
extern void nstring_init(NStringP);
|
|
|
326 |
extern void nstring_init_length(NStringP, unsigned);
|
|
|
327 |
extern void nstring_assign(NStringP, NStringP);
|
|
|
328 |
extern void nstring_copy_cstring(NStringP, CStringP);
|
|
|
329 |
extern void nstring_insert_cstring(NStringP, CStringP);
|
|
|
330 |
extern void nstring_copy(NStringP, NStringP);
|
|
|
331 |
extern CStringP nstring_to_cstring(NStringP);
|
|
|
332 |
extern unsigned nstring_hash_value(NStringP);
|
|
|
333 |
extern unsigned nstring_length(NStringP);
|
|
|
334 |
extern CStringP nstring_contents(NStringP);
|
|
|
335 |
extern CmpT nstring_compare(NStringP, NStringP);
|
|
|
336 |
extern BoolT nstring_equal(NStringP, NStringP);
|
|
|
337 |
extern BoolT nstring_ci_equal(NStringP, NStringP);
|
|
|
338 |
extern BoolT nstring_contains(NStringP, char);
|
|
|
339 |
extern BoolT nstring_is_prefix(NStringP, NStringP);
|
|
|
340 |
extern void nstring_destroy(NStringP);
|
2 |
7u83 |
341 |
|
7 |
7u83 |
342 |
extern void write_nstring(OStreamP, NStringP);
|
2 |
7u83 |
343 |
|
7 |
7u83 |
344 |
extern void dstring_init(DStringP);
|
|
|
345 |
extern unsigned dstring_length(DStringP);
|
|
|
346 |
extern void dstring_append_char(DStringP, char);
|
|
|
347 |
extern void dstring_append_cstring(DStringP, CStringP);
|
|
|
348 |
extern void dstring_append_nstring(DStringP, NStringP);
|
|
|
349 |
extern BoolT dstring_last_char_equal(DStringP, char);
|
|
|
350 |
extern void dstring_to_nstring(DStringP, NStringP);
|
|
|
351 |
extern CStringP dstring_to_cstring(DStringP);
|
|
|
352 |
extern CStringP dstring_destroy_to_cstring(DStringP);
|
|
|
353 |
extern void dstring_destroy(DStringP);
|
2 |
7u83 |
354 |
|
|
|
355 |
/*--------------------------------------------------------------------------*/
|
|
|
356 |
|
|
|
357 |
#ifdef FS_FAST
|
7 |
7u83 |
358 |
#define nstring_length(s) ((s)->length)
|
|
|
359 |
#define nstring_contents(s) ((s)->contents)
|
|
|
360 |
#define dstring_length(s) ((s)->length)
|
2 |
7u83 |
361 |
#endif /* defined (FS_FAST) */
|
|
|
362 |
|
|
|
363 |
#endif /* !defined (H_DSTRING) */
|
|
|
364 |
|
|
|
365 |
/*
|
|
|
366 |
* Local variables(smf):
|
|
|
367 |
* eval: (include::add-path-entry "../os-interface" "../generated")
|
|
|
368 |
* end:
|
|
|
369 |
**/
|