Subversion Repositories tendra.SVN

Rev

Rev 5 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
6 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
6 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:-
6 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
6 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;
6 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;
6 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.1.1.1  1998/01/17  15:57:17  release
73
 * First version to be checked into rolling release.
74
 *
75
 * Revision 1.2  1994/12/12  11:45:24  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:06:09  smf
80
 * Initial import of os-interface shared files.
81
 *
82
**/
83
 
84
/****************************************************************************/
85
 
6 7u83 86
#include <string.h>
87
 
2 7u83 88
#include "cstring.h"
89
#include "syntax.h"
90
 
91
/*--------------------------------------------------------------------------*/
92
 
93
CStringP
6 7u83 94
cstring_duplicate(CStringP cstring)
2 7u83 95
{
6 7u83 96
    unsigned length = cstring_length(cstring);
97
    CStringP tmp    = ALLOCATE_VECTOR(char, length + 1);
2 7u83 98
 
6 7u83 99
   (void)strcpy(tmp, cstring);
100
    return(tmp);
2 7u83 101
}
102
 
103
CStringP
6 7u83 104
cstring_duplicate_prefix(CStringP cstring,				  unsigned prefix)
2 7u83 105
{
6 7u83 106
    unsigned length = cstring_length(cstring);
2 7u83 107
 
108
    if (length <= prefix) {
6 7u83 109
	CStringP tmp = ALLOCATE_VECTOR(char, length + 1);
2 7u83 110
 
6 7u83 111
	(void)strcpy(tmp, cstring);
112
	return(tmp);
2 7u83 113
    } else {
6 7u83 114
	CStringP tmp = ALLOCATE_VECTOR(char, prefix + 1);
2 7u83 115
 
6 7u83 116
	(void)memcpy((GenericP)tmp,(GenericP)cstring,(SizeT)prefix);
117
	tmp[prefix] = '\0';
118
	return(tmp);
2 7u83 119
    }
120
}
121
 
122
unsigned
6 7u83 123
cstring_hash_value(CStringP cstring)
2 7u83 124
{
125
    unsigned value = 0;
126
 
127
    while (*cstring) {
6 7u83 128
	value += ((unsigned)(*cstring++));
2 7u83 129
    }
6 7u83 130
    return(value);
2 7u83 131
}
132
 
133
#ifdef FS_FAST
134
#undef cstring_length
135
#endif /* defined (FS_FAST) */
136
unsigned
6 7u83 137
cstring_length(CStringP cstring)
2 7u83 138
{
6 7u83 139
    return((unsigned)strlen(cstring));
2 7u83 140
}
141
#ifdef FS_FAST
6 7u83 142
#define cstring_length(s)	((unsigned)strlen(s))
2 7u83 143
#endif /* defined (FS_FAST) */
144
 
145
#ifdef FS_FAST
146
#undef cstring_equal
147
#endif /* defined (FS_FAST) */
148
BoolT
6 7u83 149
cstring_equal(CStringP cstring1,		       CStringP cstring2)
2 7u83 150
{
6 7u83 151
    return(strcmp(cstring1, cstring2) == 0);
2 7u83 152
}
153
#ifdef FS_FAST
6 7u83 154
#define cstring_equal(s1, s2)	(strcmp((s1), (s2)) == 0)
2 7u83 155
#endif /* defined (FS_FAST) */
156
 
157
BoolT
6 7u83 158
cstring_ci_equal(CStringP cstring1,			  CStringP cstring2)
2 7u83 159
{
160
    char c1;
161
    char c2;
162
 
163
    do {
6 7u83 164
	c1 = syntax_upcase(*cstring1++);
165
	c2 = syntax_upcase(*cstring2++);
2 7u83 166
    } while ((c1) && (c2) && (c1 == c2));
6 7u83 167
    return(c1 == c2);
2 7u83 168
}
169
 
170
BoolT
6 7u83 171
cstring_to_unsigned(CStringP  cstring,			     unsigned *num_ref)
2 7u83 172
{
173
    unsigned number = 0;
174
 
175
    if (*cstring == '\0') {
6 7u83 176
	return(FALSE);
2 7u83 177
    }
178
    do {
6 7u83 179
	int value = syntax_value(*cstring);
2 7u83 180
 
181
	if ((value == SYNTAX_NO_VALUE) || (value >= 10) ||
6 7u83 182
	   (((UINT_MAX - (unsigned)value) / (unsigned)10) < number)) {
183
	    return(FALSE);
2 7u83 184
	}
6 7u83 185
	number *= (unsigned)10;
186
	number += (unsigned)value;
187
    } while (*++cstring);
2 7u83 188
    *num_ref = number;
6 7u83 189
    return(TRUE);
2 7u83 190
}
191
 
192
#ifdef FS_FAST
193
#undef cstring_contains
194
#endif /* defined (FS_FAST) */
195
BoolT
6 7u83 196
cstring_contains(CStringP cstring,			  char     c)
2 7u83 197
{
6 7u83 198
    return(strchr(cstring, c) != NIL(CStringP));
2 7u83 199
}
200
#ifdef FS_FAST
6 7u83 201
#define cstring_contains(s, c)	(strchr((s), (c)) != NIL(CStringP))
2 7u83 202
#endif /* defined (FS_FAST) */
203
 
204
#ifdef FS_FAST
205
#undef cstring_find
206
#endif /* defined (FS_FAST) */
207
CStringP
6 7u83 208
cstring_find(CStringP cstring,		      char     c)
2 7u83 209
{
6 7u83 210
    return(strchr(cstring, c));
2 7u83 211
}
212
#ifdef FS_FAST
6 7u83 213
#define cstring_find(s, c)	(strchr((s), (c)))
2 7u83 214
#endif /* defined (FS_FAST) */
215
 
216
#ifdef FS_FAST
217
#undef cstring_find_reverse
218
#endif /* defined (FS_FAST) */
219
CStringP
6 7u83 220
cstring_find_reverse(CStringP cstring,			      char     c)
2 7u83 221
{
6 7u83 222
    return(strrchr(cstring, c));
2 7u83 223
}
224
#ifdef FS_FAST
6 7u83 225
#define cstring_find_reverse(s, c)	(strrchr((s), (c)))
2 7u83 226
#endif /* defined (FS_FAST) */