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
/**** cstring.c --- C string manipulation.
62
 *
63
 ** Author: Steve Folkes <smf@hermes.mod.uk>
64
 *
65
 **** Commentary:
66
 *
67
 * This file implements the C string manipulation facility specified in the
68
 * file "cstring.h".  See that file for more details.
69
 *
70
 **** Change Log:
71
 * $Log: cstring.c,v $
72
 * Revision 1.3  1998/03/17  11:38:15  release
73
 * Missing ';'.
74
 *
75
 * Revision 1.2  1998/03/16  11:26:34  release
76
 * Modifications prior to version 4.1.2.
77
 *
78
 * Revision 1.1.1.1  1998/01/17  15:57:45  release
79
 * First version to be checked into rolling release.
80
 *
81
 * Revision 1.2  1994/12/12  11:45:24  smf
82
 * Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
83
 * OSSG C Coding Standards.
84
 *
85
 * Revision 1.1.1.1  1994/07/25  16:06:09  smf
86
 * Initial import of os-interface shared files.
87
 *
88
**/
89
 
90
/****************************************************************************/
91
 
92
#include "cstring.h"
93
#include "syntax.h"
94
 
95
/*--------------------------------------------------------------------------*/
96
 
97
CStringP
7 7u83 98
cstring_duplicate(CStringP cstring)
2 7u83 99
{
7 7u83 100
    unsigned length = cstring_length(cstring);
101
    CStringP tmp    = ALLOCATE_VECTOR(char, length + 1);
2 7u83 102
 
7 7u83 103
  (void)strcpy(tmp, cstring);
104
    return(tmp);
2 7u83 105
}
106
 
107
CStringP
7 7u83 108
cstring_duplicate_prefix(CStringP cstring, unsigned prefix)
2 7u83 109
{
7 7u83 110
    unsigned length = cstring_length(cstring);
2 7u83 111
 
112
    if (length <= prefix) {
7 7u83 113
	CStringP tmp = ALLOCATE_VECTOR(char, length + 1);
2 7u83 114
 
7 7u83 115
	(void)strcpy(tmp, cstring);
116
	return(tmp);
2 7u83 117
    } else {
7 7u83 118
	CStringP tmp = ALLOCATE_VECTOR(char, prefix + 1);
2 7u83 119
 
7 7u83 120
	(void)memcpy((GenericP)tmp, (GenericP)cstring, (SizeT)prefix);
121
	tmp[prefix] = '\0';
122
	return(tmp);
2 7u83 123
    }
124
}
125
 
126
unsigned
7 7u83 127
cstring_hash_value(CStringP cstring)
2 7u83 128
{
129
    unsigned value = 0;
130
 
131
    while (*cstring) {
7 7u83 132
	value += ((unsigned)(*cstring++));
2 7u83 133
    }
7 7u83 134
    return(value);
2 7u83 135
}
136
 
137
#ifdef FS_FAST
138
#undef cstring_length
139
#endif /* defined (FS_FAST) */
140
unsigned
7 7u83 141
cstring_length(CStringP cstring)
2 7u83 142
{
7 7u83 143
    return((unsigned)strlen(cstring));
2 7u83 144
}
145
#ifdef FS_FAST
7 7u83 146
#define cstring_length(s)	((unsigned)strlen(s))
2 7u83 147
#endif /* defined (FS_FAST) */
148
 
149
#ifdef FS_FAST
150
#undef cstring_equal
151
#endif /* defined (FS_FAST) */
152
BoolT
7 7u83 153
cstring_equal(CStringP cstring1, CStringP cstring2)
2 7u83 154
{
7 7u83 155
    return(strcmp(cstring1, cstring2) == 0);
2 7u83 156
}
157
#ifdef FS_FAST
7 7u83 158
#define cstring_equal(s1, s2)	(strcmp((s1), (s2)) == 0)
2 7u83 159
#endif /* defined (FS_FAST) */
160
 
161
BoolT
7 7u83 162
cstring_ci_equal(CStringP cstring1, CStringP cstring2)
2 7u83 163
{
164
    char c1;
165
    char c2;
166
 
167
    do {
7 7u83 168
	c1 = syntax_upcase(*cstring1++);
169
	c2 = syntax_upcase(*cstring2++);
2 7u83 170
    } while ((c1) && (c2) && (c1 == c2));
7 7u83 171
    return(c1 == c2);
2 7u83 172
}
173
 
174
BoolT
7 7u83 175
cstring_to_unsigned(CStringP cstring, unsigned *num_ref)
2 7u83 176
{
177
    unsigned number = 0;
178
 
179
    if (*cstring == '\0') {
7 7u83 180
	return(FALSE);
2 7u83 181
    }
182
    do {
7 7u83 183
	int value = syntax_value(*cstring);
2 7u83 184
 
185
	if ((value == SYNTAX_NO_VALUE) || (value >= 10) ||
7 7u83 186
	    (((UINT_MAX - (unsigned)value) / (unsigned)10) < number)) {
187
	    return(FALSE);
2 7u83 188
	}
7 7u83 189
	number *= (unsigned)10;
190
	number += (unsigned)value;
2 7u83 191
    } while (*++ cstring);
192
    *num_ref = number;
7 7u83 193
    return(TRUE);
2 7u83 194
}
195
 
196
BoolT
7 7u83 197
cstring_starts(CStringP cstring, CStringP s)
2 7u83 198
{
7 7u83 199
    return(strncmp(cstring, s, strlen(s)) == 0);
2 7u83 200
}
201
 
202
#ifdef FS_FAST
203
#undef cstring_contains
204
#endif /* defined (FS_FAST) */
205
BoolT
7 7u83 206
cstring_contains(CStringP cstring, char c)
2 7u83 207
{
7 7u83 208
    return(strchr(cstring, c) != NIL(CStringP));
2 7u83 209
}
210
#ifdef FS_FAST
7 7u83 211
#define cstring_contains(s, c)	(strchr((s), (c)) != NIL(CStringP))
2 7u83 212
#endif /* defined (FS_FAST) */
213
 
214
#ifdef FS_FAST
215
#undef cstring_find
216
#endif /* defined (FS_FAST) */
217
CStringP
7 7u83 218
cstring_find(CStringP cstring, char c)
2 7u83 219
{
7 7u83 220
    return(strchr(cstring, c));
2 7u83 221
}
222
#ifdef FS_FAST
7 7u83 223
#define cstring_find(s, c)	(strchr((s), (c)))
2 7u83 224
#endif /* defined (FS_FAST) */
225
 
226
#ifdef FS_FAST
227
#undef cstring_find_reverse
228
#endif /* defined (FS_FAST) */
229
CStringP
7 7u83 230
cstring_find_reverse(CStringP cstring, char c)
2 7u83 231
{
7 7u83 232
    return(strrchr(cstring, c));
2 7u83 233
}
234
#ifdef FS_FAST
7 7u83 235
#define cstring_find_reverse(s, c)	(strrchr((s), (c)))
2 7u83 236
#endif /* defined (FS_FAST) */
237
 
238
CStringP
7 7u83 239
cstring_find_basename(CStringP cstring)
2 7u83 240
{
7 7u83 241
    CStringP bstring = cstring_find_reverse(cstring, '/');
242
    if (bstring != NIL(CStringP)) {
2 7u83 243
	cstring = bstring + 1;
244
    }
7 7u83 245
    return(cstring);
2 7u83 246
}