Don't run the benchmark by default
[profile/ivi/libxkbcommon.git] / src / 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 <ctype.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <string.h>
32
33 #include "utils.h"
34
35 void *
36 recalloc(void *ptr, size_t old_size, size_t new_size)
37 {
38     char *rtrn;
39
40     if (ptr == NULL)
41         return calloc(1, new_size);
42
43     rtrn = realloc(ptr, new_size);
44     if (rtrn && new_size > old_size)
45         memset(&rtrn[old_size], 0, new_size - old_size);
46
47     return rtrn;
48 }
49
50 static FILE *errorFile = NULL;
51 static int outCount = 0;
52 static char *preMsg = NULL;
53 static char *prefix = NULL;
54
55 bool
56 uSetErrorFile(char *name)
57 {
58     if ((errorFile != NULL) && (errorFile != stderr))
59     {
60         fprintf(errorFile, "switching to %s\n", name ? name : "stderr");
61         fclose(errorFile);
62     }
63     if (name != NULL)
64         errorFile = fopen(name, "w");
65     else
66         errorFile = stderr;
67     if (errorFile == NULL)
68     {
69         errorFile = stderr;
70         return false;
71     }
72     return true;
73 }
74
75 void
76 uInformation(const char *s, ...)
77 {
78     va_list args;
79
80     if (!errorFile)
81         errorFile = stderr;
82
83     va_start(args, s);
84     vfprintf(errorFile, s, args);
85     va_end(args);
86     fflush(errorFile);
87 }
88
89 /***====================================================================***/
90
91 void
92 uAction(const char *s, ...)
93 {
94     va_list args;
95
96     if (!errorFile)
97         errorFile = stderr;
98
99     if (prefix != NULL)
100         fprintf(errorFile, "%s", prefix);
101     fprintf(errorFile, "                  ");
102     va_start(args, s);
103     vfprintf(errorFile, s, args);
104     va_end(args);
105     fflush(errorFile);
106 }
107
108 /***====================================================================***/
109
110 void
111 uWarning(const char *s, ...)
112 {
113     va_list args;
114
115     if (!errorFile)
116         errorFile = stderr;
117
118     if ((outCount == 0) && (preMsg != NULL))
119         fprintf(errorFile, "%s\n", preMsg);
120     if (prefix != NULL)
121         fprintf(errorFile, "%s", prefix);
122     fprintf(errorFile, "Warning:          ");
123     va_start(args, s);
124     vfprintf(errorFile, s, args);
125     va_end(args);
126     fflush(errorFile);
127     outCount++;
128 }
129
130 /***====================================================================***/
131
132 void
133 uError(const char *s, ...)
134 {
135     va_list args;
136
137     if (!errorFile)
138         errorFile = stderr;
139
140     if ((outCount == 0) && (preMsg != NULL))
141         fprintf(errorFile, "%s\n", preMsg);
142     if (prefix != NULL)
143         fprintf(errorFile, "%s", prefix);
144     fprintf(errorFile, "Error:            ");
145     va_start(args, s);
146     vfprintf(errorFile, s, args);
147     va_end(args);
148     fflush(errorFile);
149     outCount++;
150 }
151
152 /***====================================================================***/
153
154 void
155 uFatalError(const char *s, ...)
156 {
157     va_list args;
158
159     if (!errorFile)
160         errorFile = stderr;
161
162     if ((outCount == 0) && (preMsg != NULL))
163         fprintf(errorFile, "%s\n", preMsg);
164     if (prefix != NULL)
165         fprintf(errorFile, "%s", prefix);
166     fprintf(errorFile, "Fatal Error:      ");
167     va_start(args, s);
168     vfprintf(errorFile, s, args);
169     va_end(args);
170     fprintf(errorFile, "                  Exiting\n");
171     fflush(errorFile);
172     outCount++;
173     exit(1);
174     /* NOTREACHED */
175 }
176
177 /***====================================================================***/
178
179 void
180 uInternalError(const char *s, ...)
181 {
182     va_list args;
183
184     if (!errorFile)
185         errorFile = stderr;
186
187     if ((outCount == 0) && (preMsg != NULL))
188         fprintf(errorFile, "%s\n", preMsg);
189     if (prefix != NULL)
190         fprintf(errorFile, "%s", prefix);
191     fprintf(errorFile, "Internal error:   ");
192     va_start(args, s);
193     vfprintf(errorFile, s, args);
194     va_end(args);
195     fflush(errorFile);
196     outCount++;
197 }