Subversion Repositories tendra.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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.c --- String manipulation.
62
 *
63
 ** Author: Steve Folkes <smf@hermes.mod.uk>
64
 *
65
 **** Commentary:
66
 *
67
 * This file implements the string manipulation facility specified in the file
68
 * "dstring.h".  See that file for more details.
69
 *
70
 **** Change Log:
71
 * $Log: dstring.c,v $
72
 * Revision 1.1.1.1  1998/01/17  15:57:44  release
73
 * First version to be checked into rolling release.
74
 *
75
 * Revision 1.2  1994/12/12  11:44:33  smf
76
 * Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
77
 * OSSG C Coding Standards.
78
 *
79
 * Revision 1.1.1.1  1994/07/25  16:05:49  smf
80
 * Initial import of library shared files.
81
 *
82
**/
83
 
84
/****************************************************************************/
85
 
86
#include "dstring.h"
87
#include "syntax.h"
88
 
89
/*--------------------------------------------------------------------------*/
90
 
91
#define DSTRING_CHUNK_SIZE 32
92
 
93
/*** Functions for manipulating nstrings.
94
**/
95
 
96
void
7 7u83 97
nstring_init(NStringP nstring)
2 7u83 98
{
99
    nstring->length   = 0;
7 7u83 100
    nstring->contents = NIL(CStringP);
2 7u83 101
}
102
 
103
void
7 7u83 104
nstring_init_length(NStringP nstring, unsigned length)
2 7u83 105
{
106
    nstring->length   = length;
7 7u83 107
    nstring->contents = ALLOCATE_VECTOR(char, length);
2 7u83 108
}
109
 
110
void
7 7u83 111
nstring_assign(NStringP to, NStringP from)
2 7u83 112
{
7 7u83 113
    to->length     = nstring_length(from);
2 7u83 114
    to->contents   = (from->contents);
115
}
116
 
117
void
7 7u83 118
nstring_copy_cstring(NStringP nstring, CStringP cstring)
2 7u83 119
{
7 7u83 120
    unsigned length = cstring_length(cstring);
2 7u83 121
 
122
    if (length > 0) {
123
	nstring->length   = length;
7 7u83 124
	nstring->contents = ALLOCATE_VECTOR(char, length);
125
	(void)memcpy((GenericP)(nstring->contents), (GenericP)cstring,
126
		     (SizeT)length);
2 7u83 127
    } else {
128
	nstring->length   = 0;
7 7u83 129
	nstring->contents = NIL(CStringP);
2 7u83 130
    }
131
}
132
 
133
void
7 7u83 134
nstring_insert_cstring(NStringP nstring, CStringP cstring)
2 7u83 135
{
7 7u83 136
    unsigned length = nstring_length(nstring);
2 7u83 137
 
138
    if (length > 0) {
7 7u83 139
	(void)memcpy((GenericP)(nstring->contents), (GenericP)cstring,
140
		     (SizeT)length);
2 7u83 141
    }
142
}
143
 
144
void
7 7u83 145
nstring_copy(NStringP to, NStringP from)
2 7u83 146
{
7 7u83 147
    unsigned length = nstring_length(from);
2 7u83 148
 
149
    if (length > 0) {
150
	to->length   = length;
7 7u83 151
	to->contents = ALLOCATE_VECTOR(char, length);
152
	(void)memcpy((GenericP)(to->contents),
153
		     (GenericP)(from->contents), (SizeT)length);
2 7u83 154
    } else {
155
	to->length   = 0;
7 7u83 156
	to->contents = NIL(CStringP);
2 7u83 157
    }
158
}
159
 
160
CStringP
7 7u83 161
nstring_to_cstring(NStringP nstring)
2 7u83 162
{
7 7u83 163
    unsigned length = nstring_length(nstring);
164
    CStringP tmp    = ALLOCATE_VECTOR(char, length + 1);
2 7u83 165
 
166
    if (length > 0) {
7 7u83 167
	(void)memcpy((GenericP)tmp, (GenericP)(nstring->contents),
168
		     (SizeT)length);
2 7u83 169
    }
7 7u83 170
    tmp[length] = '\0';
171
    return(tmp);
2 7u83 172
}
173
 
