Subversion Repositories tendra.SVN

Rev

Rev 2 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
/**** dstring.c --- String manipulation.
32
 *
33
 ** Author: Steve Folkes <smf@hermes.mod.uk>
34
 *
35
 **** Commentary:
36
 *
37
 * This file implements the string manipulation facility specified in the file
38
 * "dstring.h".  See that file for more details.
39
 *
40
 **** Change Log:
41
 * $Log: dstring.c,v $
42
 * Revision 1.1.1.1  1998/01/17  15:57:16  release
43
 * First version to be checked into rolling release.
44
 *
45
 * Revision 1.2  1994/12/12  11:44:33  smf
46
 * Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
47
 * OSSG C Coding Standards.
48
 *
49
 * Revision 1.1.1.1  1994/07/25  16:05:49  smf
50
 * Initial import of library shared files.
51
 *
52
**/
53
 
54
/****************************************************************************/
55
 
56
#include "dstring.h"
57
#include "syntax.h"
58
 
59
/*--------------------------------------------------------------------------*/
60
 
61
#define DSTRING_CHUNK_SIZE 32
62
 
63
/*** Functions for manipulating nstrings.
64
**/
65
 
66
void
67
nstring_init PROTO_N ((nstring))
68
	     PROTO_T (NStringP nstring)
69
{
70
    nstring->length   = 0;
71
    nstring->contents = NIL (CStringP);
72
}
73
 
74
void
75
nstring_init_length PROTO_N ((nstring, length))
76
		    PROTO_T (NStringP nstring X
77
			     unsigned length)
78
{
79
    nstring->length   = length;
80
    nstring->contents = ALLOCATE_VECTOR (char, length);
81
}
82
 
83
void
84
nstring_assign PROTO_N ((to, from))
85
	       PROTO_T (NStringP to X
86
			NStringP from)
87
{
88
    to->length     = nstring_length (from);
89
    to->contents   = (from->contents);
90
}
91
 
92
void
93
nstring_copy_cstring PROTO_N ((nstring, cstring))
94
		     PROTO_T (NStringP nstring X
95
			      CStringP cstring)
96
{
97
    unsigned length = cstring_length (cstring);
98
 
99
    if (length > 0) {
100
	nstring->length   = length;
101
	nstring->contents = ALLOCATE_VECTOR (char, length);
102
	(void) memcpy ((GenericP) (nstring->contents), (GenericP) cstring,
103
		       (SizeT) length);
104
    } else {
105
	nstring->length   = 0;
106
	nstring->contents = NIL (CStringP);
107
    }
108
}
109
 
110
void
111
nstring_insert_cstring PROTO_N ((nstring, cstring))
112
		       PROTO_T (NStringP nstring X
113
				CStringP cstring)
114
{
115
    unsigned length = nstring_length (nstring);
116
 
117
    if (length > 0) {
118
	(void) memcpy ((GenericP) (nstring->contents), (GenericP) cstring,
119
		       (SizeT) length);
120
    }
121
}
122
 
123
void
124
nstring_copy PROTO_N ((to, from))
125
	     PROTO_T (NStringP to X
126
		      NStringP from)
127
{
128
    unsigned length = nstring_length (from);
129
 
130
    if (length > 0) {
131
	to->length   = length;
132
	to->contents = ALLOCATE_VECTOR (char, length);
133
	(void) memcpy ((GenericP) (to->contents),
134
		       (GenericP) (from->contents), (SizeT) length);
135
    } else {
136
	to->length   = 0;
137
	to->contents = NIL (CStringP);
138
    }
139
}
140
 
141
CStringP
142
nstring_to_cstring PROTO_N ((nstring))
143
		   PROTO_T (NStringP nstring)
144
{
145
    unsigned length = nstring_length (nstring);
146
    CStringP tmp    = ALLOCATE_VECTOR (char, length + 1);
147
 
148
    if (length > 0) {
149
	(void) memcpy ((GenericP) tmp, (GenericP) (nstring->contents),
150
		       (SizeT) length);
151
    }
152
    tmp [length] = '\0';
153
    return (tmp);
154
}
155
 
156
unsigned
157
nstring_hash_value PROTO_N ((nstring))
158
		   PROTO_T (NStringP nstring)
159
{
160
    unsigned value        = 0;
161
    CStringP tmp_contents = (nstring->contents);
162
    unsigned tmp_length   = nstring_length (nstring);
163
 
164
    while (tmp_length --) {
165
	value += ((unsigned) (*tmp_contents ++));
166
    }
167
    return (value);
168
}
169
 
170
#ifdef FS_FAST
171
#undef nstring_length
172
#endif /* defined (FS_FAST) */
173
unsigned
174
nstring_length PROTO_N ((nstring))
175
	       PROTO_T (NStringP nstring)
176
{
177
    return (nstring->length);
178
}
179
#ifdef FS_FAST
180
#define nstring_length(s) ((s)->length)
181
#endif /* defined (FS_FAST) */
182
 
183
#ifdef FS_FAST
184
#undef nstring_contents
185
#endif /* defined (FS_FAST) */
186
CStringP
187
nstring_contents PROTO_N ((nstring))
188
		 PROTO_T (NStringP nstring)
