Am Dienstag, 19. Dezember 2006 22:54 schrieb Enrico Forestieri:

> I think that fileSearch was intended to have always passed filenames with
> extensions.

No, I checked that. fileSearch does either get a filename without an 
extension, or a filename with the wanted extension (see e.g. line 1187 in 
lyxfunc.C). That makes sense to me: It allows the user to omit the 
extension of a filename if there is a default extension.

> However, an addExtension method seems too trivial to be 
> implemented on its own.

It is indeed trivial, but it is very useful to avoid a misunderstanding 
like the one with fileSearch above. I know of more places where it can be 
used, and I implemented removeExtension for the very same reason (after a 
suggestion by Angus IIRC).

> What about the attached patch?

I modified it, for the reasons I gave above. Would this one be OK with you?

BTW does anybody knows what the purpose of fileOpenSearch was? Do we want 
to use that somewhere, or shall I delete it?


Georg
Index: src/support/filetools.C
===================================================================
--- src/support/filetools.C	(Revision 16356)
+++ src/support/filetools.C	(Arbeitskopie)
@@ -179,6 +179,7 @@ bool isDirWriteable(string const & path)
 }
 
 
+#if 0
 // Uses a string of paths separated by ";"s to find a file to open.
 // Can't cope with pathnames with a ';' in them. Returns full path to file.
 // If path entry begins with $$LyX/, use system_lyxdir
@@ -213,6 +214,7 @@ FileName const fileOpenSearch(string con
 	}
 	return real_file;
 }
+#endif
 
 
 /// Returns a vector of all files in directory dir having extension ext.
@@ -250,7 +252,7 @@ vector<string> const dirList(FileName co
 // Returns the real name of file name in directory path, with optional
 // extension ext.
 FileName const fileSearch(string const & path, string const & name,
-			string const & ext)
+                          string const & ext, search_mode mode)
 {
 	// if `name' is an absolute path, we ignore the setting of `path'
 	// Expand Environmentvariables in 'name'
@@ -260,9 +262,15 @@ FileName const fileSearch(string const &
 	if (isFileReadable(fullname))
 		return fullname;
 	if (ext.empty())
-		return FileName();
-	fullname = FileName(changeExtension(fullname.absFilename(), ext));
-	return isFileReadable(fullname) ? fullname : FileName();
+		// We are done.
+		return mode == allow_unreadable ? fullname : FileName();
+	// Only add the extension if it is not already the extension of
+	// fullname.
+	if (getExtension(fullname.absFilename()) != ext)
+		fullname = FileName(addExtension(fullname.absFilename(), ext));
+	if (isFileReadable(fullname) || mode == allow_unreadable)
+		return fullname;
+	return FileName();
 }
 
 
@@ -721,11 +729,6 @@ string const addPath(string const & path
 }
 
 
-/*
- Change extension of oldname to extension.
- Strips path off if no_path == true.
- If no extension on oldname, just appends.
- */
 string const changeExtension(string const & oldname, string const & extension)
 {
 	string::size_type const last_slash = oldname.rfind('/');
@@ -750,6 +753,14 @@ string const removeExtension(string cons
 }
 
 
+string const addExtension(string const & name, string const & extension)
+{
+	if (!extension.empty() && extension[0] != '.')
+		return name + '.' + extension;
+	return name + extension;
+}
+
+
 /// Return the extension of the file (not including the .)
 string const getExtension(string const & name)
 {
Index: src/support/filetools.h
===================================================================
--- src/support/filetools.h	(Revision 16356)
+++ src/support/filetools.h	(Arbeitskopie)
@@ -39,6 +39,8 @@ bool createDirectory(std::string const &
  */
 FileName const createLyXTmpDir(FileName const & deflt);
 
+#if 0
+// FIXME unused. Should this be deleted or resurrected?
 /** Find file by searching several directories.
   Uses a string of paths separated by ";"s to find a file to open.
     Can't cope with pathnames with a ';' in them. Returns full path to file.
@@ -49,6 +51,16 @@ FileName const createLyXTmpDir(FileName 
 FileName const fileOpenSearch(std::string const & path,
 				 std::string const & name,
 				 std::string const & ext = std::string());
+#endif
+
+/// How to search files
+enum search_mode {
+	// The file must exist (return an empty file name otherwise)
+	standard_mode,
+	/// Only do file name expansion, return the complete name even if
+	/// the file does not exist
+	allow_unreadable
+};
 
 /** Returns the real name of file name in directory path, with optional
   extension ext.
@@ -57,7 +69,8 @@ FileName const fileOpenSearch(std::strin
   */
 FileName const fileSearch(std::string const & path,
 			     std::string const & name,
-			     std::string const & ext = std::string());
+			     std::string const & ext = std::string(),
+			     search_mode mode = standard_mode);
 
 /// Returns a vector of all files in directory dir having extension ext.
 std::vector<std::string> const dirList(FileName const & dir,
@@ -180,6 +193,14 @@ changeExtension(std::string const & oldn
 /// Remove the extension from \p name
 std::string const removeExtension(std::string const & name);
 
+/** Add the extension \p ext to \p name.
+ Use this instead of changeExtension if you know that \p name is without
+ extension, because changeExtension would wrongly interpret \p name if it
+ contains a dot.
+ */
+std::string const
+addExtension(std::string const & name, std::string const & extension);
+
 /// Return the extension of the file (not including the .)
 std::string const getExtension(std::string const & name);
 
Index: src/lyx_main.C
===================================================================
--- src/lyx_main.C	(Revision 16356)
+++ src/lyx_main.C	(Arbeitskopie)
@@ -480,7 +480,8 @@ int LyX::init(int & argc, char * argv[])
 		// get absolute path of file and add ".lyx" to
 		// the filename if necessary
 		pimpl_->files_to_load_.push_back(fileSearch(string(),
-			os::internal_path(to_utf8(from_local8bit(argv[argi]))), "lyx"));
+			os::internal_path(to_utf8(from_local8bit(argv[argi]))),
+			"lyx", support::allow_unreadable));
 	}
 
 	if (first_start)

Reply via email to