home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap07 / patron / page.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  5KB  |  246 lines

  1. /*
  2.  * PAGE.CPP
  3.  * Patron Chapter 7
  4.  *
  5.  * Implementation of the CPage class to manage a single page.
  6.  *
  7.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Microsoft
  10.  * Internet  :  kraigb@microsoft.com
  11.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  12.  */
  13.  
  14.  
  15. #include "patron.h"
  16.  
  17.  
  18. /*
  19.  * CPage::CPage
  20.  * CPage::~CPage
  21.  *
  22.  * Constructor Parameters:
  23.  *  dwID            DWORD identifier for this page.
  24.  */
  25.  
  26. CPage::CPage(DWORD dwID)
  27.     {
  28.     //CHAPTER7MOD
  29.     m_dwID     =dwID;
  30.     m_pIStorage=NULL;
  31.     //End CHAPTER7MOD
  32.     return;
  33.     }
  34.  
  35. CPage::~CPage(void)
  36.     {
  37.     //CHAPTER7MOD
  38.     Close(FALSE);
  39.     //End CHAPTER7MOD
  40.     return;
  41.     }
  42.  
  43.  
  44.  
  45. /*
  46.  * CPage::GetID
  47.  *
  48.  * Return Value:
  49.  *  DWORD           dwID field in this page.
  50.  */
  51.  
  52. DWORD CPage::GetID(void)
  53.     {
  54.     return m_dwID;
  55.     }
  56.  
  57.  
  58.  
  59.  
  60. //CHAPTER7MOD
  61.  
  62. /*
  63.  * CPage::Open
  64.  *
  65.  * Purpose:
  66.  *  Retrieves the IStorage associated with this page.  The IStorage
  67.  *  is owned by the page and thus the page always holds a reference
  68.  *  count.  The caller should call Close or delete this page to
  69.  *  match this open.
  70.  *
  71.  *  This function may be called multiple times resulting in
  72.  *  additional reference counts on the storage each of which must be
  73.  *  matched with a call to Close.  The last Close can be done
  74.  *  through delete.
  75.  *
  76.  * Parameters:
  77.  *  pIStorage       LPSTORAGE in which this page lives.
  78.  *
  79.  * Return Value:
  80.  *  BOOL            TRUE if opening succeeds, FALSE otherwise.
  81.  */
  82.  
  83. BOOL CPage::Open(LPSTORAGE pIStorage)
  84.     {
  85.     BOOL        fNULL=FALSE;
  86.     HRESULT     hr=NOERROR;
  87.     DWORD       dwMode=STGM_TRANSACTED | STGM_READWRITE
  88.                     | STGM_SHARE_EXCLUSIVE;
  89.     OLECHAR     szTemp[32];
  90.  
  91.     if (NULL==m_pIStorage)
  92.         {
  93.         fNULL=TRUE;
  94.  
  95.         if (NULL==pIStorage)
  96.             return FALSE;
  97.  
  98.         /*
  99.          * Attempt to open the storage under this ID.  If none,
  100.          * create one.  In either case, the IStorage is either
  101.          * saved in pPage or released.
  102.          */
  103.  
  104.         GetStorageName(szTemp);
  105.  
  106.         hr=pIStorage->OpenStorage(szTemp, NULL, dwMode, NULL, 0
  107.             , &m_pIStorage);
  108.  
  109.         if (FAILED(hr))
  110.             {
  111.             hr=pIStorage->CreateStorage(szTemp, dwMode, 0, 0
  112.                 , &m_pIStorage);
  113.             }
  114.         }
  115.     else
  116.         m_pIStorage->AddRef();
  117.  
  118.     if (FAILED(hr))
  119.         {
  120.         if (fNULL)
  121.             m_pIStorage=NULL;
  122.  
  123.         return FALSE;
  124.         }
  125.  
  126.     return TRUE;
  127.     }
  128.  
  129.  
  130.  
  131.  
  132. /*
  133.  * CPage::Close
  134.  *
  135.  * Purpose:
  136.  *  Possibly commits the storage, then releases it reversing the
  137.  *  reference count from Open.
  138.  *
  139.  * Parameters:
  140.  *  fCommit         BOOL indicating if we're to commit.
  141.  *
  142.  * Return Value:
  143.  *  None
  144.  */
  145.  
  146. void CPage::Close(BOOL fCommit)
  147.     {
  148.     if (NULL==m_pIStorage)
  149.         return;
  150.  
  151.     if (fCommit)
  152.         Update();
  153.  
  154.     if (0L==m_pIStorage->Release())
  155.         m_pIStorage=NULL;
  156.  
  157.     return;
  158.     }
  159.  
  160.  
  161.  
  162.  
  163. /*
  164.  * CPage::Update
  165.  *
  166.  * Purpose:
  167.  *  Forces a common on the page if it's open.
  168.  *
  169.  * Parameters:
  170.  *  None
  171.  *
  172.  * Return Value:
  173.  *  BOOL            Always TRUE for now.
  174.  */
  175.  
  176. BOOL CPage::Update(void)
  177.     {
  178.     if (NULL!=m_pIStorage)
  179.         m_pIStorage->Commit(STGC_DEFAULT);
  180.  
  181.     return TRUE;
  182.     }
  183.  
  184.  
  185.  
  186.  
  187.  
  188. /*
  189.  * CPage::Destroy
  190.  *
  191.  * Purpose:
  192.  *  Removes this page from the given storage.  The caller should
  193.  *  eventually delete this Page object to free the storage.
  194.  *
  195.  * Parameters:
  196.  *  pIStorage       LPSTORAGE contianing this page on which to call
  197.  *                  DestroyElement
  198.  *
  199.  * Return Value:
  200.  *  None
  201.  */
  202.  
  203. void CPage::Destroy(LPSTORAGE pIStorage)
  204.     {
  205.     if (NULL!=pIStorage)
  206.         {
  207.         OLECHAR szTemp[32];
  208.  
  209.         Close(FALSE);
  210.         GetStorageName(szTemp);
  211.         pIStorage->DestroyElement(szTemp);
  212.         }
  213.  
  214.     return;
  215.     }
  216.  
  217.  
  218.  
  219.  
  220. /*
  221.  * CPage::GetStorageName
  222.  *
  223.  * Parameters:
  224.  *  pszName         LPOLESTR to a buffer in which to store the
  225.  *                  storage name for this page.
  226.  *
  227.  * Return Value:
  228.  *  UINT            Number of characters stored.
  229.  */
  230.  
  231. UINT CPage::GetStorageName(LPOLESTR pszName)
  232.     {
  233.    #ifdef WIN32ANSI
  234.     char        szTemp[32];
  235.     UINT        cch;
  236.  
  237.     cch=wsprintf(szTemp, "Page %lu", m_dwID);
  238.     MultiByteToWideChar(CP_ACP, 0, szTemp, -1, pszName, 32);
  239.     return cch;
  240.    #else
  241.     return wsprintf(pszName, TEXT("Page %lu"), m_dwID);
  242.    #endif
  243.     }
  244.  
  245. //End CHAPTER7MOD
  246.