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