1 // TortoiseSVN - a Windows shell extension for easy version control
\r
3 // Copyright (C) 2007 - TortoiseSVN
\r
5 // This program is free software; you can redistribute it and/or
\r
6 // modify it under the terms of the GNU General Public License
\r
7 // as published by the Free Software Foundation; either version 2
\r
8 // of the License, or (at your option) any later version.
\r
10 // This program is distributed in the hope that it will be useful,
\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 // GNU General Public License for more details.
\r
15 // You should have received a copy of the GNU General Public License
\r
16 // along with this program; if not, write to the Free Software Foundation,
\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
21 #include "Registry.h"
\r
22 #include "RegHistory.h"
\r
25 CRegHistory::CRegHistory() : m_nMaxHistoryItems(25)
\r
29 CRegHistory::~CRegHistory()
\r
33 bool CRegHistory::AddEntry(LPCTSTR szText)
\r
35 if (_tcslen(szText) == 0)
\r
38 if ((!m_sSection.empty())&&(!m_sKeyPrefix.empty()))
\r
40 // refresh the history from the registry
\r
41 Load(m_sSection.c_str(), m_sKeyPrefix.c_str());
\r
44 for (size_t i=0; i<m_arEntries.size(); ++i)
\r
46 if (_tcscmp(szText, m_arEntries[i].c_str())==0)
\r
48 m_arEntries.erase(m_arEntries.begin() + i);
\r
49 m_arEntries.insert(m_arEntries.begin(), szText);
\r
53 m_arEntries.insert(m_arEntries.begin(), szText);
\r
57 void CRegHistory::RemoveEntry(int pos)
\r
59 m_arEntries.erase(m_arEntries.begin() + pos);
\r
62 size_t CRegHistory::Load(LPCTSTR lpszSection, LPCTSTR lpszKeyPrefix)
\r
64 if (lpszSection == NULL || lpszKeyPrefix == NULL || *lpszSection == '\0')
\r
65 return (size_t)(-1);
\r
67 m_arEntries.clear();
\r
69 m_sSection = lpszSection;
\r
70 m_sKeyPrefix = lpszKeyPrefix;
\r
76 //keys are of form <lpszKeyPrefix><entrynumber>
\r
77 TCHAR sKey[4096] = {0};
\r
78 _stprintf_s(sKey, 4096, _T("%s\\%s%d"), lpszSection, lpszKeyPrefix, n++);
\r
79 sText = CRegStdString(sKey);
\r
82 m_arEntries.push_back(sText);
\r
84 } while (!sText.empty() && n < m_nMaxHistoryItems);
\r
86 return m_arEntries.size();
\r
89 bool CRegHistory::Save() const
\r
91 if (m_sSection.empty())
\r
94 // save history to registry
\r
95 int nMax = min((int)m_arEntries.size(), m_nMaxHistoryItems + 1);
\r
96 for (int n = 0; n < (int)m_arEntries.size(); n++)
\r
98 TCHAR sKey[4096] = {0};
\r
99 _stprintf_s(sKey, 4096, _T("%s\\%s%d"), m_sSection.c_str(), m_sKeyPrefix.c_str(), n);
\r
100 CRegStdString regkey = CRegStdString(sKey);
\r
101 regkey = m_arEntries[n];
\r
103 // remove items exceeding the max number of history items
\r
104 for (int n = nMax; ; n++)
\r
106 TCHAR sKey[4096] = {0};
\r
107 _stprintf_s(sKey, 4096, _T("%s\\%s%d"), m_sSection.c_str(), m_sKeyPrefix.c_str(), n);
\r
108 CRegStdString regkey = CRegStdString(sKey);
\r
109 if (((stdstring)regkey).empty())
\r
111 regkey.removeValue(); // remove entry
\r