1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011-2016 - TortoiseGit
4 // Copyright (C) 2003-2006, 2008, 2014, 2016 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "PersonalDictionary.h"
25 #include "PathUtils.h"
27 CPersonalDictionary::CPersonalDictionary(LONG lLanguage
/* = 0*/)
29 , m_lLanguage(lLanguage
)
33 CPersonalDictionary::~CPersonalDictionary()
38 static void OpenFileStream(T
& file
, LONG lLanguage
, std::ios_base::openmode openmode
= 0)
40 TCHAR path
[MAX_PATH
] = { 0 }; //MAX_PATH ok here.
41 swprintf_s(path
, L
"%s%ld.dic", (LPCTSTR
)CPathUtils::GetAppDataDirectory(), !lLanguage
? GetUserDefaultLCID() : lLanguage
);
43 char filepath
[MAX_PATH
+ 1] = { 0 };
44 WideCharToMultiByte(CP_ACP
, 0, path
, -1, filepath
, _countof(filepath
) - 1, nullptr, nullptr);
46 std::locale
ulocale(std::locale(), new std::codecvt_utf8
<wchar_t>);
49 file
.open(filepath
, openmode
);
52 bool CPersonalDictionary::Load()
55 TCHAR line
[PDICT_MAX_WORD_LENGTH
+ 1];
61 OpenFileStream(File
, m_lLanguage
);
68 File
.getline(line
, _countof(line
));
74 } while (File
.gcount() > 0);
80 bool CPersonalDictionary::AddWord(const CString
& sWord
)
84 if (sWord
.GetLength() >= PDICT_MAX_WORD_LENGTH
|| sWord
.IsEmpty())
90 bool CPersonalDictionary::FindWord(const CString
& sWord
)
94 // even if the load failed for some reason, we mark it as loaded
95 // and just assume an empty personal dictionary
97 std::set
<CString
>::iterator it
;
98 it
= dict
.find(sWord
);
99 return (it
!= dict
.end());
102 bool CPersonalDictionary::Save()
107 OpenFileStream(File
, m_lLanguage
, std::ios::ios_base::binary
);
108 for (const auto& line
: dict
)
109 File
<< (LPCTSTR
)line
<< L
"\n";