?? myhttpclient.cpp
字號(hào):
// If the specified key name does not exist in the map
if ( cKeys.Value () == 0 )
return FALSE ;
// Allocates memory to save pointers to key name.
// If the allocation failed, the memory pointed by the key name pointer are leaked.
::SafeInt<size_t> cbRequired ;
PCSZ * arrKeys = NULL ;
try {
cbRequired = cKeys ;
cbRequired *= sizeof (PCSZ) ;
arrKeys = (PCSZ *) ::malloc (cbRequired.Value ()) ;
} catch (::SafeIntException &) {
arrKeys = NULL ;
}
std::pair<MapIter, MapIter> pairIter = m_map.equal_range (szName) ;
MapSizeType nIdx = 0 ;
for (MapIter iter = pairIter.first; iter != pairIter.second; ++iter) {
(iter->second).Delete () ;
// Saves the key name pointer
if ( arrKeys )
arrKeys[nIdx++] = iter->first ;
}
m_map.erase (pairIter.first, pairIter.second) ;
if ( arrKeys == NULL )
return TRUE ;
for (nIdx = 0; nIdx < cKeys; nIdx++)
SAFEFREE ( arrKeys[nIdx] ) ;
SAFEFREE ( arrKeys ) ;
return TRUE ;
}
// This function deletes an element of which the key name equals to szName and the index is nIdx.
// If some data cleared, it will return TRUE, otherwise return FALSE.
template <typename HttpTool>
BOOL CHttpClientMapT<HttpTool>::Remove (PCSZ szName, DWORD nIdx)
throw (Exception &)
{
HTTPCLIENT_ASSERT (szName != NULL, "CHttpClientMapT::Remove: szName can not be NULL.") ;
std::pair<MapIter, MapIter> pairIter = m_map.equal_range (szName) ;
// If the specified key name does not exist in the map
if ( pairIter.first == pairIter.second )
return FALSE ;
MapIter iter = pairIter.first ;
for (DWORD i = 0; i < nIdx; i++) {
++iter ;
// If the nIdx is out of range.
if ( iter == pairIter.second )
return FALSE ;
}
// Deletes the element
(iter->second).Delete () ;
PCSZ szKeyName = iter->first ;
m_map.erase (iter) ;
SAFEFREE (szKeyName) ;
return TRUE ;
}
template <typename HttpTool>
BOOL CHttpClientMapT<HttpTool>::Exists (PCSZ szName, DWORD nIdx)
throw (Exception &)
{
HTTPCLIENT_ASSERT (szName != NULL, "CHttpClientMapT::Exists: szName can not be NULL.") ;
::SafeInt<MapSizeType> cKeys = m_map.count (szName) ;
return (cKeys > nIdx) ;
}
// If the szName is NULL, it will return the count of elements.
template <typename HttpTool>
DWORD CHttpClientMapT<HttpTool>::Count (PCSZ szName)
throw ()
{
::SafeInt<DWORD> cKeys ;
try {
cKeys = szName ? m_map.count (szName) : m_map.size () ;
} catch (::SafeIntException &) {
cKeys = cKeys.MaxInt () ; // This statement is never executed.
}
return cKeys.Value () ;
}
template <typename HttpTool>
BOOL CHttpClientMapT<HttpTool>::Empty (void) const
throw ()
{
return m_map.empty () ? TRUE : FALSE ;
}
// If the nIdx is not valid, it will return NULL.
template <typename HttpTool>
typename CHttpClientMapT<HttpTool>::PCSZ
CHttpClientMapT<HttpTool>::GetKey (DWORD nIdx)
throw ()
{
if ( m_map.empty () )
return NULL ;
MapIter iter = m_map.begin () ;
for (DWORD i = 0; i < nIdx; i++) {
++iter ;
// If the nIdx is not valid.
if ( iter == m_map.end () )
return NULL ;
}
return iter->first ;
}
// If the specified element is not found in position nIdx,
// returns MapValue of which szValue is NULL.
template <typename HttpTool>
typename CHttpClientMapT<HttpTool>::MapValue
CHttpClientMapT<HttpTool>::Get (DWORD nIdx)
throw ()
{
MapValue mapValue = { NULL, 0 } ;
if ( m_map.empty () )
return mapValue ;
MapIter iter = m_map.begin () ;
for (DWORD i = 0; i < nIdx; i++) {
++iter ;
// If the nIdx is not valid.
if ( iter == m_map.end () )
return mapValue ;
}
mapValue = iter->second ;
if ( mapValue.szValue == NULL )
mapValue.szValue = HttpTool::szEmptyString ;
return mapValue ;
}
// If the specified element is not found, it will return NULL.
template <typename HttpTool>
typename CHttpClientMapT<HttpTool>::PCSZ
CHttpClientMapT<HttpTool>::GetValue (DWORD nIdx)
throw ()
{
return Get (nIdx).szValue ;
}
// Returns 0 if the specified element is not found.
// (If the dwFlag in MapValue is 0, it also returns 0)
template <typename HttpTool>
DWORD CHttpClientMapT<HttpTool>::GetFlag (DWORD nIdx)
throw ()
{
return Get (nIdx).dwFlag ;
}
// If the specified element is not found, returns MapValue of which szValue is NULL.
template <typename HttpTool>
typename CHttpClientMapT<HttpTool>::MapValue
CHttpClientMapT<HttpTool>::Get (PCSZ szName, DWORD nIdx)
throw (Exception &)
{
HTTPCLIENT_ASSERT (szName != NULL, "CHttpClientMapT::Get: szName can not be NULL.") ;
MapValue mapValue = { NULL, 0 } ;
std::pair<MapIter, MapIter> pairIter ;
pairIter = m_map.equal_range (szName) ;
// If the key is not found.
if ( pairIter.first == pairIter.second )
return mapValue ;
MapIter iter = pairIter.first ;
for (DWORD i = 0; i < nIdx; i++) {
++iter ;
// If the nIdx is not valid
if ( iter == pairIter.second )
return mapValue ;
}
mapValue = iter->second ;
if ( mapValue.szValue == NULL )
mapValue.szValue = HttpTool::szEmptyString ;
return mapValue ;
}
// Returns NULL if the specified element is not found.
template <typename HttpTool>
typename CHttpClientMapT<HttpTool>::PCSZ
CHttpClientMapT<HttpTool>::GetValue (PCSZ szName, DWORD nIdx)
throw ()
{
return Get (szName, nIdx).szValue ;
}
// Returns 0 if the specified element is not found.
// (If the dwFlag in MapValue is 0, it also returns 0)
template <typename HttpTool>
DWORD CHttpClientMapT<HttpTool>::GetFlag (PCSZ szName, DWORD nIdx)
throw ()
{
return Get (szName, nIdx).dwFlag ;
}
// Adds a new MapValue. It also receives the ownership of memory pointed by szName and szValue.
// The szName and szValue must be allocated by using ::malloc.
// The szValue could be NULL.
template <typename HttpTool>
void CHttpClientMapT<HttpTool>::AddPointerDirectly (PSZ szName, PSZ szValue, BOOL dwFlag)
throw (Exception &)
{
HTTPCLIENT_ASSERT (szName != NULL, "CHttpClientMapT::AddPointerDirectly: szName can not be NULL.") ;
try {
// Checks the arithmetic overflow exception
::SafeInt<MapSizeType> cKeys = Count () ;
::SafeInt<DWORD> cdwKeys = cKeys ;
cKeys++ ;
cdwKeys++ ;
MapValue newValue = { (PCSZ) szValue, dwFlag } ;
m_map.insert (MapItem ((PCSZ) szName, newValue)) ;
} catch (::SafeIntException & e) {
HttpTool::ThrowException (e) ;
} catch (std::exception & e) {
HttpTool::ThrowException (e.what (), HTTPCLIENT_ERR_STD_EXCEPTION) ;
} catch (...) {
HttpTool::ThrowException (HTTPCLIENT_ERR_UNEXPECTED_ERROR) ;
}
}
// The szValue could be NULL.
template <typename HttpTool>
void CHttpClientMapT<HttpTool>::Add (PCSZ szName, PCSZ szValue, BOOL dwFlag)
throw (Exception &)
{
HTTPCLIENT_ASSERT (szName != NULL, "CHttpClientMapT::Add: szName can not be NULL.") ;
PSZ szNewName = NULL ;
PSZ szNewValue = NULL ;
::SafeInt<size_t> cbName, cbValue ;
try {
cbName = HttpTool::StringLen (szName) ;
cbName++ ;
cbName *= sizeof (CharType) ;
szNewName = (PSZ) ::malloc (cbName.Value ()) ;
if ( szNewName == NULL )
HttpTool::ThrowException (HTTPCLIENT_ERR_OUT_OF_MEMORY) ;
HttpTool::StringCopy (szNewName, szName) ;
if ( szValue != NULL ) {
cbValue = HttpTool::StringLen (szValue) ;
cbValue++ ;
cbValue *= sizeof (CharType) ;
szNewValue = (PSZ) ::malloc (cbValue.Value ()) ;
if ( szNewValue == NULL )
HttpTool::ThrowException (HTTPCLIENT_ERR_OUT_OF_MEMORY) ;
HttpTool::StringCopy (szNewValue, szValue) ;
}
AddPointerDirectly (szNewName, szNewValue, dwFlag) ;
} catch (::SafeIntException & e) {
SAFEFREE (szNewName) ;
SAFEFREE (szNewValue) ;
HttpTool::ThrowException (e) ;
} catch (Exception &) {
SAFEFREE (szNewName) ;
SAFEFREE (szNewValue) ;
throw ;
}
}
// If the specified element is not found, it will add a new value.
// otherwise it will modifiy the existing value.
// The szValue could be NULL.
template <typename HttpTool>
void CHttpClientMapT<HttpTool>::Set (PCSZ szName, PCSZ szValue, BOOL dwFlag, DWORD nIdx)
throw (Exception &)
{
HTTPCLIENT_ASSERT (szName != NULL, "CHttpClientMapT::Set: szName can not be NULL.") ;
std::pair<MapIter, MapIter> pairIter ;
pairIter = m_map.equal_range (szName) ;
// Adds a new value if the specified element is not found.
if ( pairIter.first == pairIter.second ) {
// The nIdx must be 0
_ASSERTE ( nIdx == 0 ) ;
Add (szName, szValue, dwFlag) ;
return ;
}
MapIter iter = pairIter.first ;
for (DWORD i = 0; i < nIdx; i++) {
++iter ;
// The nIdx must be valid
_ASSERTE ( iter != pairIter.second ) ;
}
MapValue newValue = { NULL, dwFlag } ;
if ( szValue ) {
::SafeInt<size_t> cbValue ;
try {
cbValue = HttpTool::StringLen (szValue) ;
cbValue++ ;
cbValue *= sizeof (CharType) ;
} catch (::SafeIntException & e) {
HttpTool::ThrowException (e) ;
}
newValue.szValue = (PCSZ) ::malloc (cbValue.Value ()) ;
if ( newValue.szValue == NULL )
HttpTool::ThrowException (HTTPCLIENT_ERR_OUT_OF_MEMORY) ;
HttpTool::StringCopy ((PSZ) newValue.szValue, szValue) ;
}
MapValue oldValue = iter->second ;
iter->second = newValue ;
oldValue.Delete () ;
}
template <typename HttpTool>
typename CHttpClientMapT<HttpTool>::ConstMapIter
CHttpClientMapT<HttpTool>::Begin (void) const
throw ()
{
return m_map.begin () ;
}
template <typename HttpTool>
typename CHttpClientMapT<HttpTool>::ConstMapIter
CHttpClientMapT<HttpTool>::End (void) const
throw ()
{
return m_map.end () ;
}
///////////////////////////////////////// CHttpClientMapT /////////////////////////////////////////
///////////////////////////////////////// CHttpEncoder /////////////////////////////////////////
/*!
* This method has no meaning because the input string is an Ansi string.
* It just returns the length of the input string.
*
* \param szStr [in] A string which is encoded.
* \param CodePage [in] Ignored.
* \return The number of bytes required. (Not including the terminating NULL character)
* \throw Throws a httpclientexception if an error occurs.
*/
DWORD CHttpEncoderA::AnsiEncodeLen (PCSTR szStr, UINT /* CodePage */)
throw (Exception &)
{
if ( szStr == NULL || szStr[0] == '\0' )
return 0 ;
::SafeInt<DWORD> dwLen ;
try {
dwLen = ::strlen (szStr) ;
} catch (::SafeIntException & e) {
CHttpToolA::ThrowException (e) ;
}
return dwLen.Value () ;
}
/*!
* This method has no meaning because the input string is an Ansi string.
* It just returns the copy of the input string.
*
* \param szBuff [out] A buffer to save the encoded string. The buffer can not be NULL.
* \param szStr [in] A string which is encoded.
* \param CodePage [in] Ignored.
* \return An encoded string.
* \throw Throws a httpclientexception if an error occurs.
*/
PSTR CHttpEncoderA::AnsiEncode (PSTR szBuff, PCSTR szStr, UINT /* CodePage */)
throw (Exception &)
{
HTTPCLIENT_ASSERTA (szBuff != NULL, "CHttpEncoderA::AnsiEncode: szBuff can not be NULL.") ;
if ( (szStr == NULL) || (szStr[0] == '\0') ) {
szBuff[0] = '\0' ;
return szBuff ;
}
return ::strcpy (szBuff, szStr) ;
}
/*!
* This method has no meaning because the decoded string is an Ansi string.
* It just returns the length of the input string.
*
* \param szEncoded [in] A string to decode.
* \param CodePage [in] Ignored.
* \return The number of bytes required. (Not including the terminating NULL character)
* \throw Throws a httpclientexception if an error occurs.
*/
DWORD CHttpEncoderA::AnsiDecodeLen (PCSTR szEncoded, UINT /* CodePage */)
throw (Exception &)
{
if ( szEncoded == NULL || szEncoded[0] == '\0' )
return 0 ;
::SafeInt<DWORD> dwLen ;
try {
dwLen = ::strlen (szEncoded) ;
} catch (::SafeIntException & e) {
CHttpToolA::ThrowException (e) ;
}
return dwLen.Value () ;
}
/*!
* This method has no meaning because the decoded string is an Ansi string.
* It just returns the copy of the input string.
*
* \param szBuff [out] A buffer to save the decoded string. The buffer can not be NULL.
* \param
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -