Subversion Repositories tendra.SVN

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 7
Line -... Line 1...
-
 
1
/*
-
 
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
 */
1
/*
31
/*
2
    		 Crown Copyright (c) 1997
32
    		 Crown Copyright (c) 1997
3
    
33
 
4
    This TenDRA(r) Computer Program is subject to Copyright
34
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
35
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
36
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
37
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
38
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
39
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
40
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
41
    shall be deemed to be acceptance of the following conditions:-
12
    
42
 
13
        (1) Its Recipients shall ensure that this Notice is
43
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
44
        reproduced upon any copies or amended versions of it;
15
    
45
 
16
        (2) Any amended version of it shall be clearly marked to
46
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
47
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
48
        for the relevant amendment or amendments;
19
    
49
 
20
        (3) Its onward transfer from a recipient to another
50
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
51
        party shall be deemed to be that party's acceptance of
22
        these conditions;
52
        these conditions;
23
    
53
 
24
        (4) DERA gives no warranty or assurance as to its
54
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
55
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
56
        no liability whatsoever in relation to any use to which
27
        it may be put.
57
        it may be put.
28
*/
58
*/
Line 63... Line 93...
63
#include "syntax.h"
93
#include "syntax.h"
64
 
94
 
65
/*--------------------------------------------------------------------------*/
95
/*--------------------------------------------------------------------------*/
66
 
96
 
67
CStringP
97
CStringP
68
cstring_duplicate PROTO_N ((cstring))
98
cstring_duplicate(CStringP cstring)
69
		  PROTO_T (CStringP cstring)
-
 
70
{
99
{
71
    unsigned length = cstring_length (cstring);
100
    unsigned length = cstring_length(cstring);
72
    CStringP tmp    = ALLOCATE_VECTOR (char, length + 1);
101
    CStringP tmp    = ALLOCATE_VECTOR(char, length + 1);
73
 
102
 
74
    (void) strcpy (tmp, cstring);
103
  (void)strcpy(tmp, cstring);
75
    return (tmp);
104
    return(tmp);
76
}
105
}
77
 
106
 
78
CStringP
107
CStringP
79
cstring_duplicate_prefix PROTO_N ((cstring, prefix))
108
cstring_duplicate_prefix(CStringP cstring, unsigned prefix)
80
			 PROTO_T (CStringP cstring X
-
 
81
				  unsigned prefix)
-
 
82
{
109
{
83
    unsigned length = cstring_length (cstring);
110
    unsigned length = cstring_length(cstring);
84
 
111
 
85
    if (length <= prefix) {
112
    if (length <= prefix) {
86
	CStringP tmp = ALLOCATE_VECTOR (char, length + 1);
113
	CStringP tmp = ALLOCATE_VECTOR(char, length + 1);
87
 
114
 
88
	(void) strcpy (tmp, cstring);
115
	(void)strcpy(tmp, cstring);
89
	return (tmp);
116
	return(tmp);
90
    } else {
117
    } else {
91
	CStringP tmp = ALLOCATE_VECTOR (char, prefix + 1);
118
	CStringP tmp = ALLOCATE_VECTOR(char, prefix + 1);
92
 
119
 
93
	(void) memcpy ((GenericP) tmp, (GenericP) cstring, (SizeT) prefix);
120
	(void)memcpy((GenericP)tmp, (GenericP)cstring, (SizeT)prefix);
94
	tmp [prefix] = '\0';
121
	tmp[prefix] = '\0';
95
	return (tmp);
122
	return(tmp);
96
    }
123
    }
97
}
124
}
98
 
125
 
99
unsigned
126
unsigned
100
cstring_hash_value PROTO_N ((cstring))
127
cstring_hash_value(CStringP cstring)
101
		   PROTO_T (CStringP cstring)
-
 
102
{
128
{
103
    unsigned value = 0;
129
    unsigned value = 0;
104
 
130
 
105
    while (*cstring) {
131
    while (*cstring) {
106
	value += ((unsigned) (*cstring ++));
132
	value += ((unsigned)(*cstring++));
107
    }
133
    }
108
    return (value);
134
    return(value);
109
}
135
}
110
 
136
 
