f02ea8dd6b27c01201fa949e409a08d91b7d5cc3
[ctsim.git] / include / kstddef.h
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: kstddef.h,v 1.12 2000/06/05 01:32:45 kevin Exp $
6 **  $Log: kstddef.h,v $
7 **  Revision 1.12  2000/06/05 01:32:45  kevin
8 **  Added C++ compatibility
9 **
10 **  Revision 1.11  2000/06/03 06:29:08  kevin
11 **  *** empty log message ***
12 **
13 **  Revision 1.10  2000/05/16 04:33:17  kevin
14 **  Updated documentation
15 **
16 **  Revision 1.9  2000/05/11 01:04:44  kevin
17 **  Added Microsoft Windows compatibility
18 **
19 **  Revision 1.8  2000/05/08 20:00:48  kevin
20 **  ANSI C changes
21 **
22 **  Revision 1.7  2000/05/07 12:46:19  kevin
23 **  made c++ compatible
24 **
25 **  Revision 1.6  2000/05/03 19:51:41  kevin
26 **  function renaming for phantoms and phantom elements
27 **
28 **  Revision 1.5  2000/05/02 20:00:25  kevin
29 **  *** empty log message ***
30 **
31 **  Revision 1.4  2000/04/28 18:00:55  kevin
32 **  remove unused files
33 **
34 **  Revision 1.3  2000/04/28 17:38:16  kevin
35 **  Removed unused files
36 **
37 **  Revision 1.2  2000/04/28 14:14:16  kevin
38 **  *** empty log message ***
39 **
40 **
41 **  This program is free software; you can redistribute it and/or modify
42 **  it under the terms of the GNU General Public License (version 2) as
43 **  published by the Free Software Foundation.
44 **
45 **  This program is distributed in the hope that it will be useful,
46 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
47 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
48 **  GNU General Public License for more details.
49 **
50 **  You should have received a copy of the GNU General Public License
51 **  along with this program; if not, write to the Free Software
52 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
53 ******************************************************************************/
54
55 /******************************************************************************
56  *
57  * FILE IDENTIFICATION
58  *
59  *      File Name:      STDDEF.H
60  *      Author:         Kevin Rosenberg
61  *      Purpose:        Header file containing KRL standard C definitions
62  *      Date Started:   Dec. 83
63  *
64  * DESCRIPTION
65  *      Header file contains KRL standard C definitions
66  *
67  * MODIFICATION LOG
68  *
69  *****************************************************************************/
70
71 #ifndef STDDEF_H
72 #define STDDEF_H
73
74 #ifdef HAVE_CONFIG_H
75 #include "config.h"
76 #endif
77
78 #ifdef _WIN32
79 #define snprintf _snprintf
80 #endif
81
82 #if !defined(bool) && !defined(__cplusplus)
83 typedef int bool;               /* Boolean variable type */
84 #endif
85
86 #define STR_MAX_LEN 255
87 #define STR_SIZE    STR_MAX_LEN+1
88
89 typedef unsigned char string[STR_SIZE];
90
91 #ifdef __cplusplus
92 extern "C" {
93 #endif /* __cplusplus */
94
95
96 #include <stdio.h>
97 #include <stdlib.h>
98 #include <string.h>
99 #include <stdarg.h>
100
101 #undef SHELL
102
103 #define TRUE    1
104 #define FALSE   0
105 #define OK      TRUE
106 #define ERROR   FALSE
107 #define YES     TRUE
108 #define NO      FALSE
109
110 /*----------------------------------------------------------------------*/
111
112 #define SHOW(var, fmt)  fprintf (stderr, "var = fmt\n", var)
113
114 /*----------------------------------------------------------------------*/
115
116 #define NEWLINE '\n'
117 #define TAB     '\t'
118 #define EOS     '\0'
119 #define BLANK   ' '
120 #define ETX     26      /* end of text signal for PC-DOS */
121
122 /*----------------------------------------------------------------------*/
123
124 #define MAXPATHNAME  65         /* max length of pathname + 1 */
125 #define MAXFILENAME  13         /* rootname(8) + '.'(1) + extension(3) + EOS */
126 #define MAXROOTNAME   8
127 #define MAXEXTNAME    3
128 #define MAXFULLNAME  MAXPATHNAME + MAXFILENAME
129
130 /*----------------------------------------------------------------------*/
131
132 #define ABS(x)    ((x) < 0 ? -(x) : (x))
133 #define SQR(x)    ((x) * (x))
134
135 #ifndef MAX
136 #define MAX(a,b)  ((a) > (b) ? (a) : (b))
137 #endif
138 #ifndef MIN
139 #define MIN(a,b)  ((a) <= (b) ? (a) : (b))
140 #endif
141 #define ISWAP(a,b) {int i; i = a; a = b; b = i;}
142
143 #define CLIP(n,lb,ub)   if (n < lb) n = lb; else if (n > ub) n = ub
144 #define STR_EQUAL(s1,s2) (strcmp (s1, s2) == 0)
145
146 /*----------------------------------------------------------------------*/
147
148 struct time_st {
149         int hour, minute, second, ms;
150 };
151
152 struct date_st {
153         int year, month, date, dow;
154 };
155
156 struct timedate_st {
157     struct time_st t;
158     struct date_st d;
159 };
160
161 typedef struct time_st TIME;
162 typedef struct date_st DATE;
163 typedef struct timedate_st TIMEDATE;
164
165 /*----------------------------------------------------------------------*/
166
167 #define ERR_WARNING     0
168 #define ERR_SEVERE      1
169 #define ERR_FATAL       2
170
171 /*----------------------------------------------------------------------*/
172
173
174 /* codes for open command */
175 #if MICROSOFT
176 #define OPEN_RDONLY  O_RDONLY                   /* other system use standard codes */
177 #define OPEN_WRONLY  O_WRONLY                   /* for binary */
178 #define OPEN_RDWR    O_RDWR
179 #else
180 #define OPEN_RDONLY  0                  /* other system use standard codes */
181 #define OPEN_WRONLY  1                  /* for binary */
182 #define OPEN_RDWR    2
183 #endif
184
185 /*----------------------------------------------------------------------*/
186
187 /* codes for input (inp_*) routines */
188
189 #define INP_ERROR   -1
190 #define INP_NO      FALSE
191 #define INP_YES     TRUE
192 #define INP_NEITHER 'b'
193 #define INP_RETURN  'r'
194 #define INP_BREAK   0x8000
195
196 #ifndef O_BINARY
197 #define O_BINARY (0)
198 #endif
199
200 #ifndef S_IWRITE
201 #define S_IWRITE S_IWUSR
202 #endif
203
204 /*----------------------------------------------------------------------*/
205
206 #if defined(MICROSOFT) || ! defined(SIZEOF_INT)
207    #define SIZEOF_INT 4
208    #define SIZEOF_LONG 4
209    #define SIZEOF_SHORT 2
210    #define SIZEOF_FLOAT 4
211    #define SIZEOF_DOUBLE 8
212 #endif
213
214 #if SIZEOF_INT == 4
215     typedef int kint32;
216     typedef unsigned int kuint32;
217 #elif SIZEOF_LONG == 4
218     typedef long int kint32;
219     typedef unsigned int kuint32;
220 #endif
221
222 #if SIZEOF_SHORT == 2
223     typedef short int kint16;
224     typedef unsigned short int kuint16;
225 #elif SIZEOF_INT == 2
226     typedef int kint16;
227     typedef unsigned int kuint16;
228 #endif
229
230 #if SIZEOF_FLOAT == 4
231     typedef float kfloat32;
232 #endif
233 #if SIZEOF_DOUBLE == 8
234     typedef double kfloat64;
235 #endif
236
237 /* allocnum.c */
238 float *alloc_float(int n);
239 double *alloc_double(int n);
240 int *alloc_int(int n);
241
242 /* fexist.c */
243 int file_exists(const char *fname);
244
245 /* kbasename.c */
246 char *kbasename(const char *filename);
247
248 /* iclip.c */
249 int iclip(int n, int lb, int ub);
250
251 /* s_head.c */
252 char *str_skip_head(const char *str, const char *charlist);
253
254 /* s_lower.c */
255 char *str_lower(char *s);
256
257 /* s_rmtail.c */
258 char *str_wrm_tail(char *str);
259 char *str_rm_tail(char *str, const char *charlist);
260
261 /* s_save.c */
262 char *str_save(const char *s);
263
264 /* s_upper.c */
265 char *str_upper(char *str);
266
267 /* sysalloc.c */
268 void *sys_alloc(const int nbytes, const char *name);
269
270 /* syserror.c */
271 void sys_error(int severity, const char *msg, ...);
272 void sys_verror(int severity, const char *msg, va_list arg);
273 void sys_error_level(int severity);
274
275 /* sysfopen.c */
276 FILE *sys_fopen(const char *filename, const char *mode, const char *progname);
277
278 /* sysfree.c */
279 void sys_free(void *ptr, const char *name);
280
281 /* timedate.c */
282 DATE *td_get_date(DATE *d);
283 TIME *td_get_time(TIME *t);
284 double td_current_sec(void);
285 double td_time_to_sec(TIME *t);
286 TIME *td_time_sub(const TIME *t1, const TIME *t2, TIME *tdiff);
287 TIME *td_time_add(const TIME *t1, const TIME *t2, TIME *tsum);
288 TIME *td_time_copy(TIME *to, const TIME *from);
289 TIME *td_time_norm(TIME *t);
290 void td_get_tmdt(TIMEDATE *td);
291 const char *td_str_tmdt(const TIMEDATE *td);
292 const char *td_str_time(const TIME *t);
293 const char *td_str_stime(const TIME *t);
294 const char *td_str_date(const DATE *d);
295 char *td_str_cdate(DATE *d);
296 char *td_month_name(int n);
297 char *td_day_name(int n);
298
299 /* netorder.c */
300 void *strreverse (void *dest, const void *src, size_t n);
301 int read_n32bint (kuint32 *n, int fd);
302 int write_n32bint (kuint32 const *n, int fd);
303 int read_nfloat32 (float *f, int fd);
304 int write_nfloat32 (float const *f, int fd);
305 int read_nfloat64 (double *d, int fd);
306 int write_nfloat64 (double const *d, int fd);
307
308 #ifdef __cplusplus
309 }
310 #endif /* __cplusplus */
311
312 #endif