Debian Codegraph @

gui/GraphLib/libsrc/uimxR5/src/types.c

raw
1 /*---------------------------------------------------------------------
2 * $Date: 2009-08-18 15:23:20 $ $Revision: 1.3 $
3 *---------------------------------------------------------------------
4 *
5 *
6 * Copyright (c) 1989, Visual Edge Software Ltd.
7 *
8 * ALL RIGHTS RESERVED. Permission to use, copy, modify, and
9 * distribute this software and its documentation for any purpose
10 * and without fee is hereby granted, provided that the above
11 * copyright notice appear in all copies and that both that
12 * copyright notice and this permission notice appear in supporting
13 * documentation, and that the name of Visual Edge Software not be
14 * used in advertising or publicity pertaining to distribution of
15 * the software without specific, written prior permission. The year
16 * included in the notice is the year of the creation of the work.
17 *-------------------------------------------------------------------*/
18 /*
19
20 .VERSION
21 090813 last modif KB
22
23 */
24
25
26 /*---------------------------------------------------------------------
27 Routines to add utypes, xtypes and type converters
28 Publically available from types.h are the macros:
29
30 char *u_name() -- Gives the name of a uimx type
31 int u_size() -- Gives the size of a uimx type
32 int u_interp_type() -- Gives the corresponding interpreter type
33 for a uimx type
34 char *x_name() -- Gives the name of an X type
35 int x_size() -- Gives the size of an X type
36
37 CREATION: May 11 1988
38 ----------------------------------------------------------------------*/
39
40 #include <stdio.h>
41 #include <string.h>
42
43 #include "types.h"
44 #include "valloc.h"
45
46 #define duplicate(s) (char *)strcpy(UxMalloc(strlen(s)+1), s)
47
48 /** Global counts **/
49
50 int UxXT_num_types = 0;
51 int UxUT_num_types = 0;
52 int UxXT_num_alloc = 0;
53 int UxUT_num_alloc = 0;
54
55 extern void UxInternalError();
56
57
58
59 /** Global tables **/
60
61 UIMX_conv **UxUimx_x = 0;
62 UIMXType **UxUIMX_types = 0;
63 XType **UxX_types = 0;
64 conversion *UxStr_conv = 0;
65
66 /* ---------------------------------------------------------------------------
67 NAME: UxAdd_utype(name, size, interp_type)
68 INPUT: char *name; -- name of the new type
69 int size; -- size of the new type
70 int interp_type; -- how does the interpreter represent
71 this type?
72 RETURNS: The index of the new type (i.e. the first index into UxUimx_x).
73 DESCRIPTION: Add a new UIMX type to the type conversion mechanism.
74
75 eg: UxAdd_utype("string", sizeof(char*), T_PNTR);
76
77 CREATION: May 11 1988
78 ---------------------------------------------------------------------------*/
79 int UxAdd_utype(name, size, interp_type)
80 char *name;
81 int size;
82 int interp_type;
83 {
84 int i, j;
85
86 if (UxUT_num_types % BLOCK_SIZE == 0) {
87 UxUT_num_alloc = UxUT_num_types + BLOCK_SIZE;
88 UxUIMX_types = (UIMXType**)UxRealloc(UxUIMX_types,
89 (UxUT_num_alloc) * sizeof(UIMXType*));
90 UxUimx_x = (UIMX_conv**)UxRealloc(UxUimx_x,
91 (UxUT_num_alloc) * sizeof(UIMX_conv*));
92 for (i = UxUT_num_types; i < UxUT_num_alloc; i++) {
93 UxUimx_x[i] = (UIMX_conv*)UxMalloc(
94 UxXT_num_alloc*sizeof(UIMX_conv));
95 for (j = 0; j < UxXT_num_alloc; j++)
96 UxUimx_x[i][j] = 0;
97 }
98 }
99
100 UxUIMX_types[UxUT_num_types] =
101 (UIMXType*)UxMalloc(sizeof(UIMXType));
102 u_name(UxUT_num_types) = duplicate(name);
103 u_size(UxUT_num_types) = size;
104 u_interp_type(UxUT_num_types) = interp_type;
105 u_domain_type(UxUT_num_types) = NO_TYPE;
106 return UxUT_num_types++;
107 }
108
109 /* ---------------------------------------------------------------------------
110 NAME: UxAddXtype(name, size)
111 INPUT: char *name; -- name of the new type
112 int size; -- size of the new type
113 RETURNS: The index of the new type (i.e. the second index into UxUimx_x).
114 DESCRIPTION: Add a new X type to the type conversion mechanism.
115
116 eg: UxAddXtype("Justify", sizeof(XtJustify));
117
118 CREATION: May 11 1988
119 REVISIONS: Jan 14/91 bug2008
120 -- now initializes UxStr_conv[].size to zero
121 ---------------------------------------------------------------------------*/
122 int UxAddXtype(name, size)
123 char *name;
124 int size;
125 {
126 int i, j;
127
128 if (UxXT_num_types % BLOCK_SIZE == 0)
129 {
130 UxXT_num_alloc = UxXT_num_types + BLOCK_SIZE;
131
132 UxX_types = (XType**)UxRealloc(UxX_types,
133 (UxXT_num_alloc) * sizeof(XType*));
134
135 UxStr_conv = (conversion *)UxRealloc(UxStr_conv,
136 (UxXT_num_alloc) * sizeof(conversion));
137
138 for (j = UxXT_num_types; j < UxXT_num_alloc; j++)
139 UxStr_conv[j].size = 0;
140
141 for (i = 0; i < UxUT_num_alloc; i++)
142 {
143 UxUimx_x[i] = (UIMX_conv*)UxRealloc(UxUimx_x[i],
144 (UxXT_num_alloc) * sizeof(UIMX_conv));
145
146 for (j = UxXT_num_types; j < UxXT_num_alloc; j++)
147 UxUimx_x[i][j] = NULL;
148 }
149 }
150
151 UxX_types[UxXT_num_types] = (XType*)UxMalloc(sizeof(XType));
152 x_name(UxXT_num_types) = duplicate(name);
153 x_size(UxXT_num_types) = size;
154 x_domain_type(UxXT_num_types) = NO_TYPE;
155 return UxXT_num_types++;
156 }
157
158 /* ---------------------------------------------------------------------------
159 NAME: void UxAddConv(utypei, xtypei, conv)
160 INPUT: int utypei, xtypei; -- type indexes (see above)
161 UIMX_conv conv; -- the converter
162 RETURNS:
163 DESCRIPTION: Add a new type converter to the type conversion mechanism.
164
165 eg: UxAddConv(UxUT_string,UxXT_Int,string_to_Int);
166 UxAddConv(UxUT_string,UxXT_String,u_identity);
167
168 Gives error message if overwriting previous convertor
169
170 CREATION: May 11 1988
171 ---------------------------------------------------------------------------*/
172
173 void UxAddConv(utypei, xtypei, conv)
174 int utypei, xtypei;
175 UIMX_conv conv;
176 {
177 /* First we do a sanity check on the indices */
178 if (0 > utypei || utypei > UxUT_num_types ||
179 0 > xtypei || xtypei > UxXT_num_types)
180 UxInternalError( __FILE__, __LINE__, "UxAddConv: Bad index\n");
181
182 /* If a convertor function is already installed in the indicated slot,
183 this probably means that one of the indices is wrong */
184 if ( UxUimx_x[utypei][xtypei] != NULL )
185 {
186 UxInternalError( __FILE__, __LINE__,
187 "UxAddConv: already installed(ut=%d, xt=%d)\n",
188 utypei, xtypei );
189 }
190
191 UxUimx_x[utypei][xtypei] = conv;
192 }
193
194 /* ---------------------------------------------------------------------------
195 NAME: void UxAdd_values(x_or_uimx, type, num_values, values)
196 INPUT: int x_or_uimx; -- is this a UIMX_TYPE or an X_TYPE
197 int type; -- which type?
198 int num_values; -- how many possible values are there
199 char **values; -- a list of these values (in string format)
200 RETURNS:
201 DESCRIPTION: specify the allowable values for instances of this type
202
203 eg: static char *justify_values[] = {"left", "center", "right"};
204 UxAdd_values(X_TYPE, XT_Justify, 3, justify_values);
205
206 CREATION: May 13 1988
207 ---------------------------------------------------------------------------*/
208 void UxAdd_values(x_or_uimx, type, num_values, values)
209 int x_or_uimx;
210 int type;
211 int num_values;
212 char **values;
213 {
214 int i;
215
216 if (x_or_uimx == UIMX_TYPE) {
217 u_domain_type(type) = VALUES;
218 u_num_values(type) = num_values;
219 u_values(type) = (char **)UxMalloc(num_values * sizeof(char*));
220 for (i = 0; i < num_values; i++)
221 u_values(type)[i] = duplicate(values[i]);
222 }
223 else {
224 x_domain_type(type) = VALUES;
225 x_num_values(type) = num_values;
226 x_values(type) = (char **)UxMalloc(num_values * sizeof(char*));
227 for (i = 0; i < num_values; i++)
228 x_values(type)[i] = duplicate(values[i]);
229 }
230 }
231
232
233 /***************************************************************************
234 NAME: UxAddXValues(type, uvals, xvals, size)
235 INPUT: int type : the UxXT_ type
236 char **uvals : array of possible uimx values
237 unsigned char *xvals : array of corresponding Xt values
238 int size : number of elements in arrays
239 OUTPUT: ---
240 RETURN: void
241 DESCRIPTION: Registers the two given arrays of values in UxStr_conv[].
242 Gives error message if overwriting previous values.
243
244 EXT REFERENCES: UxStr_conv[]
245 EXT EFFECTS: UxStr_conv[]
246
247 REVISIONS: Jan 14/91 bug2008
248 ---------------------------------------------------------------------------*/
249
250 void UxAddXValues(type, uvals, xvals, size)
251 int type;
252 char **uvals;
253 unsigned char *xvals;
254 int size;
255 {
256 if ( UxStr_conv[type].size != 0 )
257 {
258 /* A set of values has already been installed
259 - this probably means a bad 'type' index is being used */
260 UxInternalError( __FILE__, __LINE__,
261 "UxAddXValues: Bad index (%d)\n", type );
262 }
263
264 UxStr_conv[type].strings = uvals;
265 UxStr_conv[type].values = xvals;
266 UxStr_conv[type].size = size;
267 }
268