home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / NETWORK / netpbm_src.lzh / NETPBM / LIBTIFF / tif_apple.c < prev    next >
Text File  |  1996-11-18  |  5KB  |  220 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_apple.c,v 1.13 93/08/25 09:15:36 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library Macintosh-specific routines.
  31.  *
  32.  * These routines use only Toolbox and high-level File Manager traps.
  33.  * They make no calls to the THINK C "unix" compatibility library.  Also,
  34.  * malloc is not used directly but it is still referenced internally by
  35.  * the ANSI library in rare cases.  Heap fragmentation by the malloc ring
  36.  * buffer is therefore minimized.
  37.  *
  38.  * O_RDONLY and O_RDWR are treated identically here.  The tif_mode flag is
  39.  * checked in TIFFWriteCheck().
  40.  *
  41.  * Create below fills in a blank creator signature and sets the file type
  42.  * to 'TIFF'.  It is much better for the application to do this by Create'ing
  43.  * the file first and TIFFOpen'ing it later.
  44.  */
  45.  
  46. #include "tiffiop.h"
  47. #include <Errors.h>
  48. #include <Files.h>
  49. #include <Memory.h>
  50.  
  51. #ifdef applec
  52. #define    CtoPstr    c2pstr
  53. #endif
  54.  
  55. static tsize_t
  56. _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
  57. {
  58.     return (FSRead((short) fd, (long*) &size, (char*) buf) == noErr ?
  59.         size : (tsize_t) -1);
  60. }
  61.  
  62. static tsize_t
  63. _tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
  64. {
  65.     return (FSWrite((short) fd, (long*) &size, (char*) buf) == noErr ?
  66.         size : (tsize_t) -1);
  67. }
  68.  
  69. static toff_t
  70. _tiffSeekProc(thandle_t fd, toff_t off, int whence)
  71. {
  72.     long fpos, size;
  73.  
  74.     if (GetEOF((short) fd, &size) != noErr)
  75.         return EOF;
  76.     (void) GetFPos((short) fd, &fpos);
  77.  
  78.     switch (whence) {
  79.     case SEEK_CUR:
  80.         if (off + fpos > size)
  81.             SetEOF((short) fd, off + fpos);
  82.         if (SetFPos((short) fd, fsFromMark, off) != noErr)
  83.             return EOF;
  84.         break;
  85.     case SEEK_END:
  86.         if (off > 0)
  87.             SetEOF((short) fd, off + size);
  88.         if (SetFPos((short) fd, fsFromStart, off + size) != noErr)
  89.             return EOF;
  90.         break;
  91.     case SEEK_SET:
  92.         if (off > size)
  93.             SetEOF((short) fd, off);
  94.         if (SetFPos((short) fd, fsFromStart, off) != noErr)
  95.             return EOF;
  96.         break;
  97.     }
  98.  
  99.     return (toff_t)(GetFPos((short) fd, &fpos) == noErr ? fpos : EOF);
  100. }
  101.  
  102. static int
  103. _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
  104. {
  105.     return (0);
  106. }
  107.  
  108. static void
  109. _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
  110. {
  111. }
  112.  
  113. static int
  114. _tiffCloseProc(thandle_t fd)
  115. {
  116.     return (FSClose((short) fd));
  117. }
  118.  
  119. static toff_t
  120. _tiffSizeProc(thandle_t fd)
  121. {
  122.     long size;
  123.  
  124.     if (GetEOF((short) fd, &size) != noErr) {
  125.         TIFFError("_tiffSizeProc", "%s: Cannot get file size");
  126.         return (-1L);
  127.     }
  128.     return ((toff_t) size);
  129. }
  130.  
  131. void *
  132. _TIFFmalloc(size_t s)
  133. {
  134.     return (NewPtr(s));
  135. }
  136.  
  137. void
  138. _TIFFfree(void* p)
  139. {
  140.     DisposePtr(p);
  141. }
  142.  
  143. void *
  144. _TIFFrealloc(void* p, size_t s)
  145. {
  146.     Ptr n = p;
  147.  
  148.     SetPtrSize(p, s);
  149.     if (MemError() && (n = NewPtr(s)) != NULL) {
  150.         BlockMove(p, n, GetPtrSize(p));
  151.         DisposePtr(p);
  152.     }
  153.     return (n);
  154. }
  155.  
  156. /*
  157.  * Open a TIFF file descriptor for read/writing.
  158.  */
  159. TIFF*
  160. TIFFFdOpen(int fd, const char* name, const char* mode)
  161. {
  162.     TIFF* tif;
  163.  
  164.     tif = TIFFClientOpen(name, mode, (thandle_t) fd,
  165.         _tiffReadProc, _tiffWriteProc, _tiffSeekProc, _tiffCloseProc,
  166.         _tiffSizeProc, _tiffMapProc, _tiffUnmapProc);
  167.     if (tif)
  168.         tif->tif_fd = fd;
  169.     return (tif);
  170. }
  171.  
  172. /*
  173.  * Open a TIFF file for read/writing.
  174.  */
  175. TIFF*
  176. TIFFOpen(const char* name, const char* mode)
  177. {
  178.     static const char module[] = "TIFFOpen";
  179.     Str255 pname;
  180.     FInfo finfo;
  181.     short fref;
  182.     OSErr err;
  183.  
  184.     strcpy((char*) pname, name);
  185.     CtoPstr((char*) pname);
  186.  
  187.     switch (_TIFFgetMode(mode, module)) {
  188.     default:
  189.         return ((TIFF*) 0);
  190.     case O_RDWR | O_CREAT | O_TRUNC:
  191.         if (GetFInfo(pname, 0, &finfo) == noErr)
  192.             FSDelete(pname, 0);
  193.         /* fall through */
  194.     case O_RDWR | O_CREAT:
  195.         if ((err = GetFInfo(pname, 0, &finfo)) == fnfErr) {
  196.             if (Create(pname, 0, '    ', 'TIFF') != noErr)
  197.                 goto badCreate;
  198.             if (FSOpen(pname, 0, &fref) != noErr)
  199.                 goto badOpen;
  200.         } else if (err == noErr) {
  201.             if (FSOpen(pname, 0, &fref) != noErr)
  202.                 goto badOpen;
  203.         } else
  204.             goto badOpen;
  205.         break;
  206.     case O_RDONLY:
  207.     case O_RDWR:
  208.         if (FSOpen(pname, 0, &fref) != noErr)
  209.             goto badOpen;
  210.         break;
  211.     }
  212.     return (TIFFFdOpen((int) fref, name, mode));
  213. badCreate:
  214.     TIFFError(module, "%s: Cannot create", name);
  215.     return ((TIFF*) 0);
  216. badOpen:
  217.     TIFFError(module, "%s: Cannot open", name);
  218.     return ((TIFF*) 0);
  219. }
  220.