189
{
190
    return (nstring->contents);
191
}
192
#ifdef FS_FAST
193
#define nstring_contents(s) ((s)->contents)
194
#endif /* defined (FS_FAST) */
195
 
196
CmpT
197
nstring_compare PROTO_N ((nstring1, nstring2))
198
		PROTO_T (NStringP nstring1 X
199
			 NStringP nstring2)
200
{
201
    unsigned length = nstring_length (nstring1);
202
    int      status;
203
 
204
    if (length > nstring_length (nstring2)) {
205
	length = nstring_length (nstring2);
206
    }
207
    status = memcmp ((GenericP) (nstring1->contents),
208
		     (GenericP) (nstring2->contents), (SizeT) length);
209
    if (status < 0) {
210
	return (CMP_LT);
211
    } else if (status > 0) {
212
	return (CMP_GT);
213
    } else if (nstring_length (nstring1) < nstring_length (nstring2)) {
214
	return (CMP_LT);
215
    } else if (nstring_length (nstring1) > nstring_length (nstring2)) {
216
	return (CMP_GT);
217
    } else {
218
	return (CMP_EQ);
219
    }
220
}
221
 
222
BoolT
223
nstring_equal PROTO_N ((nstring1, nstring2))
224
	      PROTO_T (NStringP nstring1 X
225
		       NStringP nstring2)
226
{
227
    unsigned length = nstring_length (nstring1);
228
 
229
    return ((length == nstring_length (nstring2)) &&
230
	    (memcmp ((GenericP) (nstring1->contents),
231
		     (GenericP) (nstring2->contents), (SizeT) length) == 0));
232
}
233
 
234
BoolT
235
nstring_ci_equal PROTO_N ((nstring1, nstring2))
236
		 PROTO_T (NStringP nstring1 X
237
			  NStringP nstring2)
238
{
239
    unsigned length = nstring_length (nstring1);
240
 
241
    if (length == nstring_length (nstring2)) {
242
	CStringP tmp1 = (nstring1->contents);
243
	CStringP tmp2 = (nstring2->contents);
244
	char c1;
245
	char c2;
246
 
247
	do {
248
	    c1 = syntax_upcase (*tmp1 ++);
249
	    c2 = syntax_upcase (*tmp2 ++);
250
	    if (length -- == 0) {
251
		return (TRUE);
252
	    }
253
	} while (c1 == c2);
254
    }
255
    return (FALSE);
256
}
257
 
258
BoolT
259
nstring_contains PROTO_N ((nstring, c))
260
		 PROTO_T (NStringP nstring X
261
			  char     c)
262
{
263
    CStringP contents = nstring_contents (nstring);
264
    unsigned length   = nstring_length (nstring);
265
 
266
    return (memchr ((GenericP) contents, c, (SizeT) length) != NIL (GenericP));
267
}
268
 
269
BoolT
270
nstring_is_prefix PROTO_N ((nstring1, nstring2))
271
		  PROTO_T (NStringP nstring1 X
272
			   NStringP nstring2)
273
{
274
    CStringP contents1 = nstring_contents (nstring1);
275
    CStringP contents2 = nstring_contents (nstring2);
276
    unsigned length    = nstring_length (nstring2);
277
 
278
    return ((length < nstring_length (nstring1)) &&
279
	    (memcmp ((GenericP) contents1, (GenericP) contents2,
280
		     (SizeT) length) == 0));
281
}
282
 
283
void
284
nstring_destroy PROTO_N ((nstring))
285
		PROTO_T (NStringP nstring)
286
{
287
    DEALLOCATE (nstring->contents);
288
}
289
 
290
void
291
write_nstring PROTO_N ((ostream, nstring))
292
	      PROTO_T (OStreamP ostream X
293
		       NStringP nstring)
294
{
295
    unsigned length = nstring_length (nstring);
296
 
297
    if (length > 0) {
298
	write_chars (ostream, nstring->contents, length);
299
    }
300
}
301
 
302
/*** Functions for manipulating dstrings.
303
**/
304
 
305
void
306
dstring_init PROTO_N ((dstring))
307
	     PROTO_T (DStringP dstring)
308
{
309
    dstring->length     = 0;
310
    dstring->max_length = DSTRING_CHUNK_SIZE;
311
    dstring->contents   = ALLOCATE_VECTOR (char, dstring->max_length);
312
}
313
 
314
#ifdef FS_FAST
315
#undef dstring_length
316
#endif /* defined (FS_FAST) */
317
unsigned
318
dstring_length PROTO_N ((dstring))
319
	       PROTO_T (DStringP dstring)
320
{
321
    return (dstring->length);
322
}
323
#ifdef FS_FAST
324
#define dstring_length(s) ((s)->length)
325
#endif /* defined (FS_FAST) */
326
 
327
void
328
dstring_append_char PROTO_N ((dstring, c))
329
		    PROTO_T (DStringP dstring X
330
			     char     c)
