]> git.lyx.org Git - lyx.git/blobdiff - src/buffer.C
zlib stuff
[lyx.git] / src / buffer.C
index d6186200c23ef413d9cc79b0df9ea99c40e2a290..d242264a1f2a8156b4613febfdeee4c22dde0df5 100644 (file)
@@ -55,6 +55,7 @@
 
 #include "graphics/Previews.h"
 
+#include "support/LAssert.h"
 #include "support/textutils.h"
 #include "support/filetools.h"
 #include "support/path.h"
@@ -64,6 +65,7 @@
 #include "support/FileInfo.h"
 #include "support/lyxmanip.h"
 #include "support/lyxtime.h"
+#include "support/gzstream.h"
 
 #include <boost/bind.hpp>
 #include <boost/tuple/tuple.hpp>
@@ -114,7 +116,21 @@ extern BufferList bufferlist;
 
 namespace {
 
-const int LYX_FORMAT = 224;
+const int LYX_FORMAT = 225;
+
+bool openFileWrite(ofstream & ofs, string const & fname)
+{
+       ofs.open(fname.c_str());
+       if (!ofs) {
+               string const file = MakeDisplayPath(fname, 50);
+               string text = bformat(_("Could not open the specified "
+                                       "document\n%1$s."), file);
+               Alert::error(_("Could not open file"), text);
+               return false;
+       }
+       return true;
+}
+
 
 } // namespace anon
 
@@ -263,7 +279,7 @@ int Buffer::readHeader(LyXLex & lex)
                                                           "%1$s %2$s\n"),
                                                         token,
                                                         lex.getString());
-                               error(ErrorItem(_("Header error"), s, 
+                               error(ErrorItem(_("Header error"), s,
                                                -1, 0, 0));
                        }
                }
@@ -621,10 +637,28 @@ bool Buffer::writeFile(string const & fname) const
                return false;
        }
 
+       bool const compressed = (fname.substr(fname.size() - 3, 3) == ".gz");
+
+       if (compressed) {
+               gz::ogzstream ofs(fname.c_str());
+
+               if (!ofs)
+                       return false;
+
+               return do_writeFile(ofs);
+
+       }
+
        ofstream ofs(fname.c_str());
-       if (!ofs) {
+       if (!ofs)
                return false;
-       }
+
+       return do_writeFile(ofs);
+}
+
+
+bool Buffer::do_writeFile(ostream & ofs) const
+{
 
 #ifdef HAVE_LOCALE
        // Use the standard "C" locale for file output.
@@ -655,7 +689,8 @@ bool Buffer::writeFile(string const & fname) const
        // Write marker that shows file is complete
        ofs << "\n\\the_end" << endl;
 
-       ofs.close();
+       // Shouldn't really be needed....
+       //ofs.close();
 
        // how to check if close went ok?
        // Following is an attempt... (BE 20001011)
@@ -818,7 +853,7 @@ string const Buffer::asciiParagraph(Paragraph const & par,
                switch (c) {
                case Paragraph::META_INSET:
                {
-                       Inset const * inset = par.getInset(i);
+                       InsetOld const * inset = par.getInset(i);
                        if (inset) {
                                if (linelen > 0) {
                                        buffer << word;
@@ -875,13 +910,9 @@ string const Buffer::asciiParagraph(Paragraph const & par,
 
 void Buffer::writeFileAscii(string const & fname, int linelen)
 {
-       ofstream ofs(fname.c_str());
-       if (!ofs) {
-               string const file = MakeDisplayPath(fname, 50);
-               string text = bformat(_("Could not save the document\n%1$s."), file);
-               Alert::error(_("Could not save document"), text);
+       ofstream ofs;
+       if (!::openFileWrite(ofs, fname))
                return;
-       }
        writeFileAscii(ofs, linelen);
 }
 
@@ -898,25 +929,19 @@ void Buffer::writeFileAscii(ostream & os, int linelen)
 }
 
 
-
 void Buffer::makeLaTeXFile(string const & fname,
                           string const & original_path,
                           LatexRunParams const & runparams,
-                          bool only_body, bool only_preamble)
+                          bool output_preamble, bool output_body)
 {
        lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
 
-       ofstream ofs(fname.c_str());
-       if (!ofs) {
-               string const file = MakeDisplayPath(fname, 50);
-               string text = bformat(_("Could not open the specified document\n%1$s."),
-                       file);
-               Alert::error(_("Could not open file"), text);
+       ofstream ofs;
+       if (!::openFileWrite(ofs, fname))
                return;
-       }
 
        makeLaTeXFile(ofs, original_path,
-                     runparams, only_body, only_preamble);
+                     runparams, output_preamble, output_body);
 
        ofs.close();
        if (ofs.fail()) {
@@ -928,7 +953,7 @@ void Buffer::makeLaTeXFile(string const & fname,
 void Buffer::makeLaTeXFile(ostream & os,
                           string const & original_path,
                           LatexRunParams const & runparams_in,
-                          bool only_body, bool only_preamble)
+                          bool output_preamble, bool output_body)
 {
        LatexRunParams runparams = runparams_in;
        niceFile = runparams.nice; // this will be used by Insetincludes.
@@ -944,7 +969,7 @@ void Buffer::makeLaTeXFile(ostream & os,
        // first paragraph of the document. (Asger)
        texrow.start(paragraphs.begin()->id(), 0);
 
-       if (!only_body && runparams.nice) {
+       if (output_preamble && runparams.nice) {
                os << "%% " << lyx_docversion << " created this file.  "
                        "For more info, see http://www.lyx.org/.\n"
                        "%% Do not edit unless you really know what "
@@ -960,7 +985,7 @@ void Buffer::makeLaTeXFile(ostream & os,
        // input@path is set when the actual parameter
        // original_path is set. This is done for usual tex-file, but not
        // for nice-latex-file. (Matthias 250696)
-       if (!only_body) {
+       if (output_preamble) {
                if (!runparams.nice) {
                        // code for usual, NOT nice-latex-file
                        os << "\\batchmode\n"; // changed
@@ -982,13 +1007,13 @@ void Buffer::makeLaTeXFile(ostream & os,
                // Write the preamble
                runparams.use_babel = params.writeLaTeX(os, features, texrow);
 
-               if (only_preamble)
+               if (!output_body)
                        return;
 
                // make the body.
                os << "\\begin{document}\n";
                texrow.newline();
-       } // only_body
+       } // output_preamble
        lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
 
        if (!lyxrc.language_auto_begin) {
@@ -1011,7 +1036,7 @@ void Buffer::makeLaTeXFile(ostream & os,
                texrow.newline();
        }
 
-       if (!only_body) {
+       if (output_preamble) {
                os << "\\end{document}\n";
                texrow.newline();
 
@@ -1068,15 +1093,9 @@ bool Buffer::isSGML() const
 
 void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
 {
-       ofstream ofs(fname.c_str());
-
-       if (!ofs) {
-               string const file = MakeDisplayPath(fname, 50);
-               string text = bformat(_("Could not save the specified document\n%1$s.\n"),
-                       file);
-               Alert::error(_("Could not save document"), text);
+       ofstream ofs;
+       if (!::openFileWrite(ofs, fname))
                return;
-       }
 
        niceFile = nice; // this will be used by included files.
 
@@ -1128,9 +1147,9 @@ void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
                LyXLayout_ptr const & style = pit->layout();
                // treat <toc> as a special case for compatibility with old code
                if (pit->isInset(0)) {
-                       Inset * inset = pit->getInset(0);
-                       Inset::Code lyx_code = inset->lyxCode();
-                       if (lyx_code == Inset::TOC_CODE) {
+                       InsetOld * inset = pit->getInset(0);
+                       InsetOld::Code lyx_code = inset->lyxCode();
+                       if (lyx_code == InsetOld::TOC_CODE) {
                                string const temp = "toc";
                                sgml::openTag(ofs, depth, false, temp);
                                continue;
@@ -1452,7 +1471,7 @@ void Buffer::simpleLinuxDocOnePar(ostream & os,
                char c = par->getChar(i);
 
                if (c == Paragraph::META_INSET) {
-                       Inset * inset = par->getInset(i);
+                       InsetOld * inset = par->getInset(i);
                        inset->linuxdoc(this, os);
                        font_old = font;
                        continue;
@@ -1504,14 +1523,9 @@ void Buffer::simpleLinuxDocOnePar(ostream & os,
 
 void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
 {
-       ofstream ofs(fname.c_str());
-       if (!ofs) {
-               string const file = MakeDisplayPath(fname, 50);
-               string text = bformat(_("Could not save the specified document\n%1$s.\n"),
-                       file);
-               Alert::error(_("Could not save document"), text);
+       ofstream ofs;
+       if (!::openFileWrite(ofs, fname))
                return;
-       }
 
        niceFile = nice; // this will be used by Insetincludes.
 
@@ -1654,9 +1668,9 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
                        // This is a hack while paragraphs can't have
                        // attributes, like id in this case.
                        if (par->isInset(0)) {
-                               Inset * inset = par->getInset(0);
-                               Inset::Code lyx_code = inset->lyxCode();
-                               if (lyx_code == Inset::LABEL_CODE) {
+                               InsetOld * inset = par->getInset(0);
+                               InsetOld::Code lyx_code = inset->lyxCode();
+                               if (lyx_code == InsetOld::LABEL_CODE) {
                                        command_name += " id=\"";
                                        command_name += (static_cast<InsetCommand *>(inset))->getContents();
                                        command_name += '"';
@@ -1823,7 +1837,7 @@ void Buffer::simpleDocBookOnePar(ostream & os,
 
 
                if (par->isInset(i)) {
-                       Inset * inset = par->getInset(i);
+                       InsetOld * inset = par->getInset(i);
                        // don't print the inset in position 0 if desc_on == 3 (label)
                        if (i || desc_on != 3) {
                                if (style->latexparam() == "CDATA")
@@ -1995,11 +2009,11 @@ void Buffer::fillWithBibKeys(std::vector<std::pair<string, string> > & keys) con
 
        for (inset_iterator it = inset_const_iterator_begin();
                it != inset_const_iterator_end(); ++it) {
-               if (it->lyxCode() == Inset::BIBTEX_CODE)
+               if (it->lyxCode() == InsetOld::BIBTEX_CODE)
                        static_cast<InsetBibtex &>(*it).fillWithBibKeys(this, keys);
-               else if (it->lyxCode() == Inset::INCLUDE_CODE)
+               else if (it->lyxCode() == InsetOld::INCLUDE_CODE)
                        static_cast<InsetInclude &>(*it).fillWithBibKeys(keys);
-               else if (it->lyxCode() == Inset::BIBITEM_CODE) {
+               else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
                        InsetBibitem & bib = static_cast<InsetBibitem &>(*it);
                        string const key = bib.getContents();
                        string const opt = bib.getOptions();
@@ -2097,14 +2111,14 @@ void Buffer::inset_iterator::setParagraph()
 }
 
 
-Inset * Buffer::getInsetFromID(int id_arg) const
+InsetOld * Buffer::getInsetFromID(int id_arg) const
 {
        for (inset_iterator it = inset_const_iterator_begin();
                 it != inset_const_iterator_end(); ++it)
        {
                if (it->id() == id_arg)
                        return &(*it);
-               Inset * in = it->getInsetFromID(id_arg);
+               InsetOld * in = it->getInsetFromID(id_arg);
                if (in)
                        return in;
        }