summaryrefslogtreecommitdiff
path: root/app/bin/common.h
blob: f03a04ca2ef944248b6fc0966a6bcc699e68a24e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/** \file common.h
 * Defnitions of basic types
 */

/*  XTrkCad - Model Railroad CAD
 *  Copyright (C) 2005 Dave Bullis
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef COMMON_H
#define COMMON_H

// INCLUDES
#include <ctype.h>
#include <errno.h>
#include <locale.h>
#include <math.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>

#include "wlib.h"

#ifndef WINDOWS
// Unix/Mac
#include <dirent.h>
#include <unistd.h>

#define PATH_SEPARATOR "/"

#else
// Windows
#include <io.h>
#include <process.h>
#include "include/dirent.h"
#include "direct.h"
#include "getopt.h"

// DEFINES
#define UTFCONVERT
#define M_PI 3.14159265358979323846

#define F_OK (00)
#define W_OK (02)
#define R_OK (04)
#define PATH_SEPARATOR "\\"

// ALIASES for WINDOWS
#define access _access
#define unlink(a) _unlink((a))
#define rmdir(a) _rmdir((a))
#define open(name, flag, mode) _open((name), (flag), (mode))
#define close(file) _close((file))
#define getpid() _getpid()
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#define mkdir( DIR, MODE ) _mkdir( (DIR) )
#if _MSC_VER >1300
#define strnicmp _strnicmp
#define stricmp _stricmp
#define strdup _strdup
#endif
// starting from Visual Studio 2015 round is in the runtime library, fake otherwise
#if ( _MSC_VER < 1900 )
#define round(x) floor((x)+0.5)
#endif

/* suppress warning from *.bmp about conversion of int to char */
#pragma warning( disable : 4305 )
/* suppress warning about array references */
#pragma warning( disable : 6385 )
#endif

// We need to pass integer values via void* objects
// typically context vars (declared as void*) which sometimes pass pointers and some times integers
// For example see paramData_t.context.
// Also some wlib calls take a generic parameter, declared as void* but somethings used to pass integers
// These are used in logical pairs; VP=I2VP(INTEGER); ...a lot of code...; INTEGER=VP2L(VP);
// Note: we never use VP2L to manipulate integer-ized values of a pointer and all integer values we use fit in a long
#define I2VP(VAL) ((void*)(intptr_t)(VAL))
#define VP2L(VAL) ((long)(intptr_t)(VAL))


#ifndef TRUE
#define TRUE	(1)
#define FALSE	(0)
#endif

#define DIST_INF 2.0E9

#define NUM_LAYERS		(99)

// TYPEDEFS

typedef double FLOAT_T;
typedef double POS_T;
typedef double DIST_T;
typedef double ANGLE_T;
typedef double LWIDTH_T;

#define SCANF_FLOAT_FORMAT "%lf"

typedef double DOUBLE_T;
typedef double WDOUBLE_T;
typedef double FONTSIZE_T;

typedef struct {
	POS_T x,y;
} coOrd;

typedef struct {
	coOrd pt;
	int pt_type;
} pts_t;

typedef int INT_T;

typedef int BOOL_T;
typedef int EPINX_T;
typedef int CSIZE_T;
#ifndef WIN32
typedef int SIZE_T;
#endif
typedef int STATE_T;
typedef int STATUS_T;
typedef signed char TRKTYP_T;
typedef int TRKINX_T;
typedef long DEBUGF_T;
typedef int REGION_T;
typedef long SCALEINX_T;
typedef long GAUGEINX_T;
typedef long SCALEDESCINX_T;


enum paramFileState { PARAMFILE_UNLOADED = 0, PARAMFILE_NOTUSABLE, PARAMFILE_COMPATIBLE, PARAMFILE_FIT, PARAMFILE_MAXSTATE };

// DYNARRAY

typedef struct {
	int cnt;
	int max;
	void * ptr;
} dynArr_t;

#define CHECK_SIZE(T,DA)

/**
 * Append INCR mambers to DA
 * INCR is > 1 if we plan to add more members soon
 * Increments .cnt for the next member
 * Note: new members may not be empty
 */
#define DYNARR_APPEND(T,DA,INCR) \
		{ if ((DA).cnt >= (DA).max) { \
			(DA).max += (INCR); \
			CHECK_SIZE((T),(DA)) \
			(DA).ptr = MyRealloc( (DA).ptr, (DA).max * sizeof *(T*)NULL ); \
			if ( (DA).ptr == NULL ) \
				abort(); \
		} \
		(DA).cnt++; }

/**
 * Return Last member of DA
 */
