]> git.lyx.org Git - features.git/commitdiff
more use of support::FileName.
authorAndré Pönitz <poenitz@gmx.net>
Wed, 7 Nov 2007 19:52:11 +0000 (19:52 +0000)
committerAndré Pönitz <poenitz@gmx.net>
Wed, 7 Nov 2007 19:52:11 +0000 (19:52 +0000)
Would be nice if people tested read/write, and Bo perhaps the 'embedded'
feature?

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21492 a592a061-630c-0410-9148-cb99ea01b6c8

13 files changed:
src/Buffer.cpp
src/EmbeddedFiles.cpp
src/LyXFunc.cpp
src/VCBackend.cpp
src/buffer_funcs.cpp
src/insets/InsetCitation.cpp
src/support/FileName.cpp
src/support/FileName.h
src/support/Makefile.am
src/support/filetools.cpp
src/support/fs_extras.cpp [deleted file]
src/support/fs_extras.h [deleted file]
src/tex2lyx/tex2lyx.cpp

index 34e55b1fa7a02bbaeff82b2ecef0b71af2608df7..9a961e22eac2f9f6d4a6e4363a8215b001757e8d 100644 (file)
@@ -85,7 +85,6 @@
 #include "support/FileFilterList.h"
 #include "support/filetools.h"
 #include "support/Forkedcall.h"
-#include "support/fs_extras.h"
 #include "support/gzstream.h"
 #include "support/lyxlib.h"
 #include "support/os.h"
@@ -98,8 +97,6 @@
 #endif
 
 #include <boost/bind.hpp>
-#include <boost/filesystem/exception.hpp>
-#include <boost/filesystem/operations.hpp>
 #include <boost/shared_ptr.hpp>
 
 #include <algorithm>
@@ -153,7 +150,6 @@ using support::suffixIs;
 
 namespace Alert = frontend::Alert;
 namespace os = support::os;
-namespace fs = boost::filesystem;
 
 namespace {
 
@@ -859,15 +855,14 @@ bool Buffer::save() const
                        backupName = FileName(addName(lyxrc.backupdir_path,
                                                      mangledName));
                }
-               try {
-                       fs::copy_file(encodedFilename, backupName.toFilesystemEncoding(), false);
+               if (fileName().copyTo(backupName, false)) {
                        madeBackup = true;
-               } catch (fs::filesystem_error const & fe) {
+               } else {
                        Alert::error(_("Backup failure"),
                                     bformat(_("Cannot create backup file %1$s.\n"
                                               "Please check whether the directory exists and is writeable."),
                                             from_utf8(backupName.absFilename())));
-                       LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
+                       //LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
                }
        }
 
index bc7ab0b1006c1afd9947e41eb8330ba1753746c3..abc9eaf8330609be86987bbbab5cd94ddd6669ca 100644 (file)
@@ -28,7 +28,6 @@
 #include <boost/filesystem/operations.hpp>
 
 #include "support/filetools.h"
-#include "support/fs_extras.h"
 #include "support/convert.h"
 #include "support/lyxlib.h"
 #include "support/lstrings.h"
@@ -152,20 +151,20 @@ bool EmbeddedFile::extract(Buffer const * buf) const
                        return true;
        }
        // copy file
-       try {
-               // need to make directory?
-               string path = support::onlyPath(ext_file);
-               if (!fs::is_directory(path))
-                       makedir(const_cast<char*>(path.c_str()), 0755);
-               fs::copy_file(emb_file, ext_file, false);
+
+       // need to make directory?
+       string path = support::onlyPath(ext_file);
+       if (!fs::is_directory(path))
+               makedir(const_cast<char*>(path.c_str()), 0755);
+       FileName emb(emb_file);
+       FileName ext(ext_file);
+       if (emb.copyTo(ext, false))
                return true;
-       } catch (fs::filesystem_error const & fe) {
-               Alert::error(_("Copy file failure"),
-                        bformat(_("Cannot copy file %1$s to %2$s.\n"
-                                  "Please check whether the directory exists and is writeable."),
-                                       from_utf8(emb_file), from_utf8(ext_file)));
-               LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
-       }
+       Alert::error(_("Copy file failure"),
+                bformat(_("Cannot copy file %1$s to %2$s.\n"
+                                "Please check whether the directory exists and is writeable."),
+                               from_utf8(emb_file), from_utf8(ext_file)));
+       //LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
        return false;
 }
 