111
#ifdef FS_FAST
137
#ifdef FS_FAST
112
#undef cstring_length
138
#undef cstring_length
113
#endif /* defined (FS_FAST) */
139
#endif /* defined (FS_FAST) */
114
unsigned
140
unsigned
115
cstring_length PROTO_N ((cstring))
141
cstring_length(CStringP cstring)
116
	       PROTO_T (CStringP cstring)
-
 
117
{
142
{
118
    return ((unsigned) strlen (cstring));
143
    return((unsigned)strlen(cstring));
119
}
144
}
120
#ifdef FS_FAST
145
#ifdef FS_FAST
121
#define cstring_length(s) ((unsigned) strlen (s))
146
#define cstring_length(s)	((unsigned)strlen(s))
122
#endif /* defined (FS_FAST) */
147
#endif /* defined (FS_FAST) */
123
 
148
 
124
#ifdef FS_FAST
149
#ifdef FS_FAST
125
#undef cstring_equal
150
#undef cstring_equal
126
#endif /* defined (FS_FAST) */
151
#endif /* defined (FS_FAST) */
127
BoolT
152
BoolT
128
cstring_equal PROTO_N ((cstring1, cstring2))
153
cstring_equal(CStringP cstring1, CStringP cstring2)
129
	      PROTO_T (CStringP cstring1 X
-
 
130
		       CStringP cstring2)
-
 
131
{
154
{
132
    return (strcmp (cstring1, cstring2) == 0);
155
    return(strcmp(cstring1, cstring2) == 0);
133
}
156
}
134
#ifdef FS_FAST
157
#ifdef FS_FAST
135
#define cstring_equal(s1, s2) (strcmp ((s1), (s2)) == 0)
158
#define cstring_equal(s1, s2)	(strcmp((s1), (s2)) == 0)
136
#endif /* defined (FS_FAST) */
159
#endif /* defined (FS_FAST) */
137
 
160
 
138
BoolT
161
BoolT
139
cstring_ci_equal PROTO_N ((cstring1, cstring2))
162
cstring_ci_equal(CStringP cstring1, CStringP cstring2)
140
		 PROTO_T (CStringP cstring1 X
-
 
141
			  CStringP cstring2)
-
 
142
{
163
{
143
    char c1;
164
    char c1;
144
    char c2;
165
    char c2;
145
 
166
 
146
    do {
167
    do {
147
	c1 = syntax_upcase (*cstring1 ++);
168
	c1 = syntax_upcase(*cstring1++);
148
	c2 = syntax_upcase (*cstring2 ++);
169
	c2 = syntax_upcase(*cstring2++);
149
    } while ((c1) && (c2) && (c1 == c2));
170
    } while ((c1) && (c2) && (c1 == c2));
150
    return (c1 == c2);
171
    return(c1 == c2);
151
}
172
}
152
 
173
 
153
BoolT
174
BoolT
154
cstring_to_unsigned PROTO_N ((cstring, num_ref))
175
cstring_to_unsigned(CStringP cstring, unsigned *num_ref)
155
		    PROTO_T (CStringP  cstring X
-
 
156
			     unsigned *num_ref)
-
 
157
{
176
{
158
    unsigned number = 0;
177
    unsigned number = 0;
159
 
178
 
160
    if (*cstring == '\0') {
179
    if (*cstring == '\0') {
161
	return (FALSE);
180
	return(FALSE);
162
    }
181
    }
163
    do {
182
    do {
164
	int value = syntax_value (*cstring);
183
	int value = syntax_value(*cstring);
165
 
184
 
166
	if ((value == SYNTAX_NO_VALUE) || (value >= 10) ||
185
	if ((value == SYNTAX_NO_VALUE) || (value >= 10) ||
167
	    (((UINT_MAX - (unsigned) value) / (unsigned) 10) < number)) {
186
	    (((UINT_MAX - (unsigned)value) / (unsigned)10) < number)) {
168
	    return (FALSE);
187
	    return(FALSE);
169
	}
188
	}
170
	number *= (unsigned) 10;
189
	number *= (unsigned)10;
171
	number += (unsigned) value;
190
	number += (unsigned)value;
172
    } while (*++ cstring);
191
    } while (*++ cstring);
173
    *num_ref = number;
192
    *num_ref = number;
174
    return (TRUE);
193
    return(TRUE);
175
}
194
}
176
 
