Filenames in FILE_NOTIFY_INFORMATION are not null terminated, so don't use _TRUNCATE...
[TortoiseGit.git] / src / Utils / RegHistory.cpp
blob23a0a2c3c570f947149c7d4a160d3de79ad92b74
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 (_tcslen(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 (_tcscmp(szText, m_arEntries[i].c_str())==0)
48 m_arEntries.erase(m_arEntries.begin() + i);
49 m_arEntries.insert(m_arEntries.begin(), szText);
50 return false;
53 m_arEntries.insert(m_arEntries.begin(), szText);
54 return true;
57 void CRegHistory::RemoveEntry(int pos)
59 m_arEntries.erase(m_arEntries.begin() + pos);
62 size_t CRegHistory::Load(LPCTSTR lpszSection, LPCTSTR lpszKeyPrefix)
64 if (lpszSection == NULL || lpszKeyPrefix == NULL || *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 _stprintf_s(sKey, 4096, _T("%s\\%s%d"), lpszSection, lpszKeyPrefix, n++);
79 sText = CRegStdString(sKey);
80 if (!sText.empty())
82 m_arEntries.push_back(sText);
84 } while (!sText.empty() && n < m_nMaxHistoryItems);
86 return m_arEntries.size();
89 bool CRegHistory::Save() const
91 if (m_sSection.empty())
92 return false;
94 // save history to registry
95 int nMax = min((int)m_arEntries.size(), m_nMaxHistoryItems + 1);
96 for (int n = 0; n < (int)m_arEntries.size(); n++)
98 TCHAR sKey[4096] = {0};
99 _stprintf_s(sKey, 4096, _T("%s\\%s%d"), m_sSection.c_str(), m_sKeyPrefix.c_str(), n);
100 CRegStdString regkey = CRegStdString(sKey);
101 regkey = m_arEntries[n];
103 // remove items exceeding the max number of history items
104 for (int n = nMax; ; n++)
106 TCHAR sKey[4096] = {0};
107 _stprintf_s(sKey, 4096, _T("%s\\%s%d"), m_sSection.c_str(), m_sKeyPrefix.c_str(), n);
108 CRegStdString regkey = CRegStdString(sKey);
109 if (((tstring)regkey).empty())
110 break;
111 regkey.removeValue(); // remove entry
113 return true;