Subversion Repositories tendra.SVN

Rev

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

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