34ba8e0e7c6248beaa96bf811086d07c79d18a4c
[profile/ivi/libxkbcommon.git] / src / xkbcomp / utils.c
1
2   /*\
3    *
4    *                          COPYRIGHT 1990
5    *                    DIGITAL EQUIPMENT CORPORATION
6    *                       MAYNARD, MASSACHUSETTS
7    *                        ALL RIGHTS RESERVED.
8    *
9    * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
10    * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
11    * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE 
12    * FOR ANY PURPOSE.  IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED 
13    * WARRANTY.
14    *
15    * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT
16    * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN
17    * ADDITION TO THAT SET FORTH ABOVE.
18    *
19    * Permission to use, copy, modify, and distribute this software and its
20    * documentation for any purpose and without fee is hereby granted, provided
21    * that the above copyright notice appear in all copies and that both that
22    * copyright notice and this permission notice appear in supporting
23    * documentation, and that the name of Digital Equipment Corporation not be
24    * used in advertising or publicity pertaining to distribution of the 
25    * software without specific, written prior permission.
26    \*/
27
28 #include        "utils.h"
29 #include        <ctype.h>
30 #include        <stdlib.h>
31 #include        <stdarg.h>
32
33 /***====================================================================***/
34
35 Opaque
36 uAlloc(unsigned size)
37 {
38     return ((Opaque) malloc(size));
39 }
40
41 /***====================================================================***/
42
43 Opaque
44 uCalloc(unsigned n, unsigned size)
45 {
46     return ((Opaque) calloc(n, size));
47 }
48
49 /***====================================================================***/
50
51 Opaque
52 uRealloc(Opaque old, unsigned newSize)
53 {
54     if (old == NULL)
55         return ((Opaque) malloc(newSize));
56     else
57         return ((Opaque) realloc((char *) old, newSize));
58 }
59
60 /***====================================================================***/
61
62 Opaque
63 uRecalloc(Opaque old, unsigned nOld, unsigned nNew, unsigned itemSize)
64 {
65     char *rtrn;
66
67     if (old == NULL)
68         rtrn = (char *) calloc(nNew, itemSize);
69     else
70     {
71         rtrn = (char *) realloc((char *) old, nNew * itemSize);
72         if ((rtrn) && (nNew > nOld))
73         {
74             bzero(&rtrn[nOld * itemSize], (nNew - nOld) * itemSize);
75         }
76     }
77     return (Opaque) rtrn;
78 }
79
80 /***====================================================================***/
81
82 void
83 uFree(Opaque ptr)
84 {
85     if (ptr != (Opaque) NULL)
86         free((char *) ptr);
87     return;
88 }
89
90 /***====================================================================***/
91 /***                  FUNCTION ENTRY TRACKING                           ***/
92 /***====================================================================***/
93
94 static FILE *entryFile = NULL;
95 int uEntryLevel;
96
97 Boolean
98 uSetEntryFile(char *name)
99 {
100     if ((entryFile != NULL) && (entryFile != stderr))
101     {
102         fprintf(entryFile, "switching to %s\n", name ? name : "stderr");
103         fclose(entryFile);
104     }
105     if (name != NullString)
106         entryFile = fopen(name, "w");
107     else
108         entryFile = stderr;
109     if (entryFile == NULL)
110     {
111         entryFile = stderr;
112         return (False);
113     }
114     return (True);
115 }
116
117 void
118 uEntry(int l, char *s, ...)
119 {
120     int i;
121     va_list args;
122
123     if (!entryFile)
124         return;
125
126     for (i = 0; i < uEntryLevel; i++)
127     {
128         putc(' ', entryFile);
129     }
130     va_start(args, s);
131     vfprintf(entryFile, s, args);
132     va_end(args);
133     uEntryLevel += l;
134 }
135
136 void
137 uExit(int l, char *rtVal)
138 {
139     int i;
140
141     if (!entryFile)
142         return;
143
144     uEntryLevel -= l;
145     if (uEntryLevel < 0)
146         uEntryLevel = 0;
147     for (i = 0; i < uEntryLevel; i++)
148     {
149         putc(' ', entryFile);
150     }
151     fprintf(entryFile, "---> %p\n", rtVal);
152     return;
153 }
154
155 /***====================================================================***/
156 /***                    PRINT FUNCTIONS                                 ***/
157 /***====================================================================***/
158
159 FILE *uDebugFile = NULL;
160 int uDebugIndentLevel = 0;
161 int uDebugIndentSize = 4;
162
163 Boolean
164 uSetDebugFile(char *name)
165 {
166     if ((uDebugFile != NULL) && (uDebugFile != stderr))
167     {
168         fprintf(uDebugFile, "switching to %s\n", name ? name : "stderr");
169         fclose(uDebugFile);
170     }
171     if (name != NullString)
172         uDebugFile = fopen(name, "w");
173     else
174         uDebugFile = stderr;
175     if (uDebugFile == NULL)
176     {
177         uDebugFile = stderr;
178         return (False);
179     }
180     return (True);
181 }
182
183 void
184 uDebug(char *s, ...)
185 {
186     int i;
187     va_list args;
188
189     if (!uDebugFile)
190         return;
191
192     for (i = (uDebugIndentLevel * uDebugIndentSize); i > 0; i--)
193     {
194         putc(' ', uDebugFile);
195     }
196     va_start(args, s);
197     vfprintf(uDebugFile, s, args);
198     va_end(args);
199     fflush(uDebugFile);
200 }
201
202 void
203 uDebugNOI(char *s, ...)
204 {
205     va_list args;
206
207     if (!uDebugFile)
208         return;
209
210     va_start(args, s);
211     vfprintf(uDebugFile, s, args);
212     va_end(args);
213     fflush(uDebugFile);
214 }
215
216 /***====================================================================***/
217
218 static FILE *errorFile = NULL;
219 static int outCount = 0;
220 static char *preMsg = NULL;
221 static char *postMsg = NULL;
222 static char *prefix = NULL;
223
224 Boolean
225 uSetErrorFile(char *name)
226 {
227     if ((errorFile != NULL) && (errorFile != stderr))
228     {
229         fprintf(errorFile, "switching to %s\n", name ? name : "stderr");
230         fclose(errorFile);
231     }
232     if (name != NullString)
233         errorFile = fopen(name, "w");
234     else
235         errorFile = stderr;
236     if (errorFile == NULL)
237     {
238         errorFile = stderr;
239         return (False);
240     }
241     return (True);
242 }
243
244 void
245 uInformation(const char *s, ...)
246 {
247     va_list args;
248
249     if (!errorFile)
250         return;
251
252     va_start(args, s);
253     vfprintf(errorFile, s, args);
254     va_end(args);
255     fflush(errorFile);
256 }
257
258 /***====================================================================***/
259
260 void
261 uAction(const char *s, ...)
262 {
263     va_list args;
264
265     if (!errorFile)
266         return;
267
268     if (prefix != NULL)
269         fprintf(errorFile, "%s", prefix);
270     fprintf(errorFile, "                  ");
271     va_start(args, s);
272     vfprintf(errorFile, s, args);
273     va_end(args);
274     fflush(errorFile);
275 }
276
277 /***====================================================================***/
278
279 void
280 uWarning(const char *s, ...)
281 {
282     va_list args;
283
284     if (!errorFile)
285         return;
286
287     if ((outCount == 0) && (preMsg != NULL))
288         fprintf(errorFile, "%s\n", preMsg);
289     if (prefix != NULL)
290         fprintf(errorFile, "%s", prefix);
291     fprintf(errorFile, "Warning:          ");
292     va_start(args, s);
293     vfprintf(errorFile, s, args);
294     va_end(args);
295     fflush(errorFile);
296     outCount++;
297 }
298
299 /***====================================================================***/
300
301 void
302 uError(const char *s, ...)
303 {
304     va_list args;
305
306     if (!errorFile)
307         return;
308
309     if ((outCount == 0) && (preMsg != NULL))
310         fprintf(errorFile, "%s\n", preMsg);
311     if (prefix != NULL)
312         fprintf(errorFile, "%s", prefix);
313     fprintf(errorFile, "Error:            ");
314     va_start(args, s);
315     vfprintf(errorFile, s, args);
316     va_end(args);
317     fflush(errorFile);
318     outCount++;
319 }
320
321 /***====================================================================***/
322
323 void
324 uFatalError(const char *s, ...)
325 {
326     va_list args;
327
328     if (!errorFile)
329         return;
330
331     if ((outCount == 0) && (preMsg != NULL))
332         fprintf(errorFile, "%s\n", preMsg);
333     if (prefix != NULL)
334         fprintf(errorFile, "%s", prefix);
335     fprintf(errorFile, "Fatal Error:      ");
336     va_start(args, s);
337     vfprintf(errorFile, s, args);
338     va_end(args);
339     fprintf(errorFile, "                  Exiting\n");
340     fflush(errorFile);
341     outCount++;
342     exit(1);
343     /* NOTREACHED */
344 }
345
346 /***====================================================================***/
347
348 void
349 uInternalError(const char *s, ...)
350 {
351     va_list args;
352
353     if (!errorFile)
354         return;
355
356     if ((outCount == 0) && (preMsg != NULL))
357         fprintf(errorFile, "%s\n", preMsg);
358     if (prefix != NULL)
359         fprintf(errorFile, "%s", prefix);
360     fprintf(errorFile, "Internal error:   ");
361     va_start(args, s);
362     vfprintf(errorFile, s, args);
363     va_end(args);
364     fflush(errorFile);
365     outCount++;
366 }
367
368 void
369 uSetPreErrorMessage(char *msg)
370 {
371     outCount = 0;
372     preMsg = msg;
373     return;
374 }
375
376 void
377 uSetPostErrorMessage(char *msg)
378 {
379     postMsg = msg;
380     return;
381 }
382
383 void
384 uSetErrorPrefix(char *pre)
385 {
386     prefix = pre;
387     return;
388 }
389
390 void
391 uFinishUp(void)
392 {
393     if (errorFile && (outCount > 0) && (postMsg != NULL))
394         fprintf(errorFile, "%s\n", postMsg);
395     return;
396 }
397
398 /***====================================================================***/
399
400 #ifndef HAVE_STRDUP
401 char *
402 uStringDup(const char *str)
403 {
404     char *rtrn;
405
406     if (str == NULL)
407         return NULL;
408     rtrn = (char *) uAlloc(strlen(str) + 1);
409     strcpy(rtrn, str);
410     return rtrn;
411 }
412 #endif
413
414 #ifndef HAVE_STRCASECMP
415 int
416 uStrCaseCmp(const char *str1, const char *str2)
417 {
418     char buf1[512], buf2[512];
419     char c, *s;
420     register int n;
421
422     for (n = 0, s = buf1; (c = *str1++); n++)
423     {
424         if (isupper(c))
425             c = tolower(c);
426         if (n > 510)
427             break;
428         *s++ = c;
429     }
430     *s = '\0';
431     for (n = 0, s = buf2; (c = *str2++); n++)
432     {
433         if (isupper(c))
434             c = tolower(c);
435         if (n > 510)
436             break;
437         *s++ = c;
438     }
439     *s = '\0';
440     return (strcmp(buf1, buf2));
441 }
442
443 int
444 uStrCasePrefix(const char *my_prefix, char *str)
445 {
446     char c1;
447     char c2;
448     while (((c1 = *my_prefix) != '\0') && ((c2 = *str) != '\0'))
449     {
450         if (isupper(c1))
451             c1 = tolower(c1);
452         if (isupper(c2))
453             c2 = tolower(c2);
454         if (c1 != c2)
455             return 0;
456         my_prefix++;
457         str++;
458     }
459     if (c1 != '\0')
460         return 0;
461     return 1;
462 }
463
464 #endif