r94: finished c++ conversions
[ctsim.git] / include / kstddef.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **      File Name:      kstddef.h
5 **      Author:         Kevin Rosenberg
6 **      Purpose:        Header file containing KRL standard C definitions
7 **      Date Started:   Dec. 83
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: kstddef.h,v 1.16 2000/06/13 16:20:31 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 STDDEF_H
30 #define STDDEF_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 #undef SHELL
49
50 #define TRUE    1
51 #define FALSE   0
52 #define OK      TRUE
53 #define ERROR   FALSE
54 #define YES     TRUE
55 #define NO      FALSE
56
57 /*----------------------------------------------------------------------*/
58
59 #define SHOW(var, fmt)  fprintf (stderr, "var = fmt\n", var)
60
61 /*----------------------------------------------------------------------*/
62
63 #define NEWLINE '\n'
64 #define TAB     '\t'
65 #define EOS     '\0'
66 #define BLANK   ' '
67 #define ETX     26      /* end of text signal for PC-DOS */
68
69 /*----------------------------------------------------------------------*/
70
71 #define MAXPATHNAME  65         /* max length of pathname + 1 */
72 #define MAXFILENAME  13         /* rootname(8) + '.'(1) + extension(3) + EOS */
73 #define MAXROOTNAME   8
74 #define MAXEXTNAME    3
75 #define MAXFULLNAME  MAXPATHNAME + MAXFILENAME
76
77 /*----------------------------------------------------------------------*/
78
79 #define ABS(x)    ((x) < 0 ? -(x) : (x))
80 #define SQR(x)    ((x) * (x))
81
82 #define ISWAP(a,b) {int i; i = a; a = b; b = i;}
83
84 #define CLIP(n,lb,ub)   if (n < lb) n = lb; else if (n > ub) n = ub
85 #define STR_EQUAL(s1,s2) (strcmp (s1, s2) == 0)
86
87 /*----------------------------------------------------------------------*/
88
89 struct time_st {
90         int hour, minute, second, ms;
91 };
92
93 struct date_st {
94         int year, month, date, dow;
95 };
96
97 struct timedate_st {
98     struct time_st t;
99     struct date_st d;
100 };
101
102 typedef struct time_st TIME;
103 typedef struct date_st DATE;
104 typedef struct timedate_st TIMEDATE;
105
106 /*----------------------------------------------------------------------*/
107
108 #define ERR_WARNING     0
109 #define ERR_SEVERE      1
110 #define ERR_FATAL       2
111
112 /*----------------------------------------------------------------------*/
113
114
115 /* codes for open command */
116 #if MICROSOFT
117 #define OPEN_RDONLY  O_RDONLY                   /* other system use standard codes */
118 #define OPEN_WRONLY  O_WRONLY                   /* for binary */
119 #define OPEN_RDWR    O_RDWR
120 #else
121 #define OPEN_RDONLY  0                  /* other system use standard codes */
122 #define OPEN_WRONLY  1                  /* for binary */
123 #define OPEN_RDWR    2
124 #endif
125
126 /*----------------------------------------------------------------------*/
127
128 /* codes for input (inp_*) routines */
129
130 #define INP_ERROR   -1
131 #define INP_NO      FALSE
132 #define INP_YES     TRUE
133 #define INP_NEITHER 'b'
134 #define INP_RETURN  'r'
135 #define INP_BREAK   0x8000
136
137 #ifndef O_BINARY
138 #define O_BINARY (0)
139 #endif
140
141 #ifndef S_IWRITE
142 #define S_IWRITE S_IWUSR
143 #endif
144
145 /*----------------------------------------------------------------------*/
146
147 #if defined(MICROSOFT) || ! defined(SIZEOF_INT)
148    #define SIZEOF_INT 4
149    #define SIZEOF_LONG 4
150    #define SIZEOF_SHORT 2
151    #define SIZEOF_FLOAT 4
152    #define SIZEOF_DOUBLE 8
153 #endif
154
155 typedef signed char kint8;
156 typedef unsigned char kuint8;
157
158 #if SIZEOF_INT == 4
159     typedef int kint32;
160     typedef unsigned int kuint32;
161 #elif SIZEOF_LONG == 4
162     typedef long int kint32;
163     typedef unsigned int kuint32;
164 #endif
165
166 #if SIZEOF_SHORT == 2
167     typedef short int kint16;
168     typedef unsigned short int kuint16;
169 #elif SIZEOF_INT == 2
170     typedef int kint16;
171     typedef unsigned int kuint16;
172 #endif
173
174 #if SIZEOF_FLOAT == 4
175     typedef float kfloat32;
176 #endif
177 #if SIZEOF_DOUBLE == 8
178     typedef double kfloat64;
179 #endif
180
181 /* filefuncs.cpp */
182 bool file_exists(const char* fname);
183 const char* fileBasename(const char* filename);
184 FILE *sys_fopen(const char *filename, const char *mode, const char *progname);
185
186 /* strfuncs.cpp */
187 const char* str_skip_head(char* const str, const char* const charlist);
188 char* str_skip_head(char* str, char* charlist);
189 char *str_lower(char *s);
190 char *str_wrm_tail(char *str);
191 char *str_rm_tail(char *str, const char* const charlist);
192 char *str_upper(char *str);
193
194 /* syserror.cpp */
195 void sys_error(int severity, const char *msg, ...);
196 void sys_verror(int severity, const char *msg, va_list arg);
197 void sys_error_level(int severity);
198
199 /* timedate.cpp */
200 DATE *td_get_date(DATE *d);
201 TIME *td_get_time(TIME *t);
202 double td_current_sec(void);
203 double td_time_to_sec(TIME *t);
204 TIME *td_time_sub(const TIME *t1, const TIME *t2, TIME *tdiff);
205 TIME *td_time_add(const TIME *t1, const TIME *t2, TIME *tsum);
206 TIME *td_time_copy(TIME *to, const TIME *from);
207 TIME *td_time_norm(TIME *t);
208 void td_get_tmdt(TIMEDATE *td);
209 const char *td_str_tmdt(const TIMEDATE *td);
210 const char *td_str_time(const TIME *t);
211 const char *td_str_stime(const TIME *t);
212 const char *td_str_date(const DATE *d);
213 char *td_str_cdate(DATE *d);
214 char *td_month_name(int n);
215 char *td_day_name(int n);
216
217 /* netorder.cpp */
218 void *strreverse (void *dest, const void *src, size_t n);
219 int read_nint16 (kuint16 *n, int fd);
220 int write_nint16 (kuint16 const *n, int fd);
221 int read_nint32 (kuint32 *n, int fd);
222 int write_nint32 (kuint32 const *n, int fd);
223 int read_nfloat32 (float *f, int fd);
224 int write_nfloat32 (float const *f, int fd);
225 int read_nfloat64 (double *d, int fd);
226 int write_nfloat64 (double const *d, int fd);
227 void ConvertNetworkOrder (void* buffer, size_t bytes);
228 void ConvertReverseNetworkOrder (void* buffer, size_t bytes);
229
230 #endif