[LyX/master] tex2lyx: exit earlier if input file could not be found

Juergen Spitzmueller spitz at lyx.org
Wed Dec 28 12:09:02 UTC 2022


commit 9047d60c5a5ddb17e810116bf9e21db7450d0a8f
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Wed Dec 28 14:05:23 2022 +0100

    tex2lyx: exit earlier if input file could not be found
    
    In particular, do not write an empty .lyx file (#12339)
---
 src/tex2lyx/tex2lyx.cpp |   25 +++++++++++++++----------
 1 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/src/tex2lyx/tex2lyx.cpp b/src/tex2lyx/tex2lyx.cpp
index 308d5b1..8e3b667 100644
--- a/src/tex2lyx/tex2lyx.cpp
+++ b/src/tex2lyx/tex2lyx.cpp
@@ -979,16 +979,9 @@ bool tex2lyx(idocstream & is, ostream & os, string const & encoding,
 
 
 /// convert TeX from \p infilename to LyX and write it to \p os
-bool tex2lyx(FileName const & infilename_in, ostream & os, string encoding,
+bool tex2lyx(FileName const & infilename, ostream & os, string encoding,
              string const & outfiledir)
 {
-	FileName infilename = infilename_in;
-	// Like TeX, we consider files without extensions as *.tex files
-	// and append the extension if the file without ext does not exist
-	// (#12340)
-	if (!infilename.exists() && infilename.extension().empty())
-		infilename.changeExtension("tex");
-
 	// Set a sensible default encoding.
 	// This is used until an encoding command is found.
 	// For child documents use the encoding of the master, else try to
@@ -1046,6 +1039,18 @@ bool tex2lyx(FileName const & infilename_in, ostream & os, string encoding,
 bool tex2lyx(string const & infilename, FileName const & outfilename,
 	     string const & encoding)
 {
+	FileName ifname = FileName(infilename);
+	// Like TeX, we consider files without extensions as *.tex files
+	// and append the extension if the file without ext does not exist
+	// (#12340)
+	if (!ifname.exists() && ifname.extension().empty()) {
+		ifname.changeExtension("tex");
+		if (!ifname.exists()) {
+			cerr << "Could not open input file \"" << infilename
+			     << "\" for reading." << endl;
+			return false;
+		}
+	}
 	if (outfilename.isReadableFile()) {
 		if (overwrite_files) {
 			cerr << "Overwriting existing file "
@@ -1065,10 +1070,10 @@ bool tex2lyx(string const & infilename, FileName const & outfilename,
 		return false;
 	}
 #ifdef FILEDEBUG
-	cerr << "Input file: " << infilename << "\n";
+	cerr << "Input file: " << ifname << "\n";
 	cerr << "Output file: " << outfilename << "\n";
 #endif
-	return tex2lyx(FileName(infilename), os, encoding,
+	return tex2lyx(ifname, os, encoding,
 	               outfilename.onlyPath().absFileName() + '/');
 }
 


More information about the lyx-cvs mailing list