]> git.saurik.com Git - wxWidgets.git/blame - src/tiff/tif_apple.c
Rich text lib separation.
[wxWidgets.git] / src / tiff / tif_apple.c
CommitLineData
b47c832e
RR
1/* $Header$ */
2
3/*
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26
27/*
28 * TIFF Library Macintosh-specific routines.
29 *
30 * These routines use only Toolbox and high-level File Manager traps.
31 * They make no calls to the THINK C "unix" compatibility library. Also,
32 * malloc is not used directly but it is still referenced internally by
33 * the ANSI library in rare cases. Heap fragmentation by the malloc ring
34 * buffer is therefore minimized.
35 *
36 * O_RDONLY and O_RDWR are treated identically here. The tif_mode flag is
37 * checked in TIFFWriteCheck().
38 *
39 * Create below fills in a blank creator signature and sets the file type
40 * to 'TIFF'. It is much better for the application to do this by Create'ing
41 * the file first and TIFFOpen'ing it later.
00cb87b4
VZ
42 * ---------
43 * This code has been "Carbonized", and may not work with older MacOS versions.
44 * If so, grab the tif_apple.c out of an older libtiff distribution, like
45 * 3.5.5 from www.libtiff.org.
b47c832e
RR
46 */
47
48#include "tiffiop.h"
87142ecc 49#include <MacErrors.h>
b47c832e
RR
50#include <Files.h>
51#include <Memory.h>
00cb87b4 52#include <Script.h>
b47c832e 53
00cb87b4 54#if defined(__PPCC__) || defined(__SC__) || defined(__MRC__) || defined(applec)
b47c832e
RR
55#define CtoPstr c2pstr
56#endif
57
58static tsize_t
59_tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
60{
61 return (FSRead((short) fd, (long*) &size, (char*) buf) == noErr ?
62 size : (tsize_t) -1);
63}
64
65static tsize_t
66_tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
67{
68 return (FSWrite((short) fd, (long*) &size, (char*) buf) == noErr ?
69 size : (tsize_t) -1);
70}
71
72static toff_t
73_tiffSeekProc(thandle_t fd, toff_t off, int whence)
74{
75 long fpos, size;
76
77 if (GetEOF((short) fd, &size) != noErr)
78 return EOF;
79 (void) GetFPos((short) fd, &fpos);
80
81 switch (whence) {
82 case SEEK_CUR:
83 if (off + fpos > size)
84 SetEOF((short) fd, off + fpos);
85 if (SetFPos((short) fd, fsFromMark, off) != noErr)
86 return EOF;
87 break;
88 case SEEK_END:
89 if (off > 0)
90 SetEOF((short) fd, off + size);
91 if (SetFPos((short) fd, fsFromStart, off + size) != noErr)
92 return EOF;
93 break;
94 case SEEK_SET:
95 if (off > size)
96 SetEOF((short) fd, off);
97 if (SetFPos((short) fd, fsFromStart, off) != noErr)
98 return EOF;
99 break;
100 }
101
102 return (toff_t)(GetFPos((short) fd, &fpos) == noErr ? fpos : EOF);
103}
104
105static int
106_tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
107{
108 return (0);
109}
110
111static void
112_tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
113{
114}
115
116static int
117_tiffCloseProc(thandle_t fd)
118{
119 return (FSClose((short) fd));
120}
121
122static toff_t
123_tiffSizeProc(thandle_t fd)
124{
125 long size;
126
127 if (GetEOF((short) fd, &size) != noErr) {
128 TIFFError("_tiffSizeProc", "%s: Cannot get file size");
129 return (-1L);
130 }
131 return ((toff_t) size);
132}
133
134/*
135 * Open a TIFF file descriptor for read/writing.
136 */
137TIFF*
138TIFFFdOpen(int fd, const char* name, const char* mode)
139{
140 TIFF* tif;
141
142 tif = TIFFClientOpen(name, mode, (thandle_t) fd,
143 _tiffReadProc, _tiffWriteProc, _tiffSeekProc, _tiffCloseProc,
144 _tiffSizeProc, _tiffMapProc, _tiffUnmapProc);
145 if (tif)
146 tif->tif_fd = fd;
147 return (tif);
148}
149
00cb87b4
VZ
150static void ourc2pstr( char* inString )
151{
152 int sLen = strlen( inString );
153 BlockMoveData( inString, &inString[1], sLen );
154 inString[0] = sLen;
155}
156
b47c832e
RR
157/*
158 * Open a TIFF file for read/writing.
159 */
160TIFF*
161TIFFOpen(const char* name, const char* mode)
162{
163 static const char module[] = "TIFFOpen";
164 Str255 pname;
165 FInfo finfo;
166 short fref;
167 OSErr err;
00cb87b4 168 FSSpec fSpec;
b47c832e
RR
169
170 strcpy((char*) pname, name);
00cb87b4
VZ
171 ourc2pstr((char*) pname);
172
173 err = FSMakeFSSpec( 0, 0, pname, &fSpec );
b47c832e
RR
174
175 switch (_TIFFgetMode(mode, module)) {
176 default:
177 return ((TIFF*) 0);
178 case O_RDWR | O_CREAT | O_TRUNC:
00cb87b4
VZ
179 if (FSpGetFInfo(&fSpec, &finfo) == noErr)
180 FSpDelete(&fSpec);
b47c832e
RR
181 /* fall through */
182 case O_RDWR | O_CREAT:
00cb87b4
VZ
183 if ((err = FSpGetFInfo(&fSpec, &finfo)) == fnfErr) {
184 if (FSpCreate(&fSpec, ' ', 'TIFF', smSystemScript) != noErr)
b47c832e 185 goto badCreate;
00cb87b4 186 if (FSpOpenDF(&fSpec, fsRdWrPerm, &fref) != noErr)
b47c832e
RR
187 goto badOpen;
188 } else if (err == noErr) {
00cb87b4 189 if (FSpOpenDF(&fSpec, fsRdWrPerm, &fref) != noErr)
b47c832e
RR
190 goto badOpen;
191 } else
192 goto badOpen;
193 break;
194 case O_RDONLY:
00cb87b4
VZ
195 if (FSpOpenDF(&fSpec, fsRdPerm, &fref) != noErr)
196 goto badOpen;
197 break;
b47c832e 198 case O_RDWR:
00cb87b4 199 if (FSpOpenDF(&fSpec, fsRdWrPerm, &fref) != noErr)
b47c832e
RR
200 goto badOpen;
201 break;
202 }
203 return (TIFFFdOpen((int) fref, name, mode));
204badCreate:
205 TIFFError(module, "%s: Cannot create", name);
206 return ((TIFF*) 0);
207badOpen:
208 TIFFError(module, "%s: Cannot open", name);
209 return ((TIFF*) 0);
210}
211
212void
213_TIFFmemset(tdata_t p, int v, tsize_t c)
214{
215 memset(p, v, (size_t) c);
216}
217
218void
219_TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c)
220{
221 memcpy(d, s, (size_t) c);
222}
223
224int
225_TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
226{
227 return (memcmp(p1, p2, (size_t) c));
228}
229
230tdata_t
231_TIFFmalloc(tsize_t s)
232{
233 return (NewPtr((size_t) s));
234}
235
236void
237_TIFFfree(tdata_t p)
238{
239 DisposePtr(p);
240}
241
242tdata_t
243_TIFFrealloc(tdata_t p, tsize_t s)
244{
245 Ptr n = p;
246
247 SetPtrSize(p, (size_t) s);
248 if (MemError() && (n = NewPtr((size_t) s)) != NULL) {
249 BlockMove(p, n, GetPtrSize(p));
250 DisposePtr(p);
251 }
252 return ((tdata_t) n);
253}
254
255static void
256appleWarningHandler(const char* module, const char* fmt, va_list ap)
257{
258 if (module != NULL)
259 fprintf(stderr, "%s: ", module);
260 fprintf(stderr, "Warning, ");
261 vfprintf(stderr, fmt, ap);
262 fprintf(stderr, ".\n");
263}
264TIFFErrorHandler _TIFFwarningHandler = appleWarningHandler;
265
266static void
267appleErrorHandler(const char* module, const char* fmt, va_list ap)
268{
269 if (module != NULL)
270 fprintf(stderr, "%s: ", module);
271 vfprintf(stderr, fmt, ap);
272 fprintf(stderr, ".\n");
273}
274TIFFErrorHandler _TIFFerrorHandler = appleErrorHandler;