diff options
author | Tor Andersson <tor@ghostscript.com> | 2010-07-08 22:32:24 +0200 |
---|---|---|
committer | Tor Andersson <tor@ghostscript.com> | 2010-07-08 22:32:24 +0200 |
commit | 3a4c396334bc500de4c9b1e957f030835b5df65e (patch) | |
tree | c6971cea67729ac9d95aeceed172050db3444f0e | |
parent | 839eda4994874c02cbaf546a40d374a4edb16539 (diff) | |
download | mupdf-3a4c396334bc500de4c9b1e957f030835b5df65e.tar.xz |
Simplify #ifdef labyrinth and make some source more c89 compatible.
-rw-r--r-- | apps/pdfapp.c | 2 | ||||
-rw-r--r-- | apps/pdfdraw.c | 4 | ||||
-rw-r--r-- | apps/win_main.c | 3 | ||||
-rw-r--r-- | apps/x11_main.c | 4 | ||||
-rw-r--r-- | draw/blendmodes.c | 5 | ||||
-rw-r--r-- | draw/pathscan.c | 19 | ||||
-rw-r--r-- | fitz/base_error.c | 99 | ||||
-rw-r--r-- | fitz/fitz_base.h | 173 | ||||
-rw-r--r-- | fitz/fitz_draw.h | 2 | ||||
-rw-r--r-- | fitz/util_gettimeofday.c | 4 | ||||
-rw-r--r-- | mupdf/fontdump.c | 8 | ||||
-rw-r--r-- | mupdf/mupdf.h | 11 |
12 files changed, 173 insertions, 161 deletions
diff --git a/apps/pdfapp.c b/apps/pdfapp.c index 416cddef..608d62f9 100644 --- a/apps/pdfapp.c +++ b/apps/pdfapp.c @@ -105,7 +105,7 @@ void pdfapp_open(pdfapp_t *app, char *filename, int fd) file = fz_openfile(fd); app->xref = pdf_openxref(file); if (!app->xref) - pdfapp_error(app, fz_throw("cannot open PDF file '%s'", filename)); + pdfapp_error(app, fz_rethrow(-1, "cannot open PDF file '%s'", filename)); fz_dropstream(file); /* diff --git a/apps/pdfdraw.c b/apps/pdfdraw.c index c3b93f6e..8608fe62 100644 --- a/apps/pdfdraw.c +++ b/apps/pdfdraw.c @@ -396,9 +396,9 @@ static void drawpages(char *pagelist) if (benchmark) { memset(&loadtimes, 0x00, sizeof (loadtimes)); - loadtimes.min = LONG_MAX; + loadtimes.min = 1<<31; memset(&drawtimes, 0x00, sizeof (drawtimes)); - drawtimes.min = LONG_MAX; + drawtimes.min = 1<<31; } spec = fz_strsep(&pagelist, ","); diff --git a/apps/win_main.c b/apps/win_main.c index 8ab8b371..29f2b29e 100644 --- a/apps/win_main.c +++ b/apps/win_main.c @@ -48,8 +48,9 @@ void winwarn(pdfapp_t *app, char *msg) void winerror(pdfapp_t *app, fz_error error) { + /* TODO: redirect stderr to a log file and display here */ fz_catch(error, "displaying error message to user"); - MessageBoxA(hwndframe, fz_errorbuf, "MuPDF: Error", MB_ICONERROR); + MessageBoxA(hwndframe, "An error has occurred.", "MuPDF: Error", MB_ICONERROR); exit(1); } diff --git a/apps/x11_main.c b/apps/x11_main.c index 8172b651..16ce9759 100644 --- a/apps/x11_main.c +++ b/apps/x11_main.c @@ -398,7 +398,7 @@ void windocopy(pdfapp_t *app) *utf8 = 0; *latin1 = 0; - printf("oncopy utf8=%zd latin1=%zd\n", strlen(copyutf8), strlen(copylatin1)); + printf("oncopy utf8=%d latin1=%d\n", strlen(copyutf8), strlen(copylatin1)); XSetSelectionOwner(xdpy, XA_PRIMARY, xwin, copytime); @@ -438,7 +438,7 @@ void onselreq(Window requestor, Atom selection, Atom target, Atom property, Time else if (target == XA_STRING) { - printf(" -> string %zd\n", strlen(copylatin1)); + printf(" -> string %d\n", strlen(copylatin1)); XChangeProperty(xdpy, requestor, property, target, 8, PropModeReplace, (unsigned char *)copylatin1, strlen(copylatin1)); diff --git a/draw/blendmodes.c b/draw/blendmodes.c index 973d79ab..f86fa1ef 100644 --- a/draw/blendmodes.c +++ b/draw/blendmodes.c @@ -150,8 +150,11 @@ sat(int r, int g, int b) static void setsat(int r, int g, int b, int s, int *dr, int *dg, int *db) { - int *m[3] = { &r, &g, &b }; /* min, med, max */ + int *m[3]; /* min, med, max */ int *t; + m[0] = &r; + m[1] = &g; + m[2] = &b; #define SWAP(a, b) (t = a, a = b, b = t) if (*m[0] > *m[1]) SWAP(m[0], m[1]); diff --git a/draw/pathscan.c b/draw/pathscan.c index f0b17e6a..fa3a6e92 100644 --- a/draw/pathscan.c +++ b/draw/pathscan.c @@ -1,5 +1,8 @@ #include "fitz.h" +#define BBOX_MIN -(1<<20) +#define BBOX_MAX (1<<20) + /* divide and floor towards -inf */ static inline int fz_idiv(int a, int b) { @@ -26,11 +29,11 @@ fz_newgel(void) gel->len = 0; gel->edges = fz_malloc(sizeof(fz_edge) * gel->cap); - gel->clip.x0 = gel->clip.y0 = INT_MAX; - gel->clip.x1 = gel->clip.y1 = INT_MIN; + gel->clip.x0 = gel->clip.y0 = BBOX_MAX; + gel->clip.x1 = gel->clip.y1 = BBOX_MIN; - gel->bbox.x0 = gel->bbox.y0 = INT_MAX; - gel->bbox.x1 = gel->bbox.y1 = INT_MIN; + gel->bbox.x0 = gel->bbox.y0 = BBOX_MAX; + gel->bbox.x1 = gel->bbox.y1 = BBOX_MIN; return gel; } @@ -40,8 +43,8 @@ fz_resetgel(fz_gel *gel, fz_bbox clip) { if (fz_isinfiniterect(clip)) { - gel->clip.x0 = gel->clip.y0 = INT_MAX; - gel->clip.x1 = gel->clip.y1 = INT_MIN; + gel->clip.x0 = gel->clip.y0 = BBOX_MAX; + gel->clip.x1 = gel->clip.y1 = BBOX_MIN; } else { gel->clip.x0 = clip.x0 * HSCALE; @@ -50,8 +53,8 @@ fz_resetgel(fz_gel *gel, fz_bbox clip) gel->clip.y1 = clip.y1 * VSCALE; } - gel->bbox.x0 = gel->bbox.y0 = INT_MAX; - gel->bbox.x1 = gel->bbox.y1 = INT_MIN; + gel->bbox.x0 = gel->bbox.y0 = BBOX_MAX; + gel->bbox.x1 = gel->bbox.y1 = BBOX_MIN; gel->len = 0; } diff --git a/fitz/base_error.c b/fitz/base_error.c index 3045d912..5aa8768c 100644 --- a/fitz/base_error.c +++ b/fitz/base_error.c @@ -1,86 +1,81 @@ #include "fitz.h" -char fz_errorbuf[150*20] = {0}; -static int fz_errorlen = 0; -static int fz_errorclear = 1; - -static void -fz_printerror(int type, const char *file, int line, const char *func, char *msg) +void fz_warn(char *fmt, ...) { - char buf[150]; - int len; - char *s; - - s = strrchr(file, '\\'); - if (s) - file = s + 1; - - fprintf(stderr, "%c %s:%d: %s(): %s\n", type, file, line, func, msg); - - snprintf(buf, sizeof buf, "%s:%d: %s(): %s", file, line, func, msg); - buf[sizeof(buf)-1] = 0; - len = strlen(buf); + va_list ap; + fprintf(stderr, "warning: "); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fprintf(stderr, "\n"); +} - if (fz_errorclear) - { - fz_errorclear = 0; - fz_errorlen = 0; - memset(fz_errorbuf, 0, sizeof fz_errorbuf); - } +fz_error +fz_throwimp(const char *file, int line, const char *func, char *fmt, ...) +{ + va_list ap; + fprintf(stderr, "+ %s:%d: %s(): ", file, line, func); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fprintf(stderr, "\n"); + return -1; +} - if (fz_errorlen + len + 2 < sizeof fz_errorbuf) - { - memcpy(fz_errorbuf + fz_errorlen, buf, len); - fz_errorlen += len; - fz_errorbuf[fz_errorlen++] = '\n'; - fz_errorbuf[fz_errorlen] = 0; - } +fz_error +fz_rethrowimp(const char *file, int line, const char *func, fz_error cause, char *fmt, ...) +{ + va_list ap; + fprintf(stderr, "| %s:%d: %s(): ", file, line, func); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fprintf(stderr, "\n"); + return cause; } -void fz_warn(char *fmt, ...) +void +fz_catchimp(const char *file, int line, const char *func, fz_error cause, char *fmt, ...) { va_list ap; - fprintf(stderr, "warning: "); + fprintf(stderr, "\\ %s:%d: %s(): ", file, line, func); va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); fprintf(stderr, "\n"); } -fz_error fz_throwimp(const char *file, int line, const char *func, char *fmt, ...) +fz_error +fz_throwimpx(char *fmt, ...) { - char buf[150]; va_list ap; + fprintf(stderr, "+ "); va_start(ap, fmt); - vsnprintf(buf, sizeof buf, fmt, ap); + vfprintf(stderr, fmt, ap); va_end(ap); - buf[sizeof(buf)-1] = 0; - fz_printerror('+', file, line, func, buf); + fprintf(stderr, "\n"); return -1; } -fz_error fz_rethrowimp(fz_error cause, const char *file, int line, const char *func, char *fmt, ...) +fz_error +fz_rethrowimpx(fz_error cause, char *fmt, ...) { - char buf[150]; va_list ap; + fprintf(stderr, "| "); va_start(ap, fmt); - vsnprintf(buf, sizeof buf, fmt, ap); + vfprintf(stderr, fmt, ap); va_end(ap); - buf[sizeof(buf)-1] = 0; - fz_printerror('|', file, line, func, buf); + fprintf(stderr, "\n"); return cause; } -fz_error fz_catchimp(fz_error cause, const char *file, int line, const char *func, char *fmt, ...) +void +fz_catchimpx(fz_error cause, char *fmt, ...) { - char buf[150]; va_list ap; + fprintf(stderr, "\\ "); va_start(ap, fmt); - vsnprintf(buf, sizeof buf, fmt, ap); + vfprintf(stderr, fmt, ap); va_end(ap); - buf[sizeof(buf)-1] = 0; - fz_printerror('\\', file, line, func, buf); - fz_errorclear = 1; - return cause; + fprintf(stderr, "\n"); } - diff --git a/fitz/fitz_base.h b/fitz/fitz_base.h index 9563b0c9..2f810709 100644 --- a/fitz/fitz_base.h +++ b/fitz/fitz_base.h @@ -1,39 +1,31 @@ -/* - * Include the basic standard libc headers. - */ - #ifndef _FITZ_BASE_H_ #define _FITZ_BASE_H_ +/* + * Include the standard libc headers. + */ + #include <stdio.h> #include <stdlib.h> #include <stddef.h> -#include <string.h> -#include <assert.h> #include <stdarg.h> - -#include <limits.h> /* INT_MIN, MAX ... */ -#include <float.h> /* DBL_EPSILON */ +#include <string.h> #include <math.h> +#include <assert.h> #include <errno.h> +#include <float.h> /* FLT_EPSILON */ #include <fcntl.h> /* O_RDONLY & co */ -/* Stupid macros that don't exist everywhere */ - -#ifndef O_BINARY -#define O_BINARY 0 -#endif +#define nil ((void*)0) -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif +#define nelem(x) (sizeof(x)/sizeof((x)[0])) -#ifndef M_SQRT2 -#define M_SQRT2 1.41421356237309504880 -#endif +/* + * Some differences in libc can be smoothed over + */ -#ifdef _MSC_VER /* stupid stone-age compiler */ +#ifdef _MSC_VER /* Microsoft Visual C */ #pragma warning( disable: 4244 ) /* conversion from X to Y, possible loss of data */ #pragma warning( disable: 4996 ) /* The POSIX name for this item is deprecated */ @@ -41,38 +33,66 @@ #include <io.h> -extern int gettimeofday(struct timeval *tv, struct timezone *tz); - -#define inline __inline - -#define __func__ __FUNCTION__ +int gettimeofday(struct timeval *tv, struct timezone *tz); #define snprintf _snprintf #define hypotf _hypotf #define strtoll _strtoi64 -#if _MSC_VER < 1500 -#define vsnprintf _vsnprintf -#endif - -#else /* unix or close enough */ +#else /* Unix or close enough */ #include <unistd.h> +#define O_BINARY 0 + #endif -#ifndef _C99 -#ifdef __GNUC__ -#define restrict __restrict -#else -#define restrict +#ifndef M_PI +#define M_PI 3.14159265358979323846 #endif + +#ifndef M_SQRT2 +#define M_SQRT2 1.41421356237309504880 #endif +/* + * Variadic macros, inline and restrict keywords + */ + +#if __STDC_VERSION__ == 199901L /* C99 */ + +#define fz_throw(...) fz_throwimp(__FILE__, __LINE__, __func__, __VA_ARGS__) +#define fz_rethrow(cause, ...) fz_rethrowimp(__FILE__, __LINE__, __func__, cause, __VA_ARGS__) +#define fz_catch(cause, ...) fz_catchimp(__FILE__, __LINE__, __func__, cause, __VA_ARGS__) + +#elif _MSC_VER >= 1500 /* MSVC 9 or newer */ + +#define inline __inline +#define restrict __restrict +#define fz_throw(...) fz_throwimp(__FILE__, __LINE__, __FUNCTION__, __VA_ARGS__) +#define fz_rethrow(cause, ...) fz_rethrowimp(__FILE__, __LINE__, __FUNCTION__, cause, __VA_ARGS__) +#define fz_catch(cause, ...) fz_catchimp(__FILE__, __LINE__, __FUNCTION__, cause, __VA_ARGS__) + +#elif __GNUC__ >= 3 /* GCC 3 or newer */ + +#define inline __inline +#define restrict __restrict +#define fz_throw(fmt...) fz_throwimp(__FILE__, __LINE__, __FUNCTION__, fmt) +#define fz_rethrow(cause, fmt...) fz_rethrowimp(__FILE__, __LINE__, __FUNCTION__, cause, fmt) +#define fz_catch(cause, fmt...) fz_catchimp(__FILE__, __LINE__, __FUNCTION__, cause, fmt) + +#else /* Unknown or ancient */ + +#define inline +#define restrict +#define fz_throw fz_throwimpx +#define fz_rethrow fz_rethrowimpx +#define fz_catch fz_catchimpx + #endif /* - * Base Fitz runtime. + * GCC can do type checking of printf strings */ #ifndef __printflike @@ -84,76 +104,57 @@ extern int gettimeofday(struct timeval *tv, struct timezone *tz); #endif #endif -#ifndef nil -#define nil ((void*)0) -#endif +/* + * Error handling + */ -#ifndef offsetof -#define offsetof(s, m) (unsigned long)(&(((s*)0)->m)) -#endif +typedef int fz_error; -#ifndef nelem -#define nelem(x) (sizeof(x)/sizeof((x)[0])) -#endif +void fz_warn(char *fmt, ...) __printflike(1, 2); -#ifndef ABS -#define ABS(x) ( (x) < 0 ? -(x) : (x) ) -#endif +fz_error fz_throwimp(const char *file, int line, const char *func, char *fmt, ...) __printflike(4, 5); +fz_error fz_rethrowimp(const char *file, int line, const char *func, fz_error cause, char *fmt, ...) __printflike(5, 6); +void fz_catchimp(const char *file, int line, const char *func, fz_error cause, char *fmt, ...) __printflike(5, 6); -#ifndef MAX -#define MAX(a,b) ( (a) > (b) ? (a) : (b) ) -#endif +fz_error fz_throwimpx(char *fmt, ...) __printflike(1, 2); +fz_error fz_rethrowimpx(fz_error cause, char *fmt, ...) __printflike(2, 3); +void fz_catchimpx(fz_error cause, char *fmt, ...) __printflike(2, 3); -#ifndef MIN -#define MIN(a,b) ( (a) < (b) ? (a) : (b) ) -#endif +#define fz_okay ((fz_error)0) -#ifndef CLAMP +/* + * Basic runtime and utility functions + */ + +#define ABS(x) ( (x) < 0 ? -(x) : (x) ) +#define MIN(a,b) ( (a) < (b) ? (a) : (b) ) +#define MAX(a,b) ( (a) > (b) ? (a) : (b) ) #define CLAMP(x,a,b) ( (x) > (b) ? (b) : ( (x) < (a) ? (a) : (x) ) ) -#endif + +/* memory allocation */ +void *fz_malloc(int n); +void *fz_realloc(void *p, int n); +void fz_free(void *p); +char *fz_strdup(char *s); /* runtime (hah!) test for endian-ness */ int fz_isbigendian(void); +/* safe string functions */ +char *fz_strsep(char **stringp, const char *delim); +int fz_strlcpy(char *dst, const char *src, int n); +int fz_strlcat(char *dst, const char *src, int n); + /* utf-8 encoding and decoding */ int chartorune(int *rune, char *str); int runetochar(char *str, int *rune); int runelen(int c); -/* useful string functions */ -extern char *fz_strsep(char **stringp, const char *delim); -extern int fz_strlcpy(char *dst, const char *src, int n); -extern int fz_strlcat(char *dst, const char *src, int n); - /* getopt */ extern int fz_getopt(int nargc, char * const * nargv, const char *ostr); extern int fz_optind; extern char *fz_optarg; -/* memory allocation */ -void *fz_malloc(int n); -void *fz_realloc(void *p, int n); -void fz_free(void *p); -char *fz_strdup(char *s); - -/* - * Error handling. - */ - -typedef int fz_error; - -extern char fz_errorbuf[]; - -void fz_warn(char *fmt, ...) __printflike(1,2); -fz_error fz_throwimp(const char *file, int line, const char *func, char *fmt, ...) __printflike(4, 5); -fz_error fz_rethrowimp(fz_error cause, const char *file, int line, const char *func, char *fmt, ...) __printflike(5, 6); -fz_error fz_catchimp(fz_error cause, const char *file, int line, const char *func, char *fmt, ...) __printflike(5, 6); - -#define fz_throw(...) fz_throwimp(__FILE__, __LINE__, __func__, __VA_ARGS__) -#define fz_rethrow(cause, ...) fz_rethrowimp(cause, __FILE__, __LINE__, __func__, __VA_ARGS__) -#define fz_catch(cause, ...) fz_catchimp(cause, __FILE__, __LINE__, __func__, __VA_ARGS__) -#define fz_okay ((fz_error)0) - /* * Generic hash-table with fixed-length keys. */ diff --git a/fitz/fitz_draw.h b/fitz/fitz_draw.h index 0f08249e..a2f65579 100644 --- a/fitz/fitz_draw.h +++ b/fitz/fitz_draw.h @@ -34,7 +34,7 @@ typedef enum fz_blendkind_e FZ_BHUE, FZ_BSATURATION, FZ_BCOLOR, - FZ_BLUMINOSITY + FZ_BLUMINOSITY, } fz_blendkind; /* diff --git a/fitz/util_gettimeofday.c b/fitz/util_gettimeofday.c index ee89d445..425c6440 100644 --- a/fitz/util_gettimeofday.c +++ b/fitz/util_gettimeofday.c @@ -33,4 +33,8 @@ int gettimeofday(struct timeval *tv, struct timezone *tz) return 0; } +#else + +void fz_gettimeofday_dummy() { } + #endif diff --git a/mupdf/fontdump.c b/mupdf/fontdump.c index 1ca902dc..4bf4d3b2 100644 --- a/mupdf/fontdump.c +++ b/mupdf/fontdump.c @@ -45,6 +45,12 @@ main(int argc, char **argv) return 1; } + fprintf(fo, "#ifndef __STRICT_ANSI__\n"); + fprintf(fo, "#ifdef __linux__\n"); + fprintf(fo, "#define HAVE_INCBIN\n"); + fprintf(fo, "#endif\n"); + fprintf(fo, "#endif\n\n"); + for (i = 2; i < argc; i++) { fi = fopen(argv[i], "rb"); @@ -76,7 +82,7 @@ main(int argc, char **argv) fprintf(fo, "const unsigned int pdf_font_%s_len = %d;\n", name, len); - fprintf(fo, "#ifdef __linux__\n"); + fprintf(fo, "#ifdef HAVE_INCBIN\n"); fprintf(fo, "asm(\".globl pdf_font_%s_buf\");\n", name); fprintf(fo, "asm(\".balign 8\");\n"); fprintf(fo, "asm(\"pdf_font_%s_buf:\");\n", name); diff --git a/mupdf/mupdf.h b/mupdf/mupdf.h index 789e2ce3..387595a2 100644 --- a/mupdf/mupdf.h +++ b/mupdf/mupdf.h @@ -63,15 +63,14 @@ unsigned short * pdf_toucs2(fz_obj *src); typedef struct pdf_crypt_s pdf_crypt; typedef struct pdf_cryptfilter_s pdf_cryptfilter; -typedef enum pdf_cryptmethod_e pdf_cryptmethod; -enum pdf_cryptmethod_e +typedef enum pdf_cryptmethod_e { PDF_CRYPT_NONE, PDF_CRYPT_RC4, PDF_CRYPT_AESV2, PDF_CRYPT_UNKNOWN, -}; +} pdf_cryptmethod; struct pdf_cryptfilter_s { @@ -183,7 +182,7 @@ typedef enum pdf_itemkind_e PDF_KPATTERN, PDF_KSHADING, PDF_KCMAP, - PDF_KFONT + PDF_KFONT, } pdf_itemkind; pdf_store * pdf_newstore(void); @@ -563,7 +562,7 @@ typedef struct pdf_csi_s pdf_csi; enum { PDF_MFILL, - PDF_MSTROKE + PDF_MSTROKE, }; enum @@ -573,7 +572,7 @@ enum PDF_MLAB, PDF_MINDEXED, PDF_MPATTERN, - PDF_MSHADE + PDF_MSHADE, }; struct pdf_material_s |