@@ -194,20 +193,19 @@ bool EmbeddedFile::updateFromExternalFile(Buffer const * buf) const
                        return true;
        }
        // copy file
-       try {
-               // need to make directory?
-               string path = support::onlyPath(emb_file);
-               if (!fs::is_directory(path))
-                       makedir(const_cast<char*>(path.c_str()), 0755);
-               fs::copy_file(ext_file, emb_file, false);
+       FileName emb(emb_file);
+       FileName ext(ext_file);
+       // need to make directory?
+       string path = support::onlyPath(emb_file);
+       if (!fs::is_directory(path))
+               makedir(const_cast<char*>(path.c_str()), 0755);
+       if (ext.copyTo(emb, false))
                return true;
-       } catch (fs::filesystem_error const & fe) {
-               Alert::error(_("Copy file failure"),
-                        bformat(_("Cannot copy file %1$s to %2$s.\n"
-                                  "Please check whether the directory exists and is writeable."),
-                                       from_utf8(ext_file), from_utf8(emb_file)));
-               LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
-       }
+       Alert::error(_("Copy file failure"),
+                bformat(_("Cannot copy file %1$s to %2$s.\n"
+                          "Please check whether the directory exists and is writeable."),
+                               from_utf8(ext_file), from_utf8(emb_file)));
+       //LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
        return false;
 }
 
@@ -304,20 +302,19 @@ bool EmbeddedFiles::writeFile(DocFileName const & filename)
 
        ::zipFiles(zipfile.toFilesystemEncoding(), filenames);
        // copy file back
-       try {
-               fs::copy_file(zipfile.toFilesystemEncoding(), filename.toFilesystemEncoding(), false);
-       } catch (fs::filesystem_error const & fe) {
+       if (!zipfile.copyTo(filename, false)) {
                Alert::error(_("Save failure"),
                                 bformat(_("Cannot create file %1$s.\n"
                                           "Please check whether the directory exists and is writeable."),
                                         from_utf8(filename.absFilename())));
-               LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
+               //LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
        }
        return true;
 }
 
 
-EmbeddedFiles::EmbeddedFileList::const_iterator EmbeddedFiles::find(std::string filename) const
+EmbeddedFiles::EmbeddedFileList::const_iterator
+EmbeddedFiles::find(std::string filename) const
 {
        EmbeddedFileList::const_iterator it = file_list_.begin();
        EmbeddedFileList::const_iterator it_end = file_list_.end();
index 89bcdf1728067ace7ea067672138e57b1a20c2db..baeebe77c374f1cac27aad54f3046ee3b69a4e50 100644 (file)
@@ -88,7 +88,6 @@
 #include "support/environment.h"
 #include "support/FileFilterList.h"
 #include "support/filetools.h"
-#include "support/fs_extras.h"
 #include "support/lstrings.h"
 #include "support/Path.h"
 #include "support/Package.h"
index a405d2cf29251010dd1f1d15d0d9a6a2fbe987a6..3527fb2eee2fae53331ebd838a41bacca9bf772a 100644 (file)
@@ -16,7 +16,6 @@
 
 #include "support/Path.h"
 #include "support/filetools.h"
-#include "support/fs_extras.h"
 #include "support/lstrings.h"
 #include "support/Systemcall.h"
 
index c29fe0543906a9275af3aa1a81cb2396c9a243a6..7aab6ba0e06476e7feb56a16650be8c95f521d12 100644 (file)
 #include "insets/InsetInclude.h"
 
 #include "support/filetools.h"
-#include "support/fs_extras.h"
 #include "support/lyxlib.h"
 
-#include <boost/bind.hpp>
-
 using std::min;
 using std::string;
 
@@ -216,19 +213,16 @@ depth_type getItemDepth(ParIterator const & it)
                Paragraph & prev_par = *prev_it;
                depth_type const prev_depth = getDepth(prev_it);
                if (labeltype == prev_par.layout()->labeltype) {
-                       if (prev_depth < min_depth) {
+                       if (prev_depth < min_depth)
                                return prev_par.itemdepth + 1;
-                       }
-                       else if (prev_depth == min_depth) {
+                       if (prev_depth == min_depth)
                                return prev_par.itemdepth;
-                       }
                }
                min_depth = std::min(min_depth, prev_depth);
                // small optimization: if we are at depth 0, we won't
                // find anything else
-               if (prev_depth == 0) {
+               if (prev_depth == 0)
                        return 0;
-               }
        }
 }
 
