OSDN Git Service

Add Export Command
[tortoisegit/TortoiseGitJp.git] / src / Utils / RegHistory.cpp
1 // TortoiseSVN - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2007 - TortoiseSVN\r
4 \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
9 \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
14 \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
18 //\r
19 \r
20 #include "stdafx.h"\r
21 #include "Registry.h"\r
22 #include "RegHistory.h"\r
23 \r
24 \r
25 CRegHistory::CRegHistory() : m_nMaxHistoryItems(25)\r
26 {\r
27 }\r
28 \r
29 CRegHistory::~CRegHistory()\r
30 {\r
31 }\r
32 \r
33 bool CRegHistory::AddEntry(LPCTSTR szText)\r
34 {\r
35         if (_tcslen(szText) == 0)\r
36                 return false;\r
37 \r
38         if ((!m_sSection.empty())&&(!m_sKeyPrefix.empty()))\r
39         {\r
40                 // refresh the history from the registry\r
41                 Load(m_sSection.c_str(), m_sKeyPrefix.c_str());\r
42         }\r
43 \r
44         for (size_t i=0; i<m_arEntries.size(); ++i)\r
45         {\r
46                 if (_tcscmp(szText, m_arEntries[i].c_str())==0)\r
47                 {\r
48                         m_arEntries.erase(m_arEntries.begin() + i);\r
49                         m_arEntries.insert(m_arEntries.begin(), szText);\r
50                         return false;\r
51                 }\r
52         }\r
53         m_arEntries.insert(m_arEntries.begin(), szText);\r
54         return true;\r
55 }\r
56 \r
57 void CRegHistory::RemoveEntry(int pos)\r
58 {\r
59         m_arEntries.erase(m_arEntries.begin() + pos);\r
60 }\r
61 \r
62 size_t CRegHistory::Load(LPCTSTR lpszSection, LPCTSTR lpszKeyPrefix)\r
63 {\r
64         if (lpszSection == NULL || lpszKeyPrefix == NULL || *lpszSection == '\0')\r
65                 return (size_t)(-1);\r
66 \r
67         m_arEntries.clear();\r
68 \r
69         m_sSection = lpszSection;\r
70         m_sKeyPrefix = lpszKeyPrefix;\r
71 \r
72         int n = 0;\r
73         std::wstring sText;\r
74         do\r
75         {\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
80                 if (!sText.empty())\r
81                 {\r
82                         m_arEntries.push_back(sText);\r
83                 }\r
84         } while (!sText.empty() && n < m_nMaxHistoryItems);\r
85 \r
86         return m_arEntries.size();\r
87 }\r
88 \r
89 bool CRegHistory::Save() const\r
90 {\r
91         if (m_sSection.empty())\r
92                 return false;\r
93 \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
97         {\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
102         }\r
103         // remove items exceeding the max number of history items\r
104         for (int n = nMax; ; n++)\r
105         {\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
110                         break;\r
111                 regkey.removeValue(); // remove entry\r
112         }\r
113         return true;\r
114 }\r
115 \r