home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / Native / JView / javaprop.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-04  |  5.0 KB  |  252 lines

  1. /*++
  2.  
  3. (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  4.  
  5. Module Name:
  6.  
  7.     cresprop.cpp
  8.  
  9. Abstract:
  10.  
  11.     Implements the CEnumResourceJAVAPROPERTY class.
  12.  
  13. --*/
  14.  
  15. #pragma hdrstop
  16.  
  17. #include "jview.h"
  18. #include "javaprop.hpp"
  19.  
  20. // Construct a new, empty property enumerator.
  21.  
  22. CEnumJAVAPROPERTY::CEnumJAVAPROPERTY()
  23. {
  24.     m_cRef = 1;
  25.  
  26.     m_pPropList   = NULL;
  27.     m_pCurProp    = NULL;
  28.     m_cProperties = 0;
  29. }
  30.  
  31. CEnumJAVAPROPERTY::~CEnumJAVAPROPERTY()
  32. {
  33.     LPJAVAPROPLink pProp = m_pPropList;
  34.     LPJAVAPROPLink pPropTmp;
  35.     
  36.     // Walk down the list of properties freeing each node.
  37.  
  38.     while (pProp)
  39.     {
  40.         if (pProp->pszKey)   delete(pProp->pszKey);
  41.         if (pProp->pszValue) delete(pProp->pszValue);
  42.         
  43.         pPropTmp = pProp;
  44.         pProp    = pProp->pNext;
  45.  
  46.         delete(pPropTmp);
  47.     }
  48. }
  49.  
  50. STDMETHODIMP
  51. CEnumJAVAPROPERTY::QueryInterface(REFIID riid, LPVOID *ppvObj)
  52. {
  53.     HRESULT hr;
  54.  
  55.     if (riid == IID_IEnumJAVAPROPERTY || riid == IID_IUnknown) {
  56.         *ppvObj = (LPENUMJAVAPROPERTY) this;
  57.         m_cRef++;
  58.         hr = S_OK;
  59.     } else {
  60.         *ppvObj = NULL;
  61.         hr = E_NOINTERFACE;
  62.     }
  63.  
  64.     return hr;
  65. }
  66.  
  67. STDMETHODIMP_(ULONG)
  68. CEnumJAVAPROPERTY::AddRef()
  69. {
  70.     m_cRef++;
  71.  
  72.     return m_cRef;
  73. }
  74.  
  75. STDMETHODIMP_(ULONG)
  76. CEnumJAVAPROPERTY::Release()
  77. {
  78.     m_cRef--;
  79.  
  80.     if (m_cRef > 0)
  81.         return m_cRef;
  82.  
  83.     delete this;
  84.     return 0;
  85. }
  86.  
  87. STDMETHODIMP
  88. CEnumJAVAPROPERTY::Next(ULONG celt, LPJAVAPROPERTY rgelt, ULONG
  89.     *pceltFetched)
  90. {
  91.     HRESULT hr;
  92.     ULONG celtFetched;
  93.  
  94.     hr = S_OK;
  95.     celtFetched = 0;
  96.  
  97.     while (hr == S_OK && celtFetched < celt) {
  98.  
  99.         if (m_pCurProp == NULL) {
  100.             hr = S_FALSE;
  101.             break;
  102.         }
  103.  
  104.         // Copy the value.
  105.  
  106.         rgelt->pszValue = (LPOLESTR)
  107.             CoTaskMemAlloc((lstrlenW(m_pCurProp->pszValue) + 1) *
  108.             sizeof(OLECHAR));
  109.  
  110.         if (rgelt->pszValue != NULL) {
  111.             wcscpy(rgelt->pszValue, m_pCurProp->pszValue);
  112.         } else {
  113.             hr = E_OUTOFMEMORY;
  114.         }
  115.  
  116.         // Copy the key.
  117.  
  118.         if (hr == S_OK) 
  119.         {
  120.  
  121.             rgelt->pszKey = (LPOLESTR)
  122.                 CoTaskMemAlloc((lstrlenW(m_pCurProp->pszKey) + 1) *
  123.                 sizeof(OLECHAR));
  124.  
  125.             if (rgelt->pszKey != NULL) {
  126.  
  127.                 wcscpy(rgelt->pszKey, m_pCurProp->pszKey);
  128.  
  129.                 rgelt++;
  130.                 m_pCurProp = m_pCurProp->pNext;
  131.                 celtFetched++;
  132.  
  133.             } else {
  134.                 CoTaskMemFree(rgelt->pszValue);
  135.                 hr = E_OUTOFMEMORY;
  136.             }
  137.         }
  138.     }
  139.  
  140.     if (pceltFetched != NULL)
  141.         *pceltFetched = celtFetched;
  142.  
  143.     return hr;
  144. }
  145.  
  146. STDMETHODIMP
  147. CEnumJAVAPROPERTY::Skip(ULONG celt)
  148. {
  149.     HRESULT hr = S_OK;
  150.  
  151.     while ( celt > 0 )
  152.     {
  153.         if ( m_pCurProp->pNext == NULL )
  154.         {
  155.             hr = S_FALSE;
  156.             break;
  157.         }
  158.  
  159.         m_pCurProp = m_pCurProp->pNext;
  160.         celt--;
  161.     }
  162.  
  163.     return hr;
  164. }
  165.  
  166. STDMETHODIMP
  167. CEnumJAVAPROPERTY::Reset()
  168. {
  169.     m_pCurProp = m_pPropList;
  170.     return S_OK;
  171. }
  172.  
  173. STDMETHODIMP
  174. CEnumJAVAPROPERTY::Clone(LPENUMJAVAPROPERTY *ppenum)
  175. {
  176.     return E_NOTIMPL;
  177. }
  178.  
  179. // Add a property to the list of properties.  The string parameter is
  180. // expected to be of the form <key>=<value>.
  181.  
  182. BOOL CEnumJAVAPROPERTY::Add(char *pszKeyAndValue)
  183. {
  184.     char *pszKey = pszKeyAndValue;
  185.     char *pszVal = pszKey;
  186.     int   cchUnicode;
  187.  
  188.     if ( *pszKey == '\0' )
  189.         return FALSE;
  190.  
  191.     // Walk down the string looking for the '=' that seperates the key
  192.     // and value.
  193.     while ( *pszVal && *pszVal != '=' )
  194.         pszVal++;
  195.  
  196.     if ( *pszVal == '=' ) {
  197.        *pszVal = '\0';
  198.        pszVal++;
  199.     }
  200.  
  201.     // Create a new node for our linked list.
  202.     LPJAVAPROPLink pProp = new(JAVAPROPLink);
  203.  
  204.     if ( !pProp )
  205.         return FALSE;
  206.  
  207.     // We want to keep track of the key and value strings as unicode.  
  208.     // Convert them and stash them off into the property node.
  209.     
  210.     // Convert the key.
  211.  
  212.     cchUnicode = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, pszKey, -1, 
  213.                                      NULL, 0);
  214.  
  215.     pProp->pszKey = new(OLECHAR[cchUnicode]);
  216.  
  217.     if ( !pProp->pszKey )
  218.     {
  219.         delete(pProp);
  220.         return FALSE;
  221.     }
  222.  
  223.     MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,pszKey,-1,pProp->pszKey,
  224.                         cchUnicode);
  225.  
  226.     // Convert the value.
  227.  
  228.     cchUnicode = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, pszVal, 
  229.                                      -1, NULL, 0);
  230.  
  231.     pProp->pszValue = new(OLECHAR[cchUnicode]);
  232.  
  233.     if ( !pProp->pszValue )
  234.     {
  235.         delete(pProp->pszKey);
  236.         delete(pProp);
  237.         return FALSE;
  238.     }
  239.  
  240.     MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,pszVal,-1,pProp->pszValue,
  241.                         cchUnicode);
  242.  
  243.     // Link the new node into the list.
  244.  
  245.     pProp->pNext = m_pPropList;
  246.     m_pPropList  = pProp;
  247.     m_pCurProp   = m_pPropList;
  248.  
  249.     return TRUE;
  250. }
  251.  
  252.