OSDN Git Service

Add Resolve to explore context menu
[tortoisegit/TortoiseGitJp.git] / src / Utils / PersonalDictionary.cpp
1 // TortoiseSVN - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2003-2006, 2008 - 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 <fstream>\r
22 #include "PersonalDictionary.h"\r
23 #include "PathUtils.h"\r
24 \r
25 CPersonalDictionary::CPersonalDictionary(LONG lLanguage /* = 0*/) :\r
26         m_bLoaded(false)\r
27 {\r
28         m_lLanguage = lLanguage;\r
29 }\r
30 \r
31 CPersonalDictionary::~CPersonalDictionary()\r
32 {\r
33 }\r
34 \r
35 bool CPersonalDictionary::Load()\r
36 {\r
37         CString sWord;\r
38         TCHAR line[PDICT_MAX_WORD_LENGTH + 1];\r
39 \r
40         if (m_bLoaded)\r
41                 return true;\r
42         TCHAR path[MAX_PATH];           //MAX_PATH ok here.\r
43         _tcscpy_s (path, CPathUtils::GetAppDataDirectory());\r
44 \r
45         if (m_lLanguage==0)\r
46                 m_lLanguage = GetUserDefaultLCID();\r
47 \r
48         TCHAR sLang[10];\r
49         _stprintf_s(sLang, 10, _T("%ld"), m_lLanguage);\r
50         _tcscat_s(path, MAX_PATH, sLang);\r
51         _tcscat_s(path, MAX_PATH, _T(".dic"));\r
52 \r
53         std::wifstream File;\r
54         char filepath[MAX_PATH+1];\r
55         SecureZeroMemory(filepath, sizeof(filepath));\r
56         WideCharToMultiByte(CP_ACP, NULL, path, -1, filepath, MAX_PATH, NULL, NULL);\r
57         File.open(filepath);\r
58         if (!File.good())\r
59         {\r
60                 return false;\r
61         }\r
62         std::vector<std::wstring> entry;\r
63         do\r
64         {\r
65                 File.getline(line, sizeof(line)/sizeof(TCHAR));\r
66                 sWord = line;\r
67                 dict.insert(sWord);\r
68         } while (File.gcount() > 0);\r
69         File.close();\r
70         m_bLoaded = true;\r
71         return true;\r
72 }\r
73 \r
74 bool CPersonalDictionary::AddWord(const CString& sWord)\r
75 {\r
76         if (!m_bLoaded)\r
77                 Load();\r
78         if (sWord.GetLength() >= PDICT_MAX_WORD_LENGTH)\r
79                 return false;\r
80         dict.insert(sWord);\r
81         return true;\r
82 }\r
83 \r
84 bool CPersonalDictionary::FindWord(const CString& sWord)\r
85 {\r
86         if (!m_bLoaded)\r
87                 Load();\r
88         // even if the load failed for some reason, we mark it as loaded\r
89         // and just assume an empty personal dictionary\r
90         m_bLoaded = true;\r
91         std::set<CString>::iterator it;\r
92         it = dict.find(sWord);\r
93         return (it != dict.end());\r
94 }\r
95 \r
96 bool CPersonalDictionary::Save()\r
97 {\r
98         if (!m_bLoaded)\r
99                 return false;\r
100         TCHAR path[MAX_PATH];           //MAX_PATH ok here.\r
101         _tcscpy_s (path, CPathUtils::GetAppDataDirectory());\r
102 \r
103         if (m_lLanguage==0)\r
104                 m_lLanguage = GetUserDefaultLCID();\r
105 \r
106         TCHAR sLang[10];\r
107         _stprintf_s(sLang, 10, _T("%ld"), m_lLanguage);\r
108         _tcscat_s(path, MAX_PATH, sLang);\r
109         _tcscat_s(path, MAX_PATH, _T(".dic"));\r
110 \r
111         std::wofstream File;\r
112         char filepath[MAX_PATH+1];\r
113         SecureZeroMemory(filepath, sizeof(filepath));\r
114         WideCharToMultiByte(CP_ACP, NULL, path, -1, filepath, MAX_PATH, NULL, NULL);\r
115         File.open(filepath, std::ios_base::binary);\r
116         for (std::set<CString>::iterator it = dict.begin(); it != dict.end(); ++it)\r
117         {\r
118                 File << (LPCTSTR)*it << _T("\n");\r
119         }\r
120         File.close();\r
121         return true;\r
122 }\r