1 | /**CHeaderFile***************************************************************** |
---|
2 | |
---|
3 | FileName [ util.h ] |
---|
4 | |
---|
5 | PackageName [ util ] |
---|
6 | |
---|
7 | Synopsis [ Very low-level utilities ] |
---|
8 | |
---|
9 | Description [ Includes file access, pipes, forks, time, and temporary file |
---|
10 | access. ] |
---|
11 | |
---|
12 | Author [ Stephen Edwards <sedwards@eecs.berkeley.edu> and many others] |
---|
13 | |
---|
14 | Copyright [Copyright (c) 1994-1996 The Regents of the Univ. of California. |
---|
15 | All rights reserved. |
---|
16 | |
---|
17 | Permission is hereby granted, without written agreement and without license |
---|
18 | or royalty fees, to use, copy, modify, and distribute this software and its |
---|
19 | documentation for any purpose, provided that the above copyright notice and |
---|
20 | the following two paragraphs appear in all copies of this software. |
---|
21 | |
---|
22 | IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR |
---|
23 | DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT |
---|
24 | OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF |
---|
25 | CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
26 | |
---|
27 | THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, |
---|
28 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
---|
29 | FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN |
---|
30 | "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE |
---|
31 | MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.] |
---|
32 | |
---|
33 | Revision [$Id: util.h,v 1.3 2009-04-13 11:08:17 hhkim Exp $] |
---|
34 | |
---|
35 | ******************************************************************************/ |
---|
36 | |
---|
37 | #ifndef _UTIL |
---|
38 | #define _UTIL |
---|
39 | |
---|
40 | #ifndef CLOCK_RESOLUTION |
---|
41 | #define CLOCK_RESOLUTION 60 |
---|
42 | #endif |
---|
43 | |
---|
44 | #include <stdio.h> |
---|
45 | #include <ctype.h> |
---|
46 | #include <math.h> |
---|
47 | |
---|
48 | #if HAVE_UNISTD_H |
---|
49 | # include <unistd.h> |
---|
50 | #endif |
---|
51 | |
---|
52 | #if HAVE_SYS_TYPES_H |
---|
53 | # include <sys/types.h> |
---|
54 | #endif |
---|
55 | |
---|
56 | #ifdef HAVE_VARARGS_H |
---|
57 | # include <varargs.h> |
---|
58 | #endif |
---|
59 | |
---|
60 | #if STDC_HEADERS |
---|
61 | # include <stdlib.h> |
---|
62 | # include <string.h> |
---|
63 | #else |
---|
64 | # ifdef HAVE_STRCHR |
---|
65 | char * strchr(const char *, int); |
---|
66 | int strcmp(const char *, const char *); |
---|
67 | # else |
---|
68 | # define strchr index |
---|
69 | # endif |
---|
70 | # ifdef HAVE_GETENV |
---|
71 | char * getenv(const char *); |
---|
72 | # endif |
---|
73 | #endif /* STDC_HEADERS */ |
---|
74 | |
---|
75 | #if HAVE_ERRNO_H |
---|
76 | # include <errno.h> |
---|
77 | #endif |
---|
78 | |
---|
79 | /* |
---|
80 | * Ensure we have reasonable assert() and fail() functions |
---|
81 | */ |
---|
82 | |
---|
83 | #ifdef HAVE_ASSERT_H |
---|
84 | # include <assert.h> |
---|
85 | #else |
---|
86 | # ifdef NDEBUG |
---|
87 | # define assert(ex) ; |
---|
88 | # else |
---|
89 | # define assert(ex) {\ |
---|
90 | if (! (ex)) {\ |
---|
91 | (void) fprintf(stderr,\ |
---|
92 | "Assertion failed: file %s, line %d\n\"%s\"\n",\ |
---|
93 | __FILE__, __LINE__, "ex");\ |
---|
94 | (void) fflush(stdout);\ |
---|
95 | abort();\ |
---|
96 | }\ |
---|
97 | } |
---|
98 | # endif |
---|
99 | #endif |
---|
100 | |
---|
101 | #define fail(why) {\ |
---|
102 | (void) fprintf(stderr, "Fatal error: file %s, line %d\n%s\n",\ |
---|
103 | __FILE__, __LINE__, why);\ |
---|
104 | (void) fflush(stdout);\ |
---|
105 | abort();\ |
---|
106 | } |
---|
107 | |
---|
108 | /* |
---|
109 | * Support for ANSI function prototypes in non-ANSI compilers |
---|
110 | * |
---|
111 | * Usage: |
---|
112 | * extern int foo ARGS((char *, double)) |
---|
113 | */ |
---|
114 | |
---|
115 | #ifndef ARGS |
---|
116 | # ifdef __STDC__ |
---|
117 | # define ARGS(args) args |
---|
118 | # else |
---|
119 | # define ARGS(args) () |
---|
120 | # endif |
---|
121 | #endif |
---|
122 | |
---|
123 | #ifndef NULLARGS |
---|
124 | # ifdef __STDC__ |
---|
125 | # define NULLARGS (void) |
---|
126 | # else |
---|
127 | # define NULLARGS () |
---|
128 | # endif |
---|
129 | #endif |
---|
130 | |
---|
131 | /* |
---|
132 | * A little support for C++ compilers |
---|
133 | */ |
---|
134 | |
---|
135 | #ifdef __cplusplus |
---|
136 | # define EXTERN extern "C" |
---|
137 | #else |
---|
138 | # define EXTERN extern |
---|
139 | #endif |
---|
140 | |
---|
141 | /* |
---|
142 | * Support to define unused varibles |
---|
143 | */ |
---|
144 | #if defined (__GNUC__) |
---|
145 | #if (__GNUC__ >2 || __GNUC_MINOR__ >=7) && !defined(UNUSED) |
---|
146 | #define UNUSED __attribute__ ((unused)) |
---|
147 | #else |
---|
148 | #define UNUSED |
---|
149 | #endif |
---|
150 | #else |
---|
151 | #define UNUSED |
---|
152 | #endif |
---|
153 | |
---|
154 | /* |
---|
155 | * A neater way to define zero pointers |
---|
156 | * |
---|
157 | * Usage: |
---|
158 | * int * fred; |
---|
159 | * fred = NIL(int); |
---|
160 | */ |
---|
161 | |
---|
162 | #define NIL(type) ((type *) 0) |
---|
163 | |
---|
164 | /* #define USE_MM */ |
---|
165 | |
---|
166 | #ifdef USE_MM |
---|
167 | /* |
---|
168 | * assumes the memory manager is libmm.a (a deprecated (?) Octtools library) |
---|
169 | * - allows malloc(0) or realloc(obj, 0) |
---|
170 | * - catches out of memory (and calls MMout_of_memory()) |
---|
171 | * - catch free(0) and realloc(0, size) in the macros |
---|
172 | */ |
---|
173 | # define ALLOC(type, num) \ |
---|
174 | ((type *) malloc(sizeof(type) * (num))) |
---|
175 | # define REALLOC(type, obj, num) \ |
---|
176 | (obj) ? ((type *) realloc((void *) obj, sizeof(type) * (num))) : \ |
---|
177 | ((type *) malloc(sizeof(type) * (num))) |
---|
178 | # define FREE(obj) \ |
---|
179 | ((obj) ? (free((void *) (obj)), (obj) = 0) : 0) |
---|
180 | #else |
---|
181 | /* |
---|
182 | * enforce strict semantics on the memory allocator |
---|
183 | */ |
---|
184 | # define ALLOC(type, num) \ |
---|
185 | ((type *) MMalloc(sizeof(type) * (unsigned long) (num))) |
---|
186 | # define REALLOC(type, obj, num) \ |
---|
187 | ((type *) MMrealloc((void *) (obj), sizeof(type) * (unsigned long) (num))) |
---|
188 | # define FREE(obj) \ |
---|
189 | ((obj) ? (free((void *) (obj)), (obj) = 0) : 0) |
---|
190 | #endif |
---|
191 | |
---|
192 | #ifndef TRUE |
---|
193 | # define TRUE 1 |
---|
194 | #endif |
---|
195 | |
---|
196 | #ifndef FALSE |
---|
197 | # define FALSE 0 |
---|
198 | #endif |
---|
199 | |
---|
200 | #ifndef ABS |
---|
201 | # define ABS(a) ((a) < 0 ? -(a) : (a)) |
---|
202 | #endif |
---|
203 | |
---|
204 | #ifndef MAX |
---|
205 | # define MAX(a,b) ((a) > (b) ? (a) : (b)) |
---|
206 | #endif |
---|
207 | |
---|
208 | #ifndef MIN |
---|
209 | # define MIN(a,b) ((a) < (b) ? (a) : (b)) |
---|
210 | #endif |
---|
211 | |
---|
212 | #define ptime() util_cpu_time() |
---|
213 | #define print_time(t) util_print_time(t) |
---|
214 | |
---|
215 | #ifndef HUGE_VAL |
---|
216 | # ifndef HUGE |
---|
217 | # define HUGE 8.9884656743115790e+307 |
---|
218 | # endif |
---|
219 | # define HUGE_VAL HUGE |
---|
220 | #endif |
---|
221 | |
---|
222 | #ifndef MAXINT |
---|
223 | # define MAXINT (1 << 30) |
---|
224 | #endif |
---|
225 | |
---|
226 | EXTERN void util_print_cpu_stats ARGS((FILE *)); |
---|
227 | EXTERN long util_cpu_time ARGS((void)); |
---|
228 | EXTERN long util_cpu_ctime ARGS((void)); |
---|
229 | EXTERN void util_getopt_reset ARGS((void)); |
---|
230 | EXTERN int util_getopt ARGS((int, char **, char *)); |
---|
231 | EXTERN int util_check_file ARGS((char *, char *)); |
---|
232 | EXTERN char *util_path_search ARGS((char *)); |
---|
233 | EXTERN char *util_file_search ARGS((char *, char *, char *)); |
---|
234 | EXTERN char *util_print_time ARGS((long)); |
---|
235 | EXTERN int util_save_image ARGS((char *, char *)); |
---|
236 | EXTERN char *util_strsav ARGS((char *)); |
---|
237 | EXTERN char *util_inttostr ARGS((int)); |
---|
238 | EXTERN char *util_strcat3 ARGS((char *, char *, char *)); |
---|
239 | EXTERN char *util_strcat4 ARGS((char *, char *, char *, char *)); |
---|
240 | EXTERN int util_do_nothing ARGS((void)); |
---|
241 | EXTERN char *util_tilde_expand ARGS((char *)); |
---|
242 | EXTERN char *util_tempnam ARGS((char *, char *)); |
---|
243 | EXTERN FILE *util_tmpfile ARGS((void)); |
---|
244 | EXTERN void util_srandom ARGS((long)); |
---|
245 | EXTERN long util_random ARGS((void)); |
---|
246 | EXTERN long getSoftDataLimit ARGS((void)); |
---|
247 | EXTERN void MMout_of_memory ARGS((unsigned long)); |
---|
248 | EXTERN void *MMalloc ARGS((unsigned long)); |
---|
249 | EXTERN void *MMrealloc ARGS((void *, unsigned long)); |
---|
250 | EXTERN void MMfree ARGS((void *)); |
---|
251 | |
---|
252 | /* |
---|
253 | * Global variables for util_getopt() |
---|
254 | */ |
---|
255 | |
---|
256 | extern int util_optind; |
---|
257 | extern char *util_optarg; |
---|
258 | |
---|
259 | /**AutomaticStart*************************************************************/ |
---|
260 | |
---|
261 | /*---------------------------------------------------------------------------*/ |
---|
262 | /* Function prototypes */ |
---|
263 | /*---------------------------------------------------------------------------*/ |
---|
264 | |
---|
265 | /**AutomaticEnd***************************************************************/ |
---|
266 | |
---|
267 | #endif /* _UTIL */ |
---|