home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1999 October / VPR9910A.BIN / OLS / unrar32005 / unrar32005.lzh / src / mapf.cxx < prev    next >
C/C++ Source or Header  |  1998-04-03  |  1KB  |  53 lines

  1. /*
  2.  *   Copyright (c) 1998 T. Kamei (kamei@jsdlab.co.jp)
  3.  *
  4.  *   Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for any purpose is hereby granted provided
  6.  * that the above copyright notice and this permission notice appear
  7.  * in all copies of the software and related documentation.
  8.  *
  9.  *                          NO WARRANTY
  10.  *
  11.  *   THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY WARRANTIES;
  12.  * WITHOUT EVEN THE IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS
  13.  * FOR A PARTICULAR PURPOSE.
  14.  */
  15.  
  16. #include <windows.h>
  17. #include "mapf.h"
  18.  
  19. void
  20. mapf::close ()
  21. {
  22.   if (mf_base)
  23.     UnmapViewOfFile (mf_base);
  24.   if (mf_hm)
  25.     CloseHandle (mf_hm);
  26.   if (mf_hf != INVALID_HANDLE_VALUE)
  27.     CloseHandle (mf_hf);
  28.   init ();
  29. }
  30.  
  31. int
  32. mapf::open (const char *path, int mode)
  33. {
  34.   close ();
  35.   mf_hf = CreateFile (path, GENERIC_READ, FILE_SHARE_READ, 0,
  36.                       OPEN_EXISTING, mode, 0);
  37.   if (mf_hf == INVALID_HANDLE_VALUE)
  38.     return 0;
  39.  
  40.   mf_size = GetFileSize (mf_hf, 0);
  41.   if (mf_size)
  42.     {
  43.       mf_hm = CreateFileMapping (mf_hf, 0, PAGE_READONLY, 0, 0, 0);
  44.       if (!mf_hm)
  45.         return 0;
  46.  
  47.       mf_base = MapViewOfFile (mf_hm, FILE_MAP_READ, 0, 0, 0);
  48.       if (!mf_base)
  49.         return 0;
  50.     }
  51.   return 1;
  52. }
  53.