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
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
/**** cstring.c --- C string manipulation.
32
 *
33
 ** Author: Steve Folkes <smf@hermes.mod.uk>
34
 *
35
 **** Commentary:
36
 *
37
 * This file implements the C string manipulation facility specified in the
38
 * file "cstring.h".  See that file for more details.
39
 *
40
 **** Change Log:
41
 * $Log: cstring.c,v $
42
 * Revision 1.1.1.1  1998/01/17  15:57:17  release
43
 * First version to be checked into rolling release.
44
 *
45
 * Revision 1.2  1994/12/12  11:45:24  smf
46
 * Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
47
 * OSSG C Coding Standards.
48
 *
49
 * Revision 1.1.1.1  1994/07/25  16:06:09  smf
50
 * Initial import of os-interface shared files.
51
 *
52
**/
53
 
54
/****************************************************************************/
55
 
56
#include "cstring.h"
57
#include "syntax.h"
58
 
59
/*--------------------------------------------------------------------------*/
60
 
61
CStringP
62
cstring_duplicate PROTO_N ((cstring))
63
		  PROTO_T (CStringP cstring)
64
{
65
    unsigned length = cstring_length (cstring);
66
    CStringP tmp    = ALLOCATE_VECTOR (char, length + 1);
67
 
68
    (void) strcpy (tmp, cstring);
69
    return (tmp);
70
}
71
 
72
CStringP
73
cstring_duplicate_prefix PROTO_N ((cstring, prefix))
74
			 PROTO_T (CStringP cstring X
75
				  unsigned prefix)
76
{
77
    unsigned length = cstring_length (cstring);
78
 
79
    if (length <= prefix) {
80
	CStringP tmp = ALLOCATE_VECTOR (char, length + 1);
81
 
82
	(void) strcpy (tmp, cstring);
83
	return (tmp);
84
    } else {
85
	CStringP tmp = ALLOCATE_VECTOR (char, prefix + 1);
86
 
87
	(void) memcpy ((GenericP) tmp, (GenericP) cstring, (SizeT) prefix);
88
	tmp [prefix] = '\0';
89
	return (tmp);
90
    }
91
}
92
 
93
unsigned
94
cstring_hash_value PROTO_N ((cstring))
95
		   PROTO_T (CStringP cstring)
96
{
97
    unsigned value = 0;
98
 
99
    while (*cstring) {
100
	value += ((unsigned) (*cstring ++));
101
    }
102
    return (value);
103
}
104
 
105
#ifdef FS_FAST
106
#undef cstring_length
107
#endif /* defined (FS_FAST) */
108
unsigned
109
cstring_length PROTO_N ((cstring))
110
	       PROTO_T (CStringP cstring)
111
{
112
    return ((unsigned) strlen (cstring));
113
}
114
#ifdef FS_FAST
115
#define cstring_length(s) ((unsigned) strlen (s))
116
#endif /* defined (FS_FAST) */
117
 
118
#ifdef FS_FAST
119
#undef cstring_equal
120
#endif /* defined (FS_FAST) */
121
BoolT
122
cstring_equal PROTO_N ((cstring1, cstring2))
123
	      PROTO_T (CStringP cstring1 X
124
		       CStringP cstring2)
125
{
126
    return (strcmp (cstring1, cstring2) == 0);
127
}
128
#ifdef FS_FAST
129
#define cstring_equal(s1, s2) (strcmp ((s1), (s2)) == 0)
130
#endif /* defined (FS_FAST) */
131
 
132
BoolT
133
cstring_ci_equal PROTO_N ((cstring1, cstring2))
134
		 PROTO_T (CStringP cstring1 X
135
			  CStringP cstring2)
136
{
137
    char c1;
138
    char c2;
139
 
140
    do {
141
	c1 = syntax_upcase (*cstring1 ++);
142
	c2 = syntax_upcase (*cstring2 ++);
143
    } while ((c1) && (c2) && (c1 == c2));
144
    return (c1 == c2);
145
}
146
 
147
BoolT
148
cstring_to_unsigned PROTO_N ((cstring, num_ref))
149
		    PROTO_T (CStringP  cstring X
150
			     unsigned *num_ref)
151
{
152
    unsigned number = 0;
153
 
154
    if (*cstring == '\0') {
155
	return (FALSE);
156
    }
157
    do {
158
	int value = syntax_value (*cstring);
159
 
160
	if ((value == SYNTAX_NO_VALUE) || (value >= 10) ||
161
	    (((UINT_MAX - (unsigned) value) / (unsigned) 10) < number)) {
162
	    return (FALSE);
163
	}
164
	number *= (unsigned) 10;
165
	number += (unsigned) value;
166
    } while (*++ cstring);
167
    *num_ref = number;
168
    return (TRUE);
169
}
170
 
171
#ifdef FS_FAST
172
#undef cstring_contains
173
#endif /* defined (FS_FAST) */
174
BoolT
175
cstring_contains PROTO_N ((cstring, c))
176
		 PROTO_T (CStringP cstring X
177
			  char     c)
178
{
179
    return (strchr (cstring, c) != NIL (CStringP));
180
}
181
#ifdef FS_FAST
182
#define cstring_contains(s, c) (strchr ((s), (c)) != NIL (CStringP))
183
#endif /* defined (FS_FAST) */
184
 
185
#ifdef FS_FAST
186
#undef cstring_find
187
#endif /* defined (FS_FAST) */
188
CStringP
189
cstring_find PROTO_N ((cstring, c))
190
	     PROTO_T (CStringP cstring X
191
		      char     c)
192
{
193
    return (strchr (cstring, c));
194
}
195
#ifdef FS_FAST
196
#define cstring_find(s, c) (strchr ((s), (c)))
197
#endif /* defined (FS_FAST) */
198
 
199
#ifdef FS_FAST
200
#undef cstring_find_reverse
201
#endif /* defined (FS_FAST) */
202
CStringP
203
cstring_find_reverse PROTO_N ((cstring, c))
204
		     PROTO_T (CStringP cstring X
205
			      char     c)
206
{
207
    return (strrchr (cstring, c));
208
}
209
#ifdef FS_FAST
210
#define cstring_find_reverse(s, c) (strrchr ((s), (c)))
211
#endif /* defined (FS_FAST) */