index 69367f6afb175d530348613ae0ffd90e3e70ec99..fb2601d8363891b4930bf5ffe7ea550a1c826af0 100644 (file)
@@ -20,7 +20,6 @@
 #include "FuncRequest.h"
 #include "LaTeXFeatures.h"
 
-#include "support/fs_extras.h"
 #include "support/lstrings.h"
 
 #include <algorithm>
index 4d545936828b2f69569cc376716016cbdd3435c8..ba2a653ecdcb09019046b2dea14787179a134cbf 100644 (file)
@@ -39,6 +39,98 @@ using std::endl;
 
 namespace fs = boost::filesystem;
 
+// FIXME: merge this
+//
+#include <boost/filesystem/config.hpp>
+#include <boost/detail/workaround.hpp>
+#include <boost/throw_exception.hpp>
+
+#ifdef HAVE_SYS_TYPES_H
+# include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+# include <sys/stat.h>
+#endif
+#include <cerrno>
+#include <fcntl.h>
+
+
+// BOOST_POSIX or BOOST_WINDOWS specify which API to use.
+# if !defined( BOOST_WINDOWS ) && !defined( BOOST_POSIX )
+#   if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__CYGWIN__)
+#     define BOOST_WINDOWS
+#   else
+#     define BOOST_POSIX
+#   endif
+# endif
+
+#if defined (BOOST_WINDOWS)
+# define WIN32_LEAN_AND_MEAN
+# include <windows.h>
+#endif
+
+
+static bool copy_file(std::string const & source, std::string const & target, bool noclobber)
+{
+
+#ifdef BOOST_POSIX
+       int const infile = ::open(source.c_str(), O_RDONLY);
+       if (infile == -1)
+               return false;
+
+       struct stat source_stat;
+       int const ret = ::fstat(infile, &source_stat);
+       if (ret == -1) {
+               //int err = errno;
+               ::close(infile);
+       }
+
+       int const flags = O_WRONLY | O_CREAT | (noclobber ? O_EXCL : O_TRUNC);
+
+       int const outfile = ::open(target.c_str(), flags, source_stat.st_mode);
+       if (outfile == -1) {
+               int err = errno;
+               ::close(infile);
+               return false;
+       }
+
+       std::size_t const buf_sz = 32768;
+       char buf[buf_sz];
+       ssize_t in = -1;
+       ssize_t out = -1;
+
+       while (true) {
+               in = ::read(infile, buf, buf_sz);
+               if (in == -1) {
+                       break;
+               } else if (in == 0) {
+                       break;
+               } else {
+                       out = ::write(outfile, buf, in);
+                       if (out == -1) {
+                               break;
+                       }
+               }
+       }
+
+       int err = errno;
+
+       ::close(infile);
+       ::close(outfile);
+
+       if (in == -1 || out == -1)
+               return false;
+#endif
+
+#ifdef BOOST_WINDOWS
+       if (::CopyFile(source.c_str(), target.c_str(), noclobber) == 0) {
+               // CopyFile is probably not setting errno so this is most
+               // likely wrong.
+               return false;
+       }
+#endif
+}
+
 namespace lyx {
 namespace support {
 
@@ -76,6 +168,18 @@ void FileName::erase()
 }
 
 
+bool FileName::copyTo(FileName const & name, bool noclobber) const
+{
+       try {
+               copy_file(toFilesystemEncoding(), name.toFilesystemEncoding(), noclobber);
+               return true;
+       }
+       catch (...) {
+       }
+       return false;
+}
+
+
 string FileName::toFilesystemEncoding() const
 {
        QByteArray const encoded = QFile::encodeName(toqstr(name_));
@@ -568,3 +672,6 @@ bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
 
 } // namespace support
 } // namespace lyx
