[go: up one dir, main page]

Menu

[r990]: / mia2 / vistaio / dictionary.c  Maximize  Restore  History

Download this file

230 lines (201 with data), 5.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyrigh (C) 2004 Max-Planck-Institute of Cognitive Neurosience
*
* The origional VISTA library is copyrighted of University of British Columbia.
* Copyright © 1993, 1994 University of British Columbia.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* $Id: dictionary.c 51 2004-02-26 12:53:22Z jaenicke $ */
/*! \file dictionary.c
* \brief contains dictionaries that convert symbols used in V data files to values used internally
* \author Arthur Pope, UBC Laboratory for Computational Intelligentce
*/
#include "vistaio/vistaio.h"
/*
* Dictionaries for generic attributes and their values.
*/
/* Keywords for representing TRUE or FALSE: */
EXPORT_VISTA VDictEntry VBooleanDict[] = {
{"false", FALSE}
,
{"true", TRUE}
,
{"no", FALSE}
,
{"yes", TRUE}
,
{"off", FALSE}
,
{"on", TRUE}
,
{NULL}
};
/* Keywords for representing kinds of numeric representation: */
EXPORT_VISTA VDictEntry VNumericRepnDict[] = {
{"bit", VBitRepn}
,
{"double", VDoubleRepn}
,
{"float", VFloatRepn}
,
{"long", VLongRepn}
,
{"sbyte", VSByteRepn}
,
{"short", VShortRepn}
,
{"ubyte", VUByteRepn}
,
{NULL}
};
/*! \brief Look up an entry in an attribute value dictionary, by keyword.
*
* (It's assumed that dictionaries are pretty small -- a linear search
* is done.)
*
* \param dict
* \param keyword
* \return VDictEntry
*/
VDictEntry *VLookupDictKeyword (VDictEntry * dict, VStringConst keyword)
{
if (dict)
for (; dict->keyword; dict++)
if (strcmp (keyword, dict->keyword) == 0)
return dict;
return NULL;
}
/*! \brief Look up an entry in an attribute dictionary, by value.
*
* Calling sequence:
*
* VLookupDictValue (VDictEntry *dict, VRepnKind repn, xxx value)
*
* where xxx is a type that corresponds to repn.
*
* \param dict
* \param repn
* \return VDictEntry
*/
VDictEntry *VLookupDictValue (VDictEntry * dict, VRepnKind repn, ...)
{
va_list args;
VLong i_value = 0;
VDouble f_value = 0.0;
VString s_value = NULL;
VBoolean i_valid;
/* Unravel the arguments passed: */
if (!dict)
return NULL;
va_start (args, repn);
switch (repn) {
case VBitRepn:
i_value = va_arg (args, VBitPromoted);
break;
case VUByteRepn:
i_value = va_arg (args, VUBytePromoted);
break;
case VSByteRepn:
i_value = va_arg (args, VSBytePromoted);
break;
case VShortRepn:
i_value = va_arg (args, VShortPromoted);
break;
case VLongRepn:
i_value = va_arg (args, VLongPromoted);
break;
case VFloatRepn:
f_value = va_arg (args, VFloatPromoted);
break;
case VDoubleRepn:
f_value = va_arg (args, VDoublePromoted);
break;
case VBooleanRepn:
i_value = va_arg (args, VBooleanPromoted);
break;
case VStringRepn:
s_value = va_arg (args, VString);
break;
default:
VError ("VLookupDictValue: Can't lookup %s value",
VRepnName (repn));
}
va_end (args);
/* Search the dictionary by value: */
switch (repn) {
case VBitRepn:
case VUByteRepn:
case VSByteRepn:
case VShortRepn:
case VLongRepn:
case VBooleanRepn:
for (; dict->keyword; dict++) {
/* Is the entry's value only stored as a string? */
if (dict->svalue && !dict->icached) {
/* Yes -- try to convert the string to an integer, and
cache that value: */
if (!VDecodeAttrValue
(dict->svalue, NULL, VLongRepn,
&dict->ivalue))
break;
dict->icached = TRUE;
}
/* Test against the integer value stored in the entry: */
if (i_value == dict->ivalue)
return dict;
}
break;
case VFloatRepn:
case VDoubleRepn:
for (; dict->keyword; dict++) {
/* Does the entry include a cached floating point value? */
if (!dict->fcached) {
/* No -- obtain it from an integer or string value: */
if (dict->svalue) {
if (!VDecodeAttrValue
(dict->svalue, NULL, VDoubleRepn,
&dict->fvalue))
break;
} else
dict->fvalue = dict->ivalue;
dict->fcached = TRUE;
}
/* Test against the cached float value now stored in the entry: */
if (f_value == dict->fvalue)
return dict;
}
break;
case VStringRepn:
/* In case we're searching a dictionary with only integer values
stored, try to convert the supplied string value to an integer: */
i_valid =
VDecodeAttrValue (s_value, NULL, VLongRepn, &i_value);
for (; dict->keyword; dict++) {
/* If the entry includes a string value, compare with it: */
if (dict->svalue) {
if (strcmp (s_value, dict->svalue) == 0)
return dict;
}
/* Otherwise, compare with its integer value: */
else if (i_valid && i_value == dict->ivalue)
return dict;
}
break;
default:
break;
}
return NULL;
}