home *** CD-ROM | disk | FTP | other *** search
/ ftp.whtech.com / ftp.whtech.com.7z / ftp.whtech.com / emulators / v9t9 / linux / sources / V9t9 / source / OSLib / FileHandles.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-10-19  |  3.5 KB  |  188 lines

  1. #include "conf.h"
  2. #include "OSLib.h"
  3.  
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #if __MWERKS__ && !__INTEL__
  8. #include <alloca.h>
  9. #else
  10. #include <malloc.h>
  11. #endif
  12. #include <ctype.h>
  13.  
  14. #include <stdlib.h>
  15. #include <assert.h>
  16.  
  17.  
  18. /*    File handle routines.  */
  19.  
  20. /*
  21.     Currently implemented stupid 'n' simple.  
  22.     Later, we will want to use purgeable handles on the Mac
  23.     and mmap()ing on other OSes.
  24. */
  25.  
  26. static      OSError
  27. OS_LoadFileHandle(OSFileHandle * hand)
  28. {
  29.     OSError     err;
  30.     OSRef       ref;
  31.     OSSize      sz;
  32.     void       *buffer;
  33.  
  34.     hand->loaded = false;
  35.     if ((err = OS_Open(&hand->spec, OSReadOnly, &ref)) != OS_NOERR)
  36.         goto err_0;
  37.  
  38.     if ((err = OS_GetSize(ref, &sz)) != OS_NOERR)
  39.         goto err_1;
  40.  
  41.     if ((err = OS_ResizeHandle(&hand->hand, sz)) != OS_NOERR)
  42.         goto err_1;
  43.  
  44.     buffer = OS_LockHandle(&hand->hand);
  45.  
  46.     if ((err = OS_Read(ref, buffer, &sz)) != OS_NOERR)
  47.         goto err_2;
  48.  
  49.     hand->loaded = true;
  50.     hand->changed = false;
  51.  
  52.   err_2:
  53.     OS_UnlockHandle(&hand->hand);
  54.   err_1:
  55.     OS_Close(ref);
  56.   err_0:
  57.     return err;
  58.  
  59. }
  60.  
  61. static      OSError
  62. OS_WriteFileHandle(OSFileHandle * hand)
  63. {
  64.     OSError     err;
  65.     OSRef       ref;
  66.     OSSize      sz;
  67.     void       *buffer;
  68.  
  69.     if (!hand->loaded && !hand->changed)
  70.         return OS_NOERR;
  71.  
  72.     OS_Delete(&hand->spec);
  73.  
  74.     if ((err = OS_Create(&hand->spec, &OS_TEXTTYPE)) != OS_NOERR)
  75.         goto err_0;
  76.  
  77.     if ((err = OS_Open(&hand->spec, OSReadWrite, &ref)) != OS_NOERR)
  78.         goto err_0;
  79.  
  80.     if ((err = OS_GetHandleSize(&hand->hand, &sz)) != OS_NOERR)
  81.         goto err_0;
  82.  
  83.     buffer = OS_LockHandle(&hand->hand);
  84.  
  85.     if ((err = OS_Write(ref, buffer, &sz)) != OS_NOERR)
  86.         goto err_2;
  87.  
  88.     hand->changed = false;
  89.  
  90.   err_2:
  91.     OS_UnlockHandle(&hand->hand);
  92.   err_1:
  93.     OS_Close(ref);
  94.   err_0:
  95.     return err;
  96. }
  97.  
  98. /*    Create a new file handle from a given spec;
  99.     if src is non-NULL, copy this handle (don't link) */
  100. OSError
  101. OS_NewFileHandle(OSSpec * spec, OSHandle * src, bool writeable,
  102.                  OSFileHandle * hand)
  103. {
  104.     OSError     err;
  105.  
  106.     if (!writeable && src != NULL)
  107.         return OS_PERMERR;
  108.  
  109.     hand->spec = *spec;
  110.     hand->writeable = writeable;
  111.     if (src == NULL) {
  112.         err = OS_NewHandle(0, &hand->hand);
  113.  
  114.         if (err != OS_NOERR)
  115.             return err;
  116.  
  117.         /* load once at outset */
  118.         err = OS_LoadFileHandle(hand);
  119.     } else {
  120.         err = OS_CopyHandle(src, &hand->hand);
  121.  
  122.         if (err != OS_NOERR)
  123.             return err;
  124.  
  125.         hand->changed = true;
  126.         hand->loaded = true;
  127.     }
  128.     return err;
  129. }
  130.  
  131. /*    Lock a file handle into memory  */
  132. OSError
  133. OS_LockFileHandle(OSFileHandle * hand, void **ptr, OSSize * size)
  134. {
  135.     /*  Currently, we give the actual image to the caller,
  136.        but in the future, depending on 'writeable', we should
  137.        protect the memory.
  138.      */
  139.     *size = 0;
  140.  
  141.     if (!OS_ValidHandle(&hand->hand))
  142.         return OS_MEMERR;
  143.  
  144.     *ptr = OS_LockHandle(&hand->hand);
  145.     OS_GetHandleSize(&hand->hand, size);
  146.     return OS_NOERR;
  147. }
  148.  
  149. /*    Unlock file handle */
  150. OSError
  151. OS_UnlockFileHandle(OSFileHandle * hand, void *ptr)
  152. {
  153.     if (!OS_ValidHandle(&hand->hand))
  154.         return OS_MEMERR;
  155.     OS_UnlockHandle(&hand->hand);
  156.     return OS_NOERR;
  157. }
  158.  
  159. /*    Dispose file handle;
  160.     this guarantees that changes are flushed */
  161. OSError
  162. OS_FreeFileHandle(OSFileHandle * hand)
  163. {
  164.     OSError     err;
  165.  
  166.     /*  In the future, this will have already been done. */
  167.     if (hand->writeable && hand->changed) {
  168.         if ((err = OS_WriteFileHandle(hand)) != OS_NOERR)
  169.             return err;
  170.     }
  171.  
  172.     if (!OS_ValidHandle(&hand->hand))
  173.         return OS_MEMERR;
  174.  
  175.     if ((err = OS_FreeHandle(&hand->hand)) != OS_NOERR)
  176.         return err;
  177.  
  178.     hand->loaded = 0;
  179.     return OS_NOERR;
  180. }
  181.  
  182. /*    Get spec from the file handle */
  183. void
  184. OS_GetFileHandleSpec(OSFileHandle * hand, OSSpec * spec)
  185. {
  186.     *spec = hand->spec;
  187. }
  188.