Herbert's unzip et al patch.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3502 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Angus Leeming 2002-02-07 19:37:34 +00:00
parent 6b5a92d3d6
commit 2dd83864e5
9 changed files with 92 additions and 47 deletions

View File

@ -1,3 +1,10 @@
2002-02-06 Herbert Voss <voss@lyx.org>
* 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 <lasgouttes@freesurf.fr>
* ControlRef.C (getBufferName): new method to get a real buffer

View File

@ -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;

View File

@ -1,3 +1,7 @@
2002-02-07 Herbert Voss <voss@lyx.org>
* GraphicsCacheItem.C: use unzipFile() from support/filetools
2002-01-31 Herbert Voss <voss@lyx.org>
* GraphicsCacheItem.h: handle filetype without the file extension;

View File

@ -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

View File

@ -1,3 +1,9 @@
2002-02-06 Herbert Voss <voss@lyx.org>
* 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 <voss@lyx.org>
* insetgraphic.C:

View File

@ -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
//
// 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)) {
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.
// if it's a zipped one, than let LaTeX do the rest!!!
if (zippedFile(params.filename))
return params.filename;
// now we have unzipped files
string const extension = getExtFromContents(params.filename);
// Are we creating a PDF or a PS file?
// (Should actually mean, are we usind latex or pdflatex).

View File

@ -1,3 +1,8 @@
2002-02-06 Herbert Voss <voss@perce.de>
* filetools.[Ch]: fix sume bugs for detecting zipped files
adding unzipFile()
2002-02-04 Herbert Voss <voss@perce.de>
* filetools.[Ch]: add ifZippedFile() for zipped

View File

@ -33,6 +33,7 @@
#include <cerrno>
#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)

View File

@ -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);