📄 cookie.cpp
字号:
}
CString strAttributes;
if (!RenderAttributes(strAttributes))
return FALSE;
if (strAttributes.GetLength() > 0)
{
strCookie += "; ";
strCookie += strAttributes;
}
DWORD dwLenCookie = strCookie.GetLength() + 1;
*pdwLen = dwLenCookie;
if (!szCookieBuffer)
return TRUE; // caller just wanted the length
// see if buffer is big enough
if (dwLenCookie > dwLenBuff)
return FALSE; //buffer wasn't big enough
// copy the buffer
strcpy(szCookieBuffer, strCookie);
}
catch(...)
{
return FALSE;
}
return TRUE;
}
/************************************************************************/
/* */
/* Function name : RenderAttributes */
/* Description : Render attributes into a buffer */
/* */
/************************************************************************/
BOOL CCookie::RenderAttributes(CString& strAttributes)
{
strAttributes.Empty();
POSITION pos = m_Attributes.GetStartPosition();
CString key, val;
for (int i=0; pos; i++)
{
if (i)
strAttributes += ";";
m_Attributes.GetNextAssoc(pos, key, val);
strAttributes += key;
strAttributes += '=';
strAttributes += val;
}
return TRUE;
}
/************************************************************************/
/* */
/* Function name : Copy */
/* Description : Copy all data from specified cookie */
/* */
/************************************************************************/
CCookie& CCookie::Copy(const CCookie& thatCookie)
{
m_strName = thatCookie.m_strName;
m_strValue = thatCookie.m_strValue;
POSITION pos = NULL;
CString strName, strValue;
if (!thatCookie.m_Attributes.IsEmpty())
{
pos = thatCookie.m_Attributes.GetStartPosition();
while (pos)
{
thatCookie.m_Attributes.GetNextAssoc(pos, strName, strValue);
m_Attributes.SetAt(strName, strValue);
}
}
if (!thatCookie.m_Values.IsEmpty())
{
strName.Empty();
strValue.Empty();
pos = thatCookie.m_Values.GetStartPosition();
while (pos)
{
thatCookie.m_Values.GetNextAssoc(pos, strName, strValue);
m_Values.SetAt(strName, strValue);
}
}
return *this;
}
/************************************************************************/
/* */
/* Function name : GetItem */
/* Description : Retrieves the dictionary value for the specified key */
/* */
/************************************************************************/
VARIANT CCookie::GetItem(const VARIANT FAR& Key)
{
VARIANT vaResult;
VariantInit(&vaResult);
if (Key.vt == VT_ERROR)
{
// assume it's a simple cookie and return the primary value
vaResult.vt = VT_BSTR;
// vaResult.bstrVal = m_strValue.AllocSysString();
vaResult.bstrVal = m_strValue.AllocSysString();
}
else
{
// multiple value cookie
COleVariant vaKey(Key);
// make sure it's a BSTR
vaKey.ChangeType(VT_BSTR);
CString strKey = vaKey.bstrVal;
CString strValue;
// lookup value of specified key
if (m_Values.Lookup(strKey, strValue))
{
vaResult.vt = VT_BSTR;
vaResult.bstrVal = strValue.AllocSysString();
}
}
return vaResult;
}
/************************************************************************/
/* */
/* Function name : SetItem */
/* Description : Add (or modify) the cookie value(s) */
/* */
/************************************************************************/
void CCookie::SetItem(const VARIANT FAR& Key, const VARIANT FAR& Value)
{
// multiple value cookie
COleVariant vaKey(Key);
COleVariant vaValue(Value);
// make sure the're BSTR's
vaKey.ChangeType(VT_BSTR);
vaValue.ChangeType(VT_BSTR);
CString strKey = vaKey.bstrVal;
CString strValue = vaValue.bstrVal;
// first check if it's a multiple value cookie
if (!AddValue(strKey, strValue))
{
// single value
SetValue(strValue);
}
}
/************************************************************************/
/* */
/* Function name : GetNewEnum */
/* Description : Implementation of the enumerator (for each ...) */
/* */
/************************************************************************/
LPUNKNOWN CCookie::GetNewEnum()
{
CCookieEnum* pEnum = new CCookieEnum(this);
if (pEnum)
{
pEnum->m_xEnumVARIANT.Reset();
return &pEnum->m_xEnumVARIANT;
}
return NULL;
}
/************************************************************************/
/* */
/* Function name : HasKeys */
/* Description : Indicates whether or not the cookie has keys. */
/* */
/************************************************************************/
BOOL CCookie::HasKeys()
{
return !m_Values.IsEmpty();
}
/************************************************************************/
/* */
/* Function name : CCookieEnum */
/* Description : Construct enumerator class */
/* */
/************************************************************************/
CCookieEnum::CCookieEnum(CCookie *pCookie)
{
m_pCookie = pCookie;
EnableAutomation();
}
CCookieEnum::~CCookieEnum()
{
}
/************************************************************************/
/* */
/* Function name : OnFinalRelease */
/* Description : Last reference is released -> delete the object. */
/* */
/************************************************************************/
void CCookieEnum::OnFinalRelease()
{
delete this;
}
BEGIN_MESSAGE_MAP(CCookieEnum, CCmdTarget)
//{{AFX_MSG_MAP(CCookieEnum)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BEGIN_INTERFACE_MAP(CCookieEnum, CCmdTarget)
INTERFACE_PART(CCookieEnum, IID_IEnumVARIANT, EnumVARIANT)
END_INTERFACE_MAP()
/************************************************************************/
/* */
/* Function name : XEnumVARIANT */
/* Description : Enumerator constructor */
/* */
/************************************************************************/
CCookieEnum::XEnumVARIANT::XEnumVARIANT()
{
m_posCurrent = NULL;
}
/************************************************************************/
/* */
/* Function name : AddRef */
/* Description : */
/* */
/************************************************************************/
STDMETHODIMP_(ULONG) CCookieEnum::XEnumVARIANT::AddRef()
{
METHOD_PROLOGUE(CCookieEnum, EnumVARIANT)
return pThis->ExternalAddRef();
}
/************************************************************************/
/* */
/* Function name : IEnumVARIANT::Release */
/* Description : */
/* */
/************************************************************************/
STDMETHODIMP_(ULONG) CCookieEnum::XEnumVARIANT::Release()
{
METHOD_PROLOGUE(CCookieEnum, EnumVARIANT)
return pThis->ExternalRelease();
}
/************************************************************************/
/* */
/* Function name : IEnumVARIANT::QueryInterface */
/* Description : */
/* */
/************************************************************************/
STDMETHODIMP CCookieEnum::XEnumVARIANT::QueryInterface(REFIID iid, void FAR* FAR* ppvObj )
{
METHOD_PROLOGUE(CCookieEnum, EnumVARIANT)
return (HRESULT)pThis->ExternalQueryInterface((void FAR*)&iid, ppvObj);
}
/************************************************************************/
/* */
/* Function name : IEnumVARIANT::Next */
/* Description : Get the next celt items in the enumeration sequence. */
/* */
/************************************************************************/
STDMETHODIMP CCookieEnum::XEnumVARIANT::Next(ULONG celt, VARIANT FAR* rgvar, ULONG FAR* pceltFetched)
{
// This sets up the "pThis" pointer so that it points to the CCookieEnum instance
METHOD_PROLOGUE(CCookieEnum, EnumVARIANT)
HRESULT hr;
ULONG l;
CString strKey, strValue;
// pceltFetched can legally == 0
if (pceltFetched != NULL)
{
*pceltFetched = 0;
}
else
if (celt > 1)
{
return ResultFromScode(E_INVALIDARG);
}
// initialize variants
for (l=0; l < celt; l++)
{
VariantInit(&rgvar[l]);
}
// Retrieve the next celt elements.
hr = NOERROR;
for (l = 0 ; m_posCurrent != NULL && celt != 0 ; l++)
{
pThis->m_pCookie->m_Values.GetNextAssoc(m_posCurrent, strKey, strValue);
celt--;
// stuff it into a variant
rgvar[l].vt = VT_BSTR;
rgvar[l].bstrVal = strKey.AllocSysString();
if (pceltFetched != NULL)
(*pceltFetched)++;
}
if (celt != 0)
{
hr = ResultFromScode(S_FALSE);
}
return hr;
}
/************************************************************************/
/* */
/* Function name : IEnumVARIANT::Skip */
/* Description : Skip over the next celt elements in the enumeration */
/* sequence. */
/* */
/************************************************************************/
STDMETHODIMP CCookieEnum::XEnumVARIANT::Skip(unsigned long celt)
{
METHOD_PROLOGUE(CCookieEnum, EnumVARIANT)
ASSERT(pThis->m_pCookie);
if (pThis->m_pCookie == NULL)
{
return ResultFromScode(E_UNEXPECTED);
}
CString strKey, strValue;
while (m_posCurrent != NULL && celt--)
pThis->m_pCookie->m_Values.GetNextAssoc(m_posCurrent, strKey, strValue);
return (celt == 0 ? NOERROR : ResultFromScode(S_FALSE));
}
/************************************************************************/
/* */
/* Function name : IEnumVARIANT::Reset */
/* Description : Reset postion pointer */
/* */
/************************************************************************/
STDMETHODIMP CCookieEnum::XEnumVARIANT::Reset()
{
METHOD_PROLOGUE(CCookieEnum, EnumVARIANT)
ASSERT(pThis->m_pCookie);
m_posCurrent = pThis->m_pCookie->m_Values.GetStartPosition();
return NOERROR;
}
/************************************************************************/
/* */
/* Function name : IEnumVARIANT::Clone */
/* Description : Clone the collection */
/* */
/************************************************************************/
STDMETHODIMP CCookieEnum::XEnumVARIANT::Clone(IEnumVARIANT FAR* FAR* ppenum)
{
METHOD_PROLOGUE(CCookieEnum, EnumVARIANT)
CCookieEnum* p = new CCookieEnum(pThis->m_pCookie);
if (p)
{
p->m_xEnumVARIANT.m_posCurrent = m_posCurrent;
return NOERROR;
}
else
return ResultFromScode(E_OUTOFMEMORY);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -