]> git.lyx.org Git - features.git/commitdiff
Fix some zlib issues, use a temp file for lyx2lyx conversion.
authorLars Gullik Bjønnes <larsbj@gullik.org>
Tue, 2 Sep 2003 08:26:20 +0000 (08:26 +0000)
committerLars Gullik Bjønnes <larsbj@gullik.org>
Tue, 2 Sep 2003 08:26:20 +0000 (08:26 +0000)
Simplify some parts of file loading and insertion.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7631 a592a061-630c-0410-9148-cb99ea01b6c8

po/POTFILES.in
src/BufferView.C
src/ChangeLog
src/buffer.C
src/buffer.h
src/frontends/Alert.C
src/frontends/ChangeLog
src/lyxcursor.h

index 8e758eff0c131c2d0abc379b2da3a2c086f6a9b2..4942f1d64fefe58fa02133519e3c5f3e0785a943 100644 (file)
@@ -183,7 +183,6 @@ src/mathed/ref_inset.C
 src/paragraph.C
 src/paragraph_funcs.C
 src/rowpainter.C
-src/support/path_defines.C
 src/text.C
 src/text2.C
 src/text3.C
index 9524b2740ec7f1a1684789356e9132febeb4e2d0..9fe4f9726ee98da96df918f27f42997b4ea97b45 100644 (file)
@@ -47,6 +47,7 @@
 #include "support/filetools.h"
 #include "support/types.h"
 #include "support/lyxalgo.h" // lyx_count
+#include "support/LAssert.h"
 
 #include <fstream>
 
@@ -271,51 +272,15 @@ bool BufferView::insertLyXFile(string const & filen)
        //
        // Moved from lyx_cb.C (Lgb)
 {
-       if (filen.empty())
-               return false;
+       Assert(!filen.empty());
 
        string const fname = MakeAbsPath(filen);
 
-       // check if file exist
-       FileInfo const fi(fname);
-
-       if (!fi.readable()) {
-               string const file = MakeDisplayPath(fname, 50);
-               string const text =
-                       bformat(_("The specified document\n%1$s\ncould not be read."), file);
-               Alert::error(_("Could not read document"), text);
-               return false;
-       }
-
        beforeChange(text);
 
-       ifstream ifs(fname.c_str());
-       if (!ifs) {
-               string const file = MakeDisplayPath(fname, 50);
-               string const text =
-                       bformat(_("Could not open the specified document %1$s\n"), file);
-               Alert::error(_("Could not open file"), text);
-               return false;
-       }
-
-       int const c = ifs.peek();
-
-       LyXLex lex(0, 0);
-       lex.setStream(ifs);
-
-       bool res = true;
-
        text->breakParagraph(buffer()->paragraphs);
 
-       if (c == '#') {
-               // FIXME: huh ? No we won't !
-               lyxerr[Debug::INFO] << "Will insert file with header" << endl;
-               res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
-       } else {
-               lyxerr[Debug::INFO] << "Will insert file without header"
-                                   << endl;
-               res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
-       }
+       bool res = buffer()->readFile(fname, text->cursor.par());
 
        resize();
        return res;
index 5928b688cd72b6ab909aae30454de7847c118379..b987bf865133abd3f4408fafe14e7ffd3e953630 100644 (file)
@@ -1,3 +1,15 @@
+2003-09-02  Lars Gullik Bjønnes  <larsbj@gullik.net>
+
+       * buffer.C (readFile): new function, take a filename and a
+       ParagraphList::iterator
+       (readFile): adjust
+       (readFile): adjust, make it private. don't use setStream, make
+       lyx2lyx use a temporary file. Some simplificaton. Make the Alerts
+       always contain the filename. 
+
+       * BufferView.C (insertLyXFile): simplify and make it work for
+       gzipped files.
+
 2003-08-30  John Levon  <levon@movementarian.org>
 
        * Makefile.am: fix dist (from Kayvan)
index 7649cb69eca5f683bac2faaa4c67ec03e4a07f94..9b96f7ac7293df6efbf058cb5b4ff423425f22c6 100644 (file)
@@ -448,10 +448,7 @@ bool Buffer::readFile(string const & filename)
                params.compressed = true;
        }
 
