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

  1. // utils.cpp
  2. //
  3. // Created 10/02/98
  4. //
  5. // (C)Copyright 1998-1999 Microsoft Corporation, All rights reserved.
  6. //
  7.  
  8. #include "pch.hpp"
  9. #pragma hdrstop
  10.  
  11. #include "utils.hpp"
  12.  
  13.  
  14. CMapPtrToPtr::CMapPtrToPtr()
  15. {
  16.     ZeroMemory(m_Table, sizeof(m_Table));
  17. }
  18.  
  19. CMapPtrToPtr::~CMapPtrToPtr()
  20. {
  21.     DWORD dwBucketIndex;
  22.     MapPtrToPtrNode *pCurrent;
  23.     MapPtrToPtrNode *pNext;
  24.  
  25.     for (dwBucketIndex = 0; dwBucketIndex < MAP_PTR_TO_PTR_SLOTS;
  26.         dwBucketIndex++) {
  27.  
  28.         pCurrent = m_Table[dwBucketIndex];
  29.  
  30.         while (pCurrent != NULL) {
  31.             pNext = pCurrent->m_pNext;
  32.             delete(pCurrent);
  33.             pCurrent = pNext;
  34.         }
  35.     }
  36. }
  37.  
  38. HRESULT CMapPtrToPtr::Add(PCVOID pvKey, PVOID pvValue)
  39. {
  40.     HRESULT hr;
  41.     MapPtrToPtrNode *pNew;
  42.     DWORD dwBucketIndex;
  43.  
  44.     pNew = new(MapPtrToPtrNode);
  45.  
  46.     if (pNew != NULL) {
  47.  
  48.         dwBucketIndex = GetBucketIndex(pvKey);
  49.  
  50.         pNew->m_pvKey = pvKey;
  51.         pNew->m_pvValue = pvValue;
  52.  
  53.         pNew->m_pNext = m_Table[dwBucketIndex];
  54.         m_Table[dwBucketIndex] = pNew;
  55.  
  56.         hr = S_OK;
  57.  
  58.     } else {
  59.         hr = E_OUTOFMEMORY;
  60.     }
  61.  
  62.     return hr;
  63. }
  64.  
  65. HRESULT CMapPtrToPtr::Lookup(PCVOID pvKey, PVOID *ppvValue)
  66. {
  67.     HRESULT hr;
  68.     MapPtrToPtrNode *pCurrent;
  69.  
  70.     hr = E_UNEXPECTED;
  71.     *ppvValue = NULL;
  72.  
  73.     pCurrent = m_Table[GetBucketIndex(pvKey)];
  74.  
  75.     while (pCurrent != NULL) {
  76.  
  77.         if (pCurrent->m_pvKey == pvKey) {
  78.  
  79.             *ppvValue = pCurrent->m_pvValue;
  80.  
  81.             hr = S_OK;
  82.             break;
  83.         }
  84.  
  85.         pCurrent = pCurrent->m_pNext;
  86.     }
  87.  
  88.     return hr;
  89. }
  90.  
  91. HRESULT CMapPtrToPtr::Delete(PCVOID pvKey)
  92. {
  93.     HRESULT hr;
  94.     MapPtrToPtrNode *pCurrent;
  95.     MapPtrToPtrNode *pPrevious;
  96.     DWORD dwBucketIndex;
  97.  
  98.     hr = E_UNEXPECTED;
  99.  
  100.     pPrevious = NULL;
  101.     dwBucketIndex = GetBucketIndex(pvKey);
  102.     pCurrent = m_Table[dwBucketIndex];
  103.  
  104.     while (pCurrent != NULL) {
  105.  
  106.         if (pCurrent->m_pvKey == pvKey) {
  107.  
  108.             if (pPrevious != NULL) {
  109.                 pPrevious->m_pNext = pCurrent->m_pNext;
  110.             } else {
  111.                 m_Table[dwBucketIndex] = pCurrent->m_pNext;
  112.             }
  113.  
  114.             delete(pCurrent);
  115.  
  116.             hr = S_OK;
  117.             break;
  118.         }
  119.  
  120.         pPrevious = pCurrent;
  121.         pCurrent = pCurrent->m_pNext;
  122.     }
  123.  
  124.     return hr;
  125. }
  126.  
  127. VOID CMapPtrToPtr::Filter (PPTRMAPENUMFN filterfn, PVOID token)
  128. {
  129.     DWORD dwBucketIndex;
  130.     MapPtrToPtrNode *pCurrent;
  131.     MapPtrToPtrNode *pNext;
  132.     MapPtrToPtrNode **ppPrev;
  133.  
  134.     for (dwBucketIndex = 0; dwBucketIndex < MAP_PTR_TO_PTR_SLOTS;
  135.         dwBucketIndex++) {
  136.  
  137.         ppPrev = &m_Table[dwBucketIndex];
  138.         pCurrent = *ppPrev;
  139.  
  140.         while (pCurrent != NULL) {
  141.             pNext = pCurrent->m_pNext;
  142.             if (!(*filterfn)(pCurrent->m_pvKey, pCurrent->m_pvValue, token))
  143.             {
  144.                 *ppPrev = pNext;
  145.                 delete(pCurrent);
  146.             }
  147.             else
  148.             {
  149.                 ppPrev = &pCurrent->m_pNext;
  150.             }
  151.             pCurrent = pNext;
  152.         }
  153.     }
  154. }
  155.  
  156. int CMapPtrToPtr::Iterate (PPTRMAPENUMFN filterfn, PVOID token)
  157. {
  158.     int result = 1;
  159.  
  160.     DWORD dwBucketIndex;
  161.     MapPtrToPtrNode *pCurrent;
  162.  
  163.     for (dwBucketIndex = 0; dwBucketIndex < MAP_PTR_TO_PTR_SLOTS; dwBucketIndex++)
  164.     {
  165.         pCurrent = m_Table[dwBucketIndex];
  166.  
  167.         while (pCurrent != NULL)
  168.         {
  169.             result = (*filterfn)(pCurrent->m_pvKey, pCurrent->m_pvValue, token);
  170.             if (result <= 0)
  171.                 return result;
  172.  
  173.             pCurrent = pCurrent->m_pNext;
  174.         }
  175.     }
  176.  
  177.     return result;
  178. }
  179.  
  180.  
  181. HRESULT UnicodeToANSI(LPCOLESTR pcwszUnicode, int ncUnicodeLen, PSTR *ppszANSI)
  182. {
  183.    HRESULT hr = E_UNEXPECTED;
  184.  
  185.    *ppszANSI = NULL;
  186.  
  187.    int ncLen = WideCharToMultiByte(CP_ACP, 0, pcwszUnicode, ncUnicodeLen, NULL, 0, NULL, NULL);
  188.    if (ncLen >= 0)
  189.    {
  190.       // Add room for a null terminator if necessary.
  191.       if ((ncUnicodeLen == 0) || (ncUnicodeLen > 0 && pcwszUnicode[ncUnicodeLen - 1] != L'\0'))
  192.       {
  193.          ncLen++;
  194.       }
  195.  
  196.       *ppszANSI = new(char[ncLen]);
  197.       if (*ppszANSI)
  198.       {
  199.          if (WideCharToMultiByte(CP_ACP, 0, pcwszUnicode, ncUnicodeLen, *ppszANSI, ncLen, NULL, NULL) >= 0)
  200.          {
  201.             (*ppszANSI)[ncLen - 1] = '\0';
  202.             hr = S_OK;
  203.          }
  204.          else
  205.          {
  206.             delete (*ppszANSI);
  207.             *ppszANSI = NULL;
  208.             hr = E_UNEXPECTED;
  209.          }
  210.       }
  211.       else
  212.       {
  213.          hr = E_OUTOFMEMORY;
  214.       }
  215.    }
  216.    else
  217.    {
  218.       hr = E_UNEXPECTED;
  219.    }
  220.  
  221.    return hr;
  222. }
  223.  
  224.  
  225. BOOL WalkTree (HWND htree, HTREEITEM root, DWORD flags, PWALKTREECBFN pfn, PVOID token)
  226. {
  227.     BOOL result = FALSE;
  228.  
  229.     if (root == TVI_ROOT)
  230.         root = NULL;
  231.  
  232.     HTREEITEM node;
  233.     if (root == NULL)
  234.         node = TreeView_GetRoot(htree);
  235.     else
  236.         node = root;
  237.         
  238.     while (node)
  239.     {
  240.         HTREEITEM child = TreeView_GetChild(htree, node);
  241.  
  242.         if (child == NULL || !(flags & WT_LEAF_ONLY))
  243.         {
  244.             WalkTreeCBResults disp = (*pfn)(htree, node, token);
  245.             switch (disp)
  246.             {
  247.             case WTCB_STOP:
  248.                 return result;
  249.  
  250.             case WTCB_DELETE:
  251.             case WTCB_MODIFIED:
  252.                 {
  253.                     HTREEITEM next;
  254.                     // BUGBUG
  255.                     //next = TreeView_GetNextSibling(htree, node);
  256.                     //if (next == NULL)
  257.                         next = TreeView_GetParent(htree, node);
  258.                     if (disp == WTCB_DELETE)
  259.                         TreeView_DeleteItem(htree, node);
  260.                     if (node == root || next == root)
  261.                         return TRUE;
  262.                     child = NULL;
  263.                     node = next;
  264.                     continue;
  265.                 }
  266.                 // fall through
  267.             default:
  268.                 result = TRUE;
  269.                 break;
  270.             }
  271.         }
  272.     
  273.         if (child != NULL)
  274.         {
  275.             node = child;
  276.             continue;
  277.         }
  278.  
  279.         for (;;)
  280.         {
  281.             HTREEITEM sibling = TreeView_GetNextSibling(htree, node);
  282.             if (sibling != NULL)
  283.             {
  284.                 node = sibling;
  285.                 break;
  286.             }
  287.             node = TreeView_GetParent(htree, node);
  288.             if (node == root)
  289.                 break;
  290.         }
  291.     }
  292.  
  293.     return result;
  294. }
  295.  
  296.  
  297. int FitText (PSTR buf, int bufsize, PCSTR src)
  298. {
  299.     int len = min((unsigned)(bufsize-1), strlen(src));
  300.     CopyMemory(buf, src, len);
  301.     buf[len] = '\0';
  302.     return len;
  303. }
  304.  
  305.  
  306. int FormatNumber (PSTR buf, int bufsize, PCSTR fmt, ...)
  307. {
  308.     CHAR src[100];
  309.     va_list va;
  310.     va_start(va, fmt);
  311.     vsprintf(src, fmt, va);
  312.     va_end(va);
  313.     return FitText(buf, bufsize, src);
  314. }
  315.  
  316.  
  317. VOID DeletePtrArray (PVOID *rgpv, int len)
  318. {
  319.     if (rgpv != NULL)
  320.     {
  321.         for (int i = 0; i < len; i++)
  322.         {
  323.             PVOID pv = rgpv[i];
  324.             if (pv != NULL)
  325.                 delete(pv);
  326.         }
  327.     }
  328. }
  329.  
  330.