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