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"
29 using support::compare_ascii_no_case;
30 using support::isStrDbl;
31 using support::isStrInt;
33 using support::prefixIs;
43 LyXLex::LyXLex(keyword_item * tab, int num)
44 : pimpl_(new Pimpl(tab, num))
54 bool LyXLex::isOK() const
56 return pimpl_->is.good();
60 void LyXLex::setLineNo(int l)
66 int LyXLex::getLineNo() const
68 return pimpl_->lineno;
72 istream & LyXLex::getStream()
78 void LyXLex::pushTable(keyword_item * tab, int num)
80 pimpl_->pushTable(tab, num);
84 void LyXLex::popTable()
90 void LyXLex::printTable(ostream & os)
92 pimpl_->printTable(os);
96 void LyXLex::printError(string const & message) const
98 pimpl_->printError(message);
102 bool LyXLex::setFile(support::FileName const & filename)
104 return pimpl_->setFile(filename);
108 void LyXLex::setStream(istream & i)
110 pimpl_->setStream(i);
114 void LyXLex::setCommentChar(char c)
116 pimpl_->setCommentChar(c);
121 return pimpl_->lex();
125 int LyXLex::getInteger() const
127 if (isStrInt(pimpl_->getString()))
128 return convert<int>(pimpl_->getString());
129 pimpl_->printError("Bad integer `$$Token'");
134 double LyXLex::getFloat() const
136 // replace comma with dot in case the file was written with
137 // the wrong locale (should be rare, but is easy enough to
139 string const str = subst(pimpl_->getString(), ",", ".");
141 return convert<double>(str);
142 pimpl_->printError("Bad float `$$Token'");
147 string const LyXLex::getString() const
149 return pimpl_->getString();
153 docstring const LyXLex::getDocString() const
155 return pimpl_->getDocString();
159 // I would prefer to give a tag number instead of an explicit token
160 // here, but it is not possible because Buffer::readDocument uses
161 // explicit tokens (JMarc)
162 string const LyXLex::getLongString(string const & endtoken)
165 bool firstline = true;
169 // blank line in the file being read
172 string const token = trim(getString(), " \t");
174 lyxerr[Debug::PARSER] << "LongString: `"
175 << getString() << '\'' << endl;
177 // We do a case independent comparison, like search_kw does.
178 if (compare_ascii_no_case(token, endtoken) == 0)
181 string tmpstr = getString();
183 string::size_type i(tmpstr.find_first_not_of(' '));
184 if (i != string::npos)
185 prefix = tmpstr.substr(0, i);
187 lyxerr[Debug::PARSER]
188 << "Prefix = `" << prefix << "\'" << endl;
191 // further lines in long strings may have the same
192 // whitespace prefix as the first line. Remove it.
193 if (prefix.length() && prefixIs(tmpstr, prefix)) {
194 tmpstr.erase(0, prefix.length() - 1);
197 str += ltrim(tmpstr, "\t") + '\n';
201 printError("Long string not ended by `" + endtoken + '\'');
208 bool LyXLex::getBool() const
210 if (pimpl_->getString() == "true") {
212 } else if (pimpl_->getString() != "false") {
213 pimpl_->printError("Bad boolean `$$Token'. "
214 "Use \"false\" or \"true\"");
220 bool LyXLex::eatLine()
222 return pimpl_->eatLine();
226 bool LyXLex::next(bool esc)
228 return pimpl_->next(esc);
232 bool LyXLex::nextToken()
234 return pimpl_->nextToken();
238 void LyXLex::pushToken(string const & pt)
240 pimpl_->pushToken(pt);
243 LyXLex::operator void const *() const
245 // This behaviour is NOT the same as the std::streams which would
246 // use fail() here. However, our implementation of getString() et al.
247 // can cause the eof() and fail() bits to be set, even though we
248 // haven't tried to read 'em.
249 return pimpl_->is.bad() ? 0 : this;
253 bool LyXLex::operator!() const
255 return pimpl_->is.bad();
259 LyXLex & LyXLex::operator>>(std::string & s)
269 LyXLex & LyXLex::operator>>(docstring & s)
279 LyXLex & LyXLex::operator>>(double & s)
289 LyXLex & LyXLex::operator>>(int & s)
299 LyXLex & LyXLex::operator>>(unsigned int & s)
309 LyXLex & LyXLex::operator>>(bool & s)
319 /// quotes a string, e.g. for use in preferences files or as an argument of the "log" dialog
320 string const LyXLex::quoteString(string const & arg)
322 std::ostringstream os;
323 os << '"' << subst(subst(arg, "\\", "\\\\"), "\"", "\\\"") << '"';