-       LyXLex lex(0, 0);
-       lex.setFile(filename);
-
-       bool ret = readFile(lex, filename, paragraphs.begin());
+       bool ret = readFile(filename, paragraphs.begin());
 
        // After we have read a file, we must ensure that the buffer
        // language is set and used in the gui.
@@ -462,13 +459,23 @@ bool Buffer::readFile(string const & filename)
 }
 
 
-// FIXME: all the below Alerts should give the filename..
+bool Buffer::readFile(string const & filename, ParagraphList::iterator pit)
+{
+       LyXLex lex(0, 0);
+       lex.setFile(filename);
+
+       return readFile(lex, filename, pit);
+}
+
+
 bool Buffer::readFile(LyXLex & lex, string const & filename,
                      ParagraphList::iterator pit)
 {
+       Assert(!filename.empty());
+
        if (!lex.isOK()) {
                Alert::error(_("Document could not be read"),
-                       _("The specified document could not be read."));
+                            bformat(_("%1$s could not be read."), filename));
                return false;
        }
 
@@ -477,7 +484,7 @@ bool Buffer::readFile(LyXLex & lex, string const & filename,
 
        if (!lex.isOK()) {
                Alert::error(_("Document could not be read"),
-                       _("The specified document could not be read."));
+                            bformat(_("%1$s could not be read."), filename));
                return false;
        }
 
@@ -486,7 +493,8 @@ bool Buffer::readFile(LyXLex & lex, string const & filename,
                lyxerr << "Token: " << token << endl;
 
                Alert::error(_("Document format failure"),
-                       _("The specified document is not a LyX document."));
+                            bformat(_("%1$s is not a LyX document."),
+                                      filename));
                return false;
        }
 
@@ -500,62 +508,56 @@ bool Buffer::readFile(LyXLex & lex, string const & filename,
                        tmp_format.erase(dot, 1);
        int file_format = strToInt(tmp_format);
        //lyxerr << "format: " << file_format << endl;
-       if (file_format == LYX_FORMAT) {
-               // current format
-       } else if (file_format > LYX_FORMAT) {
+
+       if (file_format > LYX_FORMAT) {
                Alert::warning(_("Document format failure"),
-                       _("This document was created with a newer version of "
-                       "LyX. This is likely to cause problems."));
+                              bformat(_("%1$swas created with a newer version"
+                                        " of LyX. This is likely to cause"
+                                        " problems."),
+                                        filename));
        } else if (file_format < LYX_FORMAT) {
-               // old formats
-               if (file_format < 200) {
-                       Alert::error(_("Document format failure"),
-                               _("This LyX document is too old to be read "
-                               "by this version of LyX. Try LyX 0.10."));
+               string const tmpfile = tempName();
+               string command = LibFileSearch("lyx2lyx", "lyx2lyx");
+               if (command.empty()) {
+                       Alert::error(_("Conversion script not found"),
+                                    bformat(_("%1$s is from an earlier"
+                                              " version of LyX, but the"
+                                              " conversion script lyx2lyx"
+                                              " could not be found."),
+                                              filename));
                        return false;
-               } else if (!filename.empty()) {
-                       string command =
-                               LibFileSearch("lyx2lyx", "lyx2lyx");
-                       if (command.empty()) {
-                               Alert::error(_("Conversion script not found"),
-                                       _("The document is from an earlier version "
-                                         "of LyX, but the conversion script lyx2lyx "
-                                         "could not be found."));
-                               return false;
-                       }
-                       command += " -t"
-                               + tostr(LYX_FORMAT) + ' '
-                               + QuoteName(filename);
-                       lyxerr[Debug::INFO] << "Running '"
-                                           << command << '\''
-                                           << endl;
-                       cmd_ret const ret = RunCommand(command);
-                       if (ret.first) {
-                               Alert::error(_("Conversion script failed"),
-                                       _("The document is from an earlier version "
-                                         "of LyX, but the lyx2lyx script failed "
-                                         "to convert it."));
-                               return false;
-                       }
-                       istringstream is(STRCONV(ret.second));
-                       LyXLex tmplex(0, 0);
-                       tmplex.setStream(is);
-                       return readFile(tmplex, string(), pit);
-               } else {
-                       // This code is reached if lyx2lyx failed (for
-                       // some reason) to change the file format of
-                       // the file.
-                       Assert(false);
+               }
+               command += " -t"
+                       + tostr(LYX_FORMAT)
+                       + " -o " + tmpfile + ' '
+                       + QuoteName(filename);
+               lyxerr[Debug::INFO] << "Running '"
+                                   << command << '\''
+                                   << endl;
+               cmd_ret const ret = RunCommand(command);
+               if (ret.first != 0) {
+                       Alert::error(_("Conversion script failed"),
+                                    bformat(_("%1$s is from an earlier version"
+                                             " of LyX, but the lyx2lyx script"
+                                             " failed to convert it."),
+                                             filename));
                        return false;
+               } else {
+                       bool ret = readFile(tmpfile, pit);
+                       // Do stuff with tmpfile name and buffer name here.
+                       return ret;
                }
+
        }
+
        bool the_end = readBody(lex, pit);
        params.setPaperStuff();
 
        if (!the_end) {
                Alert::error(_("Document format failure"),
-                       _("The document ended unexpectedly, which means "
-                         "that it is probably corrupted."));
+                            bformat(_("%1$s ended unexpectedly, which means"
+                                      " that it is probably corrupted."),
+                                      filename));
        }
        return true;
 }
