Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – tendra.SVN – Blame – /branches/algol60/src/tools/tld/dstring.c – Rev 7

Subversion Repositories tendra.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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