Improve readability of version string
[TortoiseGit.git] / src / Utils / PersonalDictionary.cpp
blob9cd380a0aadacda5cc156a4695c95ea0c3d3ae7a
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2006, 2008, 2014 - 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 <fstream>
22 #include "PersonalDictionary.h"
23 #include "PathUtils.h"
25 CPersonalDictionary::CPersonalDictionary(LONG lLanguage /* = 0*/) :
26 m_bLoaded(false)
28 m_lLanguage = lLanguage;
31 CPersonalDictionary::~CPersonalDictionary()
35 bool CPersonalDictionary::Load()
37 CString sWord;
38 TCHAR line[PDICT_MAX_WORD_LENGTH + 1];
40 if (m_bLoaded)
41 return true;
42 TCHAR path[MAX_PATH] = {0}; //MAX_PATH ok here.
43 _tcscpy_s (path, CPathUtils::GetAppDataDirectory());
45 if (m_lLanguage==0)
46 m_lLanguage = GetUserDefaultLCID();
48 TCHAR sLang[10] = { 0 };
49 _stprintf_s(sLang, 10, _T("%ld"), m_lLanguage);
50 _tcscat_s(path, MAX_PATH, sLang);
51 _tcscat_s(path, MAX_PATH, _T(".dic"));
53 std::wifstream File;
54 char filepath[MAX_PATH + 1] = { 0 };
55 WideCharToMultiByte(CP_ACP, 0, path, -1, filepath, MAX_PATH, nullptr, nullptr);
56 File.open(filepath);
57 if (!File.good())
59 return false;
63 File.getline(line, _countof(line));
64 sWord = line;
65 dict.insert(sWord);
66 } while (File.gcount() > 0);
67 File.close();
68 m_bLoaded = true;
69 return true;
72 bool CPersonalDictionary::AddWord(const CString& sWord)
74 if (!m_bLoaded)
75 Load();
76 if (sWord.GetLength() >= PDICT_MAX_WORD_LENGTH)
77 return false;
78 dict.insert(sWord);
79 return true;
82 bool CPersonalDictionary::FindWord(const CString& sWord)
84 if (!m_bLoaded)
85 Load();
86 // even if the load failed for some reason, we mark it as loaded
87 // and just assume an empty personal dictionary
88 m_bLoaded = true;
89 std::set<CString>::iterator it;
90 it = dict.find(sWord);
91 return (it != dict.end());
94 bool CPersonalDictionary::Save()
96 if (!m_bLoaded)
97 return false;
98 TCHAR path[MAX_PATH] = { 0 }; //MAX_PATH ok here.
99 _tcscpy_s (path, CPathUtils::GetAppDataDirectory());
101 if (m_lLanguage==0)
102 m_lLanguage = GetUserDefaultLCID();
104 TCHAR sLang[10] = { 0 };
105 _stprintf_s(sLang, 10, _T("%ld"), m_lLanguage);
106 _tcscat_s(path, MAX_PATH, sLang);
107 _tcscat_s(path, MAX_PATH, _T(".dic"));
109 std::wofstream File;
110 char filepath[MAX_PATH + 1] = { 0 };
111 WideCharToMultiByte(CP_ACP, 0, path, -1, filepath, MAX_PATH, nullptr, nullptr);
112 File.open(filepath, std::ios_base::binary);
113 for (const auto& line : dict)
114 File << (LPCTSTR)line << _T("\n");
115 File.close();
116 return true;