174
unsigned
7 7u83 175
nstring_hash_value(NStringP nstring)
2 7u83 176
{
177
    unsigned value        = 0;
178
    CStringP tmp_contents = (nstring->contents);
7 7u83 179
    unsigned tmp_length   = nstring_length(nstring);
2 7u83 180
 
7 7u83 181
    while (tmp_length--) {
182
	value += ((unsigned)(*tmp_contents++));
2 7u83 183
    }
7 7u83 184
    return(value);
2 7u83 185
}
186
 
187
#ifdef FS_FAST
188
#undef nstring_length
189
#endif /* defined (FS_FAST) */
190
unsigned
7 7u83 191
nstring_length(NStringP nstring)
2 7u83 192
{
7 7u83 193
    return(nstring->length);
2 7u83 194
}
195
#ifdef FS_FAST
7 7u83 196
#define nstring_length(s)	((s)->length)
2 7u83 197
#endif /* defined (FS_FAST) */
198
 
199
#ifdef FS_FAST
200
#undef nstring_contents
201
#endif /* defined (FS_FAST) */
202
CStringP
7 7u83 203
nstring_contents(NStringP nstring)
2 7u83 204
{
7 7u83 205
    return(nstring->contents);
2 7u83 206
}
207
#ifdef FS_FAST
7 7u83 208
#define nstring_contents(s)	((s)->contents)
2 7u83 209
#endif /* defined (FS_FAST) */
210
 
211
CmpT
7 7u83 212
nstring_compare(NStringP nstring1, NStringP nstring2)
2 7u83 213
{
7 7u83 214
    unsigned length = nstring_length(nstring1);
2 7u83 215
    int      status;
216
 
7 7u83 217
    if (length > nstring_length(nstring2)) {
218
	length = nstring_length(nstring2);
2 7u83 219
    }
7 7u83 220
    status = memcmp((GenericP)(nstring1->contents),
221
		    (GenericP)(nstring2->contents), (SizeT)length);
2 7u83 222
    if (status < 0) {
7 7u83 223
	return(CMP_LT);
2 7u83 224
    } else if (status > 0) {
7 7u83 225
	return(CMP_GT);
226
    } else if (nstring_length(nstring1) < nstring_length(nstring2)) {
227
	return(CMP_LT);
228
    } else if (nstring_length(nstring1) > nstring_length(nstring2)) {
229
	return(CMP_GT);
2 7u83 230
    } else {
7 7u83 231
	return(CMP_EQ);
2 7u83 232
    }
233
}
234
 
235
BoolT
7 7u83 236
nstring_equal(NStringP nstring1, NStringP nstring2)
2 7u83 237
{
7 7u83 238
    unsigned length = nstring_length(nstring1);
2 7u83 239
 
7 7u83 240
    return((length == nstring_length(nstring2)) &&
241
	   (memcmp((GenericP)(nstring1->contents),
242
		   (GenericP)(nstring2->contents), (SizeT)length) == 0));
2 7u83 243
}
244
 
245
BoolT
7 7u83 246
nstring_ci_equal(NStringP nstring1, NStringP nstring2)
2 7u83 247
{
7 7u83 248
    unsigned length = nstring_length(nstring1);
2 7u83 249
 
7 7u83 250
    if (length == nstring_length(nstring2)) {
2 7u83 251
	CStringP tmp1 = (nstring1->contents);
252
	CStringP tmp2 = (nstring2->contents);
253
	char c1;
254
	char c2;
255
 
256
	do {
7 7u83 257
	    c1 = syntax_upcase(*tmp1++);
258
	    c2 = syntax_upcase(*tmp2++);
259
	    if (length-- == 0) {
260
		return(TRUE);
2 7u83 261
	    }
262
	} while (c1 == c2);
263
    }
7 7u83 264
    return(FALSE);
2 7u83 265
}
266
 
