r259: MSVC modifications
[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.12 2000/12/16 03:29:02 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 MSVC
37 #define snprintf _snprintf
38 #endif
39
40 #define STR_MAX_LEN 255
41 #define STR_SIZE    STR_MAX_LEN+1
42
43 #include <cstdio>
44 #include <cstdlib>
45 #include <cstring>
46 #include <cstdarg>
47
48 #define TRUE    1
49 #define FALSE   0
50 #define OK      TRUE
51
52 /*----------------------------------------------------------------------*/
53
54 #define SHOW(var, fmt)  { cerr << "var = " << var << endl; }
55
56 /*----------------------------------------------------------------------*/
57
58 #define NEWLINE '\n'
59 #define TAB     '\t'
60 #define EOS     '\0'
61 #define BLANK   ' '
62
63 /*----------------------------------------------------------------------*/
64
65 #define ERR_WARNING     0
66 #define ERR_SEVERE      1
67 #define ERR_FATAL       2
68
69 /*----------------------------------------------------------------------*/
70
71
72 /* codes for open command */
73 #ifdef MSVC
74 #define OPEN_RDONLY  O_RDONLY                   /* other system use standard codes */
75 #define OPEN_WRONLY  O_WRONLY                   /* for binary */
76 #define OPEN_RDWR    O_RDWR
77 #else
78 #define OPEN_RDONLY  0                  /* other system use standard codes */
79 #define OPEN_WRONLY  1                  /* for binary */
80 #define OPEN_RDWR    2
81 #endif
82
83 /*----------------------------------------------------------------------*/
84
85 #ifndef O_BINARY
86 #define O_BINARY (0)
87 #endif
88
89 #ifndef S_IWRITE
90 #define S_IWRITE S_IWUSR
91 #endif
92
93 /*----------------------------------------------------------------------*/
94
95 #if defined(MSVC) || ! defined(SIZEOF_INT)
96    #define SIZEOF_INT 4
97    #define SIZEOF_LONG 4
98    #define SIZEOF_SHORT 2
99    #define SIZEOF_FLOAT 4
100    #define SIZEOF_DOUBLE 8
101 #endif
102
103 typedef signed char kint8;
104 typedef unsigned char kuint8;
105
106 #if SIZEOF_INT == 4
107     typedef int kint32;
108     typedef unsigned int kuint32;
109 #elif SIZEOF_LONG == 4
110     typedef long int kint32;
111     typedef unsigned int kuint32;
112 #endif
113
114 #if SIZEOF_SHORT == 2
115     typedef short int kint16;
116     typedef unsigned short int kuint16;
117 #elif SIZEOF_INT == 2
118     typedef int kint16;
119     typedef unsigned int kuint16;
120 #endif
121
122 #if SIZEOF_FLOAT == 4
123     typedef float kfloat32;
124 #endif
125 #if SIZEOF_DOUBLE == 8
126     typedef double kfloat64;
127 #endif
128
129
130 inline const char* 
131 fileBasename (const char* const filename)
132 {
133   const char* p = strrchr (filename, '/');
134   return (p ? p + 1 : filename);
135 }
136
137
138 /* strfuncs.cpp */
139 char* str_skip_head(const char* str, const char* const charlist);
140 char* str_skip_head(const char* str, char* charlist);
141 char *str_lower(char *s);
142 char *str_wrm_tail(char *str);
143 char *str_rm_tail(char *str, const char* const charlist);
144 char *str_upper(char *str);
145
146 /* syserror.cpp */
147 void sys_error(int severity, const char *msg, ...);
148 void sys_verror(int severity, const char *msg, va_list arg);
149 void sys_error_level(int severity);
150
151 // Math Section
152
153 #include <cmath>
154
155 #define PI      3.14159265358979323846
156 #define HALFPI  1.57079632679489661923  /* PI divided by 2 */
157 #define QUARTPI 0.78539816339744830962  /* PI divided by 4 */
158 #define I_PI    0.31830988618379067154  /* Inverse of PI */
159 #define I_PID2  0.63661977236758134308  /* Inverse of PID2 */
160  
161 #define TWOPI   6.28318530717958647692
162 #define SQRT2   1.414213562373095049
163
164 #define F_EPSILON       1.0E-6
165 #define D_EPSILON       1.0E-10
166
167 #define ASSUMEDZERO  1E-10
168
169 typedef double GRFMTX_2D[3][3];
170 typedef double GRFMTX_3D[4][4];
171
172 inline double 
173 convertDegreesToRadians (double x)
174 { return (x * (PI/180.)); }
175
176 inline double
177 convertRadiansToDegrees (double x)
178 { return (x*(180./PI)); }
179
180 template<class T>
181 inline T nearest (double x)
182 { return (x > 0 ? static_cast<T>(x+0.5) : static_cast<T>(x-0.5)); }
183
184 inline int imax (int a, int b)\r
185 { return (a >= b ? a : b); }\r
186 \r
187 inline double dmax (double a, double b)\r
188 { return (a >= b ? a : b); }\r
189 \r
190 template<class T>
191 inline T clamp (T value, T lowerBounds, T upperBounds)
192 { return (value >= upperBounds ? upperBounds : (value <= lowerBounds ? lowerBounds : value )); }
193
194 template<class T>
195 inline T lineLength (T x1, T y1, T x2, T y2)
196 { return static_cast<T>( sqrt ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)) ); }
197
198 template<class T>
199 inline void minmax_array (const T* array, const int n, T& min, T& max)
200 {
201   max = min = array[0];
202
203   for (int i = 1; i < n; i++)
204     if (array[i] < min)
205       min = array[i];
206     else if (array[i] > max)
207       max = array[i];
208 }
209
210
211 //////////////////////////////////////////////////////////////
212 // FUNTION DECLARATIONS
213 //////////////////////////////////////////////////////////////
214
215 // clip.cpp 
216 bool clip_rect (double& x1, double& y1, double& x2, double& y2, const double rect[4]);
217 bool clip_segment (double& x1, double& y1, double& x2, double& y2, const double u, const double v);
218 bool clip_sector (double& x1, double& y1, double& x2, double& y2, const double u, const double v);
219 bool clip_circle (double& x1, double& y1, double& x2, double& y2, const double cx, const double cy, const double radius, double t1, double t2);
220 bool clip_triangle (double& x1, double& y1, double& x2, double& y2, const double u, const double v, const int clip_xaxis);
221
222
223 // xform.cpp 
224 void indent_mtx2 (GRFMTX_2D m);
225 void xlat_mtx2 (GRFMTX_2D m, const double x, const double y);
226 void scale_mtx2 (GRFMTX_2D m, const double sx, const double sy);
227 void rot_mtx2 (GRFMTX_2D m, const double theta);
228 void mult_mtx2 (const GRFMTX_2D m1, const GRFMTX_2D m2, GRFMTX_2D result);
229 void xform_mtx2 (const GRFMTX_2D m, double& x, double& y);
230 void rotate2d (double x[], double y[], int pts, double angle);
231 void xlat2d (double x[], double y[], int pts, double xoffset, double yoffset);
232 void scale2d (double x[], double y[], int pts, double xfact, double yfact);
233
234 // mathfuncs.cpp
235 double normalizeAngle (double theta);
236 double integrateSimpson (const double xmin, const double xmax, const double *y, const int np);
237
238
239 /*----------------------------------------------------------------------*/
240
241 /* screen character codes */
242
243 #define SC_BKSP           8
244 #define SC_TAB            9
245 #define SC_BLANK        ' '
246
247
248 /* audio.cpp */
249 void cio_beep(void);
250 void cio_tone(double freq, double length);
251
252 /* crtput.cpp */
253 void cio_put_c(int c);
254 void cio_put_cc(int c, int count);
255 void cio_put_str(const char *str);
256
257 /* kbget.cpp */
258 unsigned int cio_kb_getc(void);
259 void cio_kb_ungetc(unsigned int c);
260 char *cio_kb_gets(char *str, int maxlen);
261 unsigned int cio_kb_waitc(const char *astr, int beep);
262
263 // Keyboard Section
264
265 #define KEY_BKSP         8
266 #define KEY_TAB          9
267 #define KEY_RETURN      13
268 #define KEY_ESCAPE      27
269
270 // ASCII Section
271
272 #define BACKSPACE  8
273 #define LF      0x0A
274 #define CR      0x0D
275 #define BELL    0x07
276
277 #define SQUOTE    '\''
278 #define DQUOTE    '\"'
279 #define BSLASH    '\\'
280 #define BACKSLASH '\\'
281 #define SHARP     '#'
282 #define SLASH     '/'
283 #define ASTERICK  '*'
284 #define COLON     ':'
285 #define LBRACE    '{'
286 #define RBRACE    '}'
287 #define LPAREN    '('
288 #define RPAREN    ')'
289 #define LBRACK    '['
290 #define RBRACK    ']'
291 #define LANBRACK  '<'
292 #define RANBRACK  '>'
293 #define SEMICOL   ';'
294 #define UNDERLIN  '_'
295 #define COMMA     ','
296 #define CARET     '^'
297 #define TILDE     '~'
298 #define ATSIGN    '@'
299 #define AMPERSAND  '&'
300 #define EXCLAM    '!'
301 #define DOLLAR    '$'
302 #define PERCENT   '%'
303 #define PLUS      '+'
304 #define HYPHEN    '-'
305 #define EQUALS    '='
306 #define QUESTION  '?'
307 #define PERIOD    '.'
308 #define VERTBAR   '|'
309
310 #endif  /* #ifndef CTSUPPORT_H */