Prepare release and bump version numbers to 2.17.0.2
[TortoiseGit.git] / src / Utils / RegHistory.cpp
blob23a4466ba7bb413767a874a0043717caf3c1b3a8
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2019 - TortoiseGit
4 // Copyright (C) 2007 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (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 Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "stdafx.h"
22 #include "registry.h"
23 #include "RegHistory.h"
26 CRegHistory::CRegHistory() : m_nMaxHistoryItems(25)
30 CRegHistory::~CRegHistory()
34 bool CRegHistory::AddEntry(LPCWSTR szText)
36 if (!szText[0])
37 return false;
39 if ((!m_sSection.empty())&&(!m_sKeyPrefix.empty()))
41 // refresh the history from the registry
42 Load(m_sSection.c_str(), m_sKeyPrefix.c_str());
45 for (size_t i=0; i<m_arEntries.size(); ++i)
47 if (wcscmp(szText, m_arEntries[i].c_str()) == 0)
49 m_arEntries.erase(m_arEntries.cbegin() + i);
50 m_arEntries.insert(m_arEntries.cbegin(), szText);
51 return false;
54 m_arEntries.insert(m_arEntries.cbegin(), szText);
55 return true;
58 void CRegHistory::RemoveEntry(int pos)
60 m_arEntries.erase(m_arEntries.cbegin() + pos);
63 size_t CRegHistory::Load(LPCWSTR lpszSection, LPCWSTR lpszKeyPrefix)
65 if (!lpszSection || !lpszKeyPrefix || *lpszSection == '\0')
66 return size_t(-1);
68 m_arEntries.clear();
70 m_sSection = lpszSection;
71 m_sKeyPrefix = lpszKeyPrefix;
73 int n = 0;
74 std::wstring sText;
77 //keys are of form <lpszKeyPrefix><entrynumber>
78 wchar_t sKey[4096] = { 0 };
79 swprintf_s(sKey, L"%s\\%s%d", lpszSection, lpszKeyPrefix, n++);
80 sText = CRegStdString(sKey);
81 if (!sText.empty())
82 m_arEntries.push_back(sText);
83 } while (!sText.empty() && n < m_nMaxHistoryItems);
85 return m_arEntries.size();
88 bool CRegHistory::Save() const
90 if (m_sSection.empty())
91 return false;
93 // save history to registry
94 int nMax = static_cast<int>(min(m_arEntries.size(), static_cast<size_t>(m_nMaxHistoryItems) + 1));
95 for (int n = 0; n < static_cast<int>(m_arEntries.size()); ++n)
97 wchar_t sKey[4096] = { 0 };
98 swprintf_s(sKey, L"%s\\%s%d", m_sSection.c_str(), m_sKeyPrefix.c_str(), n);
99 CRegStdString regkey(sKey);
100 regkey = m_arEntries[n];
102 // remove items exceeding the max number of history items
103 for (int n = nMax; ; ++n)
105 wchar_t sKey[4096] = { 0 };
106 swprintf_s(sKey, L"%s\\%s%d", m_sSection.c_str(), m_sKeyPrefix.c_str(), n);
107 CRegStdString regkey(sKey);
108 if (static_cast<std::wstring>(regkey).empty())
109 break;
110 regkey.removeValue(); // remove entry
112 return true;