From 2dd83864e55df57b055c53b09d08b03fe6501f06 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Thu, 7 Feb 2002 19:37:34 +0000 Subject: [PATCH] Herbert's unzip et al patch. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3502 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/controllers/ChangeLog | 7 +++ src/frontends/controllers/ControlGraphics.C | 8 +++- src/graphics/ChangeLog | 4 ++ src/graphics/GraphicsCacheItem.C | 12 +----- src/insets/ChangeLog | 6 +++ src/insets/insetgraphics.C | 46 +++++++++++--------- src/support/ChangeLog | 5 +++ src/support/filetools.C | 48 ++++++++++++++------- src/support/filetools.h | 3 ++ 9 files changed, 92 insertions(+), 47 deletions(-) diff --git a/src/frontends/controllers/ChangeLog b/src/frontends/controllers/ChangeLog index 2c9d1004ff..bc13a121d2 100644 --- a/src/frontends/controllers/ChangeLog +++ b/src/frontends/controllers/ChangeLog @@ -1,3 +1,10 @@ +2002-02-06 Herbert Voss + + * ControlGraphics.[C] (readBB): search also in + g'zipped files. If it is useful than PK and compress + should also be supported. use unzipFile() from + support/filetools + 2002-02-06 Jean-Marc Lasgouttes * ControlRef.C (getBufferName): new method to get a real buffer diff --git a/src/frontends/controllers/ControlGraphics.C b/src/frontends/controllers/ControlGraphics.C index 63aa81561b..5b94c7b66b 100644 --- a/src/frontends/controllers/ControlGraphics.C +++ b/src/frontends/controllers/ControlGraphics.C @@ -36,6 +36,7 @@ #include "helper_funcs.h" // for browseFile #include "support/lstrings.h" #include "support/filetools.h" // for AddName +#include "support/syscall.h" // for zippedFile() #include "BufferView.h" using std::pair; @@ -108,8 +109,11 @@ string const ControlGraphics::readBB(string const & file) // end of the file. Than we have in the header a // %%BoundingBox: (atend) // In this case we must check until the end. - std::ifstream is(file.c_str()); - if (!contains(getExtFromContents(file),"ps")) // bb exists? + string file_ = file; + if (zippedFile(file_)) + file_ = unzipFile(file_); + std::ifstream is(file_.c_str()); + if (!contains(getExtFromContents(file_),"ps")) // bb exists? return string(); while (is) { string s; diff --git a/src/graphics/ChangeLog b/src/graphics/ChangeLog index 2fb11db637..ab5dfdf2e4 100644 --- a/src/graphics/ChangeLog +++ b/src/graphics/ChangeLog @@ -1,3 +1,7 @@ +2002-02-07 Herbert Voss + + * GraphicsCacheItem.C: use unzipFile() from support/filetools + 2002-01-31 Herbert Voss * GraphicsCacheItem.h: handle filetype without the file extension; diff --git a/src/graphics/GraphicsCacheItem.C b/src/graphics/GraphicsCacheItem.C index 8c171714fb..918dfc2c67 100644 --- a/src/graphics/GraphicsCacheItem.C +++ b/src/graphics/GraphicsCacheItem.C @@ -26,7 +26,6 @@ #include "debug.h" #include "support/LAssert.h" #include "gettext.h" -#include "support/syscall.h" #include "lyxfunc.h" using std::endl; @@ -139,9 +138,9 @@ bool GraphicsCacheItem::convertImage(string const & filename) -----------snip-------------*/ lyxerr << "GetExtension: " << GetExtension(filename_) << endl; - bool zipped = GetExtension(filename_).compare("gz") == 0; + bool const zipped = zippedFile(filename_); if (zipped) - filename_ = ChangeExtension(filename_, string()); // snip the ".gz" + filename_ = unzipFile(filename_); string const from = getExtFromContents(filename_); // get the type lyxerr << "GetExtFromContents: " << from << endl; string const to = findTargetFormat(from); @@ -149,13 +148,6 @@ bool GraphicsCacheItem::convertImage(string const & filename) if (to.empty()) return false; // manage zipped files. unzip them first into the tempdir - if (zipped) { - tempfile = lyx::tempName(string(), filename_); - // Run gunzip - string const command = "gunzip -c "+filename+" > "+tempfile; - Systemcalls one(Systemcalls::System, command); - filename_ = tempfile; - } if (from == to) { // No conversion needed! // Saves more than just time: prevents the deletion of diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index a5ce8165cc..3448c7d2b0 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,9 @@ +2002-02-06 Herbert Voss + + * insetgraphic.C: prepareFile(): don't convert the file, when + it's not viewed in LyX. Saves a lot of time when opening a + document! + 2002-02-05 Herbert Voss * insetgraphic.C: diff --git a/src/insets/insetgraphics.C b/src/insets/insetgraphics.C index 819ba4b88f..f82aca7314 100644 --- a/src/insets/insetgraphics.C +++ b/src/insets/insetgraphics.C @@ -37,10 +37,6 @@ Known BUGS: its original size and color, resizing is done in the final output, but not in the LyX window. - * EPS figures are not fully detected, they may have a lot of possible - suffixes so we need to read the file and detect if it's EPS or not. - [Implemented, need testing] - TODO Before initial production release: * Replace insetfig everywhere * Search for comments of the form @@ -461,26 +457,26 @@ string const InsetGraphics::createLatexOptions() const // before writing it to the output stream. ostringstream options; if (!params.bb.empty()) - options << " bb=" << strip(params.bb) << ",%\n"; + options << " bb=" << strip(params.bb) << ",\n"; if (params.draft) - options << " draft,%\n"; + options << " draft,\n"; if (params.clip) - options << " clip,%\n"; + options << " clip,\n"; if (params.size_type == InsetGraphicsParams::WH) { if (!params.width.zero()) - options << " width=" << params.width.asLatexString() << ",%\n"; + options << " width=" << params.width.asLatexString() << ",\n"; if (!params.height.zero()) - options << " height=" << params.height.asLatexString() << ",%\n"; + options << " height=" << params.height.asLatexString() << ",\n"; } else if (params.size_type == InsetGraphicsParams::SCALE) { if (params.scale > 0) - options << " scale=" << double(params.scale)/100.0 << ",%\n"; + options << " scale=" << double(params.scale)/100.0 << ",\n"; } if (params.keepAspectRatio) - options << " keepaspectratio,%\n"; + options << " keepaspectratio,\n"; // Make sure it's not very close to zero, a float can be effectively // zero but not exactly zero. if (!lyx::float_equal(params.rotateAngle, 0, 0.001) && params.rotate) { - options << " angle=" << params.rotateAngle << ",%\n"; + options << " angle=" << params.rotateAngle << ",\n"; if (!params.rotateOrigin.empty()) { options << " origin=" << params.rotateOrigin[0]; if (contains(params.rotateOrigin,"Top")) @@ -489,14 +485,13 @@ string const InsetGraphics::createLatexOptions() const options << 'b'; else if (contains(params.rotateOrigin,"Baseline")) options << 'B'; - options << ",%\n"; + options << ",\n"; } } if (!params.special.empty()) - options << params.special << ",%\n"; + options << params.special << ",\n"; string opts = options.str().c_str(); - opts = opts.substr(0,opts.size()-3); // delete last ",%\n" - return opts; + return opts.substr(0,opts.size()-2); // delete last ",\n" } namespace { @@ -538,13 +533,24 @@ string const InsetGraphics::prepareFile(Buffer const *buf) const // convert_place = original file directory // return original filename without the extension // - // Get the extension (format) of the original file. - // we handle it like a virtual one, so we can have - // different extensions with the same type. + // first check if file is viewed in LyX. First local + // than global + if ((params.display == InsetGraphicsParams::NONE) || + ((params.display == InsetGraphicsParams::DEFAULT) && + (lyxrc.display_graphics == "no"))) { + lyxerr << "InsetGraphics::no converting of: " << params.filename << "\n"; + return params.filename; + } // if it's a zipped one, than let LaTeX do the rest!!! - if (zippedFile(params.filename)) + if (zippedFile(params.filename)) { + lyxerr << "InsetGraphics::prepareFilter(zippedFile): return " + << params.filename << endl; return params.filename; + } // now we have unzipped files + // Get the extension (format) of the original file. + // we handle it like a virtual one, so we can have + // different extensions with the same type. string const extension = getExtFromContents(params.filename); // Are we creating a PDF or a PS file? // (Should actually mean, are we usind latex or pdflatex). diff --git a/src/support/ChangeLog b/src/support/ChangeLog index 595209e1c8..9f3ce4b56d 100644 --- a/src/support/ChangeLog +++ b/src/support/ChangeLog @@ -1,3 +1,8 @@ +2002-02-06 Herbert Voss + + * filetools.[Ch]: fix sume bugs for detecting zipped files + adding unzipFile() + 2002-02-04 Herbert Voss * filetools.[Ch]: add ifZippedFile() for zipped diff --git a/src/support/filetools.C b/src/support/filetools.C index 644e98f809..8b7b9d9c17 100644 --- a/src/support/filetools.C +++ b/src/support/filetools.C @@ -33,6 +33,7 @@ #include #include "debug.h" #include "support/lstrings.h" +#include "support/syscall.h" #include "filetools.h" #include "LSubstring.h" @@ -970,13 +971,13 @@ string const GetExtension(string const & name) // JPG JFIF // PDF %PDF-... // PNG .PNG... -// PS %!PS-Adobe-2.0 +// PS %!PS-Adobe-2.0 or 1.0, no "EPSF"! // XBM ... static char ... // XPM /* XPM */ // -// GZIP \213\037\008\008... http://www.ietf.org/rfc/rfc1952.txt +// GZIP \037\213\010\010... http://www.ietf.org/rfc/rfc1952.txt // ZIP PK... http://www.halyava.ru/document/ind_arch.htm -// Z \177\037 UNIX compress +// Z \037\177 UNIX compress /// return the "extension" which belongs to the contents string const getExtFromContents(string const & filename) { if (filename.empty() || !IsFileReadable(filename)) @@ -984,16 +985,16 @@ string const getExtFromContents(string const & filename) { ifstream ifs(filename.c_str()); if (!ifs) return string(); // Couldn't open file... - string const gzipStamp = "\213\037\008\008"; // gnuzip + string const gzipStamp = "\037\213\010\010"; // gnuzip string const zipStamp = "PK"; // PKZIP - string const compressStamp = "\177\037"; // compress - int const max_count = 50; // Maximum strings to read to attempt recognition - int count = 0; // Counter of attempts. + string const compressStamp = "\037\177"; // compress + int const max_count = 50; // Maximum strings to read + int count = 0; // Counter of attempts. string str; bool zipChecked = false; for (; count < max_count; ++count) { if (ifs.eof()) { - lyxerr[Debug::INFO] << "InsetGraphics (classifyFiletype)" + lyxerr[Debug::INFO] << "filetools(getExtFromContents)" " End of file reached and it wasn't found to be a known Type!" << endl; break; } @@ -1009,8 +1010,8 @@ string const getExtFromContents(string const & filename) { return "compress"; zipChecked = true; } - if (contains(str,"EPSF")) - return "eps"; + if (contains(str,"EPSF")) // dummy, if we have wrong file + return "eps"; // description like "%!PS-Adobe-2.0EPSF" else if (contains(str,"GIF")) return "gif"; else if (contains(str,"JFIF")) @@ -1019,14 +1020,19 @@ string const getExtFromContents(string const & filename) { return "pdf"; else if (contains(str,"PNG")) return "png"; - else if (contains(str,"%!PS-Adobe-")) - return "ps"; // eps here no more possible - else if (contains(str,"static char")) + else if (contains(str,"%!PS-Adobe")) { // eps or ps + // test if it's ps or eps + ifs >> str; + if (contains(str,"EPSF")) + return "eps"; + else + return "ps"; + } else if (contains(str,"static char")) return "xbm"; else if (contains(str,"XPM")) return "xpm"; } - lyxerr[Debug::INFO] << "InsetGraphics (classifyFiletype)" + lyxerr[Debug::INFO] << "filetools(getExtFromContents)" " Couldn't find a known Type!" << endl; return string(); } @@ -1035,11 +1041,23 @@ string const getExtFromContents(string const & filename) { /// check for zipped file bool zippedFile(string const & name) { string const type = getExtFromContents(name); - if (contains("gzip zip",type) && !type.empty()) + if (contains("gzip zip compress",type) && !type.empty()) return true; return false; } +string const unzipFile(string const & zipped_file) +{ + string const file = ChangeExtension(zipped_file, string()); + string const tempfile = lyx::tempName(string(), file); + // Run gunzip + string const command = "gunzip -c "+file+" > "+tempfile; + Systemcalls one(Systemcalls::System, command); + // test that command was executed successfully + return tempfile; +} + + // Creates a nice compact path for displaying string const MakeDisplayPath (string const & path, unsigned int threshold) diff --git a/src/support/filetools.h b/src/support/filetools.h index 5299d8ad61..1c667a1414 100644 --- a/src/support/filetools.h +++ b/src/support/filetools.h @@ -138,6 +138,9 @@ string const getExtFromContents(string const & name); /// check for zipped file bool zippedFile(string const & name); +/// unzip a file +string const unzipFile(string const & zipped_file); + /// Returns true is path is absolute bool AbsolutePath(string const & path); -- 2.39.2