1 /*  Last edited: Dec 21 13:45 1998 (fw) */
2 
3 /* $Id: regular.h,v 1.1 2002/11/14 20:00:06 lstein Exp $ */
4 
5 /***************************************************************
6  *  File regular.h  : header file for ACEDB utility functions
7  *  Author: Richard Durbin (rd@sanger.ac.uk)
8  *  Copyright (C) J Thierry-Mieg and R Durbin, 1994
9  *-------------------------------------------------------------------
10  * This file is part of the ACEDB genome database package, written by
11  * 	Richard Durbin (MRC LMB, UK) rd@mrc-lmb.cam.ac.uk, and
12  *	Jean Thierry-Mieg (CRBM du CNRS, France) mieg@kaa.cnrs-mop.fr
13  *
14  * HISTORY:
15  * Last edited: Aug 20 11:50 1997 (rbrusk)
16  * * Sep  9 16:54 1998 (edgrif): Add messErrorInit decl.
17  * * Sep  9 14:31 1998 (edgrif): Add filGetFilename decl.
18  * * Aug 20 11:50 1998 (rbrusk): AUL_FUNC_DCL
19  * * Sep  3 11:50 1998 (edgrif): Add macro version of messcrash to give
20  *              file/line info for debugging.
21  * Created: 1991 (rd)
22  *-------------------------------------------------------------------
23  */
24 
25 #ifndef DEF_REGULAR_H
26 #define DEF_REGULAR_H
27 
28 				/* library EXPORT/IMPORT symbols */
29 #if defined (WIN32)
30 #include "win32libspec.h"  /* must come before mystdlib.h...*/
31 #else
32 #define UTIL_FUNC_DCL
33 #define UTIL_VAR_DCL	extern
34 #define UTIL_FUNC_DEF
35 #define UTIL_VAR_DEF
36 #endif
37 
38 #include "mystdlib.h"   /* contains full prototypes of system calls */
39 
40 #if defined(WIN32)
41 #if defined(_DEBUG)
42 #define MEM_DEBUG /* must be defined here, before acelibspec.h */
43 #include <crtdbg.h>
44 #endif
45 UTIL_VAR_DCL char* linkDate ;
46 UTIL_VAR_DCL int isInteractive ;      /* can set FALSE, i.e. in tace */
47 #endif
48 
49 #ifdef FALSE
50   typedef int BOOL ;
51 #else
52   typedef enum {FALSE=0,TRUE=1} BOOL ;
53 #endif
54 
55 
56 
57 typedef unsigned char UCHAR ; /* for convenience */
58 
59 typedef unsigned int KEY ;
60 
61 typedef void (*VoidRoutine)(void) ;
62 typedef void (*Arg1Routine)(void *arg1) ;
63 
64 /* magic_t : the type that all magic symbols are declared of.
65    They become magic (i.e. unique) by using the pointer
66    to that unique symbol, which has been placed somewhere
67    in the address space by the compiler */
68 /* type-magics and associator codes are defined at
69    magic_t MYTYPE_MAGIC = "MYTYPE";
70    The address of the string is then used as the unique
71    identifier (as type->magic or graphAssXxx-code), and the
72    string can be used during debugging */
73 typedef char* magic_t;
74 
75 
76 
77 typedef struct freestruct
78   { KEY  key ;
79     char *text ;
80   } FREEOPT ;
81 
82 
83 /*---------------------------------------------------------------------*/
84 /* The free package for reading from files/stdout, see freesubs.c      */
85 /*                                                                     */
86 
87 UTIL_FUNC_DCL void freeinit (void) ;
88 UTIL_FUNC_DCL int freeCurrLevel(void) ;			    /* Returns current level. */
89 UTIL_FUNC_DCL char* freecard (int level) ;	/* 0 if below level (returned by freeset*) */
90 UTIL_FUNC_DCL void freecardback (void) ;  /* goes back one card */
91 UTIL_FUNC_DCL void freeforcecard (char *string);
92 UTIL_FUNC_DCL int  freesettext (char *string, char *parms) ; /* returns level to be used in freecard () */
93 UTIL_FUNC_DCL int  freesetfile (FILE *fil, char *parms) ;
94 UTIL_FUNC_DCL int  freesetpipe (FILE *fil, char *parms) ;  /* will call pclose */
95 UTIL_FUNC_DCL void freeclose(int level) ; /* closes the above */
96 UTIL_FUNC_DCL void freespecial (char *set) ;	/* set of chars to be recognized from "\n;/%\\@$" */
97 UTIL_FUNC_DCL BOOL freeread (FILE *fil) ;	/* returns FALSE if EOF */
98 UTIL_FUNC_DCL int  freeline (FILE *fil) ;	/* line number in file */
99 UTIL_FUNC_DCL int  freestreamline (int level) ;/* line number in stream(level)*/
100 UTIL_FUNC_DCL char *freeword (void) ;
101 
102 #if defined(WIN32)  /* A variation to correctly parse MS DOS/Windows pathnames */
103 UTIL_FUNC_DCL   char *freepath (void) ;
104 #else	/* NOT defined(WIN32) */
105 #define freepath freeword  /* freeword() works fine if not in WIN32 */
106 #endif	/* defined(WIN32) */
107 
108 UTIL_FUNC_DCL char *freewordcut (char *cutset, char *cutter) ;
109 UTIL_FUNC_DCL void freeback (void) ;		/* goes back one word */
110 UTIL_FUNC_DCL BOOL freeint (int *p) ;
111 UTIL_FUNC_DCL BOOL freefloat (float *p) ;
112 UTIL_FUNC_DCL BOOL freedouble (double *p) ;
113 UTIL_FUNC_DCL BOOL freekey (KEY *kpt, FREEOPT *options) ;
114 UTIL_FUNC_DCL BOOL freekeymatch (char *text, KEY *kpt, FREEOPT *options) ;
115 UTIL_FUNC_DCL void freemenu (void (*proc)(KEY), FREEOPT *options) ;
116 UTIL_FUNC_DCL char *freekey2text (KEY k, FREEOPT *o)  ;  /* Return text corresponding to key */
117 UTIL_FUNC_DCL BOOL freeselect (KEY *kpt, FREEOPT *options) ;
118 UTIL_FUNC_DCL BOOL freelevelselect (int level,
119 				    KEY *kpt, FREEOPT *options);
120 UTIL_FUNC_DCL void freedump (FREEOPT *options) ;
121 UTIL_FUNC_DCL BOOL freestep (char x) ;
122 UTIL_FUNC_DCL void freenext (void) ;
123 UTIL_FUNC_DCL BOOL freeprompt (char *prompt, char *dfault, char *fmt) ;/* gets a card */
124 UTIL_FUNC_DCL BOOL freecheck (char *fmt) ;	/* checks remaining card fits fmt */
125 UTIL_FUNC_DCL int  freefmtlength (char *fmt) ;
126 UTIL_FUNC_DCL BOOL freequery (char *query) ;
127 UTIL_FUNC_DCL char *freepos (void) ;		/* pointer to present position in card */
128 UTIL_FUNC_DCL char *freeprotect (char* text) ; /* protect so freeword() reads correctly */
129 UTIL_FUNC_DCL char* freeunprotect (char *text) ; /* reverse of protect, removes \ etc */
130 
131 UTIL_VAR_DCL char FREE_UPPER[] ;
132 #define freeupper(x)	(FREE_UPPER[(x) & 0xff])  /* table is only 128 long */
133 
134 UTIL_VAR_DCL char FREE_LOWER[] ;
135 #define freelower(x)	(FREE_LOWER[(x) & 0xff])
136 
137 
138 
139 
140 /**********************************************************************/
141 /******************** message routines - messubs.c ********************/
142 /**********************************************************************/
143 
144 /* 'Internal' functions, do not call directly.                               */
145 UTIL_FUNC_DCL void uMessSetErrorOrigin(char *filename, int line_num) ;
146 UTIL_FUNC_DCL void uMessCrash(char *format, ...) ;
147 
148 /* External Interface.                                                       */
149 /* Note that messcrash is a macro and that it makes use of the ',' operator  */
150 /* in C. This means that the messcrash macro will only produce a single C    */
151 /* statement and hence can be used within brackets etc. and will not break   */
152 /* existing code, e.g.                                                       */
153 /*                     funcblah(messcrash("hello")) ;                        */
154 /* will become:                                                              */
155 /* funcblah(uMessSetErrorOrigin(__FILE__, __LINE__), uMessCrash("hello")) ;  */
156 /*                                                                           */
157 
158 UTIL_FUNC_DEF void messErrorInit (char *progname) ; /* Record the
159 					 applications name for use
160 					 in error messages, etc */
161 UTIL_FUNC_DEF char *messGetErrorProgram (void) ; /* Returns the
162 						    application name */
163 
164 UTIL_FUNC_DCL char *messprintf (char *format, ...) ;
165 				/* sprintf into (static!) string */
166 				/* !!!! beware finite buffer size !!!! */
167 
168 UTIL_FUNC_DCL void messbeep (void) ; /* make a beep */
169 
170 UTIL_FUNC_DCL void messout (char *format, ...) ;  /* simple message */
171 UTIL_FUNC_DCL void messdump (char *format, ...) ; /* write to log file */
172 UTIL_FUNC_DCL void messerror (char *format, ...) ; /* error message and write to log file */
173 UTIL_FUNC_DCL void messExit(char *format, ...) ;  /* error message, write to log file & exit */
174 #define messcrash   uMessSetErrorOrigin(__FILE__, __LINE__), uMessCrash
175 						  /* abort - but see below */
176 UTIL_FUNC_DCL BOOL messQuery (char *text,...) ;	  /* ask yes/no question */
177 UTIL_FUNC_DCL BOOL messPrompt (char *prompt, char *dfault, char *fmt) ;
178 	/* ask for data satisfying format get results via freecard() */
179 
180 UTIL_FUNC_DCL char* messSysErrorText (void) ;
181 	/* wrapped system error message for use in messerror/crash() */
182 
183 UTIL_FUNC_DCL int messErrorCount (void);
184 	/* return numbers of error so far */
185 
186 UTIL_FUNC_DCL BOOL messIsInterruptCalled (void);
187 	/* return TRUE if an interrupt key has been pressed */
188 
189 /**** registration of callbacks for messubs ****/
190 
191 typedef void (*OutRoutine)(char*) ;
192 typedef BOOL (*QueryRoutine)(char*) ;
193 typedef BOOL (*PromptRoutine)(char*, char*, char*) ;
194 typedef BOOL (*IsInterruptRoutine)(void) ;
195 
196 UTIL_FUNC_DCL VoidRoutine	messBeepRegister (VoidRoutine func) ;
197 UTIL_FUNC_DCL OutRoutine	messOutRegister (OutRoutine func) ;
198 UTIL_FUNC_DCL OutRoutine	messDumpRegister (OutRoutine func) ;
199 UTIL_FUNC_DCL OutRoutine	messErrorRegister (OutRoutine func) ;
200 UTIL_FUNC_DCL OutRoutine	messExitRegister (OutRoutine func) ;
201 UTIL_FUNC_DCL OutRoutine	messCrashRegister (OutRoutine func) ;
202 UTIL_FUNC_DCL QueryRoutine	messQueryRegister (QueryRoutine func) ;
203 UTIL_FUNC_DCL PromptRoutine	messPromptRegister (PromptRoutine func) ;
204 UTIL_FUNC_DCL IsInterruptRoutine messIsInterruptRegister (IsInterruptRoutine func) ;
205 
206 /**** routines to catch crashes if necessary, e.g. when acedb dumping ****/
207 
208 #include <setjmp.h>
209 
210 UTIL_FUNC_DCL jmp_buf*	messCatchCrash (jmp_buf* ) ;
211 UTIL_FUNC_DCL jmp_buf*	messCatchError (jmp_buf* ) ;
212 UTIL_FUNC_DCL char*	messCaughtMessage (void) ;
213 
214   /* if a setjmp() stack context is set using messCatch*() then rather than
215      exiting or giving an error message, messCrash() and messError() will
216      longjmp() back to the context.
217      messCatch*() return the previous value. Use argument = 0 to reset.
218      messCaughtMessage() can be called from the jumped-to routine to get
219      the error message that would have been printed.
220   */
221 
222 /********************************************************************/
223 /************** memory management - memsubs.c ***********************/
224 /********************************************************************/
225 
226 typedef struct _STORE_HANDLE_STRUCT *STORE_HANDLE ; /* opaque outside memsubs.c */
227 
228 UTIL_FUNC_DCL STORE_HANDLE handleHandleCreate (STORE_HANDLE handle) ;
229 #define handleCreate() handleHandleCreate(0)
230 #define handleDestroy(handle) messfree(handle)
231 
232 #if defined(WIN32) && defined(_DEBUG)
233 #define MEM_DEBUG
234 #include <crtdbg.h>
235 #endif
236 
237 #if !defined(MEM_DEBUG)
238 
239 UTIL_FUNC_DCL void *handleAlloc (void (*final)(void *), STORE_HANDLE handle, int size) ;
240     /* handleAlloc is deprecated, use halloc, and blockSetFinalize instead */
241 UTIL_FUNC_DCL void *halloc(int size, STORE_HANDLE handle) ;
242 UTIL_FUNC_DCL char *strnew(char *old, STORE_HANDLE handle) ;
243 
244 #else		/* MEM_DEBUG from rbrusk */
245 
246 void *halloc_dbg(int size, STORE_HANDLE handle, const char *hfname, int hlineno) ;
247 UTIL_FUNC_DCL void *handleAlloc_dbg(void (*final)(void *), STORE_HANDLE handle, int size,
248 					  const char *hfname, int hlineno) ;
249 UTIL_FUNC_DCL char *strnew_dbg(char *old, STORE_HANDLE handle, const char *hfname, int hlineno) ;
250 #define halloc(s, h) halloc_dbg(s, h, __FILE__, __LINE__)
251 #define handleAlloc(f, h, s) handleAlloc_dbg(f, h, s, __FILE__, __LINE__)
252 #define strnew(o, h) strnew_dbg(o, h, __FILE__, __LINE__)
253 #define messalloc_dbg(size,fname,lineno) halloc_dbg(size, 0, fname, lineno)
254 
255 #endif
256 
257 UTIL_FUNC_DCL void blockSetFinalise(void *block, void (*final)(void *)) ;
258 UTIL_FUNC_DCL void handleSetFinalise(STORE_HANDLE handle, void (*final)(void *), void *arg) ;
259 UTIL_FUNC_DCL void handleInfo (STORE_HANDLE handle, int *number, int *size) ;
260 #define messalloc(size) halloc(size, 0)
261 UTIL_FUNC_DCL void umessfree (void *cp) ;
262 #define messfree(cp)  ((cp) ? umessfree((void*)(cp)),(cp)=0,TRUE : FALSE)
263 UTIL_FUNC_DCL void messalloccheck (void) ;	/* can be used anywhere - does nothing
264 				   unless MALLOC_CHECK set in messubs.c */
265 UTIL_FUNC_DCL int messAllocStatus (int *np) ; /* returns number of outstanding allocs
266 				   *np is total mem if MALLOC_CHECK */
267 
268 UTIL_FUNC_DCL int regExpMatch (char *cp,char *tp) ; /* in messubs.c CLH 5/23/95 */
269 
270 /********************************************************************/
271 /******** growable arrays and flexible stacks - arraysub.c **********/
272 /********************************************************************/
273 
274 /* to be included after the declarations of STORE_HANDLE etc. */
275 #include "array.h"
276 
277 /********************************************************************/
278 /************** file opening/closing from filsubs.c *****************/
279 /********************************************************************/
280 
281 UTIL_FUNC_DCL void filAddPath (char *path) ;	/* Adds a set of pathnames to the pathname stack */
282 UTIL_FUNC_DCL void filAddDir (char *dir) ;	/* Adds a single pathname to the pathname stack */
283 
284 /* returns an absolute path string for dir in relation to user's CWD */
285 /* returns pointer to internal static */
286 UTIL_FUNC_DCL char *filGetFullPath (char *dir);
287 
288 /* returns filename part of a pathname. */
289 /* returns pointer to internal static */
290 UTIL_FUNC_DCL char *filGetFilename(char *path);
291 
292 /* returns the file-extension part of a path or file-name */
293 /* returns pointer to internal static */
294 UTIL_FUNC_DCL char *filGetExtension(char *path);
295 
296 UTIL_FUNC_DCL char *filName (char *name, char *ending, char *spec) ;
297 UTIL_FUNC_DCL char *filStrictName (char *name, char *ending, char *spec) ;
298 
299 /* determines time since last modification, FALSE if no file */
300 UTIL_FUNC_DCL BOOL  filAge (char *name, char *ending,
301 			    int *diffYears, int *diffMonths, int *diffDays,
302 			    int *diffHours, int *diffMins, int *diffSecs);
303 
304 UTIL_FUNC_DCL FILE *filopen (char *name, char *ending, char *spec) ;
305 UTIL_FUNC_DCL FILE *filmail (char *address) ;
306 UTIL_FUNC_DCL void filclose (FILE* fil) ;
307 
308 UTIL_FUNC_DCL BOOL filremove (char *name, char *ending) ;
309 
310 UTIL_FUNC_DCL FILE *filtmpopen (char **nameptr, char *spec) ;
311 UTIL_FUNC_DCL BOOL filtmpremove (char *name) ;
312 UTIL_FUNC_DCL void filtmpcleanup (void) ;
313 
314 /* file chooser */
315 typedef FILE* (*QueryOpenRoutine)(char*, char*, char*, char*, char*) ;
316 UTIL_FUNC_DCL QueryOpenRoutine filQueryOpenRegister (QueryOpenRoutine new);
317 		/* allow graphic file choosers to be registered */
318 
319 UTIL_FUNC_DCL FILE *filqueryopen (char *dirname, char *filname,
320 				  char *ending, char *spec, char *title);
321 
322 	/* if dirname is given it should be DIR_BUFFER_SIZE long
323 	     and filname FILE_BUFFER_SIZE long
324 	   if not given, then default (static) buffers will be used */
325 
326 
327 /* directory access */
328 UTIL_FUNC_DCL Array filDirectoryCreate (char *dirName,
329 					char *ending,
330 					char *spec);
331 
332 UTIL_FUNC_DCL void filDirectoryDestroy (Array filDirArray);
333 
334 /*******************************************************************/
335 /************* randsubs.c random number generator ******************/
336 
337 UTIL_FUNC_DCL double randfloat (void) ;
338 UTIL_FUNC_DCL double randgauss (void) ;
339 UTIL_FUNC_DCL int randint (void) ;
340 UTIL_FUNC_DCL void randsave (int *arr) ;
341 UTIL_FUNC_DCL void randrestore (int *arr) ;
342 
343 
344 /* Unix debugging.                                                           */
345 /* put "break invokeDebugger" in your favourite debugger init file */
346 /* this function is empty, it is defined in messubs.c used in
347    messerror, messcrash and when ever you need it.
348 */
349 UTIL_FUNC_DCL void invokeDebugger(void) ;
350 
351 
352 
353 
354 /*******************************************************************/
355 /************* some WIN32 debugging utilities **********************/
356 
357 #if defined (WIN32)
358 #if defined(_DEBUG)
359 /* See win32util.cpp for these functions */
360 UTIL_FUNC_DCL const char *dbgPos( const char *caller, int lineno, const char *called ) ;
361 UTIL_FUNC_DCL void WinTrace(char *prompt, unsigned long code) ;
362 UTIL_FUNC_DCL void AceASSERT(int condition) ;
363 UTIL_FUNC_DCL void NoMemoryTracking() ;
364 #else   /* !defined(_DEBUG) */
365 #define dbgPos(c,l,fil)   (const char *)(fil)
366 #endif	/* !defined(_DEBUG) */
367 #endif	/* defined(WIN32) */
368 
369 #endif /* defined(DEF_REGULAR_H) */
370 
371 /******************************* End of File **********************************/
372