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 // I would prefer to give a tag number instead of an explicit token
151 // here, but it is not possible because Buffer::readDocument uses
152 // explicit tokens (JMarc)
153 string const LyXLex::getLongString(string const & endtoken)
156 bool firstline = true;
160 // blank line in the file being read
163 string const token = trim(getString(), " \t");
165 lyxerr[Debug::PARSER] << "LongString: `"
166 << getString() << '\'' << endl;
168 // We do a case independent comparison, like search_kw does.
169 if (compare_ascii_no_case(token, endtoken) == 0)
172 string tmpstr = getString();
174 string::size_type i(tmpstr.find_first_not_of(' '));
175 if (i != string::npos)
176 prefix = tmpstr.substr(0, i);
178 lyxerr[Debug::PARSER]
179 << "Prefix = `" << prefix << "\'" << endl;
182 // further lines in long strings may have the same
183 // whitespace prefix as the first line. Remove it.
184 if (prefix.length() && prefixIs(tmpstr, prefix)) {
185 tmpstr.erase(0, prefix.length() - 1);
188 str += ltrim(tmpstr, "\t") + '\n';
192 printError("Long string not ended by `" + endtoken + '\'');
199 bool LyXLex::getBool() const
201 if (pimpl_->getString() == "true") {
203 } else if (pimpl_->getString() != "false") {
204 pimpl_->printError("Bad boolean `$$Token'. "
205 "Use \"false\" or \"true\"");
211 bool LyXLex::eatLine()
213 return pimpl_->eatLine();
217 bool LyXLex::next(bool esc)
219 return pimpl_->next(esc);
223 bool LyXLex::nextToken()
225 return pimpl_->nextToken();
229 void LyXLex::pushToken(string const & pt)
231 pimpl_->pushToken(pt);
234 LyXLex::operator void const *() const
236 // This behaviour is NOT the same as the std::streams which would
237 // use fail() here. However, our implementation of getString() et al.
238 // can cause the eof() and fail() bits to be set, even though we
239 // haven't tried to read 'em.
240 return pimpl_->is.bad() ? 0 : this;
244 bool LyXLex::operator!() const
246 return pimpl_->is.bad();
250 LyXLex & LyXLex::operator>>(std::string & s)
260 LyXLex & LyXLex::operator>>(double & s)
270 LyXLex & LyXLex::operator>>(int & s)
280 LyXLex & LyXLex::operator>>(unsigned int & s)
290 LyXLex & LyXLex::operator>>(bool & s)
300 /// quotes a string, e.g. for use in preferences files or as an argument of the "log" dialog
301 string const LyXLex::quoteString(string const & arg)
303 std::ostringstream os;
304 os << '"' << subst(subst(arg, "\\", "\\\\"), "\"", "\\\"") << '"';