Browse Source

Attempt to fix several issues with the libigl file dialogs, so that they
correctly return an empty string rather than garbage when:
- the user cancels the dialog
- the user selects a file somehow longer than the 1024-character limit
- the file selection using zenity etc. fails for any other reason.
There is a strange erasure of the last character in the Linux version
(presumably to remove some trailing newline?) that I did not touch.

Etienne Vouga 6 years ago
parent
commit
9839ca1ffb
2 changed files with 17 additions and 9 deletions
  1. 9 5
      include/igl/file_dialog_open.cpp
  2. 8 4
      include/igl/file_dialog_save.cpp

+ 9 - 5
include/igl/file_dialog_open.cpp

@@ -21,6 +21,7 @@ IGL_INLINE std::string igl::file_dialog_open()
 {
 {
   const int FILE_DIALOG_MAX_BUFFER = 1024;
   const int FILE_DIALOG_MAX_BUFFER = 1024;
   char buffer[FILE_DIALOG_MAX_BUFFER];
   char buffer[FILE_DIALOG_MAX_BUFFER];
+  buffer[0] = '\0';
   
   
 #ifdef __APPLE__
 #ifdef __APPLE__
   // For apple use applescript hack
   // For apple use applescript hack
@@ -32,8 +33,9 @@ IGL_INLINE std::string igl::file_dialog_open()
     "   end tell\n"
     "   end tell\n"
     "   set existing_file_path to (POSIX path of (existing_file))\n"
     "   set existing_file_path to (POSIX path of (existing_file))\n"
     "\" 2>/dev/null | tr -d '\n' ","r");
     "\" 2>/dev/null | tr -d '\n' ","r");
-  while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL )
+  if( !output || fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) == NULL || fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL)
   {
   {
+    buffer[0] = '\0';
   }
   }
 #elif defined _WIN32
 #elif defined _WIN32
   
   
@@ -74,14 +76,16 @@ IGL_INLINE std::string igl::file_dialog_open()
   
   
   // For linux use zenity
   // For linux use zenity
   FILE * output = popen("/usr/bin/zenity --file-selection","r");
   FILE * output = popen("/usr/bin/zenity --file-selection","r");
-  while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL )
+  if( !output || fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) == NULL || fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL)
   {
   {
+    buffer[0] = '\0';
   }
   }
-  
-  if (strlen(buffer) > 0)
+
+  if(strlen(buffer) > 0)
   {
   {
-    buffer[strlen(buffer)-1] = 0;
+    buffer[strlen(buffer)-1] = '\0';
   }
   }
+
 #endif
 #endif
   return std::string(buffer);
   return std::string(buffer);
 }
 }

+ 8 - 4
include/igl/file_dialog_save.cpp

@@ -18,6 +18,7 @@ IGL_INLINE std::string igl::file_dialog_save()
 {
 {
   const int FILE_DIALOG_MAX_BUFFER = 1024;
   const int FILE_DIALOG_MAX_BUFFER = 1024;
   char buffer[FILE_DIALOG_MAX_BUFFER];
   char buffer[FILE_DIALOG_MAX_BUFFER];
+  buffer[0] = '\0';
 #ifdef __APPLE__
 #ifdef __APPLE__
   // For apple use applescript hack
   // For apple use applescript hack
   // There is currently a bug in Applescript that strips extensions off
   // There is currently a bug in Applescript that strips extensions off
@@ -31,8 +32,9 @@ IGL_INLINE std::string igl::file_dialog_save()
     "   end tell\n"
     "   end tell\n"
     "   set existing_file_path to (POSIX path of (existing_file))\n"
     "   set existing_file_path to (POSIX path of (existing_file))\n"
     "\" 2>/dev/null | tr -d '\n' ","r");
     "\" 2>/dev/null | tr -d '\n' ","r");
-  while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL )
+  if( !output || fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) == NULL || fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL)
   {
   {
+    buffer[0] = '\0';
   }
   }
 #elif defined _WIN32
 #elif defined _WIN32
 
 
@@ -73,14 +75,16 @@ IGL_INLINE std::string igl::file_dialog_save()
 #else
 #else
   // For every other machine type use zenity
   // For every other machine type use zenity
   FILE * output = popen("/usr/bin/zenity --file-selection --save","r");
   FILE * output = popen("/usr/bin/zenity --file-selection --save","r");
-  while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL )
+  if( !output || fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) == NULL || fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL)
   {
   {
+    buffer[0] = '\0';
   }
   }
   
   
-  if (strlen(buffer) > 0)
+  if(strlen(buffer) > 0)
   {
   {
-    buffer[strlen(buffer)-1] = 0;
+    buffer[strlen(buffer)-1] = '\0';
   }
   }
+  
 #endif
 #endif
   return std::string(buffer);
   return std::string(buffer);
 }
 }