]> git.lyx.org Git - lyx.git/blob - src/lyxlex.C
Don't launch that Alert if the graphics file isn't found. It doesn't work
[lyx.git] / src / lyxlex.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1996-2001 The LyX Team.
7  *
8  *   Generalized simple lexical analizer.
9  *   It can be used for simple syntax parsers, like lyxrc,
10  *   texclass and others to come.   [asierra30/03/96]
11  *
12  * ====================================================== */
13
14 #include <config.h>
15
16 #ifdef __GNUG__
17 #pragma implementation "lyxlex.h"
18 #endif
19
20 #include "lyxlex.h"
21 #include "lyxlex_pimpl.h"
22 #include "debug.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25
26 using std::ostream;
27 using std::istream;
28 using std::endl;
29
30
31 LyXLex::LyXLex(keyword_item * tab, int num)
32         : pimpl_(new Pimpl(tab, num))
33 {}
34
35
36 LyXLex::~LyXLex()
37 {
38         delete pimpl_;
39 }
40
41
42 bool LyXLex::isOK() const
43 {
44         return pimpl_->is.good();
45 }
46
47
48 void LyXLex::setLineNo(int l)
49 {
50         pimpl_->lineno = l;
51 }
52
53
54 int LyXLex::getLineNo() const
55 {
56         return pimpl_->lineno;
57 }
58
59
60 string const LyXLex::text() const
61 {
62         return &pimpl_->buff[0];
63 }
64
65
66 istream & LyXLex::getStream()
67 {
68         return pimpl_->is;
69 }
70
71
72 void LyXLex::pushTable(keyword_item * tab, int num)
73 {
74         pimpl_->pushTable(tab, num);
75 }
76
77
78 void LyXLex::popTable()
79 {
80         pimpl_->popTable();
81 }
82
83
84 void LyXLex::printTable(ostream & os)
85 {
86         pimpl_->printTable(os);
87 }
88
89
90 void LyXLex::printError(string const & message) const
91 {
92         pimpl_->printError(message);
93 }
94
95
96 bool LyXLex::setFile(string const & filename)
97 {
98         return pimpl_->setFile(filename);
99 }
100
101
102 void LyXLex::setStream(istream & i)
103 {
104         pimpl_->setStream(i);
105 }
106
107
108 void LyXLex::setCommentChar(char c)
109 {
110         pimpl_->setCommentChar(c);
111 }
112
113 int LyXLex::lex()
114 {
115         return pimpl_->lex();
116 }
117
118
119 int LyXLex::getInteger() const
120 {
121         if (isStrInt(pimpl_->getString())) {
122                 return strToInt(pimpl_->getString());
123         } else {
124                 pimpl_->printError("Bad integer `$$Token'");
125                 return -1;
126         }
127 }
128
129
130 float LyXLex::getFloat() const
131 {
132         // replace comma with dot in case the file was written with
133         // the wrong locale (should be rare, but is easy enough to
134         // avoid).
135         string str = subst(pimpl_->getString(), ",", ".");
136         if (isStrDbl(str))
137                 return strToDbl(str);
138         else {
139                 pimpl_->printError("Bad float `$$Token'");
140                 return -1;
141         }
142 }
143
144
145 string const LyXLex::getString() const
146 {
147         return pimpl_->getString();
148 }
149
150
151 // I would prefer to give a tag number instead of an explicit token
152 // here, but it is not possible because Buffer::readLyXformat2 uses
153 // explicit tokens (JMarc)
154 string const LyXLex::getLongString(string const & endtoken)
155 {
156         string str, prefix;
157         bool firstline = true;
158
159         while (isOK()) {
160                 if (!eatLine())
161                         // blank line in the file being read
162                         continue;
163
164                 string const token = frontStrip(strip(getString()), " \t");
165
166                 lyxerr[Debug::PARSER] << "LongString: `"
167                                       << getString() << '\'' << endl;
168
169                 // We do a case independent comparison, like search_kw
170                 // does.
171                 if (compare_no_case(token, endtoken) != 0) {
172                         string tmpstr = getString();
173                         if (firstline) {
174                                 unsigned int i = 0;
175                                 while (i < tmpstr.length()
176                                       && tmpstr[i] == ' ') {
177                                         ++i;
178                                         prefix += ' ';
179                                 }
180                                 firstline = false;
181                                 lyxerr[Debug::PARSER] << "Prefix = `" << prefix
182                                                       << '\'' << endl;
183                         }
184
185                         if (!prefix.empty()
186                             && prefixIs(tmpstr, prefix)) {
187                                 tmpstr.erase(0, prefix.length() - 1);
188                         }
189                         str += frontStrip(tmpstr, "\t") + '\n';
190                 }
191                 else // token == endtoken
192                         break;
193         }
194         if (!isOK()) {
195                 printError("Long string not ended by `" + endtoken + '\'');
196         }
197
198         return str;
199 }
200
201
202 bool LyXLex::getBool() const
203 {
204         if (compare(pimpl_->buff, "true") == 0) {
205                 return true;
206         } else if (compare(pimpl_->buff, "false") != 0) {
207                 pimpl_->printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
208         }
209         return false;
210 }
211
212
213 bool LyXLex::eatLine()
214 {
215         return pimpl_->eatLine();
216 }
217
218
219 bool LyXLex::next(bool esc)
220 {
221         return pimpl_->next(esc);
222 }
223
224
225 bool LyXLex::nextToken()
226 {
227         return pimpl_->nextToken();
228 }
229
230
231 void LyXLex::pushToken(string const & pt)
232 {
233         pimpl_->pushToken(pt);
234 }
235
236
237 int LyXLex::findToken(char const * str[])
238 {
239         int i = 0;
240
241         if (next()) {
242                 if (compare(pimpl_->buff, "default")) {
243                         while (str[i][0] && compare(str[i], pimpl_->buff)) {
244                                 ++i;
245                         }
246                         if (!str[i][0]) {
247                                 pimpl_->printError("Unknown argument `$$Token'");
248                                 i = -1;
249                         }
250                 }
251         } else {
252                 pimpl_->printError("file ended while scanning string token");
253                 i = -1;
254         }
255         return i;
256 }