File: rwTable.c

package info (click to toggle)
xpaint 2.5.1-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,488 kB
  • ctags: 2,478
  • sloc: ansic: 25,980; makefile: 36; sh: 23
file content (273 lines) | stat: -rw-r--r-- 6,122 bytes parent folder | download | duplicates (3)
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
/* +-------------------------------------------------------------------+ */
/* | Copyright 1993, David Koblas (koblas@netcom.com)		       | */
/* |								       | */
/* | Permission to use, copy, modify, and to distribute this software  | */
/* | and its documentation for any purpose is hereby granted without   | */
/* | fee, provided that the above copyright notice appear in all       | */
/* | copies and that both that copyright notice and this permission    | */
/* | notice appear in supporting documentation.	 There is no	       | */
/* | representations about the suitability of this software for	       | */
/* | any purpose.  this software is provided "as is" without express   | */
/* | or implied warranty.					       | */
/* |								       | */
/* +-------------------------------------------------------------------+ */

/* $Id: rwTable.c,v 1.6 1996/06/09 17:31:28 torsten Exp $ */

#if defined(HAVE_PARAM_H)
#include <sys/param.h>
#endif

#include <stdio.h>
#include "image.h"
#include "rwTable.h"
#include <string.h>
#include <errno.h>

#ifdef MISSING_STDARG_H
#include <varargs.h>
#else
#include <stdarg.h>
#endif

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

typedef struct {
    char *name;
    RWreadFunc read;
    RWwriteFunc write;
    RWtestFunc test;
} ImageTypes;

static Image *readMagic(char *);
static char RWtableMsg[512];

/*
**  Define all the read/write functions here.
**    [ unfortunately most compilers won't take
**	the "variable" name version of these. ]
 */

extern Image *ReadXBM(char *);
extern Image *ReadXWD(char *);
extern Image *ReadPNM(char *);
extern Image *ReadPNG(char *);
extern Image *ReadGIF(char *);
extern Image *ReadSGI(char *);
extern Image *ReadXPM(char *);
extern Image *ReadTIFF(char *);
extern Image *ReadJPEG(char *);
extern int WriteXBM(char *, Image *);
extern int WriteXWD(char *, Image *);
extern int WritePNM(char *, Image *);
extern int WriteXPM(char *, Image *);
extern int WritePNGn(char *, Image *);
extern int WritePNGi(char *, Image *);
extern int WriteGIF(char *, Image *);
extern int WriteSGI(char *, Image *);
extern int WriteTIFF(char *, Image *);
extern int WriteJPEG(char *, Image *);
extern int WritePS(char *, Image *);
extern int TestPNM(char *);
extern int TestXWD(char *);
extern int TestXBM(char *);
extern int TestXPM(char *);
extern int TestPNG(char *);
extern int TestGIF(char *);
extern int TestSGI(char *);
extern int TestTIFF(char *);
extern int TestJPEG(char *);

#define DEF_READ_ENTRY	0
#define DEF_WRITE_ENTRY	1

/* GRR 960219:  added PNG, alphabetized image types: */
static ImageTypes RWtable[] =
{
    {"Best Guess",       readMagic, NULL,      NULL    },
    {"GIF Format",       ReadGIF,   WriteGIF,  TestGIF },
#ifdef HAVE_JPEG
    {"JPEG Format",      ReadJPEG,  WriteJPEG, TestJPEG},
#endif
#ifdef HAVE_PNG   /* ReadPNG does all PNG files; no need for two entries */
    {"PNG Format",       ReadPNG,   WritePNGn, TestPNG },
    {"PNG (interlaced)", NULL,      WritePNGi, TestPNG },
#endif
    {"PPM Format",       ReadPNM,   WritePNM,  TestPNM },
    {"PS Format",        NULL,      WritePS,   NULL    },
#ifdef HAVE_SGI
    {"SGI Format",       ReadSGI,   WriteSGI,  TestSGI },
#endif
#ifdef HAVE_TIFF
    {"TIFF Format",      ReadTIFF,  WriteTIFF, TestTIFF},
#endif
    {"XBM Format",       ReadXBM,   WriteXBM,  TestXBM },
    {"XPM Format",       ReadXPM,   WriteXPM,  TestXPM },
    {"XWD Format",       ReadXWD,   WriteXWD,  TestXWD },
};

#define	NUMBER	(sizeof(RWtable) / sizeof(RWtable[0]))

static char *readList[NUMBER + 1];
static char *writeList[NUMBER + 1];

/*
**  Special reader that uses the above information.
 */
static char *usedMagicReader = NULL;

static Image *
readMagic(char *file)
{
    extern int errno;
    int i;

    errno = 0;

    for (i = 0; i < NUMBER; i++) {
	if (RWtable[i].read == NULL || RWtable[i].test == NULL)
	    continue;
	if (!RWtable[i].test(file))
	    continue;
	usedMagicReader = RWtable[i].name;
	return RWtable[i].read(file);
    }

    if (errno == 0)
	RWSetMsg("Unknown image format");

    return NULL;
}

void *
RWtableGetReaderID()
{
    return (void *) usedMagicReader;
}


/*
**  Give a name, return an "opaque" handle to some information
 */
void *
RWtableGetEntry(char *name)
{
    int i;

    for (i = 0; i < NUMBER; i++)
	if (strcmp(name, RWtable[i].name) == 0)
	    return (void *) &RWtable[i];
    return NULL;
}

char *
RWtableGetId(void *v)
{
    ImageTypes *entry = (ImageTypes *) v;

    if (entry == NULL)
	return NULL;

    return entry->name;
}

RWreadFunc
RWtableGetReader(void *entry)
{
    RWtableMsg[0] = '\0';

    if (entry == NULL)
	return RWtable[DEF_READ_ENTRY].read;

    return ((ImageTypes *) entry)->read;
}

RWwriteFunc
RWtableGetWriter(void *entry)
{
    RWtableMsg[0] = '\0';

    if (entry == NULL)
	return RWtable[DEF_WRITE_ENTRY].write;

    return ((ImageTypes *) entry)->write;
}

char **
RWtableGetReaderList()
{
    static int done = FALSE;
    int i, idx = 0;

    if (!done) {
	for (i = 0; i < NUMBER; i++)
	    if (RWtable[i].read != NULL)
		readList[idx++] = RWtable[i].name;
	readList[idx++] = NULL;
	done = TRUE;
    }
    return readList;
}

char **
RWtableGetWriterList()
{
    static int done = FALSE;
    int i, idx = 0;

    if (!done) {
	for (i = 0; i < NUMBER; i++)
	    if (RWtable[i].write != NULL)
		writeList[idx++] = RWtable[i].name;
	writeList[idx++] = NULL;
	done = TRUE;
    }
    return writeList;
}

char *
RWGetMsg()
{
#if defined(BSD4_4)
    __const extern char *__const sys_errlist[];
#elsif !defined (__GLIBC__)
    extern char *sys_errlist[];
#endif
    extern int errno;

    if (RWtableMsg[0] == '\0') {
	if (errno == 0)
	    return "";
#if defined(__STDC__) && !defined(MISSING_STRERROR)
	return strerror(errno);
#else
	return sys_errlist[errno];
#endif
    }
    return RWtableMsg;
}

#ifdef MISSING_STDARG_H
void RWSetMsg(va_alist)
va_dcl
{
    va_list ap;
    char *fmt;

    va_start(ap);
    fmt = va_arg(ap, char *);
    vsprintf(RWtableMsg, fmt, ap);
}

#else
void RWSetMsg(char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    vsprintf(RWtableMsg, fmt, ap);
}
#endif