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 / interfac / ienumstr.cpp < prev    next >
C/C++ Source or Header  |  1996-05-21  |  4KB  |  248 lines

  1. /*
  2.  * IENUMSTR.CPP
  3.  *
  4.  * Standard implementation of a string enumerator with the
  5.  * IEnumString interface that will generally not need
  6.  * modification.
  7.  *
  8.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Microsoft
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #include "ienumstr.h"
  17.  
  18.  
  19. /*
  20.  * CEnumString::CEnumString
  21.  * CEnumString::~CEnumString
  22.  *
  23.  * Parameters (Constructor):
  24.  *  pUnkRef         LPUNKNOWN to use for reference counting.
  25.  *  cstr            ULONG number of LPOLESTRs in pstr
  26.  *  prgstr          LPOLESTR * to the array to enumerate.
  27.  */
  28.  
  29. CEnumString::CEnumString(LPUNKNOWN pUnkRef, ULONG cstr
  30.     , LPOLESTR *prgstr)
  31.     {
  32.     UINT        i;
  33.  
  34.     m_cRef=0;
  35.     m_pUnkRef=pUnkRef;
  36.  
  37.     m_iCur=0;
  38.     m_cstr=cstr;
  39.     m_prgstr=new LPOLESTR[(UINT)cstr];
  40.  
  41.     if (NULL!=m_prgstr)
  42.         {
  43.         for (i=0; i < cstr; i++)
  44.             m_prgstr[i]=prgstr[i];
  45.         }
  46.  
  47.     return;
  48.     }
  49.  
  50.  
  51. CEnumString::~CEnumString(void)
  52.     {
  53.     if (NULL!=m_prgstr)
  54.         delete [] m_prgstr;
  55.  
  56.     return;
  57.     }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. /*
  65.  * CEnumString::QueryInterface
  66.  * CEnumString::AddRef
  67.  * CEnumString::Release
  68.  *
  69.  * Purpose:
  70.  *  IUnknown members for CEnumString object.
  71.  */
  72.  
  73. STDMETHODIMP CEnumString::QueryInterface(REFIID riid
  74.     , LPVOID *ppv)
  75.     {
  76.     *ppv=NULL;
  77.  
  78.     if (IID_IUnknown==riid || IID_IEnumString==riid)
  79.         *ppv=(LPVOID)this;
  80.  
  81.     if (NULL!=*ppv)
  82.         {
  83.         ((LPUNKNOWN)*ppv)->AddRef();
  84.         return NOERROR;
  85.         }
  86.  
  87.     return ResultFromScode(E_NOINTERFACE);
  88.     }
  89.  
  90.  
  91. STDMETHODIMP_(ULONG) CEnumString::AddRef(void)
  92.     {
  93.     ++m_cRef;
  94.     m_pUnkRef->AddRef();
  95.     return m_cRef;
  96.     }
  97.  
  98. STDMETHODIMP_(ULONG) CEnumString::Release(void)
  99.     {
  100.     m_pUnkRef->Release();
  101.  
  102.     if (0L!=--m_cRef)
  103.         return m_cRef;
  104.  
  105.     delete this;
  106.     return 0;
  107.     }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. /*
  116.  * CEnumString::Next
  117.  *
  118.  * Purpose:
  119.  *  Returns the next element in the enumeration.
  120.  *
  121.  * Parameters:
  122.  *  cstr            ULONG number of LPOLESTRs to return.
  123.  *  pstr            LPOLESTR in which to store the returned
  124.  *                  structures.
  125.  *  pulstr          ULONG * in which to return how many we
  126.  *                  enumerated.
  127.  *
  128.  * Return Value:
  129.  *  HRESULT         NOERROR if successful, S_FALSE otherwise,
  130.  */
  131.  
  132. STDMETHODIMP CEnumString::Next(ULONG cstr, LPOLESTR *ppstr
  133.     , ULONG *pulstr)
  134.     {
  135.     ULONG               cReturn=0L;
  136.  
  137.     if (NULL==m_prgstr)
  138.         return ResultFromScode(S_FALSE);
  139.  
  140.     if (NULL==pulstr)
  141.         {
  142.         if (1L!=cstr)
  143.             return ResultFromScode(E_POINTER);
  144.         }
  145.     else
  146.         *pulstr=0L;
  147.  
  148.     if (NULL==ppstr || m_iCur >= m_cstr)
  149.         return ResultFromScode(S_FALSE);
  150.  
  151.     while (m_iCur < m_cstr && cstr > 0)
  152.         {
  153.         *ppstr++=m_prgstr[m_iCur++];
  154.         cReturn++;
  155.         cstr--;
  156.         }
  157.  
  158.     if (NULL!=pulstr)
  159.         *pulstr=cReturn;
  160.  
  161.     return NOERROR;
  162.     }
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. /*
  171.  * CEnumString::Skip
  172.  *
  173.  * Purpose:
  174.  *  Skips the next n elements in the enumeration.
  175.  *
  176.  * Parameters:
  177.  *  cSkip           ULONG number of elements to skip.
  178.  *
  179.  * Return Value:
  180.  *  HRESULT         NOERROR if successful, S_FALSE if we could not
  181.  *                  skip the requested number.
  182.  */
  183.  
  184. STDMETHODIMP CEnumString::Skip(ULONG cSkip)
  185.     {
  186.     if (((m_iCur+cSkip) >= m_cstr) || NULL==m_prgstr)
  187.         return ResultFromScode(S_FALSE);
  188.  
  189.     m_iCur+=cSkip;
  190.     return NOERROR;
  191.     }
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198. /*
  199.  * CEnumString::Reset
  200.  *
  201.  * Purpose:
  202.  *  Resets the current element index in the enumeration to zero.
  203.  *
  204.  * Parameters:
  205.  *  None
  206.  */
  207.  
  208. STDMETHODIMP CEnumString::Reset(void)
  209.     {
  210.     m_iCur=0;
  211.     return NOERROR;
  212.     }
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219. /*
  220.  * CEnumString::Clone
  221.  *
  222.  * Purpose:
  223.  *  Returns another IEnumString with the same state as ourselves.
  224.  *
  225.  * Parameters:
  226.  *  ppEnum          LPENUMSTRING * in which to return the
  227.  *                  new object.
  228.  */
  229.  
  230. STDMETHODIMP CEnumString::Clone(LPENUMSTRING *ppEnum)
  231.     {
  232.     PCEnumString    pNew;
  233.  
  234.     *ppEnum=NULL;
  235.  
  236.     //Create the clone
  237.     pNew=new CEnumString(m_pUnkRef, m_cstr, m_prgstr);
  238.  
  239.     if (NULL==pNew)
  240.         return ResultFromScode(E_OUTOFMEMORY);
  241.  
  242.     pNew->AddRef();
  243.     pNew->m_iCur=m_iCur;
  244.  
  245.     *ppEnum=pNew;
  246.     return NOERROR;
  247.     }
  248.