1 // $Id: mru.cpp,v 1.1 2002/05/29 22:06:50 nedko Exp $
4 // Copyright (C) 2000,2001,2002 Nedko Arnaudov <nedko@users.sourceforge.net>
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 CMRUList::CMRUList(unsigned int nMaxEntries
, LPCTSTR psz_HKCU_MRUKey
, HRESULT
& rhr
)
30 m_nMaxEntries
= nMaxEntries
;
32 if (nMaxEntries
== 0 || psz_HKCU_MRUKey
== NULL
)
38 m_ppszList
= new LPTSTR
[nMaxEntries
];
45 ZeroMemory(m_ppszList
,nMaxEntries
*sizeof(LPTSTR
));
49 // open/create the key
50 nError
= RegCreateKeyEx(HKEY_CURRENT_USER
, psz_HKCU_MRUKey
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_QUERY_VALUE
|KEY_SET_VALUE
,NULL
,&m_hKey
,NULL
);
51 if (nError
!= ERROR_SUCCESS
)
53 rhr
= HRESULT_FROM_WIN32(nError
);
57 TCHAR NameBuffer
[256];
62 while(m_nCount
< m_nMaxEntries
)
64 _ultot(m_nCount
,NameBuffer
,ENTRY_BASE
);
65 nError
= RegQueryValueEx(m_hKey
,NameBuffer
,NULL
,&dwType
,NULL
,&dwEntrySize
);
66 if (nError
!= ERROR_SUCCESS
|| dwType
!= REG_SZ
)
69 m_ppszList
[m_nCount
] = (TCHAR
*) new BYTE
[dwEntrySize
];
76 nError
= RegQueryValueEx(m_hKey
,NameBuffer
,NULL
,&dwType
,(BYTE
*)m_ppszList
[m_nCount
],&dwEntrySize
);
77 if (nError
!= ERROR_SUCCESS
)
79 delete m_ppszList
[m_nCount
];
80 m_ppszList
[m_nCount
] = NULL
;
92 LPTSTR
*ppsz
= m_ppszList
;
101 VERIFY(RegCloseKey(m_hKey
) == ERROR_SUCCESS
);
106 HRESULT
CMRUList::Add(LPCTSTR pszEntry
)
108 if (m_hKey
== NULL
|| m_ppszList
== NULL
|| m_nMaxEntries
== 0 || m_nCount
> m_nMaxEntries
)
110 ASSERT(FALSE
); // Add method must be called only after successful construction
118 // existing string ???
119 for (nIndex
= 0 ; nIndex
< m_nCount
; nIndex
++)
120 if (strcmp(m_ppszList
[nIndex
],pszEntry
) == 0)
123 { // move the found entry to the top
124 pszMoveTemp
= m_ppszList
[nIndex
]; // save the found entry
127 m_ppszList
[nIndex
] = m_ppszList
[nIndex
-1];
131 m_ppszList
[0] = pszMoveTemp
;
133 hr
= WriteToRegistry();
135 return SUCCEEDED(hr
)?S_FALSE
:hr
;
143 LPTSTR pszNewEntry
= new TCHAR
[_tcslen(pszEntry
)+1];
145 return E_OUTOFMEMORY
;
147 _tcscpy(pszNewEntry
,pszEntry
);
150 LPTSTR psz
= pszNewEntry
;
153 while(nIndex
< m_nCount
)
155 pszMoveTemp
= m_ppszList
[nIndex
];
156 ASSERT(pszMoveTemp
); // what we are moving ???
157 m_ppszList
[nIndex
] = psz
;
162 // save last entry if we have not reached max entries count
163 if (nIndex
< m_nMaxEntries
)
165 m_ppszList
[nIndex
] = psz
;
170 hr
= WriteToRegistry();
172 return SUCCEEDED(hr
)?S_OK
:hr
;
175 HRESULT
CMRUList::WriteToRegistry()
177 TCHAR NameBuffer
[256];
180 LONG nLastError
= ERROR_SUCCESS
;
182 // Write the new MRU list into registry
183 for (nIndex
= 0 ; nIndex
< m_nCount
; nIndex
++)
185 _ultot(nIndex
,NameBuffer
,ENTRY_BASE
);
186 LONG nError
= RegSetValueEx(m_hKey
,NameBuffer
,0,REG_SZ
,(const BYTE
*)m_ppszList
[nIndex
],(_tcslen(m_ppszList
[nIndex
])+1)*sizeof(TCHAR
));
187 if (nError
!= ERROR_SUCCESS
)
191 return HRESULT_FROM_WIN32(nLastError
);
194 unsigned int CMRUList::GetCount()
199 LPCTSTR
CMRUList::GetEntry(unsigned int nIndex
)
201 if ((nIndex
>= m_nCount
)||(m_ppszList
== NULL
))
207 return m_ppszList
[nIndex
];