]> git.lyx.org Git - lyx.git/blob - src/PersonalWordList.cpp
Provide proper fallback if a bibliography processor is not found
[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 #include "support/lstrings.h"
21 #include "support/os.h"
22
23 #include <string>
24 #include <fstream>
25
26 using namespace std;
27 using namespace lyx::support;
28 using namespace lyx::support::os;
29
30 namespace lyx {
31
32 FileName PersonalWordList::dictfile() const
33 {
34         string fname = "pwl_" + lang_ + ".dict";
35         return FileName(addName(package().user_support().absFileName(),fname));
36 }
37
38
39 docstring_list::const_iterator PersonalWordList::begin() const
40 {
41         return words_.begin();
42 }
43
44
45 docstring_list::const_iterator PersonalWordList::end() const
46 {
47         return words_.end();
48 }
49
50
51 void PersonalWordList::load()
52 {
53         FileName fn = dictfile();
54         LYXERR(Debug::FILES, "load personal dictionary from: " << fn);
55         ifstream ifs(fn.toFilesystemEncoding().c_str());
56
57         dirty(words_.size() > 0);
58         words_.clear();
59         string line;
60         getline(ifs, line);
61         if (line == header()) {
62                 while (ifs) {
63                         getline(ifs, line);
64                         if (!line.empty() && !(line[0] == '#')) {
65                                 docstring const word = from_utf8(line);
66                                 insert(word);
67                         }
68                 }
69                 LYXERR(Debug::FILES, "valid dictionary file found: " << words_.size() << " items.");
70         } else {
71                 LYXERR(Debug::FILES, "invalid dictionary file found: header is \"" << line << "\".");
72         }
73         ifs.close();
74         dirty(false);
75 }
76
77
78 void PersonalWordList::save()
79 {
80         if (!isDirty())
81                 return;
82         FileName fn = dictfile();
83         LYXERR(Debug::FILES, "save personal dictionary at: " << fn);
84         ofstream ofs(fn.toFilesystemEncoding().c_str());
85         docstring_list::iterator it = words_.begin();
86         docstring_list::const_iterator et = words_.end();
87
88         ofs << header() << "\n";
89         for (; it != et; ++it) {
90                 ofs << to_utf8(*it) << "\n";
91         }
92         LYXERR(Debug::FILES, "count of saved items: " << words_.size());
93 }
94
95
96 bool PersonalWordList::equalwords(docstring const & w1, docstring const & w2) const
97 {
98         return w1 == w2;
99 }
100
101
102 bool PersonalWordList::exists(docstring const & word) const
103 {
104         docstring_list::const_iterator it = words_.begin();
105         docstring_list::const_iterator et = words_.end();
106         for (; it != et; ++it) {
107                 if (equalwords(word,*it))
108                         return true;
109         }
110         return false;
111 }
112
113
114 void PersonalWordList::insert(docstring const & word)
115 {
116         if (exists(word))
117                 return;
118         words_.push_back(word);
119         dirty(true);
120 }
121
122
123 void PersonalWordList::remove(docstring const & word)
124 {
125         docstring_list::iterator it = words_.begin();
126         docstring_list::const_iterator et = words_.end();
127         for (; it != et; ++it) {
128                 if (equalwords(word,*it)) {
129                         words_.erase(it);
130                         dirty(true);
131                         return;
132                 }
133         }
134 }
135
136 } // namespace lyx