From 5c4a20fe0406ca60a084f733b5fc4324ab1a0933 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Fri, 30 Nov 2007 20:57:53 +0000 Subject: [PATCH] I'll find a solution for the 'dirList problem', Abdel. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21884 a592a061-630c-0410-9148-cb99ea01b6c8 --- development/attic/cow_ptr.h | 124 ++++++++++++++++++ .../count_total_lines_of_compiled_code.sh | 3 +- src/Converter.cpp | 4 +- src/EmbeddedFiles.h | 1 + src/ModuleList.h | 1 + src/main.cpp | 6 +- src/support/FileName.cpp | 28 ++-- src/support/FileName.h | 23 ++-- src/support/filetools.h | 4 + src/support/lstrings.cpp | 79 ++++++----- src/support/lstrings.h | 1 + src/support/strfwd.h | 3 + 12 files changed, 212 insertions(+), 65 deletions(-) create mode 100644 development/attic/cow_ptr.h diff --git a/development/attic/cow_ptr.h b/development/attic/cow_ptr.h new file mode 100644 index 0000000000..685d0e4ba9 --- /dev/null +++ b/development/attic/cow_ptr.h @@ -0,0 +1,124 @@ +// -*- C++ -*- +/** + * \file cow_ptr.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Angus Leeming + * + * A pointer with copy-on-write semantics + * + * The original version of this class was written by Yonat Sharon + * and is freely available at http://ootips.org/yonat/ + * + * I modified it to use boost::shared_ptr internally, rather than use his + * home-grown equivalent. + */ + +#ifndef COW_PTR_H +#define COW_PTR_H + +#include + + +namespace lyx { +namespace support { + +template +class cow_ptr { +public: + explicit cow_ptr(T * = 0); + cow_ptr(cow_ptr const &); + cow_ptr & operator=(cow_ptr const &); + + T const & operator*() const; + T const * operator->() const; + T const * get() const; + + T & operator*(); + T * operator->(); + T * get(); + +private: + boost::shared_ptr ptr_; + void copy(); +}; + + +template +cow_ptr::cow_ptr(T * p) + : ptr_(p) +{} + + +template +cow_ptr::cow_ptr(cow_ptr const & other) + : ptr_(other.ptr_) +{} + + +template +cow_ptr & cow_ptr::operator=(cow_ptr const & other) +{ + if (&other != this) + ptr_ = other.ptr_; + return *this; +} + + +template +T const & cow_ptr::operator*() const +{ + return *ptr_; +} + + +template +T const * cow_ptr::operator->() const +{ + return ptr_.get(); +} + + +template +T const * cow_ptr::get() const +{ + return ptr_.get(); +} + + +template +T & cow_ptr::operator*() +{ + copy(); + return *ptr_; +} + + +template +T * cow_ptr::operator->() +{ + copy(); + return ptr_.get(); +} + + +template +T * cow_ptr::get() +{ + copy(); + return ptr_.get(); +} + + +template +void cow_ptr::copy() +{ + if (!ptr_.unique()) + ptr_ = boost::shared_ptr(new T(*ptr_.get())); +} + +} // namespace support +} // namespace lyx + +#endif // NOT COW_PTR_H diff --git a/development/tools/count_total_lines_of_compiled_code.sh b/development/tools/count_total_lines_of_compiled_code.sh index dc407bdb8f..0196d262c0 100755 --- a/development/tools/count_total_lines_of_compiled_code.sh +++ b/development/tools/count_total_lines_of_compiled_code.sh @@ -20,11 +20,12 @@ t=0 #for i in `find ../../src/support -name '*.cpp'` ; do #for i in `find ../../src/graphics -name '*.cpp'` ; do #for i in `find ../../src/graphics -name '*.cpp'` ; do -#for i in ../../src/*.cpp ; do +#for i in `find ../../src/support/chdir.cpp` ; do for i in `find ../.. -name '*.cpp'` ; do #echo $i #echo "g++ $inc -DQT_NO_STL -E $i" #g++ $inc -DQT_NO_STL -E $i > tmp/`basename $i` + g++ $inc -DQT_NO_STL -E $i > t l=`g++ $inc -DQT_NO_STL -E $i | wc -l` f=`cat $i | wc -l` s=$[s + l] diff --git a/src/Converter.cpp b/src/Converter.cpp index 6715e028c0..fc82114308 100644 --- a/src/Converter.cpp +++ b/src/Converter.cpp @@ -519,8 +519,8 @@ bool Converters::move(string const & fmt, string const to_base = removeExtension(to.absFilename()); string const to_extension = getExtension(to.absFilename()); - vector const files = FileName(path).dirList( - getExtension(from.absFilename())); + vector const files = + support::dirList(FileName(path), getExtension(from.absFilename())); for (vector::const_iterator it = files.begin(); it != files.end(); ++it) { string const from2 = it->absFilename(); diff --git a/src/EmbeddedFiles.h b/src/EmbeddedFiles.h index 277d9d3638..e7d80f48a8 100644 --- a/src/EmbeddedFiles.h +++ b/src/EmbeddedFiles.h @@ -15,6 +15,7 @@ #include "support/FileName.h" +#include #include /** diff --git a/src/ModuleList.h b/src/ModuleList.h index bab9a049ae..09d793a235 100644 --- a/src/ModuleList.h +++ b/src/ModuleList.h @@ -15,6 +15,7 @@ #include "support/FileName.h" #include +#include #include namespace lyx { diff --git a/src/main.cpp b/src/main.cpp index 41ce51a772..384724aecb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,10 +11,10 @@ #include -#include "support/debug.h" #include "LyX.h" -#include "support/gettext.h" +#include "support/debug.h" +#include "support/gettext.h" #include "support/os.h" #include @@ -33,6 +33,8 @@ int main(int argc, char * argv[]) // early as possible. lyx::lyxerr.setStream(std::cerr); + LYXERR0("acssdc"); + lyx::support::os::init(argc, argv); // initialize for internationalized version *EK* diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp index 7c594da86c..5704d2cb02 100644 --- a/src/support/FileName.cpp +++ b/src/support/FileName.cpp @@ -268,7 +268,7 @@ static bool rmdir(QFileInfo const & fi) QDir dir(fi.absoluteFilePath()); QFileInfoList list = dir.entryInfoList(); bool global_success = true; - for (int i = 0; i < list.size(); ++i) { + for (int i = 0; i != list.size(); ++i) { if (list.at(i).fileName() == ".") continue; if (list.at(i).fileName() == "..") @@ -313,16 +313,15 @@ bool FileName::createDirectory(int permission) const } -std::vector FileName::dirList(std::string const & ext) +std::vector dirList(FileName const & dirname, std::string const & ext) { std::vector dirlist; - if (!exists() || !isDirectory()) { - lyxerr << "FileName::dirList(): Directory \"" << absFilename() - << "\" does not exist!" << endl; + if (!dirname.isDirectory()) { + LYXERR0("Directory '" << dirname << "' does not exist!"); return dirlist; } - QDir dir(d->fi.absoluteFilePath()); + QDir dir(dirname.d->fi.absoluteFilePath()); if (!ext.empty()) { QString filter; @@ -332,17 +331,16 @@ std::vector FileName::dirList(std::string const & ext) default: filter = "*." + toqstr(ext); } dir.setNameFilters(QStringList(filter)); - LYXERR(Debug::FILES, "FileName::dirList(): filtering on extension " - << fromqstr(filter) << " is requested." << endl); + LYXERR(Debug::FILES, "filtering on extension " + << fromqstr(filter) << " is requested."); } QFileInfoList list = dir.entryInfoList(); - for (int i = 0; i < list.size(); ++i) { + for (int i = 0; i != list.size(); ++i) { FileName fi; fi.d->fi = list.at(i); dirlist.push_back(fi); - LYXERR(Debug::FILES, "FileName::dirList(): found file " - << fi.absFilename() << endl); + LYXERR(Debug::FILES, "found file " << fi); } return dirlist; @@ -641,20 +639,20 @@ void DocFileName::erase() } -string const DocFileName::relFilename(string const & path) const +string DocFileName::relFilename(string const & path) const { // FIXME UNICODE return to_utf8(makeRelPath(qstring_to_ucs4(d->fi.absoluteFilePath()), from_utf8(path))); } -string const DocFileName::outputFilename(string const & path) const +string DocFileName::outputFilename(string const & path) const { return save_abs_path_ ? absFilename() : relFilename(path); } -string const DocFileName::mangledFilename(std::string const & dir) const +string DocFileName::mangledFilename(std::string const & dir) const { // We need to make sure that every DocFileName instance for a given // filename returns the same mangled name. @@ -726,7 +724,7 @@ bool DocFileName::isZipped() const } -string const DocFileName::unzippedFilename() const +string DocFileName::unzippedFilename() const { return unzippedFileName(absFilename()); } diff --git a/src/support/FileName.h b/src/support/FileName.h index 0151c7fa81..b6a4d85086 100644 --- a/src/support/FileName.h +++ b/src/support/FileName.h @@ -15,14 +15,11 @@ #include "support/strfwd.h" #include -#include -#include 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. @@ -101,10 +98,6 @@ public: /// Creates directory. Returns true on success bool createDirectory(int permissions) const; - /// \return list files in a directory having optional extension ext.. - std::vector dirList( - std::string const & ext = std::string()); - /// Get the contents of a file as a huge std::string std::string fileContents() const; /** @@ -119,7 +112,7 @@ public: * If oldname does not have an extension, it is appended. * If the extension is empty, any extension is removed from the name. */ - void changeExtension(std::string const & extension); + void changeExtension(std::string const & extension); /** Guess the file format name (as in Format::name()) from contents. Normally you don't want to use this directly, but rather @@ -134,7 +127,7 @@ public: /// (securely) create a temporary file in the given dir with the given mask /// \p mask must be in filesystem encoding static FileName tempName(FileName const & dir = FileName(), - std::string const & mask = std::string()); + std::string const & mask = empty_string()); /// filename without path std::string onlyFileName() const; @@ -143,7 +136,7 @@ public: /// used for display in the Gui docstring displayName(int threshold = 1000) const; -private: +//private: friend class DocFileName; /// struct Private; @@ -184,9 +177,9 @@ public: bool saveAbsPath() const { return save_abs_path_; } /// \param buffer_path if empty, uses `pwd` - std::string const relFilename(std::string const & buffer_path = std::string()) const; + std::string relFilename(std::string const & buffer_path = empty_string()) const; /// \param buf_path if empty, uses `pwd` - std::string const outputFilename(std::string const & buf_path = std::string()) const; + std::string outputFilename(std::string const & buf_path = empty_string()) const; /** @returns a mangled representation of the absolute file name * suitable for use in the temp dir when, for example, converting @@ -208,13 +201,13 @@ public: * Only the mangled file name is returned. It is not prepended * with @c dir. */ - std::string const - mangledFilename(std::string const & dir = std::string()) const; + std::string + mangledFilename(std::string const & dir = empty_string()) const; /// \return true if the file is compressed. bool isZipped() const; /// \return the absolute file name without its .gz, .z, .Z extension - std::string const unzippedFilename() const; + std::string unzippedFilename() const; private: bool save_abs_path_; diff --git a/src/support/filetools.h b/src/support/filetools.h index 5bd8167fb6..383aeac2f8 100644 --- a/src/support/filetools.h +++ b/src/support/filetools.h @@ -275,6 +275,10 @@ typedef std::pair cmd_ret; cmd_ret const runCommand(std::string const & cmd); +/// \return list files in a directory having optional extension ext.. +std::vector dirList(FileName const & dir, + std::string const & ext = empty_string()); + } // namespace support } // namespace lyx diff --git a/src/support/lstrings.cpp b/src/support/lstrings.cpp index 7aeefdfb52..1952300ecd 100644 --- a/src/support/lstrings.cpp +++ b/src/support/lstrings.cpp @@ -42,6 +42,22 @@ using std::toupper; namespace lyx { +// Using this allows us to have docstring default arguments in headers +// without #include "support/docstring" there. +docstring const & empty_docstring() +{ + static docstring s; + return s; +} + +// Using this allows us to have std::string default arguments in headers +// without #include +std::string const & empty_string() +{ + static std::string s; + return s; +} + /** * Convert a QChar into a UCS4 character. * This is a hack (it does only make sense for the common part of the UCS4 @@ -232,73 +248,77 @@ int compare_ascii_no_case(docstring const & s, docstring const & s2) bool isStrInt(string const & str) { - if (str.empty()) return false; + if (str.empty()) + return false; // Remove leading and trailing white space chars. string const tmpstr = trim(str); - if (tmpstr.empty()) return false; + if (tmpstr.empty()) + return false; string::const_iterator cit = tmpstr.begin(); - if ((*cit) == '-') ++cit; + if ((*cit) == '-') + ++cit; + string::const_iterator end = tmpstr.end(); - for (; cit != end; ++cit) { - if (!isdigit((*cit))) return false; - } + for (; cit != end; ++cit) + if (!isdigit((*cit))) + return false; + return true; } bool isStrUnsignedInt(string const & str) { - if (str.empty()) return false; + if (str.empty()) + return false; // Remove leading and trailing white space chars. string const tmpstr = trim(str); - if (tmpstr.empty()) return false; + if (tmpstr.empty()) + return false; string::const_iterator cit = tmpstr.begin(); string::const_iterator end = tmpstr.end(); - for (; cit != end; ++cit) { - if (!isdigit((*cit))) return false; - } + for (; cit != end; ++cit) + if (!isdigit((*cit))) + return false; + return true; } bool isStrDbl(string const & str) { - if (str.empty()) return false; + if (str.empty()) + return false; // Remove leading and trailing white space chars. string const tmpstr = trim(str); - if (tmpstr.empty()) return false; - // if (1 < tmpstr.count('.')) return false; + if (tmpstr.empty()) + return false; + // if (tmpstr.count('.') > 1) return false; string::const_iterator cit = tmpstr.begin(); - bool found_dot(false); - if ((*cit) == '-') ++cit; + bool found_dot = false; + if (*cit == '-') + ++cit; string::const_iterator end = tmpstr.end(); for (; cit != end; ++cit) { - if (!isdigit((*cit)) - && '.' != (*cit)) { + if (!isdigit(*cit) && *cit != '.') return false; - } if ('.' == (*cit)) { - if (found_dot) { + if (found_dot) return false; - } else { - found_dot = true; - } + found_dot = true; } } return true; } -namespace { - -inline -bool isHexChar(char_type c) +static bool isHexChar(char_type c) { return c == '0' || c == '1' || @@ -318,8 +338,6 @@ bool isHexChar(char_type c) c == 'f' || c == 'F'; } -} // anon namespace - bool isHex(docstring const & str) { @@ -567,7 +585,8 @@ string const token(string const & a, char delim, int n) docstring const token(docstring const & a, char_type delim, int n) { - if (a.empty()) return docstring(); + if (a.empty()) + return docstring(); size_t k = 0; size_t i = 0; diff --git a/src/support/lstrings.h b/src/support/lstrings.h index f22e6564d8..cfb504f2db 100644 --- a/src/support/lstrings.h +++ b/src/support/lstrings.h @@ -19,6 +19,7 @@ #include "support/docstring.h" #include +#include #include diff --git a/src/support/strfwd.h b/src/support/strfwd.h index 8dfed6e794..85dfed168f 100644 --- a/src/support/strfwd.h +++ b/src/support/strfwd.h @@ -67,6 +67,9 @@ typedef std::basic_ostream > odocstream; extern odocstream & operator<<(odocstream &, char); #endif +docstring const & empty_docstring(); +std::string const & empty_string(); + } // namespace lyx #endif -- 2.39.5