]> git.lyx.org Git - lyx.git/blob - src/mathed/MathAutoCorrect.C
fix reading UTF8 encoded symbol file
[lyx.git] / src / mathed / MathAutoCorrect.C
1 /**
2  * \file MathAutoCorrect.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "MathAutoCorrect.h"
14 #include "MathData.h"
15 #include "InsetMath.h"
16 #include "MathSupport.h"
17 #include "MathParser.h"
18 #include "debug.h"
19
20 #include "support/filetools.h" //  LibFileSearch
21
22 #include <fstream>
23 #include <sstream>
24
25
26 namespace lyx {
27
28 using support::libFileSearch;
29
30 using std::string;
31 using std::ifstream;
32 using std::istream;
33 using std::istringstream;
34 using std::ostream;
35 using std::endl;
36 using std::vector;
37
38 namespace {
39
40 class Correction {
41 public:
42         ///
43         Correction() {}
44         ///
45         bool correct(MathAtom & at, char_type c) const;
46         ///
47         bool read(idocstream & is);
48         ///
49         void write(odocstream & os) const;
50 private:
51         ///
52         MathAtom from1_;
53         ///
54         char_type from2_;
55         ///
56         MathAtom to_;
57 };
58
59
60 bool Correction::read(idocstream & is)
61 {
62         docstring s1, s2, s3;
63         is >> s1 >> s2 >> s3;
64         if (!is)
65                 return false;
66         if (s2.size() != 1)
67                 return false;
68         MathArray ar1, ar3;
69         mathed_parse_cell(ar1, s1);
70         mathed_parse_cell(ar3, s3);
71         if (ar1.size() != 1 || ar3.size() != 1)
72                 return false;
73         from1_ = ar1.front();
74         from2_ = s2[0];
75         to_    = ar3.front();
76         return true;
77 }
78
79
80 void Correction::write(odocstream & os) const
81 {
82         os << "from: '" << from1_ << "' and '" << from2_
83            << "' to '" << to_ << '\'' << endl;
84 }
85
86
87 bool Correction::correct(MathAtom & at, char_type c) const
88 {
89         //lyxerr[Debug::MATHED]
90         //      << "trying to correct ar: " << at << " from: '" << from1_ << '\'' << endl;
91         if (from2_ != c)
92                 return false;
93         if (asString(at) != asString(from1_))
94                 return false;
95         lyxerr[Debug::MATHED]
96                 << "match found! subst in " << at
97                 << " from: '" << from1_ << "' to '" << to_ << '\'' << endl;
98         at = to_;
99         return true;
100 }
101
102
103 idocstream & operator>>(idocstream & is, Correction & corr)
104 {
105         corr.read(is);
106         return is;
107 }
108
109
110 odocstream & operator<<(odocstream & os, Correction & corr)
111 {
112         corr.write(os);
113         return os;
114 }
115
116
117
118
119 class Corrections {
120 public:
121         ///
122         typedef vector<Correction>::const_iterator const_iterator;
123         ///
124         Corrections() {}
125         ///
126         void insert(const Correction & corr) { data_.push_back(corr); }
127         ///
128         bool correct(MathAtom & at, char_type c) const;
129 private:
130         ///
131         vector<Correction> data_;
132 };
133
134
135 bool Corrections::correct(MathAtom & at, char_type c) const
136 {
137         for (const_iterator it = data_.begin(); it != data_.end(); ++it)
138                 if (it->correct(at, c))
139                         return true;
140         return false;
141 }
142
143
144 Corrections theCorrections;
145
146 void initAutoCorrect()
147 {
148         lyxerr[Debug::MATHED] << "reading autocorrect file" << endl;
149         string const file = libFileSearch(string(), "autocorrect");
150         if (file.empty()) {
151                 lyxerr << "Could not find autocorrect file" << endl;
152                 return;
153         }
154
155         string line;
156         ifstream is(file.c_str());
157         while (getline(is, line)) {
158                 if (line.size() == 0 || line[0] == '#') {
159                         //lyxerr[Debug::MATHED] << "ignoring line '" << line << '\'' << endl;
160                         continue;
161                 }
162                 idocstringstream il(from_utf8(line));
163
164                 //lyxerr[Debug::MATHED] << "line '" << line << '\'' << endl;
165                 Correction corr;
166                 if (corr.read(il)) {
167                         //lyxerr[Debug::MATHED] << "parsed: '" << corr << '\'' << endl;
168                         theCorrections.insert(corr);
169                 }
170         }
171
172         lyxerr[Debug::MATHED] << "done reading autocorrections." << endl;
173 }
174
175
176 } // namespace anon
177
178
179 bool math_autocorrect(MathAtom & at, char c)
180 {
181         static bool initialized = false;
182
183         if (!initialized) {
184                 initAutoCorrect();
185                 initialized = true;
186         }
187
188         return theCorrections.correct(at, c);
189 }
190
191
192 } // namespace lyx