CPatch: New memory management
[TortoiseGit.git] / src / Utils / PersonalDictionary.cpp
blob53483790a784f5f226aa56eb1903d3786806579b
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.
21 #include "stdafx.h"
22 #include <fstream>
23 #include <codecvt>
24 #include "PersonalDictionary.h"
25 #include "PathUtils.h"
27 CPersonalDictionary::CPersonalDictionary(LONG lLanguage /* = 0*/)
28 : m_bLoaded(false)
29 , m_lLanguage(lLanguage)
33 CPersonalDictionary::~CPersonalDictionary()
37 template<class T>
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>);
47 file.imbue(ulocale);
49 file.open(filepath, openmode);
52 bool CPersonalDictionary::Load()
54 CString sWord;
55 TCHAR line[PDICT_MAX_WORD_LENGTH + 1];
57 if (m_bLoaded)
58 return true;
60 std::wifstream File;
61 OpenFileStream(File, m_lLanguage);
62 if (!File.good())
64 return false;
68 File.getline(line, _countof(line));
69 sWord = line;
70 sWord.TrimRight();
71 if (sWord.IsEmpty())
72 continue;
73 dict.insert(sWord);
74 } while (File.gcount() > 0);
75 File.close();
76 m_bLoaded = true;
77 return true;
80 bool CPersonalDictionary::AddWord(const CString& sWord)
82 if (!m_bLoaded)
83 Load();
84 if (sWord.GetLength() >= PDICT_MAX_WORD_LENGTH || sWord.IsEmpty())
85 return false;
86 dict.insert(sWord);
87 return true;
90 bool CPersonalDictionary::FindWord(const CString& sWord)
92 if (!m_bLoaded)
93 Load();
94 // even if the load failed for some reason, we mark it as loaded
95 // and just assume an empty personal dictionary
96 m_bLoaded = true;
97 std::set<CString>::iterator it;
98 it = dict.find(sWord);
99 return (it != dict.end());
102 bool CPersonalDictionary::Save()
104 if (!m_bLoaded)
105 return false;
106 std::wofstream File;
107 OpenFileStream(File, m_lLanguage, std::ios::ios_base::binary);
108 for (const auto& line : dict)
109 File << (LPCTSTR)line << L"\n";
110 File.close();
111 return true;