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.

160 lines
3.1 KiB

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