+
+
+
index 188fa6bd761a90c897a171cd3e68f2e88e2aa652..a2b1c3c48d6fe3b7ca19cfd611109dab28fb063b 100644 (file)
@@ -72,6 +72,9 @@ public:
        /// return true when file/directory is writable (write test file)
        bool isDirWritable() const;
        
+       /// return true when file/directory is writable (write test file)
+       bool copyTo(FileName const & target, bool noclobber) const;
+
        /// remove directory and all contents, returns true on success
        bool destroyDirectory() const;
        /// Creates directory. Returns true on success
index 50887db40a61ac6fb965c87cfded4002006ad7f7..bb84cc85ca2fb2ca50627d143c9ac06ebd350707 100644 (file)
@@ -50,8 +50,6 @@ liblyxsupport_la_SOURCES = \
        ForkedCallQueue.h \
        ForkedcallsController.cpp \
        ForkedcallsController.h \
-       fs_extras.cpp \
-       fs_extras.h \
        getcwd.cpp \
        gzstream.cpp \
        gzstream.h \
index 0d04782818f06c4afeec3a189a813a177a52a118..7901119a6f7bebe19c54c98e2bd92d1f4b30610e 100644 (file)
@@ -24,7 +24,6 @@
 #include "support/convert.h"
 #include "support/environment.h"
 #include "support/filetools.h"
-#include "support/fs_extras.h"
 #include "support/lstrings.h"
 #include "support/lyxlib.h"
 #include "support/os.h"
@@ -396,20 +395,19 @@ string const createBufferTmpDir()
 
 FileName const createLyXTmpDir(FileName const & deflt)
 {
-       if (!deflt.empty() && deflt.absFilename() != "/tmp") {
-               if (mkdir(deflt, 0777)) {
-                       if (deflt.isDirWritable()) {
-                               // deflt could not be created because it
-                               // did exist already, so let's create our own
-                               // dir inside deflt.
-                               return createTmpDir(deflt, "lyx_tmpdir");
-                       } else {
-                               // some other error occured.
-                               return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
-                       }
-               } else
-                       return deflt;
+       if (deflt.empty() || deflt.absFilename() == "/tmp")
+               return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
+
+       if (!mkdir(deflt, 0777)) 
+               return deflt;
+
+       if (deflt.isDirWritable()) {
+               // deflt could not be created because it
+               // did exist already, so let's create our own
+               // dir inside deflt.
+               return createTmpDir(deflt, "lyx_tmpdir");
        } else {
+               // some other error occured.
                return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
        }
 }
@@ -423,7 +421,7 @@ string const onlyPath(string const & filename)
                return filename;
 
        // Find last / or start of filename
-       string::size_type j = filename.rfind('/');
+       size_t j = filename.rfind('/');
        return j == string::npos ? "./" : filename.substr(0, j + 1);
 }
 
