From 988f372843941436021a45c5d8848c514f23887c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Thu, 18 Oct 2007 21:10:35 +0000 Subject: [PATCH] use FileName::isDirectory() git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21047 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/LaTeX.cpp | 6 ++--- src/LyX.cpp | 39 +++++++++++-------------------- src/LyXFunc.cpp | 7 +++--- src/Session.cpp | 21 +++++------------ src/frontends/qt4/GuiGraphics.cpp | 7 ++---- src/insets/ExternalSupport.cpp | 6 +---- src/support/FileName.cpp | 9 ++++++- src/support/FileName.h | 4 +++- src/support/Package.cpp | 3 +-- src/support/filetools.cpp | 17 ++++++-------- src/support/lyxsum.cpp | 14 ++++------- 11 files changed, 52 insertions(+), 81 deletions(-) diff --git a/src/LaTeX.cpp b/src/LaTeX.cpp index 63f8083977..f812344485 100644 --- a/src/LaTeX.cpp +++ b/src/LaTeX.cpp @@ -771,8 +771,7 @@ namespace { bool insertIfExists(FileName const & absname, DepTable & head) { - if (absname.exists() && - !fs::is_directory(absname.toFilesystemEncoding())) { + if (absname.exists() && !absname.isDirectory()) { head.insert(absname, true); return true; } @@ -851,8 +850,7 @@ bool handleFoundFile(string const & ff, DepTable & head) // (2) foundfile is in the tmpdir // insert it into head - if (absname.exists() && - !fs::is_directory(absname.toFilesystemEncoding())) { + if (absname.exists() && !absname.isDirectory()) { // FIXME: This regex contained glo, but glo is used by the old // version of nomencl.sty. Do we need to put it back? static regex const unwanted("^.*\\.(aux|log|dvi|bbl|ind)$"); diff --git a/src/LyX.cpp b/src/LyX.cpp index 69fff59667..3c2dd305d8 100644 --- a/src/LyX.cpp +++ b/src/LyX.cpp @@ -62,7 +62,6 @@ #include "support/Systemcall.h" #include -#include #include #include @@ -84,8 +83,6 @@ using std::signal; using std::system; #endif -namespace fs = boost::filesystem; - namespace lyx { using support::addName; @@ -985,8 +982,7 @@ bool LyX::init() prependEnvPath("PATH", lyxrc.path_prefix); FileName const document_path(lyxrc.document_path); - if (document_path.exists() && - fs::is_directory(document_path.toFilesystemEncoding())) + if (document_path.exists() && document_path.isDirectory()) package().document_dir() = document_path; package().temp_dir() = createLyXTmpDir(FileName(lyxrc.tempdir_path)); @@ -1114,38 +1110,32 @@ void LyX::deadKeyBindings(KeyMap * kbmap) } -namespace { - // return true if file does not exist or is older than configure.py. -bool needsUpdate(string const & file) +static bool needsUpdate(string const & file) { // We cannot initialize configure_script directly because the package // is not initialized yet when static objects are constructed. - static string configure_script; + static FileName configure_script; static bool firstrun = true; if (firstrun) { - configure_script = FileName(addName( - package().system_support().absFilename(), - "configure.py")).toFilesystemEncoding(); + configure_script = + FileName(addName(package().system_support().absFilename(), + "configure.py")); firstrun = false; } - string const absfile = FileName(addName( - package().user_support().absFilename(), file)).toFilesystemEncoding(); - return (! fs::exists(absfile)) - || (fs::last_write_time(configure_script) - > fs::last_write_time(absfile)); -} - + FileName absfile = + FileName(addName(package().user_support().absFilename(), file)); + return !absfile.exists() + || configure_script.lastModified() > absfile.lastModified(); } bool LyX::queryUserLyXDir(bool explicit_userdir) { // Does user directory exist? - string const user_support = - package().user_support().toFilesystemEncoding(); - if (fs::exists(user_support) && fs::is_directory(user_support)) { + FileName const sup = package().user_support(); + if (sup.exists() && sup.isDirectory()) { first_start = false; return needsUpdate("lyxrc.defaults") @@ -1173,10 +1163,9 @@ bool LyX::queryUserLyXDir(bool explicit_userdir) } lyxerr << to_utf8(bformat(_("LyX: Creating directory %1$s"), - from_utf8(package().user_support().absFilename()))) - << endl; + from_utf8(sup.absFilename()))) << endl; - if (!createDirectory(package().user_support(), 0755)) { + if (!createDirectory(sup, 0755)) { // Failed, so let's exit. lyxerr << to_utf8(_("Failed to create directory. Exiting.")) << endl; diff --git a/src/LyXFunc.cpp b/src/LyXFunc.cpp index cb4c476062..b8673bb152 100644 --- a/src/LyXFunc.cpp +++ b/src/LyXFunc.cpp @@ -2077,7 +2077,7 @@ void LyXFunc::menuNew(string const & name, bool fromTemplate) filename = addName(lyxrc.document_path, "newfile" + convert(++newfile_number) + ".lyx"); while (theBufferList().exists(filename) || - fs::is_readable(FileName(filename).toFilesystemEncoding())) { + FileName(filename).isReadable()) { ++newfile_number; filename = addName(lyxrc.document_path, "newfile" + convert(newfile_number) + @@ -2403,9 +2403,8 @@ void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new) case LyXRC::RC_DISPLAY_GRAPHICS: case LyXRC::RC_DOCUMENTPATH: if (lyxrc_orig.document_path != lyxrc_new.document_path) { - string const encoded = FileName( - lyxrc_new.document_path).toFilesystemEncoding(); - if (fs::exists(encoded) && fs::is_directory(encoded)) + FileName path(lyxrc_new.document_path); + if (path.exists() && path.isDirectory()) support::package().document_dir() = FileName(lyxrc.document_path); } case LyXRC::RC_ESC_CHARS: diff --git a/src/Session.cpp b/src/Session.cpp index 60f44cde2b..dc6d0a1c0b 100644 --- a/src/Session.cpp +++ b/src/Session.cpp @@ -16,8 +16,6 @@ #include "support/Package.h" #include "support/filetools.h" -#include - #include #include #include @@ -28,8 +26,6 @@ using lyx::support::addName; using lyx::support::FileName; using lyx::support::package; -namespace fs = boost::filesystem; - using std::vector; using std::getline; using std::string; @@ -78,9 +74,8 @@ void LastFilesSection::read(istream & is) // read lastfiles FileName const file(tmp); - if (file.exists() && - !fs::is_directory(file.toFilesystemEncoding()) && - lastfiles.size() < num_lastfiles) + if (file.exists() && !file.isDirectory() + && lastfiles.size() < num_lastfiles) lastfiles.push_back(file); else LYXERR(Debug::INIT) << "LyX: Warning: Ignore last file: " << tmp << endl; @@ -133,8 +128,7 @@ void LastOpenedSection::read(istream & is) continue; FileName const file(tmp); - if (file.exists() && - !fs::is_directory(file.toFilesystemEncoding())) + if (file.exists() && !file.isDirectory()) lastopened.push_back(file); else LYXERR(Debug::INIT) << "LyX: Warning: Ignore last opened file: " << tmp << endl; @@ -188,9 +182,8 @@ void LastFilePosSection::read(istream & is) if (!absolutePath(fname)) continue; FileName const file(fname); - if (file.exists() && - !fs::is_directory(file.toFilesystemEncoding()) && - lastfilepos.size() < num_lastfilepos) + if (file.exists() && !file.isDirectory() + && lastfilepos.size() < num_lastfilepos) lastfilepos[file] = boost::tie(pit, pos); else LYXERR(Debug::INIT) << "LyX: Warning: Ignore pos of last file: " << fname << endl; @@ -269,9 +262,7 @@ void BookmarksSection::read(istream & is) continue; FileName const file(fname); // only load valid bookmarks - if (file.exists() && - !fs::is_directory(file.toFilesystemEncoding()) && - idx <= max_bookmarks) + if (file.exists() && !file.isDirectory() && idx <= max_bookmarks) bookmarks[idx] = Bookmark(file, pit, pos, 0, 0); else LYXERR(Debug::INIT) << "LyX: Warning: Ignore bookmark of file: " << fname << endl; diff --git a/src/frontends/qt4/GuiGraphics.cpp b/src/frontends/qt4/GuiGraphics.cpp index 0806413828..310b660732 100644 --- a/src/frontends/qt4/GuiGraphics.cpp +++ b/src/frontends/qt4/GuiGraphics.cpp @@ -44,7 +44,6 @@ #include "support/types.h" #include -#include #include #include @@ -68,8 +67,6 @@ using std::make_pair; using std::pair; using std::vector; -namespace fs = boost::filesystem; - namespace lyx { namespace frontend { @@ -765,8 +762,8 @@ docstring const GuiGraphics::browse(docstring const & in_name) const // Does user clipart directory exist? string clipdir = addName(package().user_support().absFilename(), "clipart"); - string const encoded_clipdir = FileName(clipdir).toFilesystemEncoding(); - if (!(fs::exists(encoded_clipdir) && fs::is_directory(encoded_clipdir))) + FileName clip(clipdir); + if (!clip.exists() && clip.isDirectory()) // No - bail out to system clipart directory clipdir = addName(package().system_support().absFilename(), "clipart"); pair dir1(_("Clipart|#C#c"), from_utf8(clipdir)); diff --git a/src/insets/ExternalSupport.cpp b/src/insets/ExternalSupport.cpp index 1e58f6774e..97e0940b8d 100644 --- a/src/insets/ExternalSupport.cpp +++ b/src/insets/ExternalSupport.cpp @@ -35,16 +35,12 @@ #include "support/os.h" #include "support/Package.h" -#include - #include using std::endl; using std::string; using std::vector; -using boost::filesystem::is_directory; - namespace lyx { @@ -260,7 +256,7 @@ void updateExternal(InsetExternalParams const & params, FileName const temp_file( support::makeAbsPath(params.filename.mangledFilename(), m_buffer->temppath())); - if (!params.filename.empty() && !is_directory(params.filename.toFilesystemEncoding())) { + if (!params.filename.empty() && !params.filename.isDirectory()) { unsigned long const from_checksum = support::sum(params.filename); unsigned long const temp_checksum = support::sum(temp_file); diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp index 18a910284d..b98beae142 100644 --- a/src/support/FileName.cpp +++ b/src/support/FileName.cpp @@ -87,7 +87,7 @@ bool FileName::exists() const } -bool FileName::isDir() const +bool FileName::isDirectory() const { return QFileInfo(toqstr(name_)).isDir(); } @@ -100,6 +100,13 @@ bool FileName::isReadOnly() const } +bool FileName::isReadable() const +{ + QFileInfo const fi(toqstr(name_)); + return fi.isReadable(); +} + + std::time_t FileName::lastModified() const { return boost::filesystem::last_write_time(toFilesystemEncoding()); diff --git a/src/support/FileName.h b/src/support/FileName.h index 77f5c32d87..b71c554a99 100644 --- a/src/support/FileName.h +++ b/src/support/FileName.h @@ -60,7 +60,9 @@ public: /// return true when file is readable but not writabel bool isReadOnly() const; /// return true when it names a directory - bool isDir() const; + bool isDirectory() const; + /// return true when file is readable + bool isReadable() const; /** * Get a FileName from \p name in the encoding used by the file system. diff --git a/src/support/Package.cpp b/src/support/Package.cpp index 0244a2f2eb..2f3bad68f1 100644 --- a/src/support/Package.cpp +++ b/src/support/Package.cpp @@ -695,8 +695,7 @@ bool check_env_var_dir(FileName const & dir, bool check_env_var_dir(FileName const & dir, string const & env_var) { - string const encoded = dir.toFilesystemEncoding(); - bool const success = (dir.exists() && fs::is_directory(encoded)); + bool const success = dir.exists() && dir.isDirectory(); if (!success) { // Put this string on a single line so that the gettext diff --git a/src/support/filetools.cpp b/src/support/filetools.cpp index 44c8b1e4f8..2d1ad77818 100644 --- a/src/support/filetools.cpp +++ b/src/support/filetools.cpp @@ -224,8 +224,7 @@ vector const dirList(FileName const & dir, string const & ext) // EXCEPTIONS FIXME. Rewrite needed when we turn on exceptions. (Lgb) vector dirlist; - string const encoded_dir = dir.toFilesystemEncoding(); - if (!(fs::exists(encoded_dir) && fs::is_directory(encoded_dir))) { + if (!(dir.exists() && dir.isDirectory())) { LYXERR(Debug::FILES) << "Directory \"" << dir << "\" does not exist to DirList." << endl; @@ -237,6 +236,7 @@ vector const dirList(FileName const & dir, string const & ext) extension += '.'; extension += ext; + string const encoded_dir = dir.toFilesystemEncoding(); fs::directory_iterator dit(encoded_dir); fs::directory_iterator end; for (; dit != end; ++dit) { @@ -1224,23 +1224,20 @@ string const readBB_from_PSFile(FileName const & file) } -int compare_timestamps(FileName const & filename1, FileName const & filename2) +int compare_timestamps(FileName const & file1, FileName const & file2) { // If the original is newer than the copy, then copy the original // to the new directory. - string const file1 = filename1.toFilesystemEncoding(); - string const file2 = filename2.toFilesystemEncoding(); int cmp = 0; - if (fs::exists(file1) && fs::exists(file2)) { - double const tmp = difftime(fs::last_write_time(file1), - fs::last_write_time(file2)); + if (file1.exists() && file2.exists()) { + double const tmp = difftime(file1.lastModified(), file2.lastModified()); if (tmp != 0) cmp = tmp > 0 ? 1 : -1; - } else if (fs::exists(file1)) { + } else if (file1.exists()) { cmp = 1; - } else if (fs::exists(file2)) { + } else if (file2.exists()) { cmp = -1; } diff --git a/src/support/lyxsum.cpp b/src/support/lyxsum.cpp index 568f7e78ee..23c0109f46 100644 --- a/src/support/lyxsum.cpp +++ b/src/support/lyxsum.cpp @@ -11,21 +11,16 @@ #include #include "support/lyxlib.h" - #include "debug.h" - #include "support/FileName.h" #include -#include #include using std::endl; using std::string; -namespace fs = boost::filesystem; - // OK, this is ugly, but it is the only workaround I found to compile // with gcc (any version) on a system which uses a non-GNU toolchain. // The problem is that gcc uses a weak symbol for a particular @@ -124,10 +119,10 @@ unsigned long sum(FileName const & file) LYXERR(Debug::FILES) << "lyx::sum() using istreambuf_iterator (fast)" << endl; - string filename = file.toFilesystemEncoding(); // a directory may be passed here so we need to test it. (bug 3622) - if (fs::is_directory(filename)) + if (file.isDirectory()) return 0; + string filename = file.toFilesystemEncoding(); ifstream ifs(filename.c_str()); if (!ifs) return 0; @@ -148,10 +143,11 @@ unsigned long sum(FileName const & file) << "lyx::sum() using istream_iterator (slow as a snail)" << endl; - string filename = file.toFilesystemEncoding(); // a directory may be passed here so we need to test it. (bug 3622) - if (fs::is_directory(filename)) + if (file.isDirectory()) return 0; + + string filename = file.toFilesystemEncoding(); ifstream ifs(filename.c_str()); if (!ifs) return 0;