From 7f666547c9039e73f9cd07125780f9333b3f1549 Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Wed, 7 Aug 2024 10:49:21 +0200 Subject: [PATCH] Fix file extension check in snapshot function --- ng/ngpkg.cpp | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/ng/ngpkg.cpp b/ng/ngpkg.cpp index e8113567..0627d650 100644 --- a/ng/ngpkg.cpp +++ b/ng/ngpkg.cpp @@ -2130,19 +2130,15 @@ namespace netgen #endif // JPEGLIB { string command; - string filename2; + std::filesystem::path filepath(filename); - filename2 = filename; + bool need_conversion = filepath.extension() != ".ppm"; + if (need_conversion) + filepath += ".ppm"; - if(filename2.substr(len-3) != ".ppm") - filename2 += ".ppm"; + cout << IM(3) << "Snapshot to file '" << filepath.string() << endl; - cout << "Snapshot to file '" << filename << endl; - - // int w = Togl_Width (togl); - // int h = Togl_Height (togl); - - ofstream outfile(filename2); + ofstream outfile(filepath); outfile << "P6" << endl << "# CREATOR: Netgen" << endl << w << " " << h << endl @@ -2153,12 +2149,10 @@ namespace netgen outfile.put (buffer[k+3*j+3*w*(h-i-1)]); outfile << flush; - if (filename2 == string(filename)) - return TCL_OK; - else + if (need_conversion) { // convert image file (Unix/Linux only): - command = string("convert -quality 100 ") + filename2 + " " + filename; + command = string("convert -quality 100 ") + filepath.string() + " " + filename; int err = system(command.c_str()); if (err != 0) @@ -2167,16 +2161,10 @@ namespace netgen return TCL_ERROR; } - command = string("rm ") + filename2; - err = system(command.c_str()); - - if (err != 0) - { - Tcl_SetResult (Togl_Interp(togl), (char*)"Cannot delete temporary file", TCL_VOLATILE); - return TCL_ERROR; - } - return TCL_OK; + std::filesystem::remove(filepath); } + + return TCL_OK; } }