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.

352 lines
7.4 KiB

  1. // File: MruList.cpp
  2. #include "precomp.h"
  3. #include "MRUList.h"
  4. #include "ConfUtil.h"
  5. CMRUList::CMRUList() :
  6. m_nValidEntries (0),
  7. m_pszRegKey (NULL),
  8. m_fDirty (FALSE)
  9. {
  10. DebugEntry(CMRUList::CMRUList);
  11. // Clear out information
  12. for (int i = 0; i < MRU_MAX_ENTRIES; i++)
  13. {
  14. m_szNames[i][0] = _T('\0');
  15. }
  16. DebugExitVOID(CMRUList::CMRUList);
  17. }
  18. CMRUList::~CMRUList()
  19. {
  20. DebugEntry(CMRUList::~CMRUList);
  21. if (m_fDirty)
  22. {
  23. Save();
  24. }
  25. delete m_pszRegKey;
  26. DebugExitVOID(CMRUList::~CMRUList);
  27. }
  28. BOOL CMRUList::ShiftEntries(int nSrc, int nDest, int cEntries)
  29. {
  30. DebugEntry(CMRUList::ShiftEntries);
  31. BOOL bRet = TRUE;
  32. ASSERT(m_nValidEntries > 0);
  33. ASSERT(nSrc >= 0 && nSrc < MRU_MAX_ENTRIES);
  34. ASSERT(nDest >= 0 && nDest < MRU_MAX_ENTRIES);
  35. ASSERT(nSrc != nDest);
  36. if ((0 == cEntries) || (cEntries > (MRU_MAX_ENTRIES - nSrc)))
  37. {
  38. TRACE_OUT(( "CMRUList::ShiftEntries: Adjusting cEntries from %d to %d",
  39. cEntries,
  40. (MRU_MAX_ENTRIES - nSrc)));
  41. cEntries = (MRU_MAX_ENTRIES - nSrc);
  42. }
  43. if (nSrc > nDest)
  44. {
  45. // Copy forwards (first to last)
  46. for (int i = 0; i < cEntries; i++)
  47. {
  48. lstrcpy(m_szNames[nDest + i], m_szNames[nSrc + i]);
  49. }
  50. }
  51. else
  52. {
  53. // Copy backwards (last to first)
  54. for (int i = (cEntries - 1); i >= 0; i--)
  55. {
  56. lstrcpy(m_szNames[nDest + i], m_szNames[nSrc + i]);
  57. }
  58. }
  59. DebugExitBOOL(CMRUList::ShiftEntries, bRet);
  60. return bRet;
  61. }
  62. BOOL CMRUList::Load(LPCTSTR pcszRegKey)
  63. {
  64. DebugEntry(CMRUList::Load);
  65. BOOL bRet = TRUE;
  66. ASSERT(pcszRegKey);
  67. delete m_pszRegKey;
  68. m_pszRegKey = PszAlloc(pcszRegKey);
  69. RegEntry reMRU(pcszRegKey, HKEY_CURRENT_USER);
  70. int nCount = 0;
  71. if (ERROR_SUCCESS == reMRU.GetError())
  72. {
  73. // Determine how many entries has been saved in the registry:
  74. nCount = reMRU.GetNumber(REGVAL_MRU_COUNT, 0);
  75. }
  76. ASSERT(nCount <= MRU_MAX_ENTRIES);
  77. for (int i = 0; i < nCount; i++)
  78. {
  79. TCHAR szRegName[MAX_PATH];
  80. LPSTR pStr;
  81. // Retrieve the name from the registry:
  82. wsprintf(szRegName, "%s%d", REGVAL_NAME_MRU_PREFIX, i);
  83. pStr = reMRU.GetString(szRegName);
  84. if( NULL == pStr )
  85. *m_szNames[i] = '\0';
  86. else
  87. lstrcpyn(m_szNames[i], pStr, MRU_MAX_STRING);
  88. }
  89. // Set the valid entries member variable:
  90. m_nValidEntries = nCount;
  91. // Clear the dirty flag since we have just loaded:
  92. m_fDirty = FALSE;
  93. DebugExitBOOL(CMRUList::Load, bRet);
  94. return bRet;
  95. }
  96. BOOL CMRUList::Save()
  97. {
  98. DebugEntry(CMRUList::Save);
  99. BOOL bRet = FALSE;
  100. if (NULL != m_pszRegKey)
  101. {
  102. RegEntry reMRU(m_pszRegKey, HKEY_CURRENT_USER);
  103. if (ERROR_SUCCESS == reMRU.GetError())
  104. {
  105. // Save the number of entries to the registry:
  106. reMRU.SetValue(REGVAL_MRU_COUNT, m_nValidEntries);
  107. for (int i = 0; i < m_nValidEntries; i++)
  108. {
  109. TCHAR szRegName[MAX_PATH];
  110. // Set the name in the registry:
  111. wsprintf(szRegName, "%s%d", REGVAL_NAME_MRU_PREFIX, i);
  112. reMRU.SetValue(szRegName, m_szNames[i]);
  113. }
  114. reMRU.FlushKey();
  115. if (ERROR_SUCCESS == reMRU.GetError())
  116. {
  117. // Clear the dirty flag since we have just saved:
  118. m_fDirty = FALSE;
  119. bRet = TRUE;
  120. }
  121. }
  122. }
  123. else
  124. {
  125. ERROR_OUT(("Can't save MRU info - no reg key stored!"));
  126. }
  127. DebugExitBOOL(CMRUList::Save, bRet);
  128. return bRet;
  129. }
  130. BOOL CMRUList::MoveEntryToTop(int nIndex)
  131. {
  132. DebugEntry(CMRUList::MoveEntryToTop);
  133. BOOL bRet = TRUE;
  134. ASSERT(nIndex >= 0 && nIndex < m_nValidEntries);
  135. if (nIndex < (m_nValidEntries - 1))
  136. {
  137. TCHAR szTempName[MRU_MAX_STRING];
  138. lstrcpy(szTempName, m_szNames[nIndex]);
  139. // Move everything down by 1:
  140. ShiftEntries(nIndex + 1, nIndex, m_nValidEntries - nIndex);
  141. lstrcpy(m_szNames[m_nValidEntries - 1], szTempName);
  142. }
  143. // Set the dirty flag:
  144. m_fDirty = TRUE;
  145. DebugExitBOOL(CMRUList::MoveEntryToTop, bRet);
  146. return bRet;
  147. }
  148. /* A D D N E W E N T R Y */
  149. /*-------------------------------------------------------------------------
  150. %%Function: AddNewEntry
  151. Return TRUE if the entry is NEW.
  152. -------------------------------------------------------------------------*/
  153. BOOL CMRUList::AddNewEntry(LPCTSTR pcszName)
  154. {
  155. DebugEntry(CMRUList::AddNewEntry);
  156. int nExistingEntry = FindEntry(pcszName);
  157. BOOL bRet = (-1 == nExistingEntry); // bRet = TRUE if this is NEW
  158. if (!bRet)
  159. {
  160. // This entry already exists, move it to the top:
  161. MoveEntryToTop(nExistingEntry);
  162. }
  163. else
  164. {
  165. // This entry doesn't exist already, so add it:
  166. if (MRU_MAX_ENTRIES == m_nValidEntries)
  167. {
  168. ShiftEntries(1, 0);
  169. m_nValidEntries--;
  170. }
  171. ASSERT(m_nValidEntries < MRU_MAX_ENTRIES);
  172. // Set the index to be one past the last current entry:
  173. int nCopyIndex = m_nValidEntries;
  174. lstrcpyn(m_szNames[nCopyIndex], pcszName, MRU_MAX_STRING - 1);
  175. // Increment the number of valid entries:
  176. m_nValidEntries++;
  177. // Set the dirty flag:
  178. m_fDirty = TRUE;
  179. }
  180. DebugExitBOOL(CMRUList::AddNewEntry, bRet);
  181. return bRet;
  182. }
  183. /* F I N D E N T R Y */
  184. /*-------------------------------------------------------------------------
  185. %%Function: FindEntry
  186. Performs a case-insensitive search for the string
  187. -------------------------------------------------------------------------*/
  188. int CMRUList::FindEntry(LPCTSTR pcszName)
  189. {
  190. for (int i = 0; i < m_nValidEntries; i++)
  191. {
  192. if (0 == lstrcmpi(m_szNames[i], pcszName))
  193. {
  194. return i;
  195. }
  196. }
  197. return -1; // not found
  198. }
  199. //--------------------------------------------------------------------------//
  200. // CMRUList::DeleteEntry. //
  201. //--------------------------------------------------------------------------//
  202. bool
  203. CMRUList::DeleteEntry
  204. (
  205. const TCHAR * const entry
  206. ){
  207. int entryIndex = FindEntry( entry );
  208. bool deleted = (entryIndex != -1);
  209. if( deleted )
  210. {
  211. ShiftEntries( entryIndex + 1, entryIndex );
  212. m_nValidEntries--;
  213. m_fDirty = TRUE;
  214. }
  215. return( deleted );
  216. } // End of CMRUList::DeleteEntry.
  217. //--------------------------------------------------------------------------//
  218. // CMRUList::ReplaceEntry. //
  219. //--------------------------------------------------------------------------//
  220. bool
  221. CMRUList::ReplaceEntry
  222. (
  223. const TCHAR * const oldEntry,
  224. const TCHAR * const newEntry
  225. ){
  226. int entryIndex = FindEntry( oldEntry );
  227. bool replaced = (entryIndex != -1);
  228. if( replaced )
  229. {
  230. lstrcpyn( m_szNames[ entryIndex ], newEntry, MRU_MAX_STRING - 1 );
  231. m_fDirty = TRUE;
  232. }
  233. return( replaced );
  234. } // End of CMRUList::ReplaceEntry.
  235. //--------------------------------------------------------------------------//
  236. // CMRUList::AppendEntry. //
  237. //--------------------------------------------------------------------------//
  238. bool
  239. CMRUList::AppendEntry
  240. (
  241. const TCHAR * const entry
  242. ){
  243. DebugEntry( CMRUList::AppendEntry );
  244. bool result;
  245. if( (result = (FindEntry( entry ) == -1)) != false )
  246. {
  247. // This entry doesn't already exist so we'll append it on...
  248. if( m_nValidEntries == MRU_MAX_ENTRIES )
  249. {
  250. // The list is full so we'll replace the last one...
  251. lstrcpyn( m_szNames[ 0 ], entry, MRU_MAX_STRING - 1 );
  252. }
  253. else
  254. {
  255. if( m_nValidEntries > 0 )
  256. {
  257. ShiftEntries( 0, 1 );
  258. }
  259. lstrcpyn( m_szNames[ 0 ], entry, MRU_MAX_STRING - 1 );
  260. m_nValidEntries++;
  261. }
  262. // Set the dirty flag...
  263. m_fDirty = TRUE;
  264. }
  265. DebugExitBOOL( CMRUList::AppendEntry, result );
  266. return( result );
  267. } // End of CMRUList::AppendEntry.