#define DYNARR_LAST(T,DA) \
		(((T*)(DA).ptr)[(DA).cnt-1])
/**
 * Return N't member of DA
 */
#define DYNARR_N(T,DA,N) \
		(((T*)(DA).ptr)[N])
/**
 * Logically empty the DA
 * .max and .ptr are untouched
 */
#define DYNARR_RESET(T,DA) \
		(DA).cnt=0
/**
 * Set number of members to N
 * If extending (.cnt > .max ), new values will be 0'd, otherwise not
 */
#define DYNARR_SET(T,DA,N) \
		{ if ((DA).max < (N)) { \
			(DA).max = (N); \
			CHECK_SIZE((T),(DA)) \
			(DA).ptr = MyRealloc( (DA).ptr, (DA).max * sizeof *(T*)NULL ); \
			if ( (DA).ptr == NULL ) \
				abort(); \
		} \
		(DA).cnt = (N); }
/**
 * Initializes DA to empty when .ptr might be garbage (ie local vars)
 * All fields are cleared
 */
#define DYNARR_INIT(T,DA) \
		{ (DA).ptr = NULL; \
		(DA).max = 0; \
		(DA).cnt = 0; \
		}
/**
 * Initializes DA to empty and frees .ptr
 */
#define DYNARR_FREE(T,DA) \
		{ if ((DA).ptr) { \
			MyFree( (DA).ptr); \
			(DA).ptr = NULL; \
		} \
		(DA).max = 0; \
		(DA).cnt = 0; }
/**
 * Removes N'th member from DA
 * (Not used)
 */
#define DYNARR_REMOVE(T,DA,N) \
		{ \
		 { if ((DA).cnt-1 > (N)) { \
				for (int i=(N);i<(DA).cnt-1;i++) { \
				(((T*)(DA).ptr)[i])= (((T*)(DA).ptr)[i+1]); \
				} \
			} \
		 } \
		if ((DA).cnt>=(N)) (DA).cnt--; \
		}

// Base DotsPerInch
#define BASE_DPI	(75.0)

// FILE VERSIONS - non-backward file format changes
// Descriptions added for Bezier, Cornu, Joint
#define VERSION_DESCRIPTION2	(12)
// Inline quoted text replaces multiline text in Notes and Cars
#define VERSION_INLINENOTE	(12)
// END is replaced by END$SEGS, END$TRK, ...
#define VERSION_NONAKEDENDS	(12)


// FORWARD TYPE DECLS
typedef struct drawCmd_t * drawCmd_p;
typedef struct track_t * track_p;
typedef struct track_t * track_cp;
typedef struct trkSeg_t * trkSeg_p;
typedef struct traverseTrack_t * traverseTrack_p;
typedef struct trkEndPt_t * trkEndPt_p;
typedef void (*doSaveCallBack_p)( void );
typedef void (*addButtonCallBack_t)(void*);
typedef STATUS_T (*procCommand_t) (wAction_t, coOrd);


// base class for extraData*_t: each of which must include this struct as the first element
typedef struct extraDataBase_t {
	TRKTYP_T trkType;
} extraDataBase_t;
// We check if TRKTYP_T in trk, trk->extraDataBase and the code context (TRKTYP) match.
// If TRKTYP is T_NOTRACK then we are dealing with T_TURNOUT/T_STRUCTURE or T_BEZIER/T_BEZLIN which
// share a log of code and have the same extraData*_t structure.
#define GET_EXTRA_DATA(TRK,TRKTYP,TYPE) \
	((TYPE*)GetTrkExtraData( (TRK), (TRKTYP) ))
extraDataBase_t * GetTrkExtraData( track_p, TRKTYP_T );


typedef struct {
	BOOL_T valid;
	DIST_T length;
	DIST_T width;
	DIST_T spacing;
} tieData_t, *tieData_p;

// Syntactic suger for exported (non-static) objects
#define EXPORT

#define COUNT(A) (sizeof(A)/sizeof(A[0]))

#define STR_SIZE		(256)
#define STR_SHORT_SIZE	(80)
#define STR_LONG_SIZE	(1024)
#define STR_HUGE_SIZE	(10240)

#define CAST_AWAY_CONST (char*)

#define TITLEMAXLEN (40)



// COMMON INCLUDES
// If you add includes here, please remove them elsewhere

#include "i18n.h"
#include "utility.h"
#include "acclkeys.h"
#include "misc.h"

// TODO - move these includes to the files that need them
#include "dlayer.h"
#include "scale.h"
#include "command.h"
#include "menu.h"

#endif