summaryrefslogtreecommitdiff
path: root/dviware/mdvi/include/common.h
blob: 67130feb7512cbfad75fbb618b034865b572d266 (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
/*
 * Copyright (C) 2000, Matias Atria
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#ifndef _MDVI_COMMON_H
#define _MDVI_COMMON_H 1

#include <stdio.h>
#include <sys/types.h>
#include <math.h>

#include "sysdeps.h"

#if STDC_HEADERS
#  include <string.h>
#endif

#if !defined(STDC_HEADERS) || defined(__STRICT_ANSI__)
#  ifndef HAVE_STRCHR
#    define strchr         index
#    define strrchr        rindex
#  endif
#  ifndef HAVE_MEMCPY
#    define memcpy(a,b,n)  bcopy((b), (a), (n))
#    define memmove(a,b,n) bcopy((b), (a), (n))
#  endif
#endif

#ifdef HAVE_MEMCPY
#define memzero(a,n) memset((a), 0, (n))
#else
#define memzero(a,n) bzero((a), (n))
#endif

typedef struct _List {
	struct _List *next;
	struct _List *prev;
} List;
#define LIST(x)	((List *)(x))

typedef struct {
	char	*data;
	size_t	size;
	size_t	length;
} Buffer;

typedef struct {
	List	*head;
	List	*tail;
	int	count;
} ListHead;
#define MDVI_EMPTY_LIST_HEAD	{NULL, NULL, 0}

typedef struct {
	char	*data;
	size_t	size;
	size_t	length;
} Dstring;

/* Functions to read numbers from streams and memory */

#define fgetbyte(p)	((unsigned)getc(p))

extern char	*program_name;

extern Ulong	fugetn __PROTO((FILE *, size_t));
extern long	fsgetn __PROTO((FILE *, size_t));
extern Ulong	mugetn __PROTO((const Uchar *, size_t));
extern long	msgetn __PROTO((const Uchar *, size_t));

/* To read from a stream (fu: unsigned, fs: signed) */
#define fuget4(p)	fugetn((p), 4)
#define fuget3(p)	fugetn((p), 3)
#define fuget2(p)	fugetn((p), 2)
#define fuget1(p)	fgetbyte(p)
#define fsget4(p)	fsgetn((p), 4)
#define fsget3(p)	fsgetn((p), 3)
#define fsget2(p)	fsgetn((p), 2)
#define fsget1(p)	fsgetn((p), 1)

/* To read from memory (mu: unsigned, ms: signed) */
#define MUGETN(p,n)	((p) += (n), mugetn((p)-(n), (n)))
#define MSGETN(p,n)	((p) += (n), msgetn((p)-(n), (n)))
#define muget4(p)	MUGETN((p), 4)
#define muget3(p)	MUGETN((p), 3)
#define muget2(p)	MUGETN((p), 2)
#define muget1(p)	MUGETN((p), 1)
#define msget4(p)	MSGETN((p), 4)
#define msget3(p)	MSGETN((p), 3)
#define msget2(p)	MSGETN((p), 2)
#define msget1(p)	MSGETN((p), 1)

#define ROUND(x,y)	(((x) + (y) - 1) / (y))
#define FROUND(x)	(int)((x) + 0.5)
#define SFROUND(x)	(int)((x) >= 0 ? floor((x) + 0.5) : ceil((x) + 0.5))

#define Max(a,b)	(((a) > (b)) ? (a) : (b))
#define Min(a,b)	(((a) < (b)) ? (a) : (b))

/* make 2byte number from 2 8bit quantities */
#define HALFWORD(a,b)	((((a) << 8) & 0xf) | (b))
#define FULLWORD(a,b,c,d) \
	((((Int8)(a) << 24) & 0xff000000) | \
	(((Uint8)(b) << 16) & 0x00ff0000) | \
	(((Uint8)(c) << 8)  & 0x0000ff00) | \
	((Uint8)(d) & 0xff))

#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define SWAPINT(a,b) \
	({ int _s = a; a = b; b = _s; })
#else
#define SWAPINT(a,b) do { int _s = a; a = b; b = _s; } while(0)
#endif

#define STREQ(a,b)	(strcmp((a), (b)) == 0)
#define STRNEQ(a,b,n)	(strncmp((a), (b), (n)) == 0)
#define STRCEQ(a,b)	(strcasecmp((a), (b)) == 0)
#define STRNCEQ(a,b,n)	(strncasecmp((a), (b), (n)) == 0)

extern char	*read_string __PROTO((FILE *, int, char *, size_t));
extern size_t	read_bcpl __PROTO((FILE *, char *, size_t, size_t));
extern char	*read_alloc_bcpl __PROTO((FILE *, size_t, size_t *));

/* miscellaneous */

extern void message __PROTO((const char *, ...));
extern void crash __PROTO((const char *, ...));
extern void fatal __PROTO((const char *, ...));
extern void error __PROTO((const char *, ...));
extern void warning __PROTO((const char *, ...));
extern int  unit2pix __PROTO((int, const char *));
extern double unit2pix_factor __PROTO((const char *));

#define LOG_NONE	-1
#define LOG_INFO	0
#define LOG_WARN	1
#define LOG_ERROR	2
#define LOG_DEBUG	3

#define DBG_OPCODE	(1 << 0)
#define DBG_FONTS	(1 << 1)
#define DBG_FILES	(1 << 2)
#define DBG_DVI		(1 << 3)
#define DBG_PARAMS	(1 << 4)
#define DBG_SPECIAL	(1 << 5)
#define DBG_DEVICE	(1 << 6)
#define DBG_GLYPHS	(1 << 7)
#define DBG_BITMAPS	(1 << 8)
#define DBG_PATHS	(1 << 9)
#define DBG_SEARCH	(1 << 10)
#define DBG_VARS	(1 << 11)
#define DBG_BITMAP_OPS	(1 << 12)
#define DBG_BITMAP_DATA	(1 << 13)
#define DBG_TYPE1	(1 << 14)
#define DBG_TT		(1 << 15)
#define DBG_FT2		(1 << 16)
#define DBG_FMAP	(1 << 17)

#define DBG_SILENT	(1 << 31)

#ifdef NODEBUG
#define DEBUGGING(x)	0
#else
#define DEBUGGING(x)	(_mdvi_debug_mask & DBG_##x)
#endif

#ifndef NODEBUG
extern Uint32 _mdvi_debug_mask;
extern void __debug __PROTO((int, const char *, ...));
#define DEBUG(x)	__debug x
#define ASSERT(x) do { \
	if(!(x)) crash("%s:%d: Assertion %s failed\n", \
		__FILE__, __LINE__, #x); \
	} while(0)
#define ASSERT_VALUE(x,y) do { \
	if((x) != (y)) \
		crash("%s:%d: Assertion failed (%d = %s != %s)\n", \
		__FILE__, __LINE__, (x), #x, #x); \
	} while(0)
#else
#define DEBUG(x)	do { } while(0)
#define ASSERT(x)	do { } while(0)
#define ASSERT_VALUE(x,y)	do { } while(0)
#endif

#define set_debug_mask(m)	(_mdvi_debug_mask = (Uint32)(m))
#define add_debug_mask(m)	(_mdvi_debug_mask |= (Uint32)(m))
#define get_debug_mask()	_mdvi_debug_mask

/* memory allocation */

extern void  xfree __PROTO((void *));
extern void *xmalloc __PROTO((size_t));
extern void *xrealloc __PROTO((void *, size_t));
extern void *xcalloc __PROTO((size_t, size_t));
extern char *xstrncpy __PROTO((char *, const char *, size_t));
extern char *xstrdup __PROTO((const char *));
extern char *xstrndup __PROTO((const char *, size_t));
extern void *xmemdup __PROTO((const void *, size_t));

/* macros to make memory allocation nicer */
#define xalloc(t)	(t *)xmalloc(sizeof(t))
#define xnalloc(t,n)	(t *)xcalloc((n), sizeof(t))
#define xresize(p,t,n)	(t *)xrealloc((p), (n) * sizeof(t))

extern int get_number __PROTO((const char *, long *));
/* return paper dimensions (in cm) */
extern int paper_dimensions __PROTO((const char *, double *, double *));

extern char *xstradd __PROTO((char *, size_t *, size_t, const char *, size_t));

extern Ulong get_mtime __PROTO((int));

/* lists */
extern void listh_init __PROTO((ListHead *));
extern void listh_prepend __PROTO((ListHead *, List *));
extern void listh_append __PROTO((ListHead *, List *));
extern void listh_add_before __PROTO((ListHead *, List *, List *));
extern void listh_add_after __PROTO((ListHead *, List *, List *));
extern void listh_remove __PROTO((ListHead *, List *));
extern void listh_concat __PROTO((ListHead *, ListHead *));
extern void listh_catcon __PROTO((ListHead *, ListHead *));

extern void buff_init __PROTO((Buffer *));
extern size_t buff_add __PROTO((Buffer *, const char *, size_t));
extern char *buff_gets __PROTO((Buffer *, size_t *));
extern void buff_free __PROTO((Buffer *));

extern char *getword __PROTO((char *, const char *, char **));
extern char *getstring __PROTO((char *, const char *, char **));

extern void dstring_init __PROTO((Dstring *));
extern int dstring_new __PROTO((Dstring *, const char *, int));
extern int dstring_append __PROTO((Dstring *, const char *, int));
extern int dstring_copy __PROTO((Dstring *, int, const char *, int));
extern int dstring_insert __PROTO((Dstring *, int, const char *, int));
extern void dstring_reset __PROTO((Dstring *));

#define dstring_length(d)	((d)->length)
#define dstring_strcat(d,s)	dstring_append((d), (s), -1)

extern char *dgets __PROTO((Dstring *, FILE *));
extern int  file_readable __PROTO((const char *));
extern int  file_exists __PROTO((const char *));
extern char *find_in_path __PROTO((const char *path, const char *file));
extern const char *file_basename __PROTO((const char *));
extern const char *file_extension __PROTO((const char *));
extern char *read_into_core __PROTO((const char *, size_t *));

/*
 * Miscellaneous macros
 */

#define LIST_FOREACH(ptr, type, list) \
	for(ptr = (type *)(list)->head; ptr; ptr = (ptr)->next)

#define Size(x)	(sizeof(x) / sizeof((x)[0]))

/* multiply a fix_word by a 32bit number */
#define B0(x)	((x) & 0xff)
#define B1(x)	B0((x) >> 8)
#define B2(x)	B0((x) >> 16)
#define B3(x)	B0((x) >> 24)
#define __tfm_mul(z,t) \
	(((((B0(t) * (z)) >> 8) + (B1(t) * (z))) >> 8) + B2(t) * (z))
#define TFMSCALE(z,t,a,b) \
	((B3(t) == 255) ? \
		__tfm_mul((z), (t)) / (b) - (a) : \
		__tfm_mul((z), (t)) / (b))
#define TFMPREPARE(x,z,a,b) do { \
	a = 16; z = (x); \
	while(z > 040000000L) { z >>= 1; a <<= 1; } \
	b = 256 / a; a *= z; \
	} while(0)

#endif /* _MDVI_COMMON_H */