]> www.fi.muni.cz Git - evince.git/blob - backend/dvi/mdvi-lib/common.h
c5a908eb098b5bf453fd52838deb71f77a4fc325
[evince.git] / backend / dvi / mdvi-lib / common.h
1 /*
2  * Copyright (C) 2000, Matias Atria
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 #ifndef _MDVI_COMMON_H
19 #define _MDVI_COMMON_H 1
20
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <math.h>
24
25 #include "sysdeps.h"
26
27 #if STDC_HEADERS
28 #  include <string.h>
29 #endif
30
31 #if !defined(STDC_HEADERS) || defined(__STRICT_ANSI__)
32 #  ifndef HAVE_STRCHR
33 #    define strchr         index
34 #    define strrchr        rindex
35 #  endif
36 #  ifndef HAVE_MEMCPY
37 #    define memcpy(a,b,n)  bcopy((b), (a), (n))
38 #    define memmove(a,b,n) bcopy((b), (a), (n))
39 #  endif
40 #endif
41
42 #ifdef HAVE_MEMCPY
43 #define memzero(a,n) memset((a), 0, (n))
44 #else
45 #define memzero(a,n) bzero((a), (n))
46 #endif
47
48 typedef struct _List {
49         struct _List *next;
50         struct _List *prev;
51 } List;
52 #define LIST(x) ((List *)(x))
53
54 typedef struct {
55         char    *data;
56         size_t  size;
57         size_t  length;
58 } Buffer;
59
60 typedef struct {
61         List    *head;
62         List    *tail;
63         int     count;
64 } ListHead;
65 #define MDVI_EMPTY_LIST_HEAD    {NULL, NULL, 0}
66
67 typedef struct {
68         char    *data;
69         size_t  size;
70         size_t  length;
71 } Dstring;
72
73 /* Functions to read numbers from streams and memory */
74
75 #define fgetbyte(p)     ((unsigned)getc(p))
76
77 extern char     *program_name;
78
79 extern Ulong    fugetn __PROTO((FILE *, size_t));
80 extern long     fsgetn __PROTO((FILE *, size_t));
81 extern Ulong    mugetn __PROTO((const Uchar *, size_t));
82 extern long     msgetn __PROTO((const Uchar *, size_t));
83
84 /* To read from a stream (fu: unsigned, fs: signed) */
85 #define fuget4(p)       fugetn((p), 4)
86 #define fuget3(p)       fugetn((p), 3)
87 #define fuget2(p)       fugetn((p), 2)
88 #define fuget1(p)       fgetbyte(p)
89 #define fsget4(p)       fsgetn((p), 4)
90 #define fsget3(p)       fsgetn((p), 3)
91 #define fsget2(p)       fsgetn((p), 2)
92 #define fsget1(p)       fsgetn((p), 1)
93
94 /* To read from memory (mu: unsigned, ms: signed) */
95 #define MUGETN(p,n)     ((p) += (n), mugetn((p)-(n), (n)))
96 #define MSGETN(p,n)     ((p) += (n), msgetn((p)-(n), (n)))
97 #define muget4(p)       MUGETN((p), 4)
98 #define muget3(p)       MUGETN((p), 3)
99 #define muget2(p)       MUGETN((p), 2)
100 #define muget1(p)       MUGETN((p), 1)
101 #define msget4(p)       MSGETN((p), 4)
102 #define msget3(p)       MSGETN((p), 3)
103 #define msget2(p)       MSGETN((p), 2)
104 #define msget1(p)       MSGETN((p), 1)
105
106 #define ROUND(x,y)      (((x) + (y) - 1) / (y))
107 #define FROUND(x)       (int)((x) + 0.5)
108 #define SFROUND(x)      (int)((x) >= 0 ? floor((x) + 0.5) : ceil((x) + 0.5))
109
110 #define Max(a,b)        (((a) > (b)) ? (a) : (b))
111 #define Min(a,b)        (((a) < (b)) ? (a) : (b))
112
113 /* make 2byte number from 2 8bit quantities */
114 #define HALFWORD(a,b)   ((((a) << 8) & 0xf) | (b))
115 #define FULLWORD(a,b,c,d) \
116         ((((Int8)(a) << 24) & 0xff000000) | \
117         (((Uint8)(b) << 16) & 0x00ff0000) | \
118         (((Uint8)(c) << 8)  & 0x0000ff00) | \
119         ((Uint8)(d) & 0xff))
120
121 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
122 #define SWAPINT(a,b) \
123         ({ int _s = a; a = b; b = _s; })
124 #else
125 #define SWAPINT(a,b) do { int _s = a; a = b; b = _s; } while(0)
126 #endif
127
128 #define STREQ(a,b)      (strcmp((a), (b)) == 0)
129 #define STRNEQ(a,b,n)   (strncmp((a), (b), (n)) == 0)
130 #define STRCEQ(a,b)     (strcasecmp((a), (b)) == 0)
131 #define STRNCEQ(a,b,n)  (strncasecmp((a), (b), (n)) == 0)
132
133 extern char     *read_string __PROTO((FILE *, int, char *, size_t));
134 extern size_t   read_bcpl __PROTO((FILE *, char *, size_t, size_t));
135 extern char     *read_alloc_bcpl __PROTO((FILE *, size_t, size_t *));
136
137 /* miscellaneous */
138
139 extern void mdvi_message __PROTO((const char *, ...));
140 extern void mdvi_crash __PROTO((const char *, ...));
141 extern void mdvi_fatal __PROTO((const char *, ...));
142 extern void mdvi_error __PROTO((const char *, ...));
143 extern void mdvi_warning __PROTO((const char *, ...));
144 extern int  unit2pix __PROTO((int, const char *));
145 extern double unit2pix_factor __PROTO((const char *));
146
147 #define LOG_NONE        -1
148 #define LOG_INFO        0
149 #define LOG_WARN        1
150 #define LOG_ERROR       2
151 #define LOG_DEBUG       3
152
153 #define DBG_OPCODE      (1 << 0)
154 #define DBG_FONTS       (1 << 1)
155 #define DBG_FILES       (1 << 2)
156 #define DBG_DVI         (1 << 3)
157 #define DBG_PARAMS      (1 << 4)
158 #define DBG_SPECIAL     (1 << 5)
159 #define DBG_DEVICE      (1 << 6)
160 #define DBG_GLYPHS      (1 << 7)
161 #define DBG_BITMAPS     (1 << 8)
162 #define DBG_PATHS       (1 << 9)
163 #define DBG_SEARCH      (1 << 10)
164 #define DBG_VARS        (1 << 11)
165 #define DBG_BITMAP_OPS  (1 << 12)
166 #define DBG_BITMAP_DATA (1 << 13)
167 #define DBG_TYPE1       (1 << 14)
168 #define DBG_TT          (1 << 15)
169 #define DBG_FT2         (1 << 16)
170 #define DBG_FMAP        (1 << 17)
171
172 #define DBG_SILENT      (1 << 31)
173
174 #ifdef NODEBUG
175 #define DEBUGGING(x)    0
176 #else
177 #define DEBUGGING(x)    (_mdvi_debug_mask & DBG_##x)
178 #endif
179
180 #ifndef NODEBUG
181 extern Uint32 _mdvi_debug_mask;
182 extern void __debug __PROTO((int, const char *, ...));
183 #define DEBUG(x)        __debug x
184 #define ASSERT(x) do { \
185         if(!(x)) mdvi_crash("%s:%d: Assertion %s failed\n", \
186                 __FILE__, __LINE__, #x); \
187         } while(0)
188 #define ASSERT_VALUE(x,y) do { \
189         if((x) != (y)) \
190                 mdvi_crash("%s:%d: Assertion failed (%d = %s != %s)\n", \
191                 __FILE__, __LINE__, (x), #x, #x); \
192         } while(0)
193 #else
194 #define DEBUG(x)        do { } while(0)
195 #define ASSERT(x)       do { } while(0)
196 #define ASSERT_VALUE(x,y)       do { } while(0)
197 #endif
198
199 #define set_debug_mask(m)       (_mdvi_debug_mask = (Uint32)(m))
200 #define add_debug_mask(m)       (_mdvi_debug_mask |= (Uint32)(m))
201 #define get_debug_mask()        _mdvi_debug_mask
202
203 /* memory allocation */
204
205 extern void  mdvi_free __PROTO((void *));
206 extern void *mdvi_malloc __PROTO((size_t));
207 extern void *mdvi_realloc __PROTO((void *, size_t));
208 extern void *mdvi_calloc __PROTO((size_t, size_t));
209 extern char *mdvi_strncpy __PROTO((char *, const char *, size_t));
210 extern char *mdvi_strdup __PROTO((const char *));
211 extern char *mdvi_strndup __PROTO((const char *, size_t));
212 extern void *mdvi_memdup __PROTO((const void *, size_t));
213 extern char *mdvi_build_path_from_cwd __PROTO((const char *));
214 extern char *mdvi_strrstr __PROTO((const char *, const char *));
215
216 /* macros to make memory allocation nicer */
217 #define xalloc(t)       (t *)mdvi_malloc(sizeof(t))
218 #define xnalloc(t,n)    (t *)mdvi_calloc((n), sizeof(t))
219 #define xresize(p,t,n)  (t *)mdvi_realloc((p), (n) * sizeof(t))
220
221 extern char *xstradd __PROTO((char *, size_t *, size_t, const char *, size_t));
222
223 extern Ulong get_mtime __PROTO((int));
224
225 /* lists */
226 extern void listh_init __PROTO((ListHead *));
227 extern void listh_prepend __PROTO((ListHead *, List *));
228 extern void listh_append __PROTO((ListHead *, List *));
229 extern void listh_add_before __PROTO((ListHead *, List *, List *));
230 extern void listh_add_after __PROTO((ListHead *, List *, List *));
231 extern void listh_remove __PROTO((ListHead *, List *));
232 extern void listh_concat __PROTO((ListHead *, ListHead *));
233 extern void listh_catcon __PROTO((ListHead *, ListHead *));
234
235 extern void buff_init __PROTO((Buffer *));
236 extern size_t buff_add __PROTO((Buffer *, const char *, size_t));
237 extern char *buff_gets __PROTO((Buffer *, size_t *));
238 extern void buff_free __PROTO((Buffer *));
239
240 extern char *getword __PROTO((char *, const char *, char **));
241 extern char *getstring __PROTO((char *, const char *, char **));
242
243 extern void dstring_init __PROTO((Dstring *));
244 extern int dstring_new __PROTO((Dstring *, const char *, int));
245 extern int dstring_append __PROTO((Dstring *, const char *, int));
246 extern int dstring_copy __PROTO((Dstring *, int, const char *, int));
247 extern int dstring_insert __PROTO((Dstring *, int, const char *, int));
248 extern void dstring_reset __PROTO((Dstring *));
249
250 #define dstring_length(d)       ((d)->length)
251 #define dstring_strcat(d,s)     dstring_append((d), (s), -1)
252
253 extern char *dgets __PROTO((Dstring *, FILE *));
254 extern int  file_readable __PROTO((const char *));
255 extern int  file_exists __PROTO((const char *));
256 extern const char *file_basename __PROTO((const char *));
257 extern const char *file_extension __PROTO((const char *));
258
259 /*
260  * Miscellaneous macros
261  */
262
263 #define LIST_FOREACH(ptr, type, list) \
264         for(ptr = (type *)(list)->head; ptr; ptr = (ptr)->next)
265
266 #define Size(x) (sizeof(x) / sizeof((x)[0]))
267
268 /* multiply a fix_word by a 32bit number */
269 #define B0(x)   ((x) & 0xff)
270 #define B1(x)   B0((x) >> 8)
271 #define B2(x)   B0((x) >> 16)
272 #define B3(x)   B0((x) >> 24)
273 #define __tfm_mul(z,t) \
274         (((((B0(t) * (z)) >> 8) + (B1(t) * (z))) >> 8) + B2(t) * (z))
275 #define TFMSCALE(z,t,a,b) \
276         ((B3(t) == 255) ? \
277                 __tfm_mul((z), (t)) / (b) - (a) : \
278                 __tfm_mul((z), (t)) / (b))
279 #define TFMPREPARE(x,z,a,b) do { \
280         a = 16; z = (x); \
281         while(z > 040000000L) { z >>= 1; a <<= 1; } \
282         b = 256 / a; a *= z; \
283         } while(0)
284
285 #endif /* _MDVI_COMMON_H */