Revert "dtudcfonted, dtudcexch: delete from repository"
[oweals/cde.git] / cde / programs / dtudcexch / excutil.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: excutil.c /main/3 1996/04/08 15:51:03 cde-fuj $ */
24 /*
25  *  (c) Copyright 1995 FUJITSU LIMITED
26  *  This is source code modified by FUJITSU LIMITED under the Joint
27  *  Development Agreement for the CDEnext PST.
28  *  This is unpublished proprietary source code of FUJITSU LIMITED
29  */
30
31 #include "excutil.h"
32
33 extern char *maintitle;
34
35 Widget excCreatePushButton(Widget parent, String name, String label,
36                            XtCallbackProc callback_func,
37                            XtPointer client_data)
38 {
39     Widget      widget;
40     XmString    xms;
41     Arg         args[20];
42     Cardinal    n;
43
44     n = 0;
45     xms = XmStringCreateLocalized(label);
46     XtSetArg(args[n], XmNlabelString, xms); n++;
47     widget = XmCreatePushButton(parent, name, args, n);
48     XmStringFree(xms);
49     XtAddCallback(widget, XmNactivateCallback,
50                   (XtCallbackProc) callback_func,
51                   (XtPointer) client_data);
52     XtManageChild(widget);
53
54     return (widget);
55 }
56
57 void excerror(Exc_data * ed, int excerrno, char * funcname, char * func)
58 {
59     switch (excerrno)
60     {
61     case EXCERRMALLOC:
62         fprintf(stderr, "dtudcexch:memory allocation error\n");
63         freeExcdata(ed);
64         exit (-1);
65     case EXCERRNOUDC:
66         fprintf(stderr, "dtudcexch:UDC is not defined in current locale\n");
67         freeExcdata(ed);
68         exit (-1);
69     }
70 }
71
72 void dialogokCB(Widget widget, int * ans, XtPointer call_data)
73 {
74     /* ok button pushed */
75     *ans = 1;
76 }
77
78 void dialogcancelCB(Widget widget, int * ans, XtPointer call_data)
79 {
80     /* cancel button pushed */
81     *ans = 2;
82 }
83
84 void AskUser(Widget parent, Exc_data * ed, char *msg, int *r, char * type)
85 /* If ok button pushed, *r = 1. */
86 /* If cancel button pushed(if exists), *r = 2. */
87 {
88     XmString                            message;
89     Widget                              dialog;
90     int                                 ans = 0;
91     Cardinal                            n;
92     Arg                                 args[20];
93
94     n = 0;
95     message = XmStringCreateLocalized(msg);
96     XtSetArg(args[n], XmNmessageString, message); n++;
97     XtSetArg(args[n], XmNtitle, maintitle); n++;
98     XtSetArg(args[n], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); n++;
99     if (strcmp(type, "warning") == 0) {
100         dialog = XmCreateWarningDialog(parent, type, args, n);
101     } else if (strcmp(type, "error") == 0) {
102         dialog = XmCreateErrorDialog(parent, type, args, n);
103         XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
104     } else if (strcmp(type, "information") == 0) {
105         dialog = XmCreateInformationDialog(parent, type, args, n);
106         XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
107     } else if (strcmp(type, "question") == 0) {
108         dialog = XmCreateQuestionDialog(parent, type, args, n);
109     } else {
110         fprintf(stderr, "error in AskUser\n");
111     }
112     XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
113     XmStringFree(message);
114     XtAddCallback(dialog, XmNokCallback, (XtCallbackProc) dialogokCB,
115                   (XtPointer) &ans);
116     XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc) dialogcancelCB,
117                   (XtPointer) &ans);
118     XtManageChild(dialog);
119     while (ans == 0) {
120         XtAppProcessEvent(ed->app, XtIMAll);
121         XSync(XtDisplay(dialog), 0);
122     }
123     *r = ans;
124     XSync(XtDisplay(dialog), 0);
125     XmUpdateDisplay(dialog);
126 }
127
128 int beki(int n, int num)
129 {
130     int a,i;
131
132     a = 1;
133     for (i=0; i<num; i++)
134         a = a * n;
135
136     return a;
137 }
138
139 int c2h(char * num)
140 {
141     int         i,n;
142     char        *c;
143
144     if ((strlen(num)==6)&&(strncmp(num,"0x",2)==0))
145     {
146         c = num;
147         c=c+2;
148         n=0;
149         for (i=3;i>=0;i--)
150         {
151             if (('0'<= *c)&&(*c <= '9'))
152                 n = n + ((*c - '0')*beki(16,i));
153             else if (('a' <= *c)&&(*c <= 'f'))
154                 n = n + ((*c - 'a'+10)*beki(16,i));
155             else
156                 Ecs("error in c2h.here");
157             c++;
158         }
159     } else
160         Ecs("error in c2h");
161
162     return n;
163 }
164 char * renge2num(char * renge)
165 {
166     char * num;
167
168     num = (char *) calloc(7, sizeof(char));
169
170     strncpy(num,renge,6);
171
172     return num;
173 }
174
175 void Ecs(char * string)
176 {
177     fprintf(stderr,"%s\n",string);
178 }
179 void Ecd(int data)
180 {
181     fprintf(stderr,"%d\n",data);
182 }
183
184 void freecsrec(UDCcsREC *rec)
185 {
186     if (rec->charset != NULL)
187         free(rec->charset);
188     if (rec->udcrenge != NULL)
189         free(rec->udcrenge);
190 }
191
192 void freecslist(int n, UDCcsREC *rec)
193 {
194     int         i;
195     UDCcsREC    *recp;
196
197     recp = rec;
198     for (i = 0; i < n; i++)
199     {
200         if (recp != NULL)
201         {
202             freecsrec(recp);
203             recp++;
204         } else {
205             break;
206         }
207     }
208 }
209
210 void freeExcdata(Exc_data *ed)
211 {
212     if (ed == NULL)
213         return;
214
215 /* Don't free ed->toplevel */
216
217     if (ed->locale != NULL)
218         free(ed->locale);
219
220     if (ed->cslist != NULL)
221         free(ed->cslist);
222
223     if (ed->xlfdname != NULL)
224         free(ed->xlfdname);
225
226     if (ed->style != NULL)
227         free(ed->style);
228
229     if (ed->size != NULL)
230         free(ed->size);
231
232 /* Don't free ed->fontfile */
233
234     if (ed->bdffile != NULL)
235         XtFree(ed->bdffile);
236
237     if (ed->udcrenge != NULL)
238         free(ed->udcrenge);
239
240     if (ed->gpf_code_list != NULL)
241         free(ed->gpf_code_list);
242
243     if (ed->bdf_code_list != NULL)
244         free(ed->bdf_code_list);
245
246     if (ed->comment_list != NULL)
247         free(ed->comment_list);
248
249     free(ed);
250 }
251
252 void Ecss(char * s1, char * s2)
253 {
254     fprintf(stderr,"%s is %s\n", s1, s2);
255 }
256 void Ecsd(char * s1, int i)
257 {
258     fprintf(stderr,"%s = %d\n", s1, i);
259 }
260
261 void checkdata(Exc_data *ed)
262 {
263     if (ed == NULL)
264         return;
265
266 /* Don't free ed->toplevel */
267
268     if (ed->locale != NULL)
269         Ecss("ed->locale", ed->locale);
270
271     Ecsd("ed->csnum", ed->csnum);
272
273     if (ed->xlfdname != NULL)
274         Ecss("ed->xlfdname",ed->xlfdname);
275
276     if (ed->fontfile != NULL)
277         Ecss("ed->fontfile",ed->fontfile);
278
279     if (ed->bdffile != NULL)
280         Ecss("ed->bdffile",ed->bdffile);
281
282     if (ed->udcrenge != NULL)
283         Ecss("ed->udcrenge",ed->udcrenge);
284
285     Ecsd("ed->code_num", ed->code_num);
286 }
287
288 void excterminate(Exc_data * ed)
289 {
290     freeExcdata(ed);
291     exit(0);
292 }
293
294 void excexit(Exc_data * ed)
295 {
296     excterminate(ed);
297 }
298
299 void freeld(ListData *ld)
300 {
301     int         i;
302     char        **cp;
303
304     if (ld != NULL) {
305         if (ld->allcode != NULL)
306             free(ld->allcode);
307         if (ld->existcode != NULL)
308             free(ld->existcode);
309         if (ld->existcode_c != NULL) {
310             cp = ld->existcode_c;
311             for (i = 0; i < ld->existcode_num; i++) {
312                 if (*cp != NULL)
313                     free(*cp);
314                 cp++;
315             }
316             free(ld->existcode_c);
317         }
318         free(ld);
319     }
320 }
321
322 int bigger(int num1, int num2)
323 {
324     if (num1 >= num2)
325         return (num1);
326     else
327         return (num2);
328 }
329
330 int smaller(int num1, int num2)
331 {
332     if (num1 < num2)
333         return (num1);
334     else
335         return (num2);
336 }