]> git.lyx.org Git - lyx.git/blob - src/PersonalWordList.cpp
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / PersonalWordList.cpp
1 /**
2  * \file PersonalWordList.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Stephan Witt
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "PersonalWordList.h"
14
15 #include "support/debug.h"
16 #include "support/docstring_list.h"
17 #include "support/filetools.h"
18 #include "support/Package.h"
19 #include "support/FileName.h"
20
21 #include <string>
22 #include <fstream>
23
24 using namespace std;
25 using namespace lyx::support;
26
27 namespace lyx {
28
29 FileName PersonalWordList::dictfile() const
30 {
31         string fname = "pwl_" + lang_ + ".dict";
32         return FileName(addName(package().user_support().absFileName(),fname));
33 }
34
35
36 docstring_list::const_iterator PersonalWordList::begin() const
37 {
38         return words_.begin();
39 }
40
41
42 docstring_list::const_iterator PersonalWordList::end() const
43 {
44         return words_.end();
45 }
46
47
48 void PersonalWordList::load()
49 {
50         FileName fn = dictfile();
51         LYXERR(Debug::FILES, "load personal dictionary from: " << fn);
52         ifstream ifs(fn.toFilesystemEncoding().c_str());
53
54         dirty(!words_.empty());
55         words_.clear();
56         string line;
57         getline(ifs, line);
58         if (line == header()) {
59                 while (ifs) {
60                         getline(ifs, line);
61                         if (!line.empty() && !(line[0] == '#')) {
62                                 docstring const word = from_utf8(line);
63                                 insert(word);
64                         }
65                 }
66                 LYXERR(Debug::FILES, "valid dictionary file found: " << words_.size() << " items.");
67         } else {
68                 LYXERR(Debug::FILES, "invalid dictionary file found: header is \"" << line << "\".");
69         }
70         ifs.close();
71         dirty(false);
72 }
73
74
75 void PersonalWordList::save()
76 {
77         if (!isDirty())
78                 return;
79         FileName fn = dictfile();
80         LYXERR(Debug::FILES, "save personal dictionary at: " << fn);
81         ofstream ofs(fn.toFilesystemEncoding().c_str());
82         docstring_list::iterator it = words_.begin();
83         docstring_list::const_iterator et = words_.end();
84
85         ofs << header() << "\n";
86         for (; it != et; ++it) {
87                 ofs << to_utf8(*it) << "\n";
88         }
89         LYXERR(Debug::FILES, "count of saved items: " << words_.size());
90 }
91
92
93 bool PersonalWordList::equalwords(docstring const & w1, docstring const & w2) const
94 {
95         return w1 == w2;
96 }
97
98
99 bool PersonalWordList::exists(docstring const & word) const
100 {
101         docstring_list::const_iterator it = words_.begin();
102         docstring_list::const_iterator et = words_.end();
103         for (; it != et; ++it) {
104                 if (equalwords(word,*it))
105                         return true;
106         }
107         return false;
108 }
109
110
111 void PersonalWordList::insert(docstring const & word)
112 {
113         if (exists(word))
114                 return;
115         words_.push_back(word);
116         dirty(true);
117 }
118
119
120 void PersonalWordList::remove(docstring const & word)
121 {
122         docstring_list::iterator it = words_.begin();
123         docstring_list::const_iterator et = words_.end();
124         for (; it != et; ++it) {
125                 if (equalwords(word,*it)) {
126                         words_.erase(it);
127                         dirty(true);
128                         return;
129                 }
130         }
131 }
132
133 } // namespace lyx