index 16af8382b780046ca2a9383807aa5c4de90e845c..cde82c260d6271772cbb9caa9e7064753bd84146 100644 (file)
@@ -72,15 +72,19 @@ public:
        /// Load the autosaved file.
        void loadAutoSaveFile();
 
+private:
        /** Inserts a file into a document
            \param par if != 0 insert the file.
            \return \c false if method fails.
        */
-       bool readFile(LyXLex &, string const &, ParagraphList::iterator pit);
+       bool readFile(LyXLex &, string const & filename,
+                     ParagraphList::iterator pit);
 
-       // FIXME: it's very silly to pass a lex in here
+public:
        /// load a new file
-       bool readFile(string const &);
+       bool readFile(string const & filename);
+
+       bool readFile(string const & filename, ParagraphList::iterator pit);
 
        /// read the header, returns number of unknown tokens
        int readHeader(LyXLex & lex);
index 984abfab875f14bd451d9dbc7204e8d0609895e3..1806e8dc90839ccb79ef3c3aa160aa460eaa0346 100644 (file)
@@ -25,66 +25,73 @@ int Alert::prompt(string const & title, string const & question,
            int default_button, int escape_button,
           string const & b1, string const & b2, string const & b3)
 {
-       if (lyx_gui::use_gui)
-               return prompt_pimpl(title, question,
-                       default_button, escape_button, b1, b2, b3);
-
-       lyxerr << title << endl;
-       lyxerr << "----------------------------------------" << endl;
-       lyxerr << question << endl;
-       lyxerr << "Assuming answer is ";
-       switch (default_button) {
+       if (!lyx_gui::use_gui || lyxerr.debugging()) {
+               lyxerr << title
+                      << "----------------------------------------"
+                      << question << endl;
+
+               lyxerr << "Assuming answer is ";
+               switch (default_button) {
                case 0: lyxerr << b1 << endl;
                case 1: lyxerr << b2 << endl;
                case 2: lyxerr << b3 << endl;
+               }
+               if (!lyx_gui::use_gui)
+                       return default_button;
        }
-       return default_button;
+
+       return prompt_pimpl(title, question,
+                           default_button, escape_button, b1, b2, b3);
+
 }
 
 
 void Alert::warning(string const & title, string const & message)
 {
+       if (!lyx_gui::use_gui || lyxerr.debugging())
+               lyxerr << "Warning: " << title
+                      << "----------------------------------------"
+                      << message << endl;
        if (lyx_gui::use_gui)
-               return warning_pimpl(title, message);
-
-       lyxerr << "Warning: " << title << endl;
-       lyxerr << "----------------------------------------" << endl;
-       lyxerr << message << endl;
+               warning_pimpl(title, message);
 }
 
 
 void Alert::error(string const & title, string const & message)
 {
-       if (lyx_gui::use_gui)
-               return error_pimpl(title, message);
+       if (!lyx_gui::use_gui || lyxerr.debugging())
+               lyxerr << "Error: " << title << '\n'
+                      << "----------------------------------------\n"
+                      << message << endl;
 
-       lyxerr << "Error: " << title << endl;
-       lyxerr << "----------------------------------------" << endl;
-       lyxerr << message << endl;
+       if (lyx_gui::use_gui)
+               error_pimpl(title, message);
 }
 
 
 void Alert::information(string const & title, string const & message)
 {
-       if (lyx_gui::use_gui)
-               return information_pimpl(title, message);
+       if (!lyx_gui::use_gui || lyxerr.debugging())
+               lyxerr << title
+                      << "----------------------------------------"
+                      << message << endl;
 
-       lyxerr << title << endl;
-       lyxerr << "----------------------------------------" << endl;
-       lyxerr << message << endl;
+       if (lyx_gui::use_gui)
+               information_pimpl(title, message);
 }
 
 
 pair<bool, string> const Alert::askForText(string const & msg,
                                           string const & dflt)
 {
-       if (!lyx_gui::use_gui) {
-               lyxerr << "----------------------------------------" << endl
-                      << msg << endl
-                      << "Assuming answer is " << dflt
+       if (!lyx_gui::use_gui || lyxerr.debugging()) {
+               lyxerr << "----------------------------------------\n"
+                      << msg << '\n'
+                      << "Assuming answer is " << dflt << '\n'
                       << "----------------------------------------" << endl;
-               return make_pair<bool, string>(true, dflt);
-       } else {
-               return askForText_pimpl(msg, dflt);
+               if (!lyx_gui::use_gui)
+                       return make_pair<bool, string>(true, dflt);
        }
+
+       return askForText_pimpl(msg, dflt);
 }
index 48e19e3f83f7593f3380427c9f2da2f2fb569514..6d4810f1ef71801e567f4309b1a6db5325ff9509 100644 (file)
@@ -1,3 +1,8 @@
+2003-09-02  Lars Gullik Bjønnes  <larsbj@gullik.net>
+
+       * Alert.C (prompt, warning, error, information, askForText): Make
+       them output the alert to stderr if debugging is turned on.
+
 2003-08-27  John Levon  <levon@movementarian.org>
 
        * screen.C: fix cursor in ERT (bug 1341)
@@ -38,7 +43,7 @@
        * Menubar.h: do not use a pimpl, but rather an abstract class
 
        * Menubar.C: removed
-       
+
 2003-07-25  Jean-Marc Lasgouttes  <lasgouttes@lyx.org>
 
        * Toolbar.h: do not use a pimpl, but rather an abstract class
index 7518ef0f4aa0b2e03205c162d83c2e44e7b0379c..c9bbb9c532f4e84baaa79da01ca79994d9d6cfa5 100644 (file)
@@ -118,7 +118,7 @@ private:
        int iy_;
 };
 
-/// 
+///
 bool operator==(LyXCursor const & a, LyXCursor const & b);
 ///
 bool operator!=(LyXCursor const & a, LyXCursor const & b);