267
BoolT
7 7u83 268
nstring_contains(NStringP nstring, char c)
2 7u83 269
{
7 7u83 270
    CStringP contents = nstring_contents(nstring);
271
    unsigned length   = nstring_length(nstring);
2 7u83 272
 
7 7u83 273
    return(memchr((GenericP)contents, c, (SizeT)length) != NIL(GenericP));
2 7u83 274
}
275
 
276
BoolT
7 7u83 277
nstring_is_prefix(NStringP nstring1,			   NStringP nstring2)
2 7u83 278
{
7 7u83 279
    CStringP contents1 = nstring_contents(nstring1);
280
    CStringP contents2 = nstring_contents(nstring2);
281
    unsigned length    = nstring_length(nstring2);
2 7u83 282
 
7 7u83 283
    return((length < nstring_length(nstring1)) &&
284
	   (memcmp((GenericP)contents1, (GenericP)contents2,
285
		   (SizeT)length) == 0));
2 7u83 286
}
287
 
288
void
7 7u83 289
nstring_destroy(NStringP nstring)
2 7u83 290
{
7 7u83 291
    DEALLOCATE(nstring->contents);
2 7u83 292
}
293
 
294
void
7 7u83 295
write_nstring(OStreamP ostream, NStringP nstring)
2 7u83 296
{
7 7u83 297
    unsigned length = nstring_length(nstring);
2 7u83 298
 
299
    if (length > 0) {
7 7u83 300
	write_chars(ostream, nstring->contents, length);
2 7u83 301
    }
302
}
303
 
304
/*** Functions for manipulating dstrings.
305
**/
306
 
307
void
7 7u83 308
dstring_init(DStringP dstring)
2 7u83 309
{
310
    dstring->length     = 0;
311
    dstring->max_length = DSTRING_CHUNK_SIZE;
7 7u83 312
    dstring->contents   = ALLOCATE_VECTOR(char, dstring->max_length);
2 7u83 313
}
314
 
315
#ifdef FS_FAST
316
#undef dstring_length
317
#endif /* defined (FS_FAST) */
318
unsigned
7 7u83 319
dstring_length(DStringP dstring)
2 7u83 320
{
7 7u83 321
    return(dstring->length);
2 7u83 322
}
323
#ifdef FS_FAST
7 7u83 324
#define dstring_length(s)	((s)->length)
2 7u83 325
#endif /* defined (FS_FAST) */
326
 
327
void
7 7u83 328
dstring_append_char(DStringP dstring, char c)
2 7u83 329
{
330
    if ((dstring->length) >= (dstring->max_length)) {
331
	CStringP tmp;
332
 
333
	dstring->max_length += DSTRING_CHUNK_SIZE;
7 7u83 334
	tmp                  = ALLOCATE_VECTOR(char, dstring->max_length);
335
	(void)memcpy((GenericP)tmp, (GenericP)(dstring->contents),
336
		     (SizeT)(dstring->length));
337
	DEALLOCATE(dstring->contents);
2 7u83 338
	dstring->contents = tmp;
339
    }
7 7u83 340
    dstring->contents[dstring->length++] = c;
2 7u83 341
}
342
 
343
void
7 7u83 344
dstring_append_cstring(DStringP dstring, CStringP cstring)
2 7u83 345
{
7 7u83 346
    unsigned clength = cstring_length(cstring);
2 7u83 347
    unsigned length  = (clength + (dstring->length));
348
 
349
    if (length > (dstring->max_length)) {
350
	CStringP tmp;
351
 
352
	while ((dstring->max_length) < length) {
353
	    dstring->max_length += DSTRING_CHUNK_SIZE;
354
	}
7 7u83 355
	tmp = ALLOCATE_VECTOR(char, dstring->max_length);
356
	(void)memcpy((GenericP)tmp, (GenericP)(dstring->contents),
357
		     (SizeT)(dstring->length));
358
	DEALLOCATE(dstring->contents);
2 7u83 359
	dstring->contents = tmp;
360
    }
7 7u83 361
  (void)memcpy((GenericP) & (dstring->contents[dstring->length]),
362
	       (GenericP)cstring, (SizeT)clength);
2 7u83 363
    dstring->length = length;
364
}
365
 
