[13] | 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.19 2009/04/11 02:04:46 fabio Exp $] |
---|
| 34 | |
---|
| 35 | ******************************************************************************/ |
---|
| 36 | |
---|
| 37 | #ifndef _UTIL |
---|
| 38 | #define _UTIL |
---|
| 39 | |
---|
| 40 | #include <stdio.h> |
---|
| 41 | #include <ctype.h> |
---|
| 42 | #include <math.h> |
---|
| 43 | |
---|
| 44 | #if HAVE_UNISTD_H |
---|
| 45 | # include <unistd.h> |
---|
| 46 | #endif |
---|
| 47 | |
---|
| 48 | #if HAVE_SYS_TYPES_H |
---|
| 49 | # include <sys/types.h> |
---|
| 50 | #endif |
---|
| 51 | |
---|
| 52 | #if HAVE_VARARGS_H |
---|
| 53 | # include <varargs.h> |
---|
| 54 | #endif |
---|
| 55 | |
---|
| 56 | #if STDC_HEADERS |
---|
| 57 | # include <stdlib.h> |
---|
| 58 | # include <string.h> |
---|
| 59 | #else |
---|
| 60 | # ifdef HAVE_STRCHR |
---|
| 61 | char * strchr(const char *, int); |
---|
| 62 | int strcmp(const char *, const char *); |
---|
| 63 | # else |
---|
| 64 | # define strchr index |
---|
| 65 | # endif |
---|
| 66 | # ifdef HAVE_GETENV |
---|
| 67 | char * getenv(const char *); |
---|
| 68 | # endif |
---|
| 69 | #endif /* STDC_HEADERS */ |
---|
| 70 | |
---|
| 71 | #if HAVE_ERRNO_H |
---|
| 72 | # include <errno.h> |
---|
| 73 | #endif |
---|
| 74 | |
---|
| 75 | /* |
---|
| 76 | * Ensure we have reasonable assert() and fail() functions |
---|
| 77 | */ |
---|
| 78 | |
---|
| 79 | #if HAVE_ASSERT_H |
---|
| 80 | # include <assert.h> |
---|
| 81 | #else |
---|
| 82 | # ifdef NDEBUG |
---|
| 83 | # define assert(ex) ; |
---|
| 84 | # else |
---|
| 85 | # define assert(ex) {\ |
---|
| 86 | if (! (ex)) {\ |
---|
| 87 | (void) fprintf(stderr,\ |
---|
| 88 | "Assertion failed: file %s, line %d\n\"%s\"\n",\ |
---|
| 89 | __FILE__, __LINE__, "ex");\ |
---|
| 90 | (void) fflush(stdout);\ |
---|
| 91 | abort();\ |
---|
| 92 | }\ |
---|
| 93 | } |
---|
| 94 | # endif |
---|
| 95 | #endif |
---|
| 96 | |
---|
| 97 | #define fail(why) {\ |
---|
| 98 | (void) fprintf(stderr, "Fatal error: file %s, line %d\n%s\n",\ |
---|
| 99 | __FILE__, __LINE__, why);\ |
---|
| 100 | (void) fflush(stdout);\ |
---|
| 101 | abort();\ |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /* |
---|
| 105 | * Support for ANSI function prototypes in non-ANSI compilers |
---|
| 106 | * |
---|
| 107 | * Usage: |
---|
| 108 | * extern int foo ARGS((char *, double)) |
---|
| 109 | */ |
---|
| 110 | |
---|
| 111 | #ifndef ARGS |
---|
| 112 | # ifdef __STDC__ |
---|
| 113 | # define ARGS(args) args |
---|
| 114 | # else |
---|
| 115 | # define ARGS(args) () |
---|
| 116 | # endif |
---|
| 117 | #endif |
---|
| 118 | |
---|
| 119 | #ifndef NULLARGS |
---|
| 120 | # ifdef __STDC__ |
---|
| 121 | # define NULLARGS (void) |
---|
| 122 | # else |
---|
| 123 | # define NULLARGS () |
---|
| 124 | # endif |
---|
| 125 | #endif |
---|
| 126 | |
---|
| 127 | /* |
---|
| 128 | * A little support for C++ compilers |
---|
| 129 | */ |
---|
| 130 | |
---|
| 131 | #ifdef __cplusplus |
---|
| 132 | # define EXTERN extern "C" |
---|
| 133 | #else |
---|
| 134 | # define EXTERN extern |
---|
| 135 | #endif |
---|
| 136 | |
---|
| 137 | /* |
---|
| 138 | * Support to define unused varibles |
---|
| 139 | */ |
---|
| 140 | #if defined (__GNUC__) |
---|
| 141 | #if (__GNUC__ >2 || __GNUC_MINOR__ >=7) && !defined(UNUSED) |
---|
| 142 | #define UNUSED __attribute__ ((unused)) |
---|
| 143 | #else |
---|
| 144 | #define UNUSED |
---|
| 145 | #endif |
---|
| 146 | #else |
---|
| 147 | #define UNUSED |
---|
| 148 | #endif |
---|
| 149 | |
---|
| 150 | /* |
---|
| 151 | * A neater way to define zero pointers |
---|
| 152 | * |
---|
| 153 | * Usage: |
---|
| 154 | * int * fred; |
---|
| 155 | * fred = NIL(int); |
---|
| 156 | */ |
---|
| 157 | |
---|
| 158 | #define NIL(type) ((type *) 0) |
---|
| 159 | |
---|
| 160 | /* #define USE_MM */ |
---|
| 161 | |
---|
| 162 | #ifdef USE_MM |
---|
| 163 | /* |
---|
| 164 | * assumes the memory manager is libmm.a (a deprecated (?) Octtools library) |
---|
| 165 | * - allows malloc(0) or realloc(obj, 0) |
---|
| 166 | * - catches out of memory (and calls MMout_of_memory()) |
---|
| 167 | * - catch free(0) and realloc(0, size) in the macros |
---|
| 168 | */ |
---|
| 169 | # define ALLOC(type, num) \ |
---|
| 170 | ((type *) malloc(sizeof(type) * (num))) |
---|
| 171 | # define REALLOC(type, obj, num) \ |
---|
| 172 | (obj) ? ((type *) realloc((void *) obj, sizeof(type) * (num))) : \ |
---|
| 173 | ((type *) malloc(sizeof(type) * (num))) |
---|
| 174 | # define FREE(obj) \ |
---|
| 175 | ((obj) ? (free((void *) (obj)), (obj) = 0) : 0) |
---|
| 176 | #else |
---|
| 177 | /* |
---|
| 178 | * enforce strict semantics on the memory allocator |
---|
| 179 | */ |
---|
| 180 | # define ALLOC(type, num) \ |
---|
| 181 | ((type *) MMalloc(sizeof(type) * (unsigned long) (num))) |
---|
| 182 | # define REALLOC(type, obj, num) \ |
---|
| 183 | ((type *) MMrealloc((void *) (obj), sizeof(type) * (unsigned long) (num))) |
---|
| 184 | # define FREE(obj) \ |
---|
| 185 | ((obj) ? (free((void *) (obj)), (obj) = 0) : 0) |
---|
| 186 | #endif |
---|
| 187 | |
---|
| 188 | #ifndef TRUE |
---|
| 189 | # define TRUE 1 |
---|
| 190 | #endif |
---|
| 191 | |
---|
| 192 | #ifndef FALSE |
---|
| 193 | # define FALSE 0 |
---|
| 194 | #endif |
---|
| 195 | |
---|
| 196 | #ifndef ABS |
---|
| 197 | # define ABS(a) ((a) < 0 ? -(a) : (a)) |
---|
| 198 | #endif |
---|
| 199 | |
---|
| 200 | #ifndef MAX |
---|
| 201 | # define MAX(a,b) ((a) > (b) ? (a) : (b)) |
---|
| 202 | #endif |
---|
| 203 | |
---|
| 204 | #ifndef MIN |
---|
| 205 | # define MIN(a,b) ((a) < (b) ? (a) : (b)) |
---|
| 206 | #endif |
---|
| 207 | |
---|
| 208 | #define ptime() util_cpu_time() |
---|
| 209 | #define print_time(t) util_print_time(t) |
---|
| 210 | |
---|
| 211 | #ifndef HUGE_VAL |
---|
| 212 | # ifndef HUGE |
---|
| 213 | # define HUGE 8.9884656743115790e+307 |
---|
| 214 | # endif |
---|
| 215 | # define HUGE_VAL HUGE |
---|
| 216 | #endif |
---|
| 217 | |
---|
| 218 | #ifndef MAXINT |
---|
| 219 | # define MAXINT (1 << 30) |
---|
| 220 | #endif |
---|
| 221 | |
---|
| 222 | EXTERN void util_print_cpu_stats ARGS((FILE *)); |
---|
| 223 | EXTERN long util_cpu_time ARGS((void)); |
---|
| 224 | EXTERN long util_cpu_ctime ARGS((void)); |
---|
| 225 | EXTERN void util_getopt_reset ARGS((void)); |
---|
| 226 | EXTERN int util_getopt ARGS((int, char * const *, char const *)); |
---|
| 227 | EXTERN int util_check_file ARGS((char const *, char const *)); |
---|
| 228 | EXTERN char *util_path_search ARGS((char const *)); |
---|
| 229 | EXTERN char *util_file_search ARGS((char const *, char *, char const *)); |
---|
| 230 | EXTERN char *util_print_time ARGS((long)); |
---|
| 231 | EXTERN int util_save_image ARGS((char const *, char const *)); |
---|
| 232 | EXTERN char *util_strsav ARGS((char const *)); |
---|
| 233 | EXTERN char *util_inttostr ARGS((int)); |
---|
| 234 | EXTERN char *util_strcat3 ARGS((char const *, char const *, char const *)); |
---|
| 235 | EXTERN char *util_strcat4 ARGS((char const *, char const *, char const *, char const *)); |
---|
| 236 | EXTERN int util_do_nothing ARGS((void)); |
---|
| 237 | EXTERN char *util_tilde_expand ARGS((char const *)); |
---|
| 238 | EXTERN char *util_tempnam ARGS((char const *, char const *)); |
---|
| 239 | EXTERN FILE *util_tmpfile ARGS((void)); |
---|
| 240 | EXTERN void util_srandom ARGS((long)); |
---|
| 241 | EXTERN long util_random ARGS((void)); |
---|
| 242 | EXTERN unsigned long getSoftDataLimit ARGS((void)); |
---|
| 243 | EXTERN void MMout_of_memory ARGS((unsigned long)); |
---|
| 244 | EXTERN void *MMalloc ARGS((unsigned long)); |
---|
| 245 | EXTERN void *MMrealloc ARGS((void *, unsigned long)); |
---|
| 246 | EXTERN void MMfree ARGS((void *)); |
---|
| 247 | |
---|
| 248 | /* |
---|
| 249 | * Global variables for util_getopt() |
---|
| 250 | */ |
---|
| 251 | |
---|
| 252 | extern int util_optind; |
---|
| 253 | extern char *util_optarg; |
---|
| 254 | |
---|
| 255 | /**AutomaticStart*************************************************************/ |
---|
| 256 | |
---|
| 257 | /*---------------------------------------------------------------------------*/ |
---|
| 258 | /* Function prototypes */ |
---|
| 259 | /*---------------------------------------------------------------------------*/ |
---|
| 260 | |
---|
| 261 | /**AutomaticEnd***************************************************************/ |
---|
| 262 | |
---|
| 263 | #endif /* _UTIL */ |
---|