From 6412ac52b6bf44f80f95deb8804a7613668a613f Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Tue, 3 Jun 2003 10:14:52 +0000 Subject: [PATCH] Add new copyFileToDir helper function. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7091 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/support/ChangeLog | 6 ++++++ src/support/filetools.C | 37 +++++++++++++++++++++++++++++++++++++ src/support/filetools.h | 8 ++++++++ src/support/os.h | 2 +- 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/support/ChangeLog b/src/support/ChangeLog index 659d5d1ca0..7ab024522d 100644 --- a/src/support/ChangeLog +++ b/src/support/ChangeLog @@ -1,3 +1,9 @@ +2003-06-02 Angus Leeming + + * filetools.[Ch] (copyFileToDir): new helper function. + + * os.h: remove trailing semi-colon from end of namespace os block. + 2003-06-01 Angus Leeming * filetools.h (LibScriptSearch): give the function a meaningful diff --git a/src/support/filetools.C b/src/support/filetools.C index 4cff72828d..c808c461a8 100644 --- a/src/support/filetools.C +++ b/src/support/filetools.C @@ -1320,3 +1320,40 @@ string const readBB_from_PSFile(string const & file) readBB_lyxerrMessage(file_, zipped, "no bb found"); return string(); } + + +string copyFileToDir(string const & path, string const & file_in) +{ + lyx::Assert(AbsolutePath(path)); + + // First, make the file path relative to path. + string file_out = MakeRelPath(path, NormalizePath(file_in)); + file_out = os::slashify_path(file_out); + + // Now generate a unique filename. + // Remove the extension. + file_out = ChangeExtension(file_out, string()); + // Replace '/' in the file name with '_' + file_out = subst(file_out, "/", "_"); + // Replace '.' in the file name with '_' + file_out = subst(file_out, ".", "_"); + // Add the extension back on + file_out = ChangeExtension(file_out, GetExtension(file_in)); + // Put this file in the buffer's temp dir + file_out = MakeAbsPath(file_out, path); + + // If the original is newer than the copy, then copy the original + // to the new directory. + FileInfo fi(file_in); + FileInfo fi2(file_out); + + bool success = true; + if (fi.exist()) { + if (!fi2.exist() || + difftime(fi.getModificationTime(), + fi2.getModificationTime()) >= 0) + success = lyx::copy(file_in, file_out); + } + + return success ? file_out : string(); +} diff --git a/src/support/filetools.h b/src/support/filetools.h index 7baca7d90d..e5cd6855a5 100644 --- a/src/support/filetools.h +++ b/src/support/filetools.h @@ -11,6 +11,7 @@ #include #include #include "LString.h" +#include "os.h" /// remove directory and all contents, returns 0 on success @@ -200,6 +201,13 @@ void removeAutosaveFile(string const & filename); /// read the BoundingBox entry from a ps/eps/pdf-file string const readBB_from_PSFile(string const & file); +/** Copy \param file to directory \param path. The file name is manipulated + so that eg some/path/to/file becomes some_path_to_file. + \returns this file name if the file is copied successfully, else + \returns an empty string. + */ +string copyFileToDir(string const & path, string const & file); + typedef std::pair cmd_ret; cmd_ret const RunCommand(string const & cmd); diff --git a/src/support/os.h b/src/support/os.h index 0364f724a4..330e9a801f 100644 --- a/src/support/os.h +++ b/src/support/os.h @@ -43,6 +43,6 @@ namespace os { char const * popen_read_mode(); // void warn(string const & mesg); -}; +} #endif -- 2.39.2