]> git.lyx.org Git - lyx.git/blobdiff - src/Lexer.cpp
* docstream: factorize out some code and introduce odocfstream::reset()
[lyx.git] / src / Lexer.cpp
index 4406d074977dbeb64ec4f02475a2eeb83104443f..d8dbe629e6d44dfceafb896cdc29624409a5d211 100644 (file)
 
 #include "Lexer.h"
 
-#include "debug.h"
+#include "support/debug.h"
 
 #include "support/convert.h"
 #include "support/filetools.h"
+#include "support/gzstream.h"
 #include "support/lstrings.h"
 #include "support/lyxalgo.h"
 #include "support/types.h"
 #include "support/unicode.h"
 
-#include <boost/iostreams/filtering_streambuf.hpp>
-#include <boost/iostreams/filter/gzip.hpp>
-#include <boost/iostreams/device/file.hpp>
-#include <boost/utility.hpp>
-
-namespace io = boost::iostreams;
+#include <boost/noncopyable.hpp>
 
 #include <functional>
 #include <istream>
@@ -42,7 +38,6 @@ namespace lyx {
 
 using support::compare_ascii_no_case;
 using support::FileName;
-using support::getFormatFromContents;
 using support::isStrDbl;
 using support::isStrInt;
 using support::ltrim;
@@ -110,7 +105,7 @@ public:
        std::filebuf fb_;
 
        /// gz_ is only used to open files, the stream is accessed through is.
-       io::filtering_istreambuf gz_;
+       gz::gzstreambuf gz_;
 
        /// the stream that we use.
        std::istream is;
@@ -257,32 +252,31 @@ void Lexer::Pimpl::popTable()
 bool Lexer::Pimpl::setFile(FileName const & filename)
 {
        // Check the format of the file.
-       string const format = getFormatFromContents(filename);
+       string const format = filename.guessFormatFromContents();
 
        if (format == "gzip" || format == "zip" || format == "compress") {
-               LYXERR(Debug::LYXLEX) << "lyxlex: compressed" << endl;
-
+               LYXERR(Debug::LYXLEX, "lyxlex: compressed");
                // The check only outputs a debug message, because it triggers
                // a bug in compaq cxx 6.2, where is_open() returns 'true' for
                // a fresh new filebuf.  (JMarc)
-               if (!gz_.empty() || istream::off_type(is.tellg()) > -1)
-                       LYXERR(Debug::LYXLEX) << "Error in Lexer::setFile: "
-                               "file or stream already set." << endl;
-               gz_.push(io::gzip_decompressor());
-               gz_.push(io::file_source(filename.toFilesystemEncoding()));
+               if (gz_.is_open() || istream::off_type(is.tellg()) > -1)
+                       LYXERR(Debug::LYXLEX, "Error in LyXLex::setFile: "
+                               "file or stream already set.");
+               gz_.open(filename.toFilesystemEncoding().c_str(), ios::in);
                is.rdbuf(&gz_);
                name = filename.absFilename();
                lineno = 0;
-               return gz_.component<io::file_source>(1)->is_open() && is.good();
+               return gz_.is_open() && is.good();
        } else {
-               LYXERR(Debug::LYXLEX) << "lyxlex: UNcompressed" << endl;
+               LYXERR(Debug::LYXLEX, "lyxlex: UNcompressed");
 
                // The check only outputs a debug message, because it triggers
                // a bug in compaq cxx 6.2, where is_open() returns 'true' for
                // a fresh new filebuf.  (JMarc)
-               if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
-                       LYXERR(Debug::LYXLEX) << "Error in Lexer::setFile: "
-                               "file or stream already set." << endl;
+               if (fb_.is_open() || istream::off_type(is.tellg()) > 0) {
+                       LYXERR(Debug::LYXLEX, "Error in Lexer::setFile: "
+                               "file or stream already set.");
+               }
                fb_.open(filename.toFilesystemEncoding().c_str(), ios::in);
                is.rdbuf(&fb_);
                name = filename.absFilename();
@@ -294,9 +288,10 @@ bool Lexer::Pimpl::setFile(FileName const & filename)
 
 void Lexer::Pimpl::setStream(istream & i)
 {
-       if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
-               LYXERR(Debug::LYXLEX)  << "Error in Lexer::setStream: "
-                       "file or stream already set." << endl;
+       if (fb_.is_open() || istream::off_type(is.tellg()) > 0) {
+               LYXERR(Debug::LYXLEX, "Error in Lexer::setStream: "
+                       "file or stream already set.");
+       }
        is.rdbuf(i.rdbuf());
        lineno = 0;
 }
@@ -338,8 +333,7 @@ bool Lexer::Pimpl::next(bool esc /* = false */)
                                string dummy;
                                getline(is, dummy);
 
-                               LYXERR(Debug::LYXLEX) << "Comment read: `" << c
-                                                     << dummy << '\'' << endl;
+                               LYXERR(Debug::LYXLEX, "Comment read: `" << c << dummy << '\'');
 #else
                                // unfortunately ignore is buggy (Lgb)
                                is.ignore(100, '\n');
@@ -417,7 +411,7 @@ bool Lexer::Pimpl::next(bool esc /* = false */)
                        c = cc;
 
                        // skip ','s
-                       if (c == ',') 
+                       if (c == ',')
                                continue;
 
                        if (c == commentChar) {
@@ -427,8 +421,7 @@ bool Lexer::Pimpl::next(bool esc /* = false */)
                                string dummy;
                                getline(is, dummy);
 
-                               LYXERR(Debug::LYXLEX) << "Comment read: `" << c
-                                                     << dummy << '\'' << endl;
+                               LYXERR(Debug::LYXLEX, "Comment read: `" << c << dummy << '\'');
 #else
                                // but ignore is also still buggy (Lgb)
                                // This is fast (Lgb)
@@ -525,7 +518,7 @@ int Lexer::Pimpl::search_kw(char const * const tag) const
 int Lexer::Pimpl::lex()
 {
        //NOTE: possible bug.
-       if (next() && status == LEX_TOKEN) 
+       if (next() && status == LEX_TOKEN)
                return search_kw(getString().c_str());
        return status;
 }
@@ -540,8 +533,7 @@ bool Lexer::Pimpl::eatLine()
        while (is && c != '\n') {
                is.get(cc);
                c = cc;
-               //LYXERR(Debug::LYXLEX) << "Lexer::EatLine read char: `"
-               //                    << c << '\'' << endl;
+               //LYXERR(Debug::LYXLEX, "Lexer::EatLine read char: `" << c << '\'');
                if (c != '\r')
                        buff.push_back(c);
        }
@@ -619,7 +611,7 @@ bool Lexer::Pimpl::nextToken()
 
 bool Lexer::Pimpl::inputAvailable()
 {
-       return is.good(); 
+       return is.good();
 }
 
 
@@ -771,7 +763,7 @@ string const Lexer::getString() const
 docstring const Lexer::getDocString() const
 {
        lastReadOk_ = pimpl_->status == LEX_DATA || pimpl_->status == LEX_TOKEN;
-       
+
        if (lastReadOk_)
                return pimpl_->getDocString();
 
@@ -794,8 +786,7 @@ string const Lexer::getLongString(string const & endtoken)
 
                string const token = trim(getString(), " \t");
 
-               LYXERR(Debug::PARSER) << "LongString: `"
-                                     << getString() << '\'' << endl;
+               LYXERR(Debug::PARSER, "LongString: `" << getString() << '\'');
 
                // We do a case independent comparison, like search_kw does.
                if (compare_ascii_no_case(token, endtoken) == 0)
@@ -807,8 +798,7 @@ string const Lexer::getLongString(string const & endtoken)
                        if (i != string::npos)
                                prefix = tmpstr.substr(0, i);
                        firstline = false;
-                       LYXERR(Debug::PARSER)
-                               << "Prefix = `" << prefix << "\'" << endl;
+                       LYXERR(Debug::PARSER, "Prefix = `" << prefix << "\'");
                }
 
                // further lines in long strings may have the same