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-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 55... Line 85...
55
#include "key.h"
85
#include "key.h"
56
 
86
 
57
/*--------------------------------------------------------------------------*/
87
/*--------------------------------------------------------------------------*/
58
 
88
 
59
void
89
void
60
key_init_from_string PROTO_N ((key, string, number))
90
key_init_from_string(KeyP key, NStringP string, unsigned number)
61
		     PROTO_T (KeyP     key X
-
 
62
			      NStringP string X
-
 
63
			      unsigned number)
-
 
64
{
91
{
65
    key->type   = KT_STRING;
92
    key->type   = KT_STRING;
66
    nstring_assign (&(key->string), string);
93
    nstring_assign(&(key->string), string);
67
    key->number = number;
94
    key->number = number;
68
}
95
}
69
 
96
 
70
void
97
void
71
key_init_from_number PROTO_N ((key, number))
98
key_init_from_number(KeyP key, unsigned number)
72
		     PROTO_T (KeyP     key X
-
 
73
			      unsigned number)
-
 
74
{
99
{
75
    key->type   = KT_NUMERIC;
100
    key->type   = KT_NUMERIC;
76
    key->number = number;
101
    key->number = number;
77
}
102
}
78
 
103
 
79
CmpT
104
CmpT
80
key_compare PROTO_N ((key1, key2))
105
key_compare(KeyP key1, KeyP key2)
81
	    PROTO_T (KeyP key1 X
-
 
82
		     KeyP key2)
-
 
83
{
106
{
84
    if ((key1->type) < (key2->type)) {
107
    if ((key1->type) < (key2->type)) {
85
	return (CMP_LT);
108
	return(CMP_LT);
86
    } else if ((key1->type) > (key2->type)) {
109
    } else if ((key1->type) > (key2->type)) {
87
	return (CMP_GT);
110
	return(CMP_GT);
88
    }
111
    }
89
    switch (key1->type) EXHAUSTIVE {
112
    switch (key1->type)EXHAUSTIVE {
90
      case KT_STRING:
113
      case KT_STRING:
91
	return (nstring_compare (&(key1->string), &(key2->string)));
114
	return(nstring_compare(&(key1->string), &(key2->string)));
92
      case KT_NUMERIC:
115
      case KT_NUMERIC:
93
	if (key1->number < key2->number) {
116
	if (key1->number < key2->number) {
94
	    return (CMP_LT);
117
	    return(CMP_LT);
95
	} else if (key1->number > key2->number) {
118
	} else if (key1->number > key2->number) {
96
	    return (CMP_GT);
119
	    return(CMP_GT);
97
	} else {
120
	} else {
98
	    return (CMP_EQ);
121
	    return(CMP_EQ);
99
	}
122
	}
100
    }
123
    }
101
    UNREACHED;
124
    UNREACHED;
102
}
125
}
103
 
126
 
104
#ifdef FS_FAST
127
#ifdef FS_FAST
105
#undef key_is_string
128
#undef key_is_string
106
#endif /* defined (FS_FAST) */
129
#endif /* defined (FS_FAST) */
107
BoolT
130
BoolT
108
key_is_string PROTO_N ((key))
131
key_is_string(KeyP key)
109
	      PROTO_T (KeyP key)
-
 
110
{
132
{
111
    return (key->type == KT_STRING);
133
    return(key->type == KT_STRING);
112
}
134
}
113
#ifdef FS_FAST
135
#ifdef FS_FAST
114
#define key_is_string(k) ((k)->type == KT_STRING)
136
#define key_is_string(k)	((k)->type == KT_STRING)
115
#endif /* defined (FS_FAST) */
137
#endif /* defined (FS_FAST) */
116
 
138
 
117
#ifdef FS_FAST
139
#ifdef FS_FAST
118
#undef key_get_string
140
#undef key_get_string
119
#endif /* defined (FS_FAST) */
141
#endif /* defined (FS_FAST) */
120
NStringP
142
NStringP
121
key_get_string PROTO_N ((key))
143
key_get_string(KeyP key)
122
	       PROTO_T (KeyP key)
-
 
123
{
144
{
124
    ASSERT (key->type == KT_STRING);
145
    ASSERT(key->type == KT_STRING);
125
    return (&(key->string));
146
    return(&(key->string));
126
}
147
}
127
#ifdef FS_FAST
148
#ifdef FS_FAST
128
#define key_get_string(k) (&((k)->string))
149
#define key_get_string(k)	(&((k)->string))
129
#endif /* defined (FS_FAST) */
150
#endif /* defined (FS_FAST) */
130
 
151
 
131
#ifdef FS_FAST
152
#ifdef FS_FAST
132
#undef key_get_number
153
#undef key_get_number
133
#endif /* defined (FS_FAST) */
154
#endif /* defined (FS_FAST) */
134
unsigned
155
unsigned
135
key_get_number PROTO_N ((key))
156
key_get_number(KeyP key)
136
	       PROTO_T (KeyP key)
-
 
137
{
157
{
138
    return (key->number);
158
    return(key->number);
139
}
159
}
140
#ifdef FS_FAST
160
#ifdef FS_FAST
141
#define key_get_number(k) ((k)->number)
161
#define key_get_number(k)	((k)->number)
142
#endif /* defined (FS_FAST) */
162
#endif /* defined (FS_FAST) */
143
 
163
 
144
unsigned
164
unsigned
145
key_hash_value PROTO_N ((key))
165
key_hash_value(KeyP key)
146
	       PROTO_T (KeyP key)
-
 
147
{
166
{
148
    switch (key->type) EXHAUSTIVE {
167
    switch (key->type)EXHAUSTIVE {
149
      case KT_NUMERIC:
168
      case KT_NUMERIC:
150
	return (key->number);
169
	return(key->number);
151
      case KT_STRING:
170
      case KT_STRING:
152
	return (nstring_hash_value (&(key->string)));
171
	return(nstring_hash_value(&(key->string)));
153
    }
172
    }
154
    UNREACHED;
173
    UNREACHED;
155
}
174
}
156
 
175
 
157
void
176
void
158
write_key PROTO_N ((ostream, key))
177
write_key(OStreamP ostream, KeyP key)
159
	  PROTO_T (OStreamP ostream X
-
 
160
		   KeyP     key)
-
 
161
{
178
{
162
    switch (key->type) EXHAUSTIVE {
179
    switch (key->type)EXHAUSTIVE {
163
      case KT_STRING:
180
      case KT_STRING:
164
	write_nstring (ostream, &(key->string));
181
	write_nstring(ostream, &(key->string));
165
	break;
182
	break;
166
      case KT_NUMERIC:
183
      case KT_NUMERIC:
167
	write_unsigned (ostream, key->number);
184
	write_unsigned(ostream, key->number);
168
	break;
185
	break;
169
    }
186
    }
170
}
187
}
171

188

172
/*
189
/*