366
void
7 7u83 367
dstring_append_nstring(DStringP dstring, NStringP nstring)
2 7u83 368
{
7 7u83 369
    unsigned nlength = nstring_length(nstring);
2 7u83 370
    unsigned length  = (nlength + (dstring->length));
371
 
372
    if (length > (dstring->max_length)) {
373
	CStringP tmp;
374
 
375
	while ((dstring->max_length) < length) {
376
	    dstring->max_length += DSTRING_CHUNK_SIZE;
377
	}
7 7u83 378
	tmp = ALLOCATE_VECTOR(char, dstring->max_length);
379
	(void)memcpy((GenericP)tmp, (GenericP)(dstring->contents),
380
		     (SizeT)(dstring->length));
381
	DEALLOCATE(dstring->contents);
2 7u83 382
	dstring->contents = tmp;
383
    }
7 7u83 384
  (void)memcpy((GenericP) & (dstring->contents[dstring->length]),
385
	       (GenericP)nstring_contents(nstring), (SizeT)nlength);
2 7u83 386
    dstring->length = length;
387
}
388
 
389
BoolT
7 7u83 390
dstring_last_char_equal(DStringP dstring, char c)
2 7u83 391
{
7 7u83 392
    return((dstring->length) &&
393
	   ((dstring->contents[dstring->length - 1]) == c));
2 7u83 394
}
395
 
396
void
7 7u83 397
dstring_to_nstring(DStringP dstring, NStringP nstring)
2 7u83 398
{
399
    if (dstring->length > 0) {
400
	nstring->length   = (dstring->length);
7 7u83 401
	nstring->contents = ALLOCATE_VECTOR(char, dstring->length);
402
	(void)memcpy((GenericP)(nstring->contents),
403
		     (GenericP)(dstring->contents),
404
		     (SizeT)(dstring->length));
2 7u83 405
    } else {
406
	nstring->length   = 0;
7 7u83 407
	nstring->contents = NIL(CStringP);
2 7u83 408
    }
409
}
410
 
411
CStringP
7 7u83 412
dstring_to_cstring(DStringP dstring)
2 7u83 413
{
7 7u83 414
    CStringP tmp = ALLOCATE_VECTOR(char, dstring->length + 1);
2 7u83 415
 
416
    if (dstring->length > 0) {
7 7u83 417
	(void)memcpy((GenericP)tmp, (GenericP)(dstring->contents),
418
		     (SizeT)(dstring->length));
2 7u83 419
    }
7 7u83 420
    tmp[dstring->length] = '\0';
421
    return(tmp);
2 7u83 422
}
423
 
424
CStringP
7 7u83 425
dstring_destroy_to_cstring(DStringP dstring)
2 7u83 426
{
427
    CStringP tmp;
428
 
429
    if ((dstring->length) >= (dstring->max_length)) {
7 7u83 430
	tmp = ALLOCATE_VECTOR(char, (dstring->length) + 1);
431
	(void)memcpy((GenericP)tmp, (GenericP)(dstring->contents),
432
		     (SizeT)(dstring->length));
433
	DEALLOCATE(dstring->contents);
2 7u83 434
    } else {
435
	tmp = (dstring->contents);
436
    }
7 7u83 437
    tmp[dstring->length] = '\0';
2 7u83 438
    dstring->length       = 0;
439
    dstring->max_length   = 0;
7 7u83 440
    dstring->contents     = NIL(CStringP);
441
    return(tmp);
2 7u83 442
}
443
 
444
void
7 7u83 445
dstring_destroy(DStringP dstring)
2 7u83 446
{
7 7u83 447
    DEALLOCATE(dstring->contents);
2 7u83 448
}
449
 
450
/*
451
 * Local variables(smf):
452
 * eval: (include::add-path-entry "../os-interface" "../generated")
453
 * end:
454
**/