Dropped unused variables
[TortoiseGit.git] / src / Utils / PersonalDictionary.cpp
blob51a0b65b25ed8910638d234b3af5017a82789e71
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2006, 2008 - 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]; //MAX_PATH ok here.
43 _tcscpy_s (path, CPathUtils::GetAppDataDirectory());
45 if (m_lLanguage==0)
46 m_lLanguage = GetUserDefaultLCID();
48 TCHAR sLang[10];
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];
55 SecureZeroMemory(filepath, sizeof(filepath));
56 WideCharToMultiByte(CP_ACP, NULL, path, -1, filepath, MAX_PATH, NULL, NULL);
57 File.open(filepath);
58 if (!File.good())
60 return false;
64 File.getline(line, _countof(line));
65 sWord = line;
66 dict.insert(sWord);
67 } while (File.gcount() > 0);
68 File.close();
69 m_bLoaded = true;
70 return true;
73 bool CPersonalDictionary::AddWord(const CString& sWord)
75 if (!m_bLoaded)
76 Load();
77 if (sWord.GetLength() >= PDICT_MAX_WORD_LENGTH)
78 return false;
79 dict.insert(sWord);
80 return true;
83 bool CPersonalDictionary::FindWord(const CString& sWord)
85 if (!m_bLoaded)
86 Load();
87 // even if the load failed for some reason, we mark it as loaded
88 // and just assume an empty personal dictionary
89 m_bLoaded = true;
90 std::set<CString>::iterator it;
91 it = dict.find(sWord);
92 return (it != dict.end());
95 bool CPersonalDictionary::Save()
97 if (!m_bLoaded)
98 return false;
99 TCHAR path[MAX_PATH]; //MAX_PATH ok here.
100 _tcscpy_s (path, CPathUtils::GetAppDataDirectory());
102 if (m_lLanguage==0)
103 m_lLanguage = GetUserDefaultLCID();
105 TCHAR sLang[10];
106 _stprintf_s(sLang, 10, _T("%ld"), m_lLanguage);
107 _tcscat_s(path, MAX_PATH, sLang);
108 _tcscat_s(path, MAX_PATH, _T(".dic"));
110 std::wofstream File;
111 char filepath[MAX_PATH+1];
112 SecureZeroMemory(filepath, sizeof(filepath));
113 WideCharToMultiByte(CP_ACP, NULL, path, -1, filepath, MAX_PATH, NULL, NULL);
114 File.open(filepath, std::ios_base::binary);
115 for (std::set<CString>::iterator it = dict.begin(); it != dict.end(); ++it)
117 File << (LPCTSTR)*it << _T("\n");
119 File.close();
120 return true;