On Tue, Dec 19, 2006 at 02:08:11PM +0100, Georg Baum wrote:
> Enrico Forestieri wrote:
>
> > Been there, done that. The attached would be the patch you are looking
> > for, but it only fixes the assertion, not the other bug I mention.
> > This is because fileSearch is designed to replace an extension, not to
> > add one.
>
> I don't think that was intended. I rather think that the problem is that
> changeExtension does not really know if a file has no extension. Maybe we
> should introduce aan addExtension and use that if we know that a filename
> has no extension?

I think that fileSearch was intended to have always passed filenames with
extensions. However, an addExtension method seems too trivial to be
implemented on its own.

> > Of course, it would be possible to modify fileSearch such that
> > it adds the extension when must_be_readable is false, but I find this
> > approach rather ugly.
>
> I agree. Nevertheless I like your second patch much more, because it makes
> the intention clear. In a second step we can introduce addExtension() and
> use that in fileSearch.

What about the attached patch?

-- 
Enrico
Index: src/support/filetools.C
===================================================================
--- src/support/filetools.C     (revision 16339)
+++ src/support/filetools.C     (working copy)
@@ -250,7 +250,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, int mode)
 {
        // if `name' is an absolute path, we ignore the setting of `path'
        // Expand Environmentvariables in 'name'
@@ -259,9 +259,14 @@ FileName const fileSearch(string const &
        // search first without extension, then with it.
        if (isFileReadable(fullname))
                return fullname;
-       if (ext.empty())
-               return FileName();
-       fullname = FileName(changeExtension(fullname.absFilename(), ext));
+       if (ext.empty() || getExtension(fullname.absFilename()) == ext)
+               return (mode & allow_unreadable) ? fullname : FileName();
+       if (mode & add_extension)
+               fullname = FileName(fullname.absFilename() + '.' + ext);
+       else
+               fullname = FileName(changeExtension(fullname.absFilename(), 
ext));
+       if (mode & allow_unreadable)
+               return fullname;
        return isFileReadable(fullname) ? fullname : FileName();
 }
 
Index: src/support/filetools.h
===================================================================
--- src/support/filetools.h     (revision 16339)
+++ src/support/filetools.h     (working copy)
@@ -50,6 +50,15 @@ FileName const fileOpenSearch(std::strin
                                 std::string const & name,
                                 std::string const & ext = std::string());
 
+/// How to deal with status and extension of a file when searching for it.
+/// Default: a file must be readable and the extension will replace an
+/// existing one, rather than being unconditionally added.
+enum search_mode {
+       standard_mode    = 0,
+       allow_unreadable = 1 << 0,
+       add_extension    = 1 << 1
+};
+
 /** Returns the real name of file name in directory path, with optional
   extension ext.
   The file is searched in the given path (unless it is an absolute
@@ -57,7 +66,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(),
+                            int mode = standard_mode);
 
 /// Returns a vector of all files in directory dir having extension ext.
 std::vector<std::string> const dirList(FileName const & dir,
Index: src/lyx_main.C
===================================================================
--- src/lyx_main.C      (revision 16339)
+++ src/lyx_main.C      (working copy)
@@ -77,6 +77,8 @@ using support::createLyXTmpDir;
 using support::destroyDir;
 using support::FileName;
 using support::fileSearch;
+using support::allow_unreadable;
+using support::add_extension;
 using support::getEnv;
 using support::i18nLibFileSearch;
 using support::libFileSearch;
@@ -480,7 +482,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", allow_unreadable | add_extension));
        }
 
        if (first_start)

Reply via email to