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.

174 lines
3.4 KiB

  1. #include "pmcfg.h"
  2. #include "mru.h"
  3. PpMRU::PpMRU(int nSize) : m_nSize(nSize), m_ppszList(NULL)
  4. {
  5. }
  6. BOOL
  7. PpMRU::init()
  8. {
  9. m_ppszList = new LPTSTR[m_nSize];
  10. if (m_ppszList == NULL)
  11. {
  12. return FALSE;
  13. }
  14. ZeroMemory(m_ppszList, m_nSize * sizeof(LPTSTR));
  15. return TRUE;
  16. }
  17. PpMRU::~PpMRU()
  18. {
  19. int nIndex;
  20. if(m_ppszList)
  21. {
  22. for(nIndex = 0; nIndex < m_nSize; nIndex++)
  23. {
  24. if(m_ppszList[nIndex])
  25. delete [] m_ppszList[nIndex];
  26. }
  27. delete [] m_ppszList;
  28. }
  29. }
  30. BOOL
  31. PpMRU::insert
  32. (
  33. LPCTSTR sz
  34. )
  35. {
  36. int nIndex;
  37. LPTSTR szNew;
  38. //
  39. // If the string is already in the list, just
  40. // reshuffle so that it is at the top. Even
  41. // simpler, if the string is already the first
  42. // item in the list then do nothing!
  43. //
  44. if(m_ppszList[0] && lstrcmp(sz, m_ppszList[0]) == 0)
  45. return TRUE;
  46. for(nIndex = 1; nIndex < m_nSize && m_ppszList[nIndex]; nIndex++)
  47. {
  48. if(lstrcmp(sz, m_ppszList[nIndex]) == 0)
  49. {
  50. LPTSTR szTemp = m_ppszList[nIndex];
  51. for(int nIndex2 = nIndex; nIndex2 > 0; nIndex2--)
  52. m_ppszList[nIndex2] = m_ppszList[nIndex2 - 1];
  53. m_ppszList[0] = szTemp;
  54. return TRUE;
  55. }
  56. }
  57. //
  58. // New item in list. Allocate memory, copy and
  59. // shove list down.
  60. //
  61. szNew = new TCHAR[lstrlen(sz) + 1];
  62. if(!szNew)
  63. return FALSE;
  64. lstrcpy(szNew, sz);
  65. if(m_ppszList[m_nSize - 1])
  66. delete [] m_ppszList[m_nSize - 1];
  67. for(nIndex = m_nSize - 1; nIndex > 0; nIndex--)
  68. {
  69. m_ppszList[nIndex] = m_ppszList[nIndex - 1];
  70. }
  71. m_ppszList[0] = szNew;
  72. return TRUE;
  73. }
  74. LPCTSTR
  75. PpMRU::operator[]
  76. (
  77. int nIndex
  78. )
  79. {
  80. return m_ppszList[nIndex];
  81. }
  82. BOOL
  83. PpMRU::load
  84. (
  85. LPCTSTR szSection,
  86. LPCTSTR szFilename
  87. )
  88. {
  89. int nIndex;
  90. TCHAR achNumBuf[20];
  91. TCHAR achBuf[MAX_PATH];
  92. for(nIndex = 0; nIndex < m_nSize; nIndex++)
  93. {
  94. _itot(nIndex + 1, achNumBuf, 10);
  95. GetPrivateProfileString(szSection,
  96. achNumBuf,
  97. TEXT(""),
  98. achBuf,
  99. MAX_PATH,
  100. szFilename);
  101. if(lstrlen(achBuf))
  102. {
  103. m_ppszList[nIndex] = new TCHAR[lstrlen(achBuf) + 1];
  104. if(m_ppszList[nIndex])
  105. lstrcpy(m_ppszList[nIndex], achBuf);
  106. }
  107. else
  108. {
  109. m_ppszList[nIndex] = NULL;
  110. }
  111. }
  112. return TRUE;
  113. }
  114. BOOL
  115. PpMRU::save
  116. (
  117. LPCTSTR szSection,
  118. LPCTSTR szFilename
  119. )
  120. {
  121. int nIndex;
  122. TCHAR achNumBuf[20];
  123. // Make sure any previously existing section is erased.
  124. WritePrivateProfileString(szSection, NULL, NULL, szFilename);
  125. // Now save all the entries.
  126. for(nIndex = 0; nIndex < m_nSize && m_ppszList[nIndex]; nIndex++)
  127. {
  128. _itot(nIndex + 1, achNumBuf, 10);
  129. WritePrivateProfileString(szSection,
  130. achNumBuf,
  131. m_ppszList[nIndex],
  132. szFilename
  133. );
  134. }
  135. return TRUE;
  136. }