Source code of Windows XP (NT5)
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.

142 lines
3.1 KiB

  1. /*++
  2. Copyright (C) 1993-1999 Microsoft Corporation
  3. Module Name:
  4. strtable.cpp
  5. Abstract:
  6. Implementation of a string table handler. The CStringTable
  7. class hides details of storage from the user. The strings might
  8. be cached, or they might be loaded as necessary. In either case,
  9. we must know the number of strings so we know whether or not to
  10. reload strings.
  11. --*/
  12. #include <windows.h>
  13. #include <malloc.h>
  14. #include "polyline.h"
  15. #include "strtable.h"
  16. // Create global instance of table
  17. CStringTable StringTable;
  18. /*
  19. * CStringTable::CStringTable
  20. * CStringTable::~CStringTable
  21. *
  22. * Constructor Parameters:
  23. * hInst HANDLE to the application instance from which we
  24. * load strings.
  25. */
  26. CStringTable::CStringTable(void)
  27. {
  28. m_ppszTable = NULL;
  29. }
  30. CStringTable::~CStringTable(void)
  31. {
  32. INT i;
  33. // Free the loaded strings and table
  34. if (NULL != m_ppszTable)
  35. {
  36. for (i=0; i<m_cStrings; i++)
  37. {
  38. if (m_ppszTable[i] != NULL)
  39. free(m_ppszTable[i]);
  40. }
  41. free(m_ppszTable);
  42. }
  43. }
  44. /*
  45. * CStringTable::Init
  46. *
  47. * Purpose:
  48. * Initialization function for a StringTable that is prone to
  49. * failure. If this fails then the caller is responsible for
  50. * guaranteeing that the destructor is called quickly.
  51. *
  52. * Parameters:
  53. * idsMin UINT first identifier in the stringtable
  54. * idsMax UINT last identifier in the stringtable.
  55. *
  56. * Return Value:
  57. * BOOL TRUE if the function is successful, FALSE
  58. * otherwise.
  59. */
  60. BOOL CStringTable::Init(UINT idsMin, UINT idsMax)
  61. {
  62. UINT i;
  63. m_idsMin = idsMin;
  64. m_idsMax = idsMax;
  65. m_cStrings = (idsMax - idsMin + 1);
  66. //Allocate space for the pointer table.
  67. m_ppszTable = (LPTSTR *)malloc(sizeof(LPTSTR) * m_cStrings);
  68. if (NULL==m_ppszTable)
  69. return FALSE;
  70. // Clear all table entries
  71. for (i=0; i<m_cStrings; i++)
  72. m_ppszTable[i] = NULL;
  73. return TRUE;
  74. }
  75. /*
  76. * CStringTable::operator[]
  77. *
  78. * Purpose:
  79. * Returns a pointer to the requested string in the stringtable or
  80. * NULL if the specified string does not exist.
  81. */
  82. LPTSTR CStringTable::operator[] (const UINT uID)
  83. {
  84. TCHAR szBuf[CCHSTRINGMAX];
  85. LPTSTR psz;
  86. INT iLen;
  87. static TCHAR szMissing[] = TEXT("????");
  88. // if string not in range, return NULL
  89. if (uID < m_idsMin || uID > m_idsMax)
  90. return szMissing;
  91. // if already loaded, return it
  92. if (m_ppszTable[uID - m_idsMin] != NULL)
  93. return m_ppszTable[uID - m_idsMin];
  94. BEGIN_CRITICAL_SECTION
  95. // if selected string not loaded, load it now
  96. if (m_ppszTable[uID - m_idsMin] == NULL)
  97. {
  98. iLen = LoadString(g_hInstance, uID, szBuf, CCHSTRINGMAX - 1);
  99. if (iLen == 0)
  100. lstrcpy(szBuf, szMissing);
  101. psz = (LPTSTR)malloc((iLen + 1) * sizeof(TCHAR));
  102. if (psz != NULL)
  103. {
  104. lstrcpy(psz, szBuf);
  105. m_ppszTable[uID - m_idsMin] = psz;
  106. }
  107. }
  108. END_CRITICAL_SECTION
  109. // Now return selected pointer
  110. return m_ppszTable[uID - m_idsMin];
  111. }