New helper function, currently used by insetgraphics, but also used by 
insetexternal in my tree.

-- 
Angus
Index: insets/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/ChangeLog,v
retrieving revision 1.696
diff -u -p -r1.696 ChangeLog
--- insets/ChangeLog	2 Jun 2003 16:40:38 -0000	1.696
+++ insets/ChangeLog	2 Jun 2003 17:22:49 -0000
@@ -1,3 +1,7 @@
+2003-06-02  Angus Leeming  <[EMAIL PROTECTED]>
+
+	* insetgraphics.C (prepareFile): use new helper function copy_file_to_dir.
+
 2003-06-02  John Levon  <[EMAIL PROTECTED]>
 
 	* insettabular.C: fix resize of column width
Index: insets/insetgraphics.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetgraphics.C,v
retrieving revision 1.179
diff -u -p -r1.179 insetgraphics.C
--- insets/insetgraphics.C	2 Jun 2003 10:03:22 -0000	1.179
+++ insets/insetgraphics.C	2 Jun 2003 17:22:50 -0000
@@ -602,31 +602,14 @@ string const InsetGraphics::prepareFile(
 		<< "\tthe orig file is: " << orig_file_with_path << endl;
 
 	if (lyxrc.use_tempdir) {
-		string const ext_tmp = GetExtension(orig_file_with_path);
-		// without ext and /
-		temp_file = subst(
-			ChangeExtension(orig_file_with_path, string()), "/", "_");
-		// without dots and again with ext
-		temp_file = ChangeExtension(
-			subst(temp_file, ".", "_"), ext_tmp);
-		// now we have any_dir_file.ext
-		temp_file = MakeAbsPath(temp_file, buf->tmppath);
-		lyxerr[Debug::GRAPHICS]
-			<< "\tchanged to: " << temp_file << endl;
-
-		// if the file doen't exists, copy it into the tempdir
-		if (file_has_changed || !IsFileReadable(temp_file)) {
-			bool const success = lyx::copy(orig_file_with_path, temp_file);
-			lyxerr[Debug::GRAPHICS]
-				<< "\tcopying from " << orig_file_with_path << " to "
-				<< temp_file
-				<< (success ? " succeeded\n" : " failed\n");
-			if (!success) {
-				string str = bformat(_("Could not copy the file\n%1$s\n"
-					"into the temporary directory."), orig_file_with_path);
-				Alert::error(_("Graphics display failed"), str);
-				return orig_file;
-			}
+		temp_file = copy_file_to_dir(buf->tmppath,
+					     orig_file_with_path);
+		if (temp_file.empty()) {
+			string str = bformat(_("Could not copy the file\n%1$s\n"
+					       "into the temporary directory."),
+					     orig_file_with_path);
+			Alert::error(_("Graphics display failed"), str);
+			return orig_file;
 		}
 
 		if (from == to) {
Index: support/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/ChangeLog,v
retrieving revision 1.174
diff -u -p -r1.174 ChangeLog
--- support/ChangeLog	1 Jun 2003 20:49:48 -0000	1.174
+++ support/ChangeLog	2 Jun 2003 17:22:51 -0000
@@ -1,3 +1,7 @@
+2003-06-02  Angus Leeming  <[EMAIL PROTECTED]>
+
+	* filetools.[Ch] (copy_file_to_dir): new helper function.
+
 2003-06-01  Angus Leeming  <[EMAIL PROTECTED]>
 
 	* filetools.h (LibScriptSearch): give the function a meaningful
Index: support/filetools.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/filetools.h,v
retrieving revision 1.42
diff -u -p -r1.42 filetools.h
--- support/filetools.h	1 Jun 2003 20:49:48 -0000	1.42
+++ support/filetools.h	2 Jun 2003 17:22:51 -0000
@@ -200,6 +200,13 @@ void removeAutosaveFile(string const & f
 /// 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 copy_file_to_dir(string const & path, string const & file);
+
 typedef std::pair<int, string> cmd_ret;
 
 cmd_ret const RunCommand(string const & cmd);
Index: support/filetools.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/filetools.C,v
retrieving revision 1.152
diff -u -p -r1.152 filetools.C
--- support/filetools.C	31 May 2003 09:24:46 -0000	1.152
+++ support/filetools.C	2 Jun 2003 17:22:53 -0000
@@ -1320,3 +1320,42 @@ string const readBB_from_PSFile(string c
 	readBB_lyxerrMessage(file_, zipped, "no bb found");
 	return string();
 }
+
+
+string copy_file_to_dir(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 = 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();
+}
+
+

Reply via email to