2 |
7u83 |
1 |
/*
|
7 |
7u83 |
2 |
* Copyright (c) 2002-2006 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 |
*
|
7 |
7u83 |
97 |
** Function: void nstring_init(NStringP nstring)
|
2 |
7u83 |
98 |
** Exceptions:
|
|
|
99 |
*
|
|
|
100 |
* This function initialises the specified nstring to be an empty nstring.
|
|
|
101 |
*
|
7 |
7u83 |
102 |
** Function: void nstring_init_length(NStringP nstring, unsigned length)
|
2 |
7u83 |
103 |
** Exceptions: XX_dalloc_no_memory
|
|
|
104 |
*
|
|
|
105 |
* This function initialises the specified nstring to be an nstring of the
|
|
|
106 |
* specified length. The initial contents are unspecified.
|
|
|
107 |
*
|
7 |
7u83 |
108 |
** Function: void nstring_assign(NStringP to, NStringP from)
|
2 |
7u83 |
109 |
** Exceptions:
|
|
|
110 |
*
|
|
|
111 |
* This function assigns the from nstring to the to nstring. The from nstring
|
|
|
112 |
* should not be used afterwards, without reinitialising it.
|
|
|
113 |
*
|
7 |
7u83 |
114 |
** Function: void nstring_copy_cstring(NStringP nstring, CStringP cstring)
|
2 |
7u83 |
115 |
** Exceptions: XX_dalloc_no_memory
|
|
|
116 |
*
|
|
|
117 |
* This function initialises the specified nstring from the content of the
|
|
|
118 |
* specified cstring.
|
|
|
119 |
*
|
7 |
7u83 |
120 |
** Function: void nstring_insert_cstring(NStringP nstring, CStringP cstring)
|
2 |
7u83 |
121 |
** Exceptions:
|
|
|
122 |
*
|
|
|
123 |
* This function inserts the specified cstring into the specified nstring.
|
|
|
124 |
* Sufficient characters are copied to fill up the nstring.
|
|
|
125 |
*
|
7 |
7u83 |
126 |
** Function: void nstring_copy(NStringP to, NStringP from)
|
2 |
7u83 |
127 |
** Exceptions: XX_dalloc_no_memory
|
|
|
128 |
*
|
|
|
129 |
* This function copies the specified from nstring into the specified to
|
|
|
130 |
* nstring.
|
|
|
131 |
*
|
7 |
7u83 |
132 |
** Function: CStringP nstring_to_cstring(NStringP nstring)
|
2 |
7u83 |
133 |
** Exceptions: XX_dalloc_no_memory
|
|
|
134 |
*
|
|
|
135 |
* This function returns a dynamically allocated cstring copy of the specified
|
|
|
136 |
* nstring. If the nstring contains null characters, then the characters
|
|
|
137 |
* after the first null will be ignored in the cstring (although they will
|
|
|
138 |
* still be part of it).
|
|
|
139 |
*
|
7 |
7u83 |
140 |
** Function: unsigned nstring_hash_value(NStringP nstring)
|
2 |
7u83 |
141 |
** Exceptions:
|
|
|
142 |
*
|
|
|
143 |
* This function returns the hash value associated with the specified nstring.
|
|
|
144 |
* This value is guaranteed to be identical for all nstrings with the same
|
|
|
145 |
* content.
|
|
|
146 |
*
|
7 |
7u83 |
147 |
** Function: unsigned nstring_length(NStringP nstring)
|
2 |
7u83 |
148 |
** Exceptions:
|
|
|
149 |
*
|
|
|
150 |
* This function returns the length of the specified nstring.
|
|
|
151 |
*
|
7 |
7u83 |
152 |
** Function: CStringP nstring_contents(NStringP nstring)
|
2 |
7u83 |
153 |
** Exceptions:
|
|
|
154 |
*
|
|
|
155 |
* This function returns the contents of the specified nstring.
|
|
|
156 |
*
|
7 |
7u83 |
157 |
** Function: CmpT nstring_compare(NStringP nstring1, NStringP nstring2)
|
2 |
7u83 |
158 |
** Exceptions:
|
|
|
159 |
*
|
|
|
160 |
* This function returns ``CMP_LT'', ``CMP_EQ'', or ``CMP_GT'', depending on
|
|
|
161 |
* whether the content of nstring1 is lexicographically less than, equal to,
|
|
|
162 |
* or greater than the content of nstring2.
|
|
|
163 |
*
|
7 |
7u83 |
164 |
** Function: BoolT nstring_equal(NStringP nstring1, NStringP nstring2)
|
2 |
7u83 |
165 |
** Exceptions:
|
|
|
166 |
*
|
|
|
167 |
* This function returns true if the specified nstrings have the same content,
|
|
|
168 |
* and false otherwise.
|
|
|
169 |
*
|
7 |
7u83 |
170 |
** Function: BoolT nstring_ci_equal(NStringP nstring1, NStringP nstring2)
|
2 |
7u83 |
171 |
** Exceptions:
|
|
|
172 |
*
|
|
|
173 |
* This function returns true if the specified nstrings have the same content
|
|
|
174 |
* (ignoring differences in case), and false otherwise.
|
|
|
175 |
*
|
7 |
7u83 |
176 |
** Function: BoolT nstring_contains(NStringP nstring, char c)
|
2 |
7u83 |
177 |
** Exceptions:
|
|
|
178 |
*
|
|
|
179 |
* This function returns true if the specified nstring contains the specified
|
|
|
180 |
* character, and false otherwise.
|
|
|
181 |
*
|
7 |
7u83 |
182 |
** Function: BoolT nstring_is_prefix(NStringP nstring1, NStringP nstring2)
|
2 |
7u83 |
183 |
** Exceptions:
|
|
|
184 |
*
|
|
|
185 |
* This function returns true if the second nstring is a prefix of the first
|
|
|
186 |
* nstring, and false otherwise.
|
|
|
187 |
*
|
7 |
7u83 |
188 |
** Function: void nstring_destroy(NStringP nstring)
|
2 |
7u83 |
189 |
** Exceptions:
|
|
|
190 |
*
|
|
|
191 |
* This function deallocates the contents of the specified nstring.
|
|
|
192 |
*
|
7 |
7u83 |
193 |
** Function: void write_nstring(OStreamP stream, NStringP nstring)
|
2 |
7u83 |
194 |
** Exceptions: XX_dalloc_no_memory, XX_ostream_write_error
|
|
|
195 |
*
|
|
|
196 |
* This function writes the content of the specified nstring to the specified
|
|
|
197 |
* ostream.
|
|
|
198 |
*
|
7 |
7u83 |
199 |
** Function: void dstring_init(DStringP dstring)
|
2 |
7u83 |
200 |
** Exceptions: XX_dalloc_no_memory
|
|
|
201 |
*
|
|
|
202 |
* This function initialises the specified dstring to be an empty dstring.
|
|
|
203 |
*
|
7 |
7u83 |
204 |
** Function: unsigned dstring_length(DStringP dstring)
|
2 |
7u83 |
205 |
** Exceptions:
|
|
|
206 |
*
|
|
|
207 |
* This function returns the length of the specified dstring.
|
|
|
208 |
*
|
7 |
7u83 |
209 |
** Function: void dstring_append_char(DStringP dstring, char c)
|
2 |
7u83 |
210 |
** Exceptions: XX_dalloc_no_memory
|
|
|
211 |
*
|
|
|
212 |
* This function appends the specified character to the specified dstring.
|
|
|
213 |
*
|
7 |
7u83 |
214 |
** Function: void dstring_append_cstring(DStringP dstring, CStringP cstring
|
2 |
7u83 |
215 |
** Exceptions: XX_dalloc_no_memory
|
|
|
216 |
*
|
|
|
217 |
* This function appends the content of the specified cstring to the specified
|
|
|
218 |
* dstring.
|
|
|
219 |
*
|
7 |
7u83 |
220 |
** Function: void dstring_append_nstring(DStringP dstring, NStringP nstring
|
2 |
7u83 |
221 |
** Exceptions: XX_dalloc_no_memory
|
|
|
222 |
*
|
|
|
223 |
* This function appends the content of the specified nstring to the specified
|
|
|
224 |
* dstring.
|
|
|
225 |
*
|
7 |
7u83 |
226 |
** Function: BoolT dstring_last_char_equal(DStringP dstring, char c)
|
2 |
7u83 |
227 |
** Exceptions:
|
|
|
228 |
*
|
|
|
229 |
* This function returns true if the last character of the specified dstring
|
|
|
230 |
* is the same as the specified character, and false otherwise. If the
|
|
|
231 |
* dstring is empty, then false is always returned.
|
|
|
232 |
*
|
7 |
7u83 |
233 |
** Function: void dstring_to_nstring(DStringP dstring, NStringP nstring_ref)
|
2 |
7u83 |
234 |
** Exceptions: XX_dalloc_no_memory
|
|
|
235 |
*
|
|
|
236 |
* This function copies the content of the specified dstring into the
|
|
|
237 |
* specified nstring.
|
|
|
238 |
*
|
7 |
7u83 |
239 |
** Functions: CStringP dstring_to_cstring(DStringP dstring)
|
2 |
7u83 |
240 |
** Exceptions: XX_dalloc_no_memory
|
|
|
241 |
*
|
|
|
242 |
* This function copies the content of the specified dstring into a
|
|
|
243 |
* dynamically allocated cstring, and returns it.
|
|
|
244 |
*
|
7 |
7u83 |
245 |
** Function: CStringP dstring_destroy_to_cstring(DStringP dstring)
|
2 |
7u83 |
246 |
** Exceptions: XX_dalloc_no_memory
|
|
|
247 |
*
|
|
|
248 |
* This function does the equivalent of a call to ``dstring_to_cstring''
|
|
|
249 |
* followed by a call to ``dstring_destroy''. It returns a cstring that is
|
|
|
250 |
* normally the internal cstring of the dstring (if there isn't enough room
|
|
|
251 |
* for a null character at the end, then the cstring will need to be
|
|
|
252 |
* reallocated).
|
|
|
253 |
*
|
7 |
7u83 |
254 |
** Function: void dstring_destroy(DStringP dstring)
|
2 |
7u83 |
255 |
** Exceptions:
|
|
|
256 |
*
|
|
|
257 |
* This function deallocates the contents of the specified dstring.
|
|
|
258 |
*
|
|
|
259 |
**** Change log:
|
|
|
260 |
* $Log: dstring.h,v $
|
|
|
261 |
* Revision 1.1.1.1 1998/01/17 15:57:17 release
|
|
|
262 |
* First version to be checked into rolling release.
|
|
|
263 |
*
|
|
|
264 |
* Revision 1.2 1994/12/12 11:44:35 smf
|
|
|
265 |
* Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
|
|
|
266 |
* OSSG C Coding Standards.
|
|
|
267 |
*
|
|
|
268 |
* Revision 1.1.1.1 1994/07/25 16:05:49 smf
|
|
|
269 |
* Initial import of library shared files.
|
|
|
270 |
*
|
|
|
271 |
**/
|
|
|
272 |
|
|
|
273 |
/****************************************************************************/
|
|
|
274 |
|
|
|
275 |
#ifndef H_DSTRING
|
|
|
276 |
#define H_DSTRING
|
|
|
277 |
|
|
|
278 |
#include "os-interface.h"
|
|
|
279 |
#include "cstring.h"
|
|
|
280 |
#include "dalloc.h"
|
|
|
281 |
#include "ostream.h"
|
|
|
282 |
|
|
|
283 |
/*--------------------------------------------------------------------------*/
|
|
|
284 |
|
|
|
285 |
typedef struct NStringT {
|
|
|
286 |
unsigned length;
|
|
|
287 |
CStringP contents;
|
|
|
288 |
} NStringT, *NStringP;
|
|
|
289 |
|
|
|
290 |
typedef struct DStringT {
|
|
|
291 |
unsigned length;
|
|
|
292 |
unsigned max_length;
|
|
|
293 |
CStringP contents;
|
|
|
294 |
} DStringT, *DStringP;
|
|
|
295 |
|
|
|
296 |
/*--------------------------------------------------------------------------*/
|
|
|
297 |
|
|
|
298 |
extern void nstring_init
|
7 |
7u83 |
299 |
(NStringP);
|
2 |
7u83 |
300 |
extern void nstring_init_length
|
7 |
7u83 |
301 |
(NStringP, unsigned);
|
2 |
7u83 |
302 |
extern void nstring_assign
|
7 |
7u83 |
303 |
(NStringP, NStringP);
|
2 |
7u83 |
304 |
extern void nstring_copy_cstring
|
7 |
7u83 |
305 |
(NStringP, CStringP);
|
2 |
7u83 |
306 |
extern void nstring_insert_cstring
|
7 |
7u83 |
307 |
(NStringP, CStringP);
|
2 |
7u83 |
308 |
extern void nstring_copy
|
7 |
7u83 |
309 |
(NStringP, NStringP);
|
2 |
7u83 |
310 |
extern CStringP nstring_to_cstring
|
7 |
7u83 |
311 |
(NStringP);
|
2 |
7u83 |
312 |
extern unsigned nstring_hash_value
|
7 |
7u83 |
313 |
(NStringP);
|
2 |
7u83 |
314 |
extern unsigned nstring_length
|
7 |
7u83 |
315 |
(NStringP);
|
2 |
7u83 |
316 |
extern CStringP nstring_contents
|
7 |
7u83 |
317 |
(NStringP);
|
2 |
7u83 |
318 |
extern CmpT nstring_compare
|
7 |
7u83 |
319 |
(NStringP, NStringP);
|
2 |
7u83 |
320 |
extern BoolT nstring_equal
|
7 |
7u83 |
321 |
(NStringP, NStringP);
|
2 |
7u83 |
322 |
extern BoolT nstring_ci_equal
|
7 |
7u83 |
323 |
(NStringP, NStringP);
|
2 |
7u83 |
324 |
extern BoolT nstring_contains
|
7 |
7u83 |
325 |
(NStringP, char);
|
2 |
7u83 |
326 |
extern BoolT nstring_is_prefix
|
7 |
7u83 |
327 |
(NStringP, NStringP);
|
2 |
7u83 |
328 |
extern void nstring_destroy
|
7 |
7u83 |
329 |
(NStringP);
|
2 |
7u83 |
330 |
|
|
|
331 |
extern void write_nstring
|
7 |
7u83 |
332 |
(OStreamP, NStringP);
|
2 |
7u83 |
333 |
|
|
|
334 |
extern void dstring_init
|
7 |
7u83 |
335 |
(DStringP);
|
2 |
7u83 |
336 |
extern unsigned dstring_length
|
7 |
7u83 |
337 |
(DStringP);
|
2 |
7u83 |
338 |
extern void dstring_append_char
|
7 |
7u83 |
339 |
(DStringP, char);
|
2 |
7u83 |
340 |
extern void dstring_append_cstring
|
7 |
7u83 |
341 |
(DStringP, CStringP);
|
2 |
7u83 |
342 |
extern void dstring_append_nstring
|
7 |
7u83 |
343 |
(DStringP, NStringP);
|
2 |
7u83 |
344 |
extern BoolT dstring_last_char_equal
|
7 |
7u83 |
345 |
(DStringP, char);
|
2 |
7u83 |
346 |
extern void dstring_to_nstring
|
7 |
7u83 |
347 |
(DStringP, NStringP);
|
2 |
7u83 |
348 |
extern CStringP dstring_to_cstring
|
7 |
7u83 |
349 |
(DStringP);
|
2 |
7u83 |
350 |
extern CStringP dstring_destroy_to_cstring
|
7 |
7u83 |
351 |
(DStringP);
|
2 |
7u83 |
352 |
extern void dstring_destroy
|
7 |
7u83 |
353 |
(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 |
**/
|