From e017570aa8b846b4e902ac11067f58f5043a64ae Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Wed, 5 Dec 2007 10:32:49 +0000 Subject: [PATCH] Introducing FileNameList, cleanup some headers and put back dirList() into FileName. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21965 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Buffer.cpp | 22 ++++++++++++++-------- src/Buffer.h | 12 ++++++------ src/Converter.cpp | 13 +++++++------ src/insets/InsetBibtex.cpp | 8 +++++--- src/insets/InsetBibtex.h | 10 ++++++---- src/insets/InsetCitation.cpp | 11 ++++++----- src/insets/InsetInclude.cpp | 8 +++++--- src/insets/InsetInclude.h | 7 ++++--- src/support/FileName.cpp | 34 ++++++++++++++++++++++++++++++++++ src/support/FileName.h | 6 +++++- src/support/FileNameList.h | 34 ++++++++++++++++++++++++++++++++++ src/support/FileZipListDir.h | 10 ---------- src/support/filetools.cpp | 33 --------------------------------- 13 files changed, 126 insertions(+), 82 deletions(-) create mode 100644 src/support/FileNameList.h diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 71b58902ee..0b2c71b1cc 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -79,6 +79,7 @@ #include "support/convert.h" #include "support/debug.h" #include "support/FileFilterList.h" +#include "support/FileNameList.h" #include "support/filetools.h" #include "support/ForkedCalls.h" #include "support/gettext.h" @@ -129,6 +130,7 @@ using support::changeExtension; using support::cmd_ret; using support::createBufferTmpDir; using support::FileName; +using support::FileNameList; using support::libFileSearch; using support::latex_path; using support::ltrim; @@ -229,6 +231,10 @@ public: /// Undo undo_; + + /// A cache for the bibfiles (including bibfiles of loaded child + /// documents), needed for appropriate update of natbib labels. + mutable FileNameList bibfilesCache_; }; @@ -1450,22 +1456,22 @@ void Buffer::updateBibfilesCache() const return; } - bibfilesCache_.clear(); + d->bibfilesCache_.clear(); for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) { if (it->lyxCode() == BIBTEX_CODE) { InsetBibtex const & inset = static_cast(*it); - vector const bibfiles = inset.getFiles(*this); - bibfilesCache_.insert(bibfilesCache_.end(), + FileNameList const bibfiles = inset.getFiles(*this); + d->bibfilesCache_.insert(d->bibfilesCache_.end(), bibfiles.begin(), bibfiles.end()); } else if (it->lyxCode() == INCLUDE_CODE) { InsetInclude & inset = static_cast(*it); inset.updateBibfilesCache(*this); - vector const & bibfiles = + FileNameList const & bibfiles = inset.getBibfilesCache(*this); - bibfilesCache_.insert(bibfilesCache_.end(), + d->bibfilesCache_.insert(d->bibfilesCache_.end(), bibfiles.begin(), bibfiles.end()); } @@ -1473,7 +1479,7 @@ void Buffer::updateBibfilesCache() const } -vector const & Buffer::getBibfilesCache() const +FileNameList const & Buffer::getBibfilesCache() const { // if this is a child document and the parent is already loaded // use the parent's cache instead @@ -1483,10 +1489,10 @@ vector const & Buffer::getBibfilesCache() const return tmp->getBibfilesCache(); // We update the cache when first used instead of at loading time. - if (bibfilesCache_.empty()) + if (d->bibfilesCache_.empty()) const_cast(this)->updateBibfilesCache(); - return bibfilesCache_; + return d->bibfilesCache_; } diff --git a/src/Buffer.h b/src/Buffer.h index 944530d338..5e61cb1116 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -14,7 +14,6 @@ #include "insets/InsetCode.h" -#include "support/FileName.h" #include "support/strfwd.h" #include "support/types.h" @@ -54,6 +53,11 @@ class GuiBufferDelegate; class WorkAreaManager; } +namespace support { +class FileName; +class FileNameList; +} + /** The buffer object. * This is the buffer object. It contains all the informations about * a document loaded into LyX. @@ -298,7 +302,7 @@ public: void updateBibfilesCache() const; /// Return the cache with all bibfiles in use (including bibfiles /// of loaded child documents). - std::vector const & getBibfilesCache() const; + support::FileNameList const & getBibfilesCache() const; /// void getLabelList(std::vector &) const; @@ -453,10 +457,6 @@ private: /// The pointer never changes although *pimpl_'s contents may. Impl * const d; - /// A cache for the bibfiles (including bibfiles of loaded child - /// documents), needed for appropriate update of natbib labels. - mutable std::vector bibfilesCache_; - frontend::GuiBufferDelegate * gui_; }; diff --git a/src/Converter.cpp b/src/Converter.cpp index e6f67460fe..136227b219 100644 --- a/src/Converter.cpp +++ b/src/Converter.cpp @@ -25,7 +25,9 @@ #include "frontends/alert.h" #include "support/debug.h" +#include "support/FileNameList.h" #include "support/filetools.h" +#include "support/FileZipListDir.h" #include "support/gettext.h" #include "support/lstrings.h" #include "support/lyxlib.h" @@ -33,7 +35,6 @@ #include "support/Package.h" #include "support/Path.h" #include "support/Systemcall.h" -#include "support/FileZipListDir.h" using std::find_if; using std::string; @@ -49,6 +50,7 @@ using support::changeExtension; using support::compare_ascii_no_case; using support::contains; using support::FileName; +using support::FileNameList; using support::getExtension; using support::libFileSearch; using support::libScriptSearch; @@ -511,8 +513,6 @@ bool Converters::convert(Buffer const * buffer, bool Converters::move(string const & fmt, FileName const & from, FileName const & to, bool copy) { - if (from == to) - return true; bool no_errors = true; string const path = onlyPath(from.absFilename()); @@ -520,9 +520,10 @@ bool Converters::move(string const & fmt, string const to_base = removeExtension(to.absFilename()); string const to_extension = getExtension(to.absFilename()); - vector const files = - support::dirList(FileName(path), getExtension(from.absFilename())); - for (vector::const_iterator it = files.begin(); + FileNameList const files = FileName(path).dirList(getExtension(from.absFilename())); + if (from == to) + return true; + for (FileNameList::const_iterator it = files.begin(); it != files.end(); ++it) { string const from2 = it->absFilename(); string const file2 = onlyFilename(from2); diff --git a/src/insets/InsetBibtex.cpp b/src/insets/InsetBibtex.cpp index 63f870576c..3f92e03234 100644 --- a/src/insets/InsetBibtex.cpp +++ b/src/insets/InsetBibtex.cpp @@ -29,6 +29,7 @@ #include "support/ExceptionMessage.h" #include "support/docstream.h" +#include "support/FileNameList.h" #include "support/filetools.h" #include "support/lstrings.h" #include "support/lyxlib.h" @@ -48,6 +49,7 @@ using support::contains; using support::copy; using support::DocFileName; using support::FileName; +using support::FileNameList; using support::findtexfile; using support::isValidLaTeXFilename; using support::latex_path; @@ -333,12 +335,12 @@ int InsetBibtex::latex(Buffer const & buffer, odocstream & os, } -vector const InsetBibtex::getFiles(Buffer const & buffer) const +FileNameList const InsetBibtex::getFiles(Buffer const & buffer) const { FileName path(buffer.filePath()); support::PathChanger p(path); - vector vec; + FileNameList vec; string tmp; // FIXME UNICODE @@ -601,7 +603,7 @@ namespace { void InsetBibtex::fillWithBibKeys(Buffer const & buffer, BiblioInfo & keylist, InsetIterator const & /*di*/) const { - vector const files = getFiles(buffer); + FileNameList const files = getFiles(buffer); for (vector::const_iterator it = files.begin(); it != files.end(); ++ it) { // This bibtex parser is a first step to parse bibtex files diff --git a/src/insets/InsetBibtex.h b/src/insets/InsetBibtex.h index 2b8e8dea6a..25ce300c9b 100644 --- a/src/insets/InsetBibtex.h +++ b/src/insets/InsetBibtex.h @@ -16,11 +16,13 @@ #include "InsetCommand.h" #include "BiblioInfo.h" -#include "support/FileName.h" - - namespace lyx { +namespace support { +class FileNameList; +} + + /** Used to insert BibTeX's information */ class InsetBibtex : public InsetCommand { @@ -41,7 +43,7 @@ public: virtual void fillWithBibKeys(Buffer const &, BiblioInfo &, InsetIterator const &) const; /// - std::vector const getFiles(Buffer const &) const; + support::FileNameList const getFiles(Buffer const &) const; /// bool addDatabase(std::string const &); /// diff --git a/src/insets/InsetCitation.cpp b/src/insets/InsetCitation.cpp index 43920a695c..5d5bbdacd2 100644 --- a/src/insets/InsetCitation.cpp +++ b/src/insets/InsetCitation.cpp @@ -22,13 +22,17 @@ #include "support/lstrings.h" #include "support/docstream.h" +#include "support/FileNameList.h" #include +using std::string; +using std::vector; namespace lyx { using support::FileName; +using support::FileNameList; using support::getStringFromVector; using support::getVectorFromString; using support::ltrim; @@ -37,9 +41,6 @@ using support::rtrim; using support::split; using support::tokenPos; -using std::string; -using std::vector; - namespace { vector const init_possible_cite_commands() @@ -152,10 +153,10 @@ docstring const getNatbibLabel(Buffer const & buffer, BiblioInfo biblist; - vector const & bibfilesCache = buffer.getBibfilesCache(); + FileNameList const & bibfilesCache = buffer.getBibfilesCache(); // compare the cached timestamps with the actual ones. bool changed = false; - for (vector::const_iterator it = bibfilesCache.begin(); + for (FileNameList::const_iterator it = bibfilesCache.begin(); it != bibfilesCache.end(); ++ it) { FileName const f = *it; std::time_t lastw = f.lastModified(); diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp index ba1207c283..e8d91dfced 100644 --- a/src/insets/InsetInclude.cpp +++ b/src/insets/InsetInclude.cpp @@ -44,6 +44,7 @@ #include "insets/InsetListingsParams.h" #include "support/docstream.h" +#include "support/FileNameList.h" #include "support/filetools.h" #include "support/lstrings.h" // contains #include "support/lyxalgo.h" @@ -63,6 +64,7 @@ using support::contains; using support::copy; using support::DocFileName; using support::FileName; +using support::FileNameList; using support::getVectorFromString; using support::isLyXFilename; using support::isValidLaTeXFilename; @@ -726,17 +728,17 @@ void InsetInclude::updateBibfilesCache(Buffer const & buffer) } -std::vector const & +FileNameList const & InsetInclude::getBibfilesCache(Buffer const & buffer) const { Buffer * const tmp = getChildBuffer(buffer, params()); if (tmp) { tmp->setParent(0); - std::vector const & cache = tmp->getBibfilesCache(); + FileNameList const & cache = tmp->getBibfilesCache(); tmp->setParent(&buffer); return cache; } - static std::vector const empty; + static FileNameList const empty; return empty; } diff --git a/src/insets/InsetInclude.h b/src/insets/InsetInclude.h index 50e95babc7..f495d1d9ed 100644 --- a/src/insets/InsetInclude.h +++ b/src/insets/InsetInclude.h @@ -21,8 +21,6 @@ #include "Counters.h" #include "EmbeddedFiles.h" -#include "support/FileName.h" - #include namespace lyx { @@ -32,6 +30,9 @@ class Dimension; class LaTeXFeatures; class RenderMonitoredPreview; +namespace support { +class FileNameList; +} /// for including tex/lyx files class InsetInclude : public InsetCommand { @@ -73,7 +74,7 @@ public: * Return an empty vector if the child doc is not loaded. * \param buffer the Buffer containing this inset. */ - std::vector const & + support::FileNameList const & getBibfilesCache(Buffer const & buffer) const; /// EDITABLE editable() const { return IS_EDITABLE; } diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp index cc476eecc8..2c8fa57b1e 100644 --- a/src/support/FileName.cpp +++ b/src/support/FileName.cpp @@ -11,6 +11,7 @@ #include #include "support/FileName.h" +#include "support/FileNameList.h" #include "support/debug.h" #include "support/filetools.h" @@ -230,6 +231,39 @@ bool FileName::isDirWritable() const } +FileNameList FileName::dirList(std::string const & ext) const +{ + FileNameList dirlist; + if (!isDirectory()) { + LYXERR0("Directory '" << *this << "' does not exist!"); + return dirlist; + } + + QDir dir = d->fi.absoluteDir(); + + if (!ext.empty()) { + QString filter; + switch (ext[0]) { + case '.': filter = "*" + toqstr(ext); break; + case '*': filter = toqstr(ext); break; + default: filter = "*." + toqstr(ext); + } + dir.setNameFilters(QStringList(filter)); + LYXERR(Debug::FILES, "filtering on extension " + << fromqstr(filter) << " is requested."); + } + + QFileInfoList list = dir.entryInfoList(); + for (int i = 0; i != list.size(); ++i) { + FileName fi(fromqstr(list.at(i).absoluteFilePath())); + dirlist.push_back(fi); + LYXERR(Debug::FILES, "found file " << fi); + } + + return dirlist; +} + + FileName FileName::tempName(FileName const & dir, std::string const & mask) { return support::tempName(dir, mask); diff --git a/src/support/FileName.h b/src/support/FileName.h index be47e158f7..c293f66021 100644 --- a/src/support/FileName.h +++ b/src/support/FileName.h @@ -20,6 +20,9 @@ namespace lyx { namespace support { +/// Defined in "FileNameList.h". +class FileNameList; + /** * Class for storing file names. * The file name may be empty. If it is not empty it is an absolute path. @@ -82,6 +85,8 @@ public: bool isWritable() const; /// return true when file/directory is writable (write test file) bool isDirWritable() const; + /// \return list other files in the directory having optional extension 'ext'. + FileNameList dirList(std::string const & ext) const; /// copy a file /// \return true when file/directory is writable (write test file) @@ -226,7 +231,6 @@ private: bool operator==(DocFileName const &, DocFileName const &); bool operator!=(DocFileName const &, DocFileName const &); - } // namespace support } // namespace lyx diff --git a/src/support/FileNameList.h b/src/support/FileNameList.h new file mode 100644 index 0000000000..20b6a85af7 --- /dev/null +++ b/src/support/FileNameList.h @@ -0,0 +1,34 @@ +// -*- C++ -*- +/** + * \file FileNameList.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Abdelrazak Younes + * + * Full author contact details are available in file CREDITS. + */ + +#ifndef FILENAMELIST_H +#define FILENAMELIST_H + +#include "support/FileName.h" + +#include + +namespace lyx { +namespace support { + +/** + * Class for storing file name list. + * std::vector can not be forward declared in a simple way. Creating a class solves + * this problem. + */ +class FileNameList: public std::vector +{ +}; + +} // namespace support +} // namespace lyx + +#endif // FILENAMELIST_H diff --git a/src/support/FileZipListDir.h b/src/support/FileZipListDir.h index d49e0500ce..8dd051fcdd 100644 --- a/src/support/FileZipListDir.h +++ b/src/support/FileZipListDir.h @@ -19,16 +19,6 @@ #include #include -namespace lyx { -namespace support { - -/// \return list other files in the directory having optional extension 'ext'. -std::vector dirList(FileName const & filename, std::string const & ext); - - -} // namespace support -} // namespace lyx - /// The following functions are implemented in minizip/zipunzip.cpp, and are not in /// the lyx::support namespace diff --git a/src/support/filetools.cpp b/src/support/filetools.cpp index 13d018932f..cc77812708 100644 --- a/src/support/filetools.cpp +++ b/src/support/filetools.cpp @@ -938,38 +938,5 @@ int compare_timestamps(FileName const & file1, FileName const & file2) } -std::vector dirList(FileName const & filename, std::string const & ext) -{ - std::vector dirlist; - if (!filename.isDirectory()) { - LYXERR0("Directory '" << filename << "' does not exist!"); - return dirlist; - } - - QDir dir(toqstr(filename.absoluteFilePath())); - - if (!ext.empty()) { - QString filter; - switch (ext[0]) { - case '.': filter = "*" + toqstr(ext); break; - case '*': filter = toqstr(ext); break; - default: filter = "*." + toqstr(ext); - } - dir.setNameFilters(QStringList(filter)); - LYXERR(Debug::FILES, "filtering on extension " - << fromqstr(filter) << " is requested."); - } - - QFileInfoList list = dir.entryInfoList(); - for (int i = 0; i != list.size(); ++i) { - FileName fi(fromqstr(list.at(i).absoluteFilePath())); - dirlist.push_back(fi); - LYXERR(Debug::FILES, "found file " << fi); - } - - return dirlist; -} - - } //namespace support } // namespace lyx