home *** CD-ROM | disk | FTP | other *** search
- // utils.cpp
- //
- // Created 10/02/98
- //
- // (C)Copyright 1998-1999 Microsoft Corporation, All rights reserved.
- //
-
- #include "pch.hpp"
- #pragma hdrstop
-
- #include "utils.hpp"
-
-
- CMapPtrToPtr::CMapPtrToPtr()
- {
- ZeroMemory(m_Table, sizeof(m_Table));
- }
-
- CMapPtrToPtr::~CMapPtrToPtr()
- {
- DWORD dwBucketIndex;
- MapPtrToPtrNode *pCurrent;
- MapPtrToPtrNode *pNext;
-
- for (dwBucketIndex = 0; dwBucketIndex < MAP_PTR_TO_PTR_SLOTS;
- dwBucketIndex++) {
-
- pCurrent = m_Table[dwBucketIndex];
-
- while (pCurrent != NULL) {
- pNext = pCurrent->m_pNext;
- delete(pCurrent);
- pCurrent = pNext;
- }
- }
- }
-
- HRESULT CMapPtrToPtr::Add(PCVOID pvKey, PVOID pvValue)
- {
- HRESULT hr;
- MapPtrToPtrNode *pNew;
- DWORD dwBucketIndex;
-
- pNew = new(MapPtrToPtrNode);
-
- if (pNew != NULL) {
-
- dwBucketIndex = GetBucketIndex(pvKey);
-
- pNew->m_pvKey = pvKey;
- pNew->m_pvValue = pvValue;
-
- pNew->m_pNext = m_Table[dwBucketIndex];
- m_Table[dwBucketIndex] = pNew;
-
- hr = S_OK;
-
- } else {
- hr = E_OUTOFMEMORY;
- }
-
- return hr;
- }
-
- HRESULT CMapPtrToPtr::Lookup(PCVOID pvKey, PVOID *ppvValue)
- {
- HRESULT hr;
- MapPtrToPtrNode *pCurrent;
-
- hr = E_UNEXPECTED;
- *ppvValue = NULL;
-
- pCurrent = m_Table[GetBucketIndex(pvKey)];
-
- while (pCurrent != NULL) {
-
- if (pCurrent->m_pvKey == pvKey) {
-
- *ppvValue = pCurrent->m_pvValue;
-
- hr = S_OK;
- break;
- }
-
- pCurrent = pCurrent->m_pNext;
- }
-
- return hr;
- }
-
- HRESULT CMapPtrToPtr::Delete(PCVOID pvKey)
- {
- HRESULT hr;
- MapPtrToPtrNode *pCurrent;
- MapPtrToPtrNode *pPrevious;
- DWORD dwBucketIndex;
-
- hr = E_UNEXPECTED;
-
- pPrevious = NULL;
- dwBucketIndex = GetBucketIndex(pvKey);
- pCurrent = m_Table[dwBucketIndex];
-
- while (pCurrent != NULL) {
-
- if (pCurrent->m_pvKey == pvKey) {
-
- if (pPrevious != NULL) {
- pPrevious->m_pNext = pCurrent->m_pNext;
- } else {
- m_Table[dwBucketIndex] = pCurrent->m_pNext;
- }
-
- delete(pCurrent);
-
- hr = S_OK;
- break;
- }
-
- pPrevious = pCurrent;
- pCurrent = pCurrent->m_pNext;
- }
-
- return hr;
- }
-
- VOID CMapPtrToPtr::Filter (PPTRMAPENUMFN filterfn, PVOID token)
- {
- DWORD dwBucketIndex;
- MapPtrToPtrNode *pCurrent;
- MapPtrToPtrNode *pNext;
- MapPtrToPtrNode **ppPrev;
-
- for (dwBucketIndex = 0; dwBucketIndex < MAP_PTR_TO_PTR_SLOTS;
- dwBucketIndex++) {
-
- ppPrev = &m_Table[dwBucketIndex];
- pCurrent = *ppPrev;
-
- while (pCurrent != NULL) {
- pNext = pCurrent->m_pNext;
- if (!(*filterfn)(pCurrent->m_pvKey, pCurrent->m_pvValue, token))
- {
- *ppPrev = pNext;
- delete(pCurrent);
- }
- else
- {
- ppPrev = &pCurrent->m_pNext;
- }
- pCurrent = pNext;
- }
- }
- }
-
- int CMapPtrToPtr::Iterate (PPTRMAPENUMFN filterfn, PVOID token)
- {
- int result = 1;
-
- DWORD dwBucketIndex;
- MapPtrToPtrNode *pCurrent;
-
- for (dwBucketIndex = 0; dwBucketIndex < MAP_PTR_TO_PTR_SLOTS; dwBucketIndex++)
- {
- pCurrent = m_Table[dwBucketIndex];
-
- while (pCurrent != NULL)
- {
- result = (*filterfn)(pCurrent->m_pvKey, pCurrent->m_pvValue, token);
- if (result <= 0)
- return result;
-
- pCurrent = pCurrent->m_pNext;
- }
- }
-
- return result;
- }
-
-
- HRESULT UnicodeToANSI(LPCOLESTR pcwszUnicode, int ncUnicodeLen, PSTR *ppszANSI)
- {
- HRESULT hr = E_UNEXPECTED;
-
- *ppszANSI = NULL;
-
- int ncLen = WideCharToMultiByte(CP_ACP, 0, pcwszUnicode, ncUnicodeLen, NULL, 0, NULL, NULL);
- if (ncLen >= 0)
- {
- // Add room for a null terminator if necessary.
- if ((ncUnicodeLen == 0) || (ncUnicodeLen > 0 && pcwszUnicode[ncUnicodeLen - 1] != L'\0'))
- {
- ncLen++;
- }
-
- *ppszANSI = new(char[ncLen]);
- if (*ppszANSI)
- {
- if (WideCharToMultiByte(CP_ACP, 0, pcwszUnicode, ncUnicodeLen, *ppszANSI, ncLen, NULL, NULL) >= 0)
- {
- (*ppszANSI)[ncLen - 1] = '\0';
- hr = S_OK;
- }
- else
- {
- delete (*ppszANSI);
- *ppszANSI = NULL;
- hr = E_UNEXPECTED;
- }
- }
- else
- {
- hr = E_OUTOFMEMORY;
- }
- }
- else
- {
- hr = E_UNEXPECTED;
- }
-
- return hr;
- }
-
-
- BOOL WalkTree (HWND htree, HTREEITEM root, DWORD flags, PWALKTREECBFN pfn, PVOID token)
- {
- BOOL result = FALSE;
-
- if (root == TVI_ROOT)
- root = NULL;
-
- HTREEITEM node;
- if (root == NULL)
- node = TreeView_GetRoot(htree);
- else
- node = root;
-
- while (node)
- {
- HTREEITEM child = TreeView_GetChild(htree, node);
-
- if (child == NULL || !(flags & WT_LEAF_ONLY))
- {
- WalkTreeCBResults disp = (*pfn)(htree, node, token);
- switch (disp)
- {
- case WTCB_STOP:
- return result;
-
- case WTCB_DELETE:
- case WTCB_MODIFIED:
- {
- HTREEITEM next;
- // BUGBUG
- //next = TreeView_GetNextSibling(htree, node);
- //if (next == NULL)
- next = TreeView_GetParent(htree, node);
- if (disp == WTCB_DELETE)
- TreeView_DeleteItem(htree, node);
- if (node == root || next == root)
- return TRUE;
- child = NULL;
- node = next;
- continue;
- }
- // fall through
- default:
- result = TRUE;
- break;
- }
- }
-
- if (child != NULL)
- {
- node = child;
- continue;
- }
-
- for (;;)
- {
- HTREEITEM sibling = TreeView_GetNextSibling(htree, node);
- if (sibling != NULL)
- {
- node = sibling;
- break;
- }
- node = TreeView_GetParent(htree, node);
- if (node == root)
- break;
- }
- }
-
- return result;
- }
-
-
- int FitText (PSTR buf, int bufsize, PCSTR src)
- {
- int len = min((unsigned)(bufsize-1), strlen(src));
- CopyMemory(buf, src, len);
- buf[len] = '\0';
- return len;
- }
-
-
- int FormatNumber (PSTR buf, int bufsize, PCSTR fmt, ...)
- {
- CHAR src[100];
- va_list va;
- va_start(va, fmt);
- vsprintf(src, fmt, va);
- va_end(va);
- return FitText(buf, bufsize, src);
- }
-
-
- VOID DeletePtrArray (PVOID *rgpv, int len)
- {
- if (rgpv != NULL)
- {
- for (int i = 0; i < len; i++)
- {
- PVOID pv = rgpv[i];
- if (pv != NULL)
- delete(pv);
- }
- }
- }
-
-