home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / Sessions / Traut / ZStrings / Source / Win32 / Win32ZStringTools.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  4.5 KB  |  173 lines

  1. /*==================================================================
  2.     File:        Win32ZStringTools.cpp
  3.  
  4.     Contains:    Defines the class behaviors for the application
  5.  
  6.     Written by:    Nalini Prakash
  7.  
  8.     Copyright:    2000-2001 Connectix Corporation
  9.     
  10.     This source has been placed into the public domain by
  11.     Connectix Corporation. You have the right to modify, 
  12.     distribute or use this code without any legal limitations
  13.     or finanicial/licensing requirements. Connectix is not 
  14.     liable for any problems that result from the use of this 
  15.     code.
  16.     
  17.     If you have comments, feedback, questions, or would like
  18.     to submit bug fixes or updates to this code, please email
  19.     opensource@connectix.com.
  20. ==================================================================*/
  21.  
  22. #include "StdAfx.h"
  23. #include "Win32ZStringTools.h"
  24. #include "Win32ZStringToolsDlg.h"
  25.  
  26. #include "ZStringTypes.h"
  27. #include "resource.h"
  28.  
  29. #ifdef _DEBUG
  30. #define new DEBUG_NEW
  31. #undef THIS_FILE
  32. static char THIS_FILE[] = __FILE__;
  33. #endif
  34.  
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CWin32ZStringToolsApp
  37.  
  38. BEGIN_MESSAGE_MAP(CWin32ZStringToolsApp, CWinApp)
  39.     //{{AFX_MSG_MAP(CWin32ZStringToolsApp)
  40.     //}}AFX_MSG_MAP
  41.     ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  42. END_MESSAGE_MAP()
  43.  
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CWin32ZStringToolsApp construction
  46.  
  47. CWin32ZStringToolsApp::CWin32ZStringToolsApp()
  48. {
  49. }
  50.  
  51. /////////////////////////////////////////////////////////////////////////////
  52. // The one and only CWin32ZStringToolsApp object
  53.  
  54. CWin32ZStringToolsApp theApp;
  55.  
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CWin32ZStringToolsApp initialization
  58.  
  59. BOOL CWin32ZStringToolsApp::InitInstance()
  60. {
  61.     AfxEnableControlContainer();
  62.  
  63.     // Standard initialization
  64.     // If you are not using these features and wish to reduce the size
  65.     //  of your final executable, you should remove from the following
  66.     //  the specific initialization routines you do not need.
  67.  
  68. #ifdef _AFXDLL
  69.     Enable3dControls();            // Call this when using MFC in a shared DLL
  70. #else
  71.     Enable3dControlsStatic();    // Call this when linking to MFC statically
  72. #endif
  73.  
  74.     ::OleInitialize(NULL);        // Enable Drag & drop
  75.  
  76.     CWin32ZStringToolsDlg dlg;
  77.     m_pMainWnd = &dlg;
  78.     int nResponse = dlg.DoModal();
  79.  
  80.     // Since the dialog has been closed, return FALSE so that we exit the
  81.     //  application, rather than start the application's message pump.
  82.     return FALSE;
  83. }
  84.  
  85.  
  86. /*------------------------------------------------------------------
  87.     OpenMemMappedFile
  88.         
  89.           Open file and create memory mapped view.
  90. ------------------------------------------------------------------*/
  91.  
  92. BOOL
  93. OpenMemMappedFile(
  94.     const char *        inPathName,
  95.     HANDLE &            outFileHandle,
  96.     HANDLE &            outMemfileHandle,
  97.     char *&                outData,
  98.     Z_UInt32 &            outSize)
  99. {
  100.     outSize = 0;
  101.     outData = NULL;
  102.     outFileHandle = INVALID_HANDLE_VALUE;
  103.  
  104.     // Open the file.
  105.     outFileHandle = ::CreateFile(
  106.                         inPathName,                    // file name
  107.                         GENERIC_READ,                // access mode
  108.                         FILE_SHARE_READ,            // share mode
  109.                         NULL,                        // SD
  110.                         OPEN_EXISTING,                // how to create, fails if file doesn't exist
  111.                         FILE_ATTRIBUTE_NORMAL,      // file attributes
  112.                         NULL);                        // handle to template file
  113.  
  114.     if (outFileHandle == INVALID_HANDLE_VALUE)
  115.         return false;
  116.  
  117.     DWORD sizeLow = 0;
  118.     DWORD sizeHigh = 0;
  119.     sizeLow = ::GetFileSize (outFileHandle, &sizeHigh);
  120.     outSize = sizeLow;
  121.  
  122.     // Create file mapping.
  123.     outMemfileHandle = ::CreateFileMapping(
  124.                                 outFileHandle,            // handle to file
  125.                                 NULL,                    // security
  126.                                 PAGE_READONLY,            // protection
  127.                                 sizeHigh,                // high-order DWORD of size
  128.                                 outSize,                // low-order DWORD of size
  129.                                 NULL);                    // object name
  130.  
  131.     if (outMemfileHandle)
  132.     {
  133.         // Map file into memory.
  134.         outData = (char *)::MapViewOfFile(
  135.                     outMemfileHandle,        // handle to file-mapping object
  136.                     FILE_MAP_READ,            // access mode
  137.                     0,                        // high-order DWORD of offset
  138.                     0,                        // low-order DWORD of offset
  139.                     0);                        // number of bytes to map - map entire file.
  140.     }
  141.     else
  142.     {
  143.         // Display Error Message.
  144.         DWORD err = ::GetLastError();
  145.         CloseHandle(outFileHandle);
  146.         outFileHandle = INVALID_HANDLE_VALUE;
  147.     }
  148.  
  149.     return (outData != NULL); 
  150. }
  151.  
  152.  
  153. /*------------------------------------------------------------------
  154.     CloseFiles
  155. ------------------------------------------------------------------*/
  156.  
  157. void 
  158. CloseFiles(
  159.     void *        inData,
  160.     HANDLE &    inMemFileHandle,
  161.     HANDLE &    inFileHandle)
  162. {
  163.     ::UnmapViewOfFile(inData);
  164.     inData = 0;
  165.  
  166.     ::CloseHandle(inMemFileHandle);
  167.     inMemFileHandle = INVALID_HANDLE_VALUE;
  168.  
  169.     ::CloseHandle(inFileHandle);
  170.     inFileHandle = INVALID_HANDLE_VALUE; 
  171. }
  172.  
  173.