331
{
332
    if ((dstring->length) >= (dstring->max_length)) {
333
	CStringP tmp;
334
 
335
	dstring->max_length += DSTRING_CHUNK_SIZE;
336
	tmp                  = ALLOCATE_VECTOR (char, dstring->max_length);
337
	(void) memcpy ((GenericP) tmp, (GenericP) (dstring->contents),
338
		       (SizeT) (dstring->length));
339
	DEALLOCATE (dstring->contents);
340
	dstring->contents = tmp;
341
    }
342
    dstring->contents [dstring->length ++] = c;
343
}
344
 
345
void
346
dstring_append_cstring PROTO_N ((dstring, cstring))
347
		       PROTO_T (DStringP dstring X
348
				CStringP cstring)
349
{
350
    unsigned clength = cstring_length (cstring);
351
    unsigned length  = (clength + (dstring->length));
352
 
353
    if (length > (dstring->max_length)) {
354
	CStringP tmp;
355
 
356
	while ((dstring->max_length) < length) {
357
	    dstring->max_length += DSTRING_CHUNK_SIZE;
358
	}
359
	tmp = ALLOCATE_VECTOR (char, dstring->max_length);
360
	(void) memcpy ((GenericP) tmp, (GenericP) (dstring->contents),
361
		       (SizeT) (dstring->length));
362
	DEALLOCATE (dstring->contents);
363
	dstring->contents = tmp;
364
    }
365
    (void) memcpy ((GenericP) &(dstring->contents [dstring->length]),
366
		   (GenericP) cstring, (SizeT) clength);
367
    dstring->length = length;
368
}
369
 
370
void
371
dstring_append_nstring PROTO_N ((dstring, nstring))
372
		       PROTO_T (DStringP dstring X
373
				NStringP nstring)
374
{
375
    unsigned nlength = nstring_length (nstring);
376
    unsigned length  = (nlength + (dstring->length));
377
 
378
    if (length > (dstring->max_length)) {
379
	CStringP tmp;
380
 
381
	while ((dstring->max_length) < length) {
382
	    dstring->max_length += DSTRING_CHUNK_SIZE;
383
	}
384
	tmp = ALLOCATE_VECTOR (char, dstring->max_length);
385
	(void) memcpy ((GenericP) tmp, (GenericP) (dstring->contents),
386
		       (SizeT) (dstring->length));
387
	DEALLOCATE (dstring->contents);
388
	dstring->contents = tmp;
389
    }
390
    (void) memcpy ((GenericP) &(dstring->contents [dstring->length]),
391
		   (GenericP) nstring_contents (nstring), (SizeT) nlength);
392
    dstring->length = length;
393
}
394
 
395
BoolT
396
dstring_last_char_equal PROTO_N ((dstring, c))
397
			PROTO_T (DStringP dstring X
398
				 char     c)
399
{
400
    return ((dstring->length) &&
401
	    ((dstring->contents [dstring->length - 1]) == c));
402
}
403
 
404
void
405
dstring_to_nstring PROTO_N ((dstring, nstring))
406
		   PROTO_T (DStringP dstring X
407
			    NStringP nstring)
408
{
409
    if (dstring->length > 0) {
410
	nstring->length   = (dstring->length);
411
	nstring->contents = ALLOCATE_VECTOR (char, dstring->length);
412
	(void) memcpy ((GenericP) (nstring->contents),
413
		       (GenericP) (dstring->contents),
414
		       (SizeT) (dstring->length));
415
    } else {
416
	nstring->length   = 0;
417
	nstring->contents = NIL (CStringP);
418
    }
419
}
420
 
421
CStringP
422
dstring_to_cstring PROTO_N ((dstring))
423
		   PROTO_T (DStringP dstring)
424
{
425
    CStringP tmp = ALLOCATE_VECTOR (char, dstring->length + 1);
426
 
427
    if (dstring->length > 0) {
428
	(void) memcpy ((GenericP) tmp, (GenericP) (dstring->contents),
429
		       (SizeT) (dstring->length));
430
    }
431
    tmp [dstring->length] = '\0';
432
    return (tmp);
433
}
434
 
435
CStringP
436
dstring_destroy_to_cstring PROTO_N ((dstring))
437
			   PROTO_T (DStringP dstring)
438
{
439
    CStringP tmp;
440
 
441
    if ((dstring->length) >= (dstring->max_length)) {
442
	tmp = ALLOCATE_VECTOR (char, (dstring->length) + 1);
443
	(void) memcpy ((GenericP) tmp, (GenericP) (dstring->contents),
444
		       (SizeT) (dstring->length));
445
	DEALLOCATE (dstring->contents);
446
    } else {
447
	tmp = (dstring->contents);
448
    }
449
    tmp [dstring->length] = '\0';
450
    dstring->length       = 0;
451
    dstring->max_length   = 0;
452
    dstring->contents     = NIL (CStringP);
453
    return (tmp);
454
}
455
 
456
void
457
dstring_destroy PROTO_N ((dstring))
458
		PROTO_T (DStringP dstring)
459
{
460
    DEALLOCATE (dstring->contents);
461
}
462
 
463
/*
464
 * Local variables(smf):
465
 * eval: (include::add-path-entry "../os-interface" "../generated")
466
 * end:
467
**/