From 34a773b8cfe22ed278d101a13ad554a61fda526b Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Sat, 18 Nov 2006 12:49:47 +0000 Subject: [PATCH] Split FileName into FileName and DocFileName. FileName will be used for all file names, and DocFileName contains the special extensions for documents, included figures etc. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@15965 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/insets/insetbibtex.C | 6 ++-- src/insets/insetexternal.h | 2 +- src/insets/insetgraphics.C | 4 +-- src/insets/insetgraphicsParams.h | 2 +- src/insets/insetinclude.C | 8 +++--- src/support/filename.C | 47 +++++++++++++++++++++++--------- src/support/filename.h | 47 +++++++++++++++++++++++++++----- 7 files changed, 85 insertions(+), 31 deletions(-) diff --git a/src/insets/insetbibtex.C b/src/insets/insetbibtex.C index 5c59adf702..d0f8ff7d66 100644 --- a/src/insets/insetbibtex.C +++ b/src/insets/insetbibtex.C @@ -44,7 +44,7 @@ using support::ascii_lowercase; using support::changeExtension; using support::contains; using support::copy; -using support::FileName; +using support::DocFileName; using support::findtexfile; using support::isFileReadable; using support::latex_path; @@ -175,7 +175,7 @@ int InsetBibtex::latex(Buffer const & buffer, odocstream & os, isFileReadable(in_file)) { // mangledFilename() needs the extension - database = removeExtension(FileName(in_file).mangledFilename()); + database = removeExtension(DocFileName(in_file).mangledFilename()); string const out_file = makeAbsPath(database + ".bib", buffer.getMasterBuffer()->temppath()); @@ -231,7 +231,7 @@ int InsetBibtex::latex(Buffer const & buffer, odocstream & os, isFileReadable(in_file)) { // use new style name base = removeExtension( - FileName(in_file).mangledFilename()); + DocFileName(in_file).mangledFilename()); string const out_file = makeAbsPath(base + ".bst", buffer.getMasterBuffer()->temppath()); bool const success = copy(in_file, out_file); diff --git a/src/insets/insetexternal.h b/src/insets/insetexternal.h index 1ae2dcf2de..0ca0a06fd2 100644 --- a/src/insets/insetexternal.h +++ b/src/insets/insetexternal.h @@ -79,7 +79,7 @@ public: std::string const & templatename() const { return templatename_; } /// The external file. - support::FileName filename; + support::DocFileName filename; /// How the inset is to be displayed by LyX. external::DisplayType display; /// The scale of the displayed graphic (if shown). diff --git a/src/insets/insetgraphics.C b/src/insets/insetgraphics.C index 16d12a2094..13e457b702 100644 --- a/src/insets/insetgraphics.C +++ b/src/insets/insetgraphics.C @@ -95,7 +95,7 @@ using support::bformat; using support::changeExtension; using support::compare_timestamps; using support::contains; -using support::FileName; +using support::DocFileName; using support::float_equal; using support::getExtension; using support::isFileReadable; @@ -483,7 +483,7 @@ copyFileIfNeeded(string const & file_in, string const & file_out) std::pair const -copyToDirIfNeeded(FileName const & file, string const & dir) +copyToDirIfNeeded(DocFileName const & file, string const & dir) { using support::rtrim; diff --git a/src/insets/insetgraphicsParams.h b/src/insets/insetgraphicsParams.h index f8ee53d462..8b2a628424 100644 --- a/src/insets/insetgraphicsParams.h +++ b/src/insets/insetgraphicsParams.h @@ -30,7 +30,7 @@ class InsetGraphicsParams { public: /// Image filename. - support::FileName filename; + support::DocFileName filename; /// Scaling the Screen inside Lyx unsigned int lyxscale; /// How to display the image inside LyX diff --git a/src/insets/insetinclude.C b/src/insets/insetinclude.C index aa57fb7831..f0954516f4 100644 --- a/src/insets/insetinclude.C +++ b/src/insets/insetinclude.C @@ -59,7 +59,7 @@ using support::bformat; using support::changeExtension; using support::contains; using support::copy; -using support::FileName; +using support::DocFileName; using support::getFileContents; using support::isFileReadable; using support::isLyXFilename; @@ -390,7 +390,7 @@ int InsetInclude::latex(Buffer const & buffer, odocstream & os, // write it to a file (so far the complete file) string const exportfile = changeExtension(incfile, ".tex"); - string const mangled = FileName(changeExtension(included_file, + string const mangled = DocFileName(changeExtension(included_file, ".tex")).mangledFilename(); string const writefile = makeAbsPath(mangled, m_buffer->temppath()); @@ -524,7 +524,7 @@ int InsetInclude::docbook(Buffer const & buffer, odocstream & os, if (loadIfNeeded(buffer, params_)) { Buffer * tmp = theBufferList().getBuffer(included_file); - string const mangled = FileName(writefile).mangledFilename(); + string const mangled = DocFileName(writefile).mangledFilename(); writefile = makeAbsPath(mangled, buffer.getMasterBuffer()->temppath()); if (!runparams.nice) @@ -568,7 +568,7 @@ void InsetInclude::validate(LaTeXFeatures & features) const writefile = included_file; if (!features.runparams().nice && !isVerbatim(params_)) { - incfile = FileName(writefile).mangledFilename(); + incfile = DocFileName(writefile).mangledFilename(); writefile = makeAbsPath(incfile, buffer.getMasterBuffer()->temppath()); } diff --git a/src/support/filename.C b/src/support/filename.C index 609c93b258..63c15df3fe 100644 --- a/src/support/filename.C +++ b/src/support/filename.C @@ -31,18 +31,39 @@ namespace support { FileName::FileName() - : save_abs_path_(true) {} -FileName::FileName(string const & abs_filename, bool save_abs) - : name_(abs_filename), save_abs_path_(save_abs), zipped_valid_(false) +FileName::FileName(string const & abs_filename) + : name_(abs_filename) { BOOST_ASSERT(absolutePath(name_)); } -void FileName::set(string const & name, string const & buffer_path) +bool operator==(FileName const & lhs, FileName const & rhs) +{ + return lhs.absFilename() == rhs.absFilename(); +} + + +bool operator!=(FileName const & lhs, FileName const & rhs) +{ + return lhs.absFilename() != rhs.absFilename(); +} + + +DocFileName::DocFileName() + : save_abs_path_(true) +{} + + +DocFileName::DocFileName(string const & abs_filename, bool save_abs) + : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false) +{} + + +void DocFileName::set(string const & name, string const & buffer_path) { save_abs_path_ = absolutePath(name); name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path); @@ -50,28 +71,28 @@ void FileName::set(string const & name, string const & buffer_path) } -void FileName::erase() +void DocFileName::erase() { name_.erase(); zipped_valid_ = false; } -string const FileName::relFilename(string const & path) const +string const DocFileName::relFilename(string const & path) const { return makeRelPath(name_, path); } -string const FileName::outputFilename(string const & path) const +string const DocFileName::outputFilename(string const & path) const { return save_abs_path_ ? name_ : makeRelPath(name_, path); } -string const FileName::mangledFilename(std::string const & dir) const +string const DocFileName::mangledFilename(std::string const & dir) const { - // We need to make sure that every FileName instance for a given + // We need to make sure that every DocFileName instance for a given // filename returns the same mangled name. typedef map MangledMap; static MangledMap mangledNames; @@ -125,7 +146,7 @@ string const FileName::mangledFilename(std::string const & dir) const } -bool FileName::isZipped() const +bool DocFileName::isZipped() const { if (!zipped_valid_) { zipped_ = zippedFile(name_); @@ -135,20 +156,20 @@ bool FileName::isZipped() const } -string const FileName::unzippedFilename() const +string const DocFileName::unzippedFilename() const { return unzippedFileName(name_); } -bool operator==(FileName const & lhs, FileName const & rhs) +bool operator==(DocFileName const & lhs, DocFileName const & rhs) { return lhs.absFilename() == rhs.absFilename() && lhs.saveAbsPath() == rhs.saveAbsPath(); } -bool operator!=(FileName const & lhs, FileName const & rhs) +bool operator!=(DocFileName const & lhs, DocFileName const & rhs) { return !(lhs == rhs); } diff --git a/src/support/filename.h b/src/support/filename.h index 285f86efca..0d468bd66b 100644 --- a/src/support/filename.h +++ b/src/support/filename.h @@ -19,13 +19,49 @@ namespace lyx { namespace support { +/** + * Class for storing file names. + * The file name may be empty. If it is not empty it is an absolute path. + * The file may or may not exist. + */ class FileName { -public: +protected: + /// Constructor for empty filenames (only needed for DocFileName) FileName(); +public: + /** Constructor for nonempty filenames. + * \param abs_filename the file in question. Must have an absolute path. + */ + FileName(std::string const & abs_filename); + /// Is this filename empty? + bool empty() const { return name_.empty(); } + /// get the absolute file name + std::string const absFilename() const { return name_; } +protected: + /// The absolute file name. + /// The encoding is currently unspecified, anything else than ASCII + /// may or may not work. + std::string name_; +}; + + +bool operator==(FileName const &, FileName const &); +bool operator!=(FileName const &, FileName const &); + + +/** + * Class for storing file names that appear in documents (e. g. child + * documents, included figures etc). + * The file name must not denote a file in our temporary directory, but a + * file that the user chose. + */ +class DocFileName : public FileName { +public: + DocFileName(); /** \param abs_filename the file in question. Must have an absolute path. * \param save_abs_path how is the file to be output to file? */ - FileName(std::string const & abs_filename, bool save_abs_path = true); + DocFileName(std::string const & abs_filename, bool save_abs_path = true); /** \param filename the file in question. May have either a relative * or an absolute path. @@ -35,10 +71,8 @@ public: void set(std::string const & filename, std::string const & buffer_path); void erase(); - bool empty() const { return name_.empty(); } bool saveAbsPath() const { return save_abs_path_; } - std::string const absFilename() const { return name_; } /// \param buffer_path if empty, uses `pwd` std::string const relFilename(std::string const & buffer_path = std::string()) const; /// \param buf_path if empty, uses `pwd` @@ -73,7 +107,6 @@ public: std::string const unzippedFilename() const; private: - std::string name_; bool save_abs_path_; /// Cache for isZipped() because zippedFile() is expensive mutable bool zipped_; @@ -82,8 +115,8 @@ private: }; -bool operator==(FileName const &, FileName const &); -bool operator!=(FileName const &, FileName const &); +bool operator==(DocFileName const &, DocFileName const &); +bool operator!=(DocFileName const &, DocFileName const &); } // namespace support