|
|
@@ -1,9 +1,9 @@
|
|
|
// This file is part of libigl, a simple c++ geometry processing library.
|
|
|
-//
|
|
|
+//
|
|
|
// Copyright (C) 2014 Daniele Panozzo <[email protected]>
|
|
|
-//
|
|
|
-// This Source Code Form is subject to the terms of the Mozilla Public License
|
|
|
-// v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
|
|
+//
|
|
|
+// This Source Code Form is subject to the terms of the Mozilla Public License
|
|
|
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
|
|
// obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
#include "file_dialog_save.h"
|
|
|
#include <cstdio>
|
|
|
@@ -18,6 +18,9 @@ IGL_INLINE std::string igl::file_dialog_save()
|
|
|
{
|
|
|
const int FILE_DIALOG_MAX_BUFFER = 1024;
|
|
|
char buffer[FILE_DIALOG_MAX_BUFFER];
|
|
|
+ buffer[0] = '\0';
|
|
|
+ buffer[FILE_DIALOG_MAX_BUFFER - 1] = 'x'; // Initialize last character with a char != '\0'
|
|
|
+
|
|
|
#ifdef __APPLE__
|
|
|
// For apple use applescript hack
|
|
|
// There is currently a bug in Applescript that strips extensions off
|
|
|
@@ -31,8 +34,19 @@ IGL_INLINE std::string igl::file_dialog_save()
|
|
|
" end tell\n"
|
|
|
" set existing_file_path to (POSIX path of (existing_file))\n"
|
|
|
"\" 2>/dev/null | tr -d '\n' ","r");
|
|
|
- while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL )
|
|
|
+ if (output)
|
|
|
{
|
|
|
+ auto ret = fgets(buffer, FILE_DIALOG_MAX_BUFFER, output);
|
|
|
+ if (ret == NULL || ferror(output))
|
|
|
+ {
|
|
|
+ // I/O error
|
|
|
+ buffer[0] = '\0';
|
|
|
+ }
|
|
|
+ if (buffer[FILE_DIALOG_MAX_BUFFER - 1] == '\0')
|
|
|
+ {
|
|
|
+ // File name too long, buffer has been filled, so we return empty string instead
|
|
|
+ buffer[0] = '\0';
|
|
|
+ }
|
|
|
}
|
|
|
#elif defined _WIN32
|
|
|
|
|
|
@@ -47,7 +61,7 @@ IGL_INLINE std::string igl::file_dialog_save()
|
|
|
ofn.lStructSize = sizeof(ofn);
|
|
|
ofn.hwndOwner = NULL;//hwnd;
|
|
|
ofn.lpstrFile = new char[100];
|
|
|
- // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
|
|
|
+ // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
|
|
|
// use the contents of szFile to initialize itself.
|
|
|
ofn.lpstrFile[0] = '\0';
|
|
|
ofn.nMaxFile = sizeof(szFile);
|
|
|
@@ -58,7 +72,7 @@ IGL_INLINE std::string igl::file_dialog_save()
|
|
|
ofn.lpstrInitialDir = NULL;
|
|
|
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
|
|
|
|
|
|
- // Display the Open dialog box.
|
|
|
+ // Display the Open dialog box.
|
|
|
int pos = 0;
|
|
|
if (GetSaveFileName(&ofn)==TRUE)
|
|
|
{
|
|
|
@@ -73,14 +87,27 @@ IGL_INLINE std::string igl::file_dialog_save()
|
|
|
#else
|
|
|
// For every other machine type use zenity
|
|
|
FILE * output = popen("/usr/bin/zenity --file-selection --save","r");
|
|
|
- while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL )
|
|
|
+ if (output)
|
|
|
{
|
|
|
+ auto ret = fgets(buffer, FILE_DIALOG_MAX_BUFFER, output);
|
|
|
+ if (ret == NULL || ferror(output))
|
|
|
+ {
|
|
|
+ // I/O error
|
|
|
+ buffer[0] = '\0';
|
|
|
+ }
|
|
|
+ if (buffer[FILE_DIALOG_MAX_BUFFER - 1] == '\0')
|
|
|
+ {
|
|
|
+ // File name too long, buffer has been filled, so we return empty string instead
|
|
|
+ buffer[0] = '\0';
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- if (strlen(buffer) > 0)
|
|
|
+
|
|
|
+ // Replace last '\n' by '\0'
|
|
|
+ if(strlen(buffer) > 0)
|
|
|
{
|
|
|
- buffer[strlen(buffer)-1] = 0;
|
|
|
+ buffer[strlen(buffer)-1] = '\0';
|
|
|
}
|
|
|
+
|
|
|
#endif
|
|
|
return std::string(buffer);
|
|
|
}
|