1 /***********************************************************************/
2 /* Open Visualization Data Explorer                                    */
3 /* (C) Copyright IBM Corp. 1989,1999                                   */
4 /* ALL RIGHTS RESERVED                                                 */
5 /* This code licensed under the                                        */
6 /*    "IBM PUBLIC LICENSE - Open Visualization Data Explorer"          */
7 /***********************************************************************/
8 
9 #include <dxconfig.h>
10 
11 #if defined(HAVE_STRING_H)
12 #include <string.h>
13 #endif
14 
15 #include "interact.h"
16 
17 #define MAXRANK 16
18 #define MSGLEN 240
19 
20 static int readvalue(Object,char *,char *,Object *);
21 
22 #if 0
23 static int compare_arrays(Array *a, Array *b,int items,int dim,Type type);
24 static Error read_and_compare(Object cvalue,Object svalue,Object uvalue,int *on,          Object *out);
25 #endif
26 
m_Toggle(Object * in,Object * out)27 int m_Toggle(Object *in, Object *out)
28 {
29    struct einfo ei;
30    char *id, *label;
31    Object idobj;
32    char *set="set= ",*unset="unset= ";
33    int shape[MAXRANK];
34    int toggle,change;
35 
36    ei.msgbuf = NULL;
37    out[0] = in[1];
38 
39    /* get id */
40    if (!DXExtractString(in[0], &id)){
41        DXSetError(ERROR_BAD_PARAMETER,"#10000","id");
42        goto error;
43    }
44    idobj=in[0];
45 
46    /* read toggle value (set=1, unset=0) */
47    if (!in[2])
48       toggle=0;
49    else{
50       if (!DXExtractInteger(in[2],&toggle)){
51 	 DXSetError(ERROR_INTERNAL,"toggle value is not integer");
52 	 goto error;
53       }
54    }
55 
56    /* read set value */
57    change = _dxfcheck_obj_cache(in[3],id,3,idobj);
58    if (change==0) set=NULL;
59    if (in[3]){	/* set value is data-driven */
60       if (toggle==1){
61 	 if (!readvalue(in[3],id,set,out))
62             goto error;
63       }
64       else{
65 	 if (!readvalue(in[3],id,set,NULL))
66 	    goto error;
67       }
68    }
69 
70    /* read unset value */
71    change = _dxfcheck_obj_cache(in[4],id,4,idobj);
72    if (change==0) unset=NULL;
73    if (in[4]){
74       if (toggle==0){
75 	 if (!readvalue(in[4],id,unset,out))
76             goto error;
77       }
78       else{
79 	 if (!readvalue(in[4],id,unset,NULL))
80 	    goto error;
81       }
82    }
83 
84    /* read label */
85    if (in[5]){
86       if (!DXExtractString(in[5], &label)){
87          DXSetError(ERROR_BAD_PARAMETER,"#10200","label");
88          goto error;
89       }
90       ei.maxlen = (int)strlen(label)+SLOP;
91       ei.msgbuf = (char *)DXAllocateZero(ei.maxlen);
92       ei.atend = 0;
93       if (!ei.msgbuf)
94          goto error;
95       ei.mp =ei.msgbuf;
96       shape[0] = (int)strlen(label);
97       sprintf(ei.mp, "label="); while(*ei.mp) ei.mp++;
98       if (!_dxfprint_message(label,&ei,TYPE_STRING,1,shape,1))
99          goto error;
100       DXUIMessage(id,ei.msgbuf);
101       DXFree(ei.msgbuf);
102    }
103 
104 
105    return OK;
106 
107 error:
108    if (out[0]) out[0]=NULL;
109    if (ei.msgbuf) DXFree(ei.msgbuf);
110    return ERROR;
111 }
112 
113 
114 /* routine for reading, and optionally sending UIMessage or setting output */
readvalue(Object o,char * id,char * value,Object * out)115 int readvalue(Object o,char *id,char *value,Object *out)
116 {
117    struct einfo ei;
118    char *set_string;
119    int items,shape[MAXRANK],rank;
120    Type type;
121    Category cat;
122    void *p_set;
123 
124    /* setup message */
125    ei.maxlen = MSGLEN;
126    ei.atend = 0;
127    if (value) ei.msgbuf = (char *)DXAllocateZero(ei.maxlen+SLOP);
128    if (!ei.msgbuf)
129       goto error;
130    ei.mp =ei.msgbuf;
131 
132       if (!DXExtractString((Object)o,&set_string)){
133          if (!DXGetArrayInfo((Array)o,&items,&type,&cat,&rank,shape))
134             return ERROR;
135          p_set = DXGetArrayData((Array)o);
136          if (rank==0) shape[0]=1;
137          if (value){
138             sprintf(ei.mp, "%s",value); while(*ei.mp) ei.mp++;
139 	    if (!_dxfprint_message(p_set,&ei,type,rank,shape,items))
140 	       goto error;
141 	 }
142 	 if (out){
143             out[0] = (Object)DXNewArrayV(type,CATEGORY_REAL,rank,shape);
144             if(!out[0] || !DXAddArrayData((Array)out[0],0,items,(Pointer)p_set))
145 	       goto error;
146 	 }
147       }
148       else{
149          shape[0] = (int)strlen(set_string);
150          if (value){
151             sprintf(ei.mp,"%s",value); while(*ei.mp) ei.mp++;
152 	    if (!_dxfprint_message(set_string,&ei,TYPE_STRING,1,shape,1))
153                goto error;
154 	 }
155 	 if (out) out[0] = (Object)DXNewString(set_string);
156       }
157 
158    if (value) {
159       DXUIMessage(id,ei.msgbuf);
160       DXFree(ei.msgbuf);
161    }
162    return OK;
163 
164 error:
165       return ERROR;
166 }
167 
168 #if 0
169 static Error
170 read_and_compare(Object cvalue,Object svalue,Object uvalue,int *on,Object *out)
171 {
172    char *cstring,*sstring;
173    void *p_set,*p_current;
174    int items,i,shape[MAXRANK],sp[MAXRANK],rank,r;
175    int n,dim;
176    Type type,t;
177    Category cat,c;
178 
179       if (!DXExtractString((Object)cvalue,&cstring)){
180          if (!DXGetArrayInfo((Array)cvalue,&items,&type,&cat,&rank,shape))
181             return ERROR;
182          p_current = DXGetArrayData((Array)cvalue);
183 	 /* compare current array with previous set array */
184 	 if (DXGetArrayInfo((Array)svalue,&i,&t,&c,&r,sp)){
185 	    if (items==i && type==t && cat==c && rank==r ){
186 	       if (rank==0) dim=1;
187 	       else dim=sp[0];
188 	       *on = compare_arrays((Array)cvalue,(Array)svalue,items,dim,type);
189             }
190 	 }
191 	 else
192 	    *on=0; 	/* toggle is unset */
193 
194          if (out){
195             out[0] = (Object)DXNewArrayV(type,CATEGORY_REAL,rank,shape);
196             if(!out[0] || !DXAddArrayData((Array)out[0],0,items,(Pointer)p_current))
197                goto error;
198          }
199       }
200       else{
201 	 if (DXExtractString((Object)svalue,&sstring)){
202 	       if(!strcmp(cstring,sstring))
203 		  *on=1;
204 	       else
205 		  *on=0;
206 	       if (out) out[0] = (Object)DXNewString(cstring);
207 	 }
208 	 else
209 	    *on=0;
210       }
211       return OK;
212 
213 error:
214       return ERROR;
215 }
216 
217 
218 int
219 compare_arrays(Array *a, Array *b,int items,int dim,Type type)
220 {
221    int n,count=0;
222    int *a_int,*b_int;
223    float *a_f,*b_f;
224 
225       switch(type){
226 	 case(TYPE_INT):
227 	       a_int = (int *)DXGetArrayData((Array)a);
228 	       b_int = (int *)DXGetArrayData((Array)b);
229 	       for (n=0; n<items*dim; n++){
230 	          if (a_int[n] == b_int[n])
231 	             count++;
232 	       }
233 	       break;
234 	 case(TYPE_FLOAT):
235 	       a_f = (float *)DXGetArrayData((Array)a);
236 	       b_f = (float *)DXGetArrayData((Array)b);
237 	       for (n=0; n<items*dim; n++){
238 	          if (a_f[n] == b_f[n])
239 	          count++;
240 	       }
241 	       break;
242 	 default:
243 	       return(-1);
244 	 }
245 	 if (count==items*dim)   	/* arrays are the same */
246 	    return (1);
247 	 else
248 	     return(0);
249 
250 }
251 #endif
252