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