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.

120 lines
2.3 KiB

  1. // RegStringArray.cpp: implementation of the CRegStringBuffer class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "RegStringBuffer.h"
  5. #include <stdlib.h>
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. CRegStringBuffer::CRegStringBuffer(int arraySize, int elementSize)
  10. : m_arraySize(arraySize), m_elementSize(elementSize), m_cellsUsed(0)
  11. {
  12. m_Array = new TCHAR*[arraySize];
  13. if (m_Array == NULL)
  14. {
  15. LOG0(LOG_ERROR, "Couldn't allocate CRegStringBuffer");
  16. }
  17. else
  18. {
  19. for (int i=0; i<arraySize; i++)
  20. {
  21. m_Array[i] = new TCHAR[elementSize];
  22. if (m_Array[i] == NULL)
  23. {
  24. LOG0(LOG_ERROR, "Couldn't allocate CRegStringBuffer");
  25. break;
  26. }
  27. }
  28. }
  29. }
  30. CRegStringBuffer::~CRegStringBuffer()
  31. {
  32. for (int i=0; i<m_arraySize; i++)
  33. {
  34. delete[] m_Array[i];
  35. }
  36. delete[] m_Array;
  37. }
  38. int mMax(int a, int b)
  39. {
  40. if (a > b)
  41. return a;
  42. else
  43. return b;
  44. }
  45. TCHAR** CRegStringBuffer::Access(int NumElements, int ElementSize)
  46. {
  47. if ((NumElements > m_arraySize)
  48. || (ElementSize > m_elementSize))
  49. {
  50. //delete the data structure
  51. for (int i=0; i<m_arraySize; i++)
  52. {
  53. delete[] m_Array[i];
  54. }
  55. delete[] m_Array;
  56. //assign new larger dimensions
  57. m_arraySize = mMax(m_arraySize, NumElements);
  58. m_elementSize = mMax(m_elementSize, ElementSize);
  59. //reallocate the array
  60. /*m_Array = new TCHAR*[m_arraySize];
  61. for (int i=0; i<m_arraySize; i++)
  62. {
  63. m_Array[i] = new TCHAR[m_elementSize];
  64. }*/
  65. m_Array = new TCHAR*[m_arraySize];
  66. if (m_Array == NULL)
  67. {
  68. LOG0(LOG_ERROR, "Couldn't allocate CRegStringBuffer");
  69. }
  70. else
  71. {
  72. for (int i=0; i<m_arraySize; i++)
  73. {
  74. m_Array[i] = new TCHAR[m_elementSize];
  75. if (m_Array[i] == NULL)
  76. {
  77. LOG0(LOG_ERROR, "Couldn't allocate CRegStringBuffer");
  78. break;
  79. }
  80. }
  81. }
  82. }
  83. return m_Array;
  84. }
  85. int __cdecl compare( const void *arg1, const void *arg2 )
  86. {
  87. /* Compare all of both strings: */
  88. return _tcscmp( * ( TCHAR** ) arg1, * ( TCHAR** ) arg2 );
  89. }
  90. void CRegStringBuffer::Sort(int NumElements)
  91. {
  92. qsort( (void *)m_Array, NumElements, sizeof(TCHAR*), compare );
  93. }