Add proper extension as needed in Save as and Export

Introduce the new FileName method ensureExtension, which does the following:
* if the extension is already correct (in a case-insensitive way), do nothing.
* if it is not correct, add the extension to the file name.

This is different from changeExtension that will fail in a case where
the file contains dots, but not a real extension, like newfile2.1.

Use this new method in renameBuffer() and exportBufferAs().

Fixes bug #11008.
This commit is contained in:
Jean-Marc Lasgouttes 2022-07-17 20:10:53 +02:00
parent d7eecccfd4
commit 61d68d05bd
3 changed files with 20 additions and 4 deletions

View File

@ -3080,8 +3080,7 @@ bool GuiView::renameBuffer(Buffer & b, docstring const & newname, RenameKind kin
dlg.setButton1(qt_("D&ocuments"), toqstr(lyxrc.document_path));
dlg.setButton2(qt_("&Templates"), toqstr(lyxrc.template_path));
if (!isLyXFileName(fname.absFileName()))
fname.changeExtension(".lyx");
fname.ensureExtension(".lyx");
string const path = as_template ?
getTemplatesPath(b)
@ -3099,8 +3098,7 @@ bool GuiView::renameBuffer(Buffer & b, docstring const & newname, RenameKind kin
if (fname.empty())
return false;
if (!isLyXFileName(fname.absFileName()))
fname.changeExtension(".lyx");
fname.ensureExtension(".lyx");
}
// fname is now the new Buffer location.
@ -3248,6 +3246,8 @@ bool GuiView::exportBufferAs(Buffer & b, docstring const & iformat)
if (fmt_name.empty() || fname.empty())
return false;
fname.ensureExtension(theFormats().extension(fmt_name));
// fname is now the new Buffer location.
if (fname.exists()) {
docstring const file = makeDisplayPath(fname.absFileName(), 30);

View File

@ -812,6 +812,19 @@ void FileName::changeExtension(string const & extension)
}
void FileName::ensureExtension(string const & extension)
{
string ext;
// Make sure the extension starts with a dot
if (!extension.empty() && extension[0] != '.')
ext= '.' + extension;
else
ext = extension;
if (!suffixIs(ascii_lowercase(absFileName()), ext))
set(absFileName() + ext);
}
docstring const FileName::relPath(string const & path) const
{
// FIXME UNICODE

View File

@ -177,6 +177,9 @@ public:
*/
void changeExtension(std::string const & extension);
/// Add extension to the file name if it is not already there
void ensureExtension(std::string const & extension);
static FileName fromFilesystemEncoding(std::string const & name);
/// get the current working directory