home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / DevTools / MachOViewer / Source / MappedFile.m < prev    next >
Encoding:
Text File  |  1994-02-10  |  2.9 KB  |  193 lines

  1. //H+
  2. //    Implementation of a simple object representing a memory-mapped file.
  3. //H-
  4.  
  5. /* $Log:    MappedFile.m,v $
  6. Revision 1.2  94/02/10  22:20:08  ediger
  7. added - (char *)errorString method to assist in error handling
  8.  
  9. Revision 1.1  94/02/07  21:38:37  ediger
  10. Initial revision
  11.  */
  12.  
  13. #import <MappedFile.h>
  14.  
  15. @implementation MappedFile
  16.  
  17. static char MappedFileRcsIdent[] = "$Id: MappedFile.m,v 1.2 94/02/10 22:20:08 ediger Exp Locker: ediger $";
  18.  
  19. //F+  -init
  20. //F-
  21. - init
  22. {
  23.     [super init];
  24.  
  25.     mappedAddress = NULL;
  26.     lastErrno = 0;
  27.     lastKernReturn = KERN_SUCCESS;
  28.     FileName = NULL;
  29.     fd = -1;  // -1 signifies that file descriptor is not used.
  30.  
  31.     return self;
  32. }
  33.  
  34. //F+  -free
  35. //F-
  36. - free
  37. {
  38.     if (fd > -1 || mappedAddress != NULL)
  39.         [self unmap];
  40.  
  41.     if (FileName != NULL)
  42.         free(FileName);
  43.  
  44.     [super free];
  45.  
  46.     return self;
  47. }
  48.  
  49. //F+  -(BOOL)map
  50. //
  51. //    Returns TRUE if memory-mapping of file worked, FALSE if it failed.
  52. //
  53. //    Must set name of file to map first.
  54. //F-
  55. - (BOOL)map
  56. {
  57.     assert(FileName != NULL);
  58.  
  59.     if (fd > -1 || mappedAddress != NULL)
  60.         [self unmap];
  61.  
  62.     fd = open(FileName, O_RDONLY);
  63.     if (fd < 0)
  64.     {
  65.         lastErrno = errno;
  66.         return FALSE;
  67.     }
  68.  
  69.     if (fstat(fd, &statBuf) < 0)
  70.     {
  71.         lastErrno = errno;
  72.         close(fd);
  73.         return FALSE;
  74.     }
  75.  
  76.     lastKernReturn = map_fd(fd, (vm_offset_t)NULL,
  77.         (vm_offset_t *)&mappedAddress, TRUE, statBuf.st_size);
  78.  
  79.     if (lastKernReturn != KERN_SUCCESS)
  80.     {
  81.         close(fd);
  82.         return FALSE;
  83.     }
  84.  
  85.     return TRUE;
  86. }
  87.  
  88. //F+  -(BOOL)unmap
  89. //
  90. //    Returns TRUE if unmapping of file worked, FALSE if it failed.
  91. //
  92. //F-
  93. - (BOOL)unmap
  94. {
  95.     BOOL fSuccess;
  96.  
  97.     fSuccess = FALSE;
  98.  
  99.     if (mappedAddress == NULL || fd == -1)
  100.         return TRUE;  // allow repeated use of this message
  101.  
  102.     lastKernReturn = vm_deallocate(task_self(), (vm_address_t)mappedAddress,
  103.         statBuf.st_size);
  104.     if (lastKernReturn == KERN_SUCCESS)
  105.         fSuccess = TRUE;
  106.     else
  107.         mappedAddress = NULL;
  108.  
  109.     if (close(fd) < 0)
  110.     {
  111.         lastErrno = errno;
  112.         fSuccess = FALSE;
  113.     } else
  114.         fd = -1;
  115.  
  116.     return fSuccess;
  117. }
  118.  
  119. //F+    -(char *)address
  120. //F-
  121. - (char *)address
  122. {
  123.     return mappedAddress;
  124. }
  125.  
  126. //F+    -(char *)filename
  127. //F-
  128. - (char *)filename
  129. {
  130.     return FileName;
  131. }
  132.  
  133. //F+    -(int)size
  134. //F-
  135. - (int)size
  136. {
  137.     return statBuf.st_size;
  138. }
  139.  
  140. //F+    -(int)lastUnixError
  141. //F-
  142. - (int)lastUnixError
  143. {
  144.     return lastErrno;
  145. }
  146.  
  147. //F+    -(kern_return_t)lastMachError
  148. //F-
  149. - (kern_return_t)lastMachError
  150. {
  151.     return lastKernReturn;
  152. }
  153.  
  154. //F+    -filename:(char *)fileName
  155. //
  156. //    Set filename of file to be memory mapped.
  157. //    Possibly an unwise pseudo-method-name overloading.
  158. //
  159. //F-
  160. - filename:(char *)fileName
  161. {
  162.     if (fileName != NULL && strlen(fileName) > 0)
  163.     {
  164.         if (FileName != NULL)
  165.             free(FileName);
  166.  
  167.         FileName = malloc(strlen(fileName) + 1);
  168.  
  169.         if (FileName != NULL)
  170.             strcpy(FileName, fileName);
  171.     }
  172.  
  173.     return self;
  174. }
  175.  
  176. //F+
  177. //F-
  178. - (char *)errorString
  179. {
  180.     char *bprErrorString = NULL;
  181.  
  182.     if (lastErrno != 0)
  183.         bprErrorString = sys_errlist[lastErrno];
  184.     else if (lastKernReturn != KERN_SUCCESS)
  185.         bprErrorString = mach_error_string(lastKernReturn);
  186.     else
  187.         bprErrorString = "No error";
  188.  
  189.     return bprErrorString;
  190. }
  191.  
  192. @end
  193.