r117: *** empty log message ***
[ctsim.git] / include / ctsupport.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **      File Name:      ctsupport.h
5 **      Author:         Kevin Rosenberg
6 **      Purpose:        Header file for CT support library
7 **      Date Started:   Dec. 83
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: ctsupport.h,v 1.4 2000/06/22 10:17:28 kevin Exp $
13 **
14 **
15 **  This program is free software; you can redistribute it and/or modify
16 **  it under the terms of the GNU General Public License (version 2) as
17 **  published by the Free Software Foundation.
18 **
19 **  This program is distributed in the hope that it will be useful,
20 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
21 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 **  GNU General Public License for more details.
23 **
24 **  You should have received a copy of the GNU General Public License
25 **  along with this program; if not, write to the Free Software
26 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27 ******************************************************************************/
28
29 #ifndef CTSUPPORT_H
30 #define CTSUPPORT_H
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #ifdef _WIN32
37 #define snprintf _snprintf
38 #endif
39
40 #define STR_MAX_LEN 255
41 #define STR_SIZE    STR_MAX_LEN+1
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <stdarg.h>
47
48 #define TRUE    1
49 #define FALSE   0
50 #define OK      TRUE
51 #define ERROR   FALSE
52 #define YES     TRUE
53 #define NO      FALSE
54
55 /*----------------------------------------------------------------------*/
56
57 #define SHOW(var, fmt)  { cerr << "var = " << var << endl; }
58
59 /*----------------------------------------------------------------------*/
60
61 #define NEWLINE '\n'
62 #define TAB     '\t'
63 #define EOS     '\0'
64 #define BLANK   ' '
65
66 /*----------------------------------------------------------------------*/
67
68 struct time_st {
69         int hour, minute, second, ms;
70 };
71
72 struct date_st {
73         int year, month, date, dow;
74 };
75
76 struct timedate_st {
77     struct time_st t;
78     struct date_st d;
79 };
80
81 typedef struct time_st TIME;
82 typedef struct date_st DATE;
83 typedef struct timedate_st TIMEDATE;
84
85 /*----------------------------------------------------------------------*/
86
87 #define ERR_WARNING     0
88 #define ERR_SEVERE      1
89 #define ERR_FATAL       2
90
91 /*----------------------------------------------------------------------*/
92
93
94 /* codes for open command */
95 #if MICROSOFT
96 #define OPEN_RDONLY  O_RDONLY                   /* other system use standard codes */
97 #define OPEN_WRONLY  O_WRONLY                   /* for binary */
98 #define OPEN_RDWR    O_RDWR
99 #else
100 #define OPEN_RDONLY  0                  /* other system use standard codes */
101 #define OPEN_WRONLY  1                  /* for binary */
102 #define OPEN_RDWR    2
103 #endif
104
105 /*----------------------------------------------------------------------*/
106
107 #ifndef O_BINARY
108 #define O_BINARY (0)
109 #endif
110
111 #ifndef S_IWRITE
112 #define S_IWRITE S_IWUSR
113 #endif
114
115 /*----------------------------------------------------------------------*/
116
117 #if defined(MICROSOFT) || ! defined(SIZEOF_INT)
118    #define SIZEOF_INT 4
119    #define SIZEOF_LONG 4
120    #define SIZEOF_SHORT 2
121    #define SIZEOF_FLOAT 4
122    #define SIZEOF_DOUBLE 8
123 #endif
124
125 typedef signed char kint8;
126 typedef unsigned char kuint8;
127
128 #if SIZEOF_INT == 4
129     typedef int kint32;
130     typedef unsigned int kuint32;
131 #elif SIZEOF_LONG == 4
132     typedef long int kint32;
133     typedef unsigned int kuint32;
134 #endif
135
136 #if SIZEOF_SHORT == 2
137     typedef short int kint16;
138     typedef unsigned short int kuint16;
139 #elif SIZEOF_INT == 2
140     typedef int kint16;
141     typedef unsigned int kuint16;
142 #endif
143
144 #if SIZEOF_FLOAT == 4
145     typedef float kfloat32;
146 #endif
147 #if SIZEOF_DOUBLE == 8
148     typedef double kfloat64;
149 #endif
150
151
152 /* filefuncs.cpp */
153 bool fileExists (const char* fname);
154 const char* fileBasename (const char* filename);
155
156 /* strfuncs.cpp */
157 char* str_skip_head(const char* str, const char* const charlist);
158 char* str_skip_head(const char* str, char* charlist);
159 char *str_lower(char *s);
160 char *str_wrm_tail(char *str);
161 char *str_rm_tail(char *str, const char* const charlist);
162 char *str_upper(char *str);
163
164 /* syserror.cpp */
165 void sys_error(int severity, const char *msg, ...);
166 void sys_verror(int severity, const char *msg, va_list arg);
167 void sys_error_level(int severity);
168
169 /* timedate.cpp */
170 DATE *td_get_date(DATE *d);
171 TIME *td_get_time(TIME *t);
172 double td_current_sec(void);
173 double td_time_to_sec(TIME *t);
174 TIME *td_time_sub(const TIME *t1, const TIME *t2, TIME *tdiff);
175 TIME *td_time_add(const TIME *t1, const TIME *t2, TIME *tsum);
176 TIME *td_time_copy(TIME *to, const TIME *from);
177 TIME *td_time_norm(TIME *t);
178 void td_get_tmdt(TIMEDATE *td);
179 const char *td_str_tmdt(const TIMEDATE *td);
180 const char *td_str_time(const TIME *t);
181 const char *td_str_stime(const TIME *t);
182 const char *td_str_date(const DATE *d);
183 char *td_str_cdate(DATE *d);
184 char *td_month_name(int n);
185 char *td_day_name(int n);
186
187
188 // Math Section
189
190 #include <math.h>
191
192 #define PI      3.14159265358979323846
193 #define HALFPI  1.57079632679489661923  /* PI divided by 2 */
194 #define QUARTPI 0.78539816339744830962  /* PI divided by 4 */
195 #define I_PI    0.31830988618379067154  /* Inverse of PI */
196 #define I_PID2  0.63661977236758134308  /* Inverse of PID2 */
197  
198 #define TWOPI   6.28318530717958647692
199 #define SQRT2   1.414213562373095049
200
201 #define F_EPSILON       1.0E-6
202 #define D_EPSILON       1.0E-10
203
204 #define ASSUMEDZERO  1E-10
205
206 typedef double GRFMTX_2D[3][3];
207 typedef double GRFMTX_3D[4][4];
208
209 inline double 
210 convertDegreesToRadians (double x)
211 { return (x * (PI/180.)); }
212
213 inline double
214 convertRadiansToDegrees (double x)
215 { return (x*(180./PI)); }
216
217 template<class T>
218 inline T nearest (double x)
219 { return (x > 0 ? static_cast<T>(x+0.5) : static_cast<T>(x-0.5)); }
220
221 template<class T>
222 inline T clamp (T value, T lowerBounds, T upperBounds)
223 { return (value >= upperBounds ? upperBounds : (value <= lowerBounds ? lowerBounds : value )); }
224
225 template<class T>
226 inline T lineLength (T x1, T y1, T x2, T y2)
227 { return static_cast<T>( sqrt ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)) ); }
228
229 template<class T>
230 inline void minmax_array (const T* array, const int n, T& min, T& max)
231 {
232   max = min = array[0];
233
234   for (int i = 1; i < n; i++)
235     if (array[i] < min)
236       min = array[i];
237     else if (array[i] > max)
238       max = array[i];
239 }
240
241
242 //////////////////////////////////////////////////////////////
243 // FUNTION DECLARATIONS
244 //////////////////////////////////////////////////////////////
245
246 // clip.cpp 
247 int clip_rect (double& x1, double& y1, double& x2, double& y2, const double rect[4]);
248 int clip_segment (double& x1, double& y1, double& x2, double& y2, const double u, const double v);
249 int clip_sector (double& x1, double& y1, double& x2, double& y2, const double u, const double v);
250 int clip_circle (double& x1, double& y1, double& x2, double& y2, const double cx, const double cy, const double radius, double t1, double t2);
251 int clip_triangle (double& x1, double& y1, double& x2, double& y2, const double u, const double v, const int clip_xaxis);
252
253
254 // xform.cpp 
255 void indent_mtx2 (GRFMTX_2D m);
256 void xlat_mtx2 (GRFMTX_2D m, const double x, const double y);
257 void scale_mtx2 (GRFMTX_2D m, const double sx, const double sy);
258 void rot_mtx2 (GRFMTX_2D m, const double theta);
259 void mult_mtx2 (const GRFMTX_2D m1, const GRFMTX_2D m2, GRFMTX_2D result);
260 void xform_mtx2 (const GRFMTX_2D m, double& x, double& y);
261 void rotate2d (double x[], double y[], int pts, double angle);
262 void xlat2d (double x[], double y[], int pts, double xoffset, double yoffset);
263 void scale2d (double x[], double y[], int pts, double xfact, double yfact);
264
265 // mathfuncs.cpp
266 double normalizeAngle (double theta);
267 double integrateSimpson (const double xmin, const double xmax, const double *y, const int np);
268
269
270 // Console I/O Section
271
272 #define C_BLACK      0                  /* color codes */
273 #define C_BLUE       1
274 #define C_GREEN      2
275 #define C_CYAN       3
276 #define C_RED        4
277 #define C_MAGENTA    5
278 #define C_BROWN      6
279 #define C_WHITE      7
280 #define C_GREY       8
281 #define C_LTBLUE     9
282 #define C_LTGREEN   10
283 #define C_LTCYAN    11
284 #define C_LTRED     12
285 #define C_LTMAGENTA 13
286 #define C_YELLOW    14
287 #define C_LTWHITE   15
288
289 /*----------------------------------------------------------------------*/
290
291 /* screen character codes */
292
293 #define SC_BKSP           8
294 #define SC_TAB            9
295 #define SC_BLANK        ' '
296
297
298 /* audio.cpp */
299 void cio_beep(void);
300 void cio_tone(double freq, double length);
301
302 /* crtput.cpp */
303 void cio_put_c(int c);
304 void cio_put_cc(int c, int count);
305 void cio_put_str(const char *str);
306
307 /* kbget.cpp */
308 unsigned int cio_kb_getc(void);
309 void cio_kb_ungetc(unsigned int c);
310 char *cio_kb_gets(char *str, int maxlen);
311 unsigned int cio_kb_waitc(const char *astr, int beep);
312
313 // Keyboard Section
314
315 #define KEY_BKSP         8
316 #define KEY_TAB          9
317 #define KEY_RETURN      13
318 #define KEY_ESCAPE      27
319
320 // ASCII Section
321
322 #define BACKSPACE  8
323 #define LF      0x0A
324 #define CR      0x0D
325 #define BELL    0x07
326
327 #define SQUOTE    '\''
328 #define DQUOTE    '\"'
329 #define BSLASH    '\\'
330 #define BACKSLASH '\\'
331 #define SHARP     '#'
332 #define SLASH     '/'
333 #define ASTERICK  '*'
334 #define COLON     ':'
335 #define LBRACE    '{'
336 #define RBRACE    '}'
337 #define LPAREN    '('
338 #define RPAREN    ')'
339 #define LBRACK    '['
340 #define RBRACK    ']'
341 #define LANBRACK  '<'
342 #define RANBRACK  '>'
343 #define SEMICOL   ';'
344 #define UNDERLIN  '_'
345 #define COMMA     ','
346 #define CARET     '^'
347 #define TILDE     '~'
348 #define ATSIGN    '@'
349 #define AMPERSAND  '&'
350 #define EXCLAM    '!'
351 #define DOLLAR    '$'
352 #define PERCENT   '%'
353 #define PLUS      '+'
354 #define HYPHEN    '-'
355 #define EQUALS    '='
356 #define QUESTION  '?'
357 #define PERIOD    '.'
358 #define VERTBAR   '|'
359
360 #endif  /* #ifndef ASCII_H */