195
 
177
BoolT
196
BoolT
178
cstring_starts PROTO_N ((cstring, s))
197
cstring_starts(CStringP cstring, CStringP s)
179
	       PROTO_T (CStringP cstring X
-
 
180
			CStringP s)
-
 
181
{
198
{
182
    return (strncmp (cstring, s, strlen (s)) == 0);
199
    return(strncmp(cstring, s, strlen(s)) == 0);
183
}
200
}
184
 
201
 
185
#ifdef FS_FAST
202
#ifdef FS_FAST
186
#undef cstring_contains
203
#undef cstring_contains
187
#endif /* defined (FS_FAST) */
204
#endif /* defined (FS_FAST) */
188
BoolT
205
BoolT
189
cstring_contains PROTO_N ((cstring, c))
206
cstring_contains(CStringP cstring, char c)
190
		 PROTO_T (CStringP cstring X
-
 
191
			  char     c)
-
 
192
{
207
{
193
    return (strchr (cstring, c) != NIL (CStringP));
208
    return(strchr(cstring, c) != NIL(CStringP));
194
}
209
}
195
#ifdef FS_FAST
210
#ifdef FS_FAST
196
#define cstring_contains(s, c) (strchr ((s), (c)) != NIL (CStringP))
211
#define cstring_contains(s, c)	(strchr((s), (c)) != NIL(CStringP))
197
#endif /* defined (FS_FAST) */
212
#endif /* defined (FS_FAST) */
198
 
213
 
199
#ifdef FS_FAST
214
#ifdef FS_FAST
200
#undef cstring_find
215
#undef cstring_find
201
#endif /* defined (FS_FAST) */
216
#endif /* defined (FS_FAST) */
202
CStringP
217
CStringP
203
cstring_find PROTO_N ((cstring, c))
218
cstring_find(CStringP cstring, char c)
204
	     PROTO_T (CStringP cstring X
-
 
205
		      char     c)
-
 
206
{
219
{
207
    return (strchr (cstring, c));
220
    return(strchr(cstring, c));
208
}
221
}
209
#ifdef FS_FAST
222
#ifdef FS_FAST
210
#define cstring_find(s, c) (strchr ((s), (c)))
223
#define cstring_find(s, c)	(strchr((s), (c)))
211
#endif /* defined (FS_FAST) */
224
#endif /* defined (FS_FAST) */
212
 
225
 
213
#ifdef FS_FAST
226
#ifdef FS_FAST
214
#undef cstring_find_reverse
227
#undef cstring_find_reverse
215
#endif /* defined (FS_FAST) */
228
#endif /* defined (FS_FAST) */
216
CStringP
229
CStringP
217
cstring_find_reverse PROTO_N ((cstring, c))
230
cstring_find_reverse(CStringP cstring, char c)
218
		     PROTO_T (CStringP cstring X
-
 
219
			      char     c)
-
 
220
{
231
{
221
    return (strrchr (cstring, c));
232
    return(strrchr(cstring, c));
222
}
233
}
223
#ifdef FS_FAST
234
#ifdef FS_FAST
224
#define cstring_find_reverse(s, c) (strrchr ((s), (c)))
235
#define cstring_find_reverse(s, c)	(strrchr((s), (c)))
225
#endif /* defined (FS_FAST) */
236
#endif /* defined (FS_FAST) */
226
 
237
 
227
CStringP
238
CStringP
228
cstring_find_basename PROTO_N ((cstring))
239
cstring_find_basename(CStringP cstring)
229
		      PROTO_T (CStringP cstring)
-
 
230
{
240
{
231
    CStringP bstring = cstring_find_reverse (cstring, '/');
241
    CStringP bstring = cstring_find_reverse(cstring, '/');
232
    if (bstring != NIL (CStringP)) {
242
    if (bstring != NIL(CStringP)) {
233
	cstring = bstring + 1;
243
	cstring = bstring + 1;
234
    }
244
    }
235
    return (cstring);
245
    return(cstring);
236
}
246
}