diff --git a/src/support/fs_extras.cpp b/src/support/fs_extras.cpp
deleted file mode 100644 (file)
index 0a3dfe0..0000000
+++ /dev/null
@@ -1,131 +0,0 @@
-// -*- C++ -*-
-/* \file fs_extras.cpp
- * This file is part of LyX, the document processor.
- * Licence details can be found in the file COPYING.
- *
- * \author Lars Gullik Bjønnes
- *
- * Full author contact details are available in file CREDITS.
- */
-
-
-#include <config.h>
-
-#include "fs_extras.h"
-
-#include <boost/filesystem/config.hpp>
-#include <boost/detail/workaround.hpp>
-#include <boost/throw_exception.hpp>
-
-#ifdef HAVE_SYS_TYPES_H
-# include <sys/types.h>
-#endif
-#ifdef HAVE_SYS_STAT_H
-# include <sys/stat.h>
-#endif
-#include <cerrno>
-#include <fcntl.h>
-
-
-// BOOST_POSIX or BOOST_WINDOWS specify which API to use.
-# if !defined( BOOST_WINDOWS ) && !defined( BOOST_POSIX )
-#   if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__CYGWIN__)
-#     define BOOST_WINDOWS
-#   else
-#     define BOOST_POSIX
-#   endif
-# endif
-
-#if defined (BOOST_WINDOWS)
-# define WIN32_LEAN_AND_MEAN
-# include <windows.h>
-#endif
-
-namespace fs = boost::filesystem;
-
-namespace boost {
-namespace filesystem {
-
-void copy_file(path const & source, path const & target, bool noclobber)
-{
-
-#ifdef BOOST_POSIX
-       int const infile = ::open(source.string().c_str(), O_RDONLY);
-       if (infile == -1) {
-               boost::throw_exception(
-                       filesystem_path_error(
-                               "boost::filesystem::copy_file",
-                               source, target,
-                               fs::lookup_errno(errno)));
-       }
-
-               struct stat source_stat;
-               int const ret = ::fstat(infile, &source_stat);
-               if (ret == -1) {
-               int err = errno;
-               ::close(infile);
-               boost::throw_exception(filesystem_path_error(
-                          "boost::filesystem::copy_file",
-                          source, target,
-                          fs::lookup_errno(err)));
-       }
-
-       int const flags = O_WRONLY | O_CREAT | (noclobber ? O_EXCL : O_TRUNC);
-
-       int const outfile = ::open(target.string().c_str(), flags, source_stat.st_mode);
-       if (outfile == -1) {
-               int err = errno;
-               ::close(infile);
-               boost::throw_exception(
-                       filesystem_path_error(
-                               "boost::filesystem::copy_file",
-                               source, target,
-                               fs::lookup_errno(err)));
-       }
-
-       std::size_t const buf_sz = 32768;
-       char buf[buf_sz];
-       ssize_t in = -1;
-       ssize_t out = -1;
-
-       while (true) {
-               in = ::read(infile, buf, buf_sz);
-               if (in == -1) {
-                       break;
-               } else if (in == 0) {
-                       break;
-               } else {
-                       out = ::write(outfile, buf, in);
-                       if (out == -1) {
-                               break;
-                       }
-               }
-       }
-
-       int err = errno;
-
-       ::close(infile);
-       ::close(outfile);
-
-       if (in == -1 || out == -1)
-               boost::throw_exception(
-                       filesystem_path_error(
-                               "boost::filesystem::copy_file",
-                               source, target,
-                               fs::lookup_errno(err)));
-#endif
-#ifdef BOOST_WINDOWS
-       if (::CopyFile(source.string().c_str(), target.string().c_str(), noclobber) == 0) {
-               // CopyFile is probably not setting errno so this is most
-               // likely wrong.
-               boost::throw_exception(
-                       filesystem_path_error(
-                               "boost::filesystem::copy_file",
-                               source, target,
-                               fs::lookup_error_code(errno)));
-       }
-#endif
-}
-
-}
-}
diff --git a/src/support/fs_extras.h b/src/support/fs_extras.h
deleted file mode 100644 (file)
index cdb0267..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-// -*- C++ -*-
-/* \file fs_extras.h
- * This file is part of LyX, the document processor.
- * Licence details can be found in the file COPYING.
- *
- * \author Lars Gullik Bjønnes
- *
- * Full author contact details are available in file CREDITS.
- */
-
-#include <boost/filesystem/path.hpp>
-
-
-namespace boost {
-namespace filesystem {
-
-void copy_file(path const & source, path const & target, bool noclobber);
-
-}
-}
index f2af56d9a7c5b28d64c6c683f4b6f232f4978701..5c987b177bada98dd971b55da0071e1d093aa740 100644 (file)
@@ -21,7 +21,6 @@
 
 #include "support/convert.h"
 #include "support/filetools.h"
-#include "support/fs_extras.h"
 #include "support/lstrings.h"
 #include "support/lyxlib.h"
 #include "support/ExceptionMessage.h"