Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.5 KiB

  1. //
  2. // StrArray.cpp
  3. //
  4. // A very simple string array implementation, intended to be small
  5. // rather than scalable or particularly fast.
  6. //
  7. // History:
  8. //
  9. // 10/05/1999 KenSh Created
  10. //
  11. #include "stdafx.h"
  12. #include "StrArray.h"
  13. #include "Util.h"
  14. CStringArray::CStringArray()
  15. {
  16. m_prgpStrings = NULL;
  17. m_prgItemData = NULL;
  18. m_cStrings = 0;
  19. }
  20. CStringArray::~CStringArray()
  21. {
  22. RemoveAll();
  23. }
  24. void CStringArray::RemoveAll()
  25. {
  26. for (int i = 0; i < m_cStrings; i++)
  27. free(m_prgpStrings[i]);
  28. free(m_prgpStrings);
  29. free(m_prgItemData);
  30. m_prgpStrings = NULL;
  31. m_prgItemData = NULL;
  32. m_cStrings = 0;
  33. }
  34. int CStringArray::Add(LPCTSTR pszNewElement)
  35. {
  36. LPTSTR* ppsz = (LPTSTR*)realloc(m_prgpStrings, (1+m_cStrings) * sizeof(LPTSTR));
  37. DWORD* pdw = (DWORD*)realloc(m_prgItemData, (1+m_cStrings) * sizeof(DWORD));
  38. // update whatever was successfully reallocated
  39. if (ppsz)
  40. m_prgpStrings = ppsz;
  41. if (pdw)
  42. m_prgItemData = pdw;
  43. // if both allocated, we have room.
  44. if (ppsz && pdw)
  45. {
  46. int nIndex = m_cStrings++;
  47. m_prgpStrings[nIndex] = lstrdup(pszNewElement);
  48. m_prgItemData[nIndex] = 0;
  49. return nIndex;
  50. }
  51. return -1;
  52. }
  53. void CStringArray::RemoveAt(int nIndex)
  54. {
  55. ASSERT(nIndex >= 0 && nIndex < m_cStrings);
  56. free(m_prgpStrings[nIndex]);
  57. m_cStrings--;
  58. for ( ; nIndex < m_cStrings; nIndex++)
  59. {
  60. m_prgpStrings[nIndex] = m_prgpStrings[nIndex+1];
  61. m_prgItemData[nIndex] = m_prgItemData[nIndex+1];
  62. }
  63. }