make FileName::isZipped more efficient by caching previous results

* src/insets/insetgraphics.C
	(copyToDirIfNeeded): replace file_in and zipped arguments with a
	FileName argument
	(InsetGraphics::prepareFile): adjust call of copyToDirIfNeeded

	* src/support/filename.C
	(FileName::FileName): set zipped_valid_
	(FileName::set): ditto
	(FileName::erase): ditto
	(isZipped): use zipped_

	* src/support/filename.[Ch]
	(zipped_): new cache for isZipped()
	(zipped_valid_): new, tell whether zipped_ is valid


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@14376 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Georg Baum 2006-07-08 14:16:56 +00:00
parent a7fe209a13
commit 82a4214f7b
3 changed files with 22 additions and 15 deletions

View File

@ -484,25 +484,24 @@ copyFileIfNeeded(string const & file_in, string const & file_out)
std::pair<CopyStatus, string> const std::pair<CopyStatus, string> const
copyToDirIfNeeded(string const & file_in, string const & dir, bool zipped) copyToDirIfNeeded(FileName const & file, string const & dir)
{ {
using support::rtrim; using support::rtrim;
BOOST_ASSERT(absolutePath(file_in)); string const file_in = file.absFilename();
string const only_path = support::onlyPath(file_in); string const only_path = support::onlyPath(file_in);
if (rtrim(support::onlyPath(file_in) , "/") == rtrim(dir, "/")) if (rtrim(support::onlyPath(file_in) , "/") == rtrim(dir, "/"))
return std::make_pair(IDENTICAL_PATHS, file_in); return std::make_pair(IDENTICAL_PATHS, file_in);
string mangled = FileName(file_in).mangledFilename(); string mangled = file.mangledFilename();
if (zipped) { if (file.isZipped()) {
// We need to change _eps.gz to .eps.gz. The mangled name is // We need to change _eps.gz to .eps.gz. The mangled name is
// still unique because of the counter in mangledFilename(). // still unique because of the counter in mangledFilename().
// We can't just call mangledFilename() with the zip // We can't just call mangledFilename() with the zip
// extension removed, because base.eps and base.eps.gz may // extension removed, because base.eps and base.eps.gz may
// have different content but would get the same mangled // have different content but would get the same mangled
// name in this case. // name in this case.
string const base = removeExtension(unzippedFileName(file_in)); string const base = removeExtension(file.unzippedFilename());
string::size_type const ext_len = file_in.length() - base.length(); string::size_type const ext_len = file_in.length() - base.length();
mangled[mangled.length() - ext_len] = '.'; mangled[mangled.length() - ext_len] = '.';
} }
@ -563,11 +562,6 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
if (runparams.dryrun) if (runparams.dryrun)
return stripExtensionIfPossible(rel_file); return stripExtensionIfPossible(rel_file);
// If the file is compressed and we have specified that it
// should not be uncompressed, then just return its name and
// let LaTeX do the rest!
bool const zipped = params().filename.isZipped();
// temp_file will contain the file for LaTeX to act on if, for example, // temp_file will contain the file for LaTeX to act on if, for example,
// we move it to a temp dir or uncompress it. // we move it to a temp dir or uncompress it.
string temp_file = orig_file; string temp_file = orig_file;
@ -590,7 +584,7 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
CopyStatus status; CopyStatus status;
boost::tie(status, temp_file) = boost::tie(status, temp_file) =
copyToDirIfNeeded(orig_file, temp_path, zipped); copyToDirIfNeeded(params().filename, temp_path);
if (status == FAILURE) if (status == FAILURE)
return orig_file; return orig_file;
@ -606,7 +600,10 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
string const tex_format = (runparams.flavor == OutputParams::LATEX) ? string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
"latex" : "pdflatex"; "latex" : "pdflatex";
if (zipped) { // If the file is compressed and we have specified that it
// should not be uncompressed, then just return its name and
// let LaTeX do the rest!
if (params().filename.isZipped()) {
if (params().noUnzip) { if (params().noUnzip) {
// We don't know whether latex can actually handle // We don't know whether latex can actually handle
// this file, but we can't check, because that would // this file, but we can't check, because that would

View File

@ -36,7 +36,7 @@ FileName::FileName()
FileName::FileName(string const & abs_filename, bool save_abs) FileName::FileName(string const & abs_filename, bool save_abs)
: name_(abs_filename), save_abs_path_(save_abs) : name_(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
{ {
BOOST_ASSERT(absolutePath(name_)); BOOST_ASSERT(absolutePath(name_));
} }
@ -46,12 +46,14 @@ void FileName::set(string const & name, string const & buffer_path)
{ {
save_abs_path_ = absolutePath(name); save_abs_path_ = absolutePath(name);
name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path); name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path);
zipped_valid_ = false;
} }
void FileName::erase() void FileName::erase()
{ {
name_.erase(); name_.erase();
zipped_valid_ = false;
} }
@ -125,7 +127,11 @@ string const FileName::mangledFilename(std::string const & dir) const
bool FileName::isZipped() const bool FileName::isZipped() const
{ {
return zippedFile(name_); if (!zipped_valid_) {
zipped_ = zippedFile(name_);
zipped_valid_ = true;
}
return zipped_;
} }

View File

@ -75,6 +75,10 @@ public:
private: private:
std::string name_; std::string name_;
bool save_abs_path_; bool save_abs_path_;
/// Cache for isZipped() because zippedFile() is expensive
mutable bool zipped_;
/// Is zipped_ valid?
mutable bool zipped_valid_;
}; };