3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
11 * Full author contact details are available in file CREDITS.
19 #include "lyxlex_pimpl.h"
21 #include "support/convert.h"
22 #include "support/lstrings.h"
26 using lyx::support::compare_ascii_no_case;
27 using lyx::support::isStrDbl;
28 using lyx::support::isStrInt;
29 using lyx::support::ltrim;
30 using lyx::support::prefixIs;
31 using lyx::support::subst;
32 using lyx::support::trim;
40 LyXLex::LyXLex(keyword_item * tab, int num)
41 : pimpl_(new Pimpl(tab, num))
51 bool LyXLex::isOK() const
53 return pimpl_->is.good();
57 void LyXLex::setLineNo(int l)
63 int LyXLex::getLineNo() const
65 return pimpl_->lineno;
69 istream & LyXLex::getStream()
75 void LyXLex::pushTable(keyword_item * tab, int num)
77 pimpl_->pushTable(tab, num);
81 void LyXLex::popTable()
87 void LyXLex::printTable(ostream & os)
89 pimpl_->printTable(os);
93 void LyXLex::printError(string const & message) const
95 pimpl_->printError(message);
99 bool LyXLex::setFile(string const & filename)
101 return pimpl_->setFile(filename);
105 void LyXLex::setStream(istream & i)
107 pimpl_->setStream(i);
111 void LyXLex::setCommentChar(char c)
113 pimpl_->setCommentChar(c);
118 return pimpl_->lex();
122 int LyXLex::getInteger() const
124 if (isStrInt(pimpl_->getString()))
125 return convert<int>(pimpl_->getString());
126 pimpl_->printError("Bad integer `$$Token'");
131 double LyXLex::getFloat() const
133 // replace comma with dot in case the file was written with
134 // the wrong locale (should be rare, but is easy enough to
136 string const str = subst(pimpl_->getString(), ",", ".");
138 return convert<double>(str);
139 pimpl_->printError("Bad float `$$Token'");
144 string const LyXLex::getString() const
146 return pimpl_->getString();
150 lyx::docstring const LyXLex::getDocString() const
152 return pimpl_->getDocString();
156 // I would prefer to give a tag number instead of an explicit token
157 // here, but it is not possible because Buffer::readDocument uses
158 // explicit tokens (JMarc)
159 string const LyXLex::getLongString(string const & endtoken)
162 bool firstline = true;
166 // blank line in the file being read
169 string const token = trim(getString(), " \t");
171 lyxerr[Debug::PARSER] << "LongString: `"
172 << getString() << '\'' << endl;
174 // We do a case independent comparison, like search_kw does.
175 if (compare_ascii_no_case(token, endtoken) == 0)
178 string tmpstr = getString();
180 string::size_type i(tmpstr.find_first_not_of(' '));
181 if (i != string::npos)
182 prefix = tmpstr.substr(0, i);
184 lyxerr[Debug::PARSER]
185 << "Prefix = `" << prefix << "\'" << endl;
188 // further lines in long strings may have the same
189 // whitespace prefix as the first line. Remove it.
190 if (prefix.length() && prefixIs(tmpstr, prefix)) {
191 tmpstr.erase(0, prefix.length() - 1);
194 str += ltrim(tmpstr, "\t") + '\n';
198 printError("Long string not ended by `" + endtoken + '\'');
205 bool LyXLex::getBool() const
207 if (pimpl_->getString() == "true") {
209 } else if (pimpl_->getString() != "false") {
210 pimpl_->printError("Bad boolean `$$Token'. "
211 "Use \"false\" or \"true\"");
217 bool LyXLex::eatLine()
219 return pimpl_->eatLine();
223 bool LyXLex::next(bool esc)
225 return pimpl_->next(esc);
229 bool LyXLex::nextToken()
231 return pimpl_->nextToken();
235 void LyXLex::pushToken(string const & pt)
237 pimpl_->pushToken(pt);
240 LyXLex::operator void const *() const
242 // This behaviour is NOT the same as the std::streams which would
243 // use fail() here. However, our implementation of getString() et al.
244 // can cause the eof() and fail() bits to be set, even though we
245 // haven't tried to read 'em.
246 return pimpl_->is.bad() ? 0 : this;
250 bool LyXLex::operator!() const
252 return pimpl_->is.bad();
256 LyXLex & LyXLex::operator>>(std::string & s)
266 LyXLex & LyXLex::operator>>(double & s)
276 LyXLex & LyXLex::operator>>(int & s)
286 LyXLex & LyXLex::operator>>(unsigned int & s)
296 LyXLex & LyXLex::operator>>(bool & s)
306 /// quotes a string, e.g. for use in preferences files or as an argument of the "log" dialog
307 string const LyXLex::quoteString(string const & arg)
309 std::ostringstream os;
310 os << '"' << subst(subst(arg, "\\", "\\\\"), "\"", "\\\"") << '"';