[glom] Add and use utility functions for creating temporary files.



commit 25562202f0dff5e3e671577107a633cb4bc82b75
Author: Murray Cumming <murrayc murrayc com>
Date:   Wed Nov 2 14:27:21 2011 +0100

    Add and use utility functions for creating temporary files.
    
    * glom/libglom/utils.[h|cc]: Added get_temp_file_uri(),
    get_temp_file_path(), get_temp_directory_uri() and
    get_temp_directory_path(), using the correct awkward code that
    avoids overwriting existing files.
    * glom/application.cc:
    * glom/libglom/connectionpool_backends/postgres_self.cc:
    * glom/libglom/report_builder.cc:
    * glom/utility_widgets/imageglom.cc:
    * tests/import/utils.cc:
    * tests/test_document_autosave.cc:
    * tests/test_selfhosting_new_empty.cc:
    * tests/test_selfhosting_utils.cc: Use these functions instead
    of repeating the same stuff badly.
    * Makefile_tests.am: Link to libglom so the import tests can use
    the new utility functions.

 ChangeLog                                          |   20 +++++
 Makefile_tests.am                                  |    4 +-
 glom/application.cc                                |    5 +-
 .../connectionpool_backends/postgres_self.cc       |    5 +-
 glom/libglom/report_builder.cc                     |    8 +-
 glom/libglom/utils.cc                              |   75 +++++++++++++++++++-
 glom/libglom/utils.h                               |    6 ++
 glom/utility_widgets/imageglom.cc                  |   22 +-----
 tests/import/utils.cc                              |   22 +++---
 tests/test_document_autosave.cc                    |    9 +--
 tests/test_selfhosting_new_empty.cc                |    4 +-
 tests/test_selfhosting_utils.cc                    |    4 +-
 12 files changed, 130 insertions(+), 54 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 3190176..5d41832 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,25 @@
 2011-11-02  Murray Cumming  <murrayc murrayc com>
 
+	Add and use utility functions for creating temporary files.
+
+	* glom/libglom/utils.[h|cc]: Added get_temp_file_uri(), 
+	get_temp_file_path(), get_temp_directory_uri() and 
+	get_temp_directory_path(), using the correct awkward code that 
+	avoids overwriting existing files.
+	* glom/application.cc:
+	* glom/libglom/connectionpool_backends/postgres_self.cc:
+	* glom/libglom/report_builder.cc:
+	* glom/utility_widgets/imageglom.cc:
+	* tests/import/utils.cc:
+	* tests/test_document_autosave.cc:
+	* tests/test_selfhosting_new_empty.cc:
+	* tests/test_selfhosting_utils.cc: Use these functions instead 
+	of repeating the same stuff badly.
+	* Makefile_tests.am: Link to libglom so the import tests can use 
+	the new utility functions.
+
+2011-11-02  Murray Cumming  <murrayc murrayc com>
+
 	Make connections to central PostgreSQL servers work again.
 
 	* glom/libglom/connectionpool_backends/postgres_central.cc: Do not 
diff --git a/Makefile_tests.am b/Makefile_tests.am
index 1943c72..f827acb 100644
--- a/Makefile_tests.am
+++ b/Makefile_tests.am
@@ -116,7 +116,7 @@ tests_import_test_parsing_SOURCES =	\
 	tests/import/utils.cc\
 	tests/import/utils.h\
 	tests/import/test_parsing.cc
-tests_import_test_parsing_LDADD = $(GLOM_LIBS)
+tests_import_test_parsing_LDADD = $(tests_ldadd)
 tests_import_test_parsing_CPPFLAGS = $(tests_cppflags) $(glom_test_import_defines)
 
 tests_import_test_signals_SOURCES =	\
@@ -125,7 +125,7 @@ tests_import_test_signals_SOURCES =	\
 	tests/import/utils.cc\
 	tests/import/utils.h\
 	tests/import/test_signals.cc
-tests_import_test_signals_LDADD = $(GLOM_LIBS)
+tests_import_test_signals_LDADD = $(tests_ldadd)
 tests_import_test_signals_CPPFLAGS = $(tests_cppflags)
 
 
diff --git a/glom/application.cc b/glom/application.cc
index 5b4e82f..57d0fce 100644
--- a/glom/application.cc
+++ b/glom/application.cc
@@ -2713,9 +2713,8 @@ bool Application::do_restore_backup(const Glib::ustring& backup_uri)
   }
 
   //Create a temporary directory into which we will untar the tarball:
-  std::string path_tmp = Glib::build_filename(
-    Glib::get_tmp_dir(), Glib::path_get_basename(filename_tarball));
-  path_tmp += "_extracted";
+  const std::string path_tmp = Utils::get_temp_file_path(
+    Glib::path_get_basename(filename_tarball), "_extracted");
 
   //Make sure that the directory does not exist already:
   const Glib::ustring uri_tmp = Glib::filename_to_uri(path_tmp);
diff --git a/glom/libglom/connectionpool_backends/postgres_self.cc b/glom/libglom/connectionpool_backends/postgres_self.cc
index a9a4fd7..ef2e87f 100644
--- a/glom/libglom/connectionpool_backends/postgres_self.cc
+++ b/glom/libglom/connectionpool_backends/postgres_self.cc
@@ -226,9 +226,8 @@ Backend::InitErrors PostgresSelfHosted::initialize(const SlotProgress& slot_prog
   // initdb creates a new postgres database cluster:
 
   //Get file:// URI for the tmp/ directory:
-  const std::string temp_pwfile = Glib::build_filename(Glib::get_tmp_dir(), "glom_initdb_pwfile");
-  Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(temp_pwfile);
-  const std::string temp_pwfile_uri = file->get_uri();
+  const std::string temp_pwfile = Utils::get_temp_file_path("glom_initdb_pwfile");
+  const Glib::ustring temp_pwfile_uri = Glib::filename_to_uri(temp_pwfile);
   const bool pwfile_creation_succeeded = create_text_file(temp_pwfile_uri, password);
   g_assert(pwfile_creation_succeeded);
 
diff --git a/glom/libglom/report_builder.cc b/glom/libglom/report_builder.cc
index ab7e4e7..8caf696 100644
--- a/glom/libglom/report_builder.cc
+++ b/glom/libglom/report_builder.cc
@@ -480,16 +480,16 @@ void ReportBuilder::report_build_records_vertical_group(const FoundSet& found_se
   }
 }
 
+//TODO: Return a URI
 std::string ReportBuilder::report_build_and_save(const FoundSet& found_set, const sharedptr<const Report>& report)
 {
   const Glib::ustring contents = report_build(found_set, report);
 
  //Save it to a temporary file and show it in a browser:
-  const std::string temp_path = Glib::build_filename(
-    Glib::get_tmp_dir(), "glom_printout.html");
-  std::cout << G_STRFUNC << ": temp_path=" << temp_path << std::endl;
+  const Glib::ustring temp_uri = Utils::get_temp_file_uri("glom_printout", "html");
+  std::cout << G_STRFUNC << ": temp_uri=" << temp_uri << std::endl;
 
-  Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(temp_path);
+  Glib::RefPtr<Gio::File> file = Gio::File::create_for_uri(temp_uri);
   Glib::RefPtr<Gio::FileOutputStream> stream;
 
   //Create the file if it does not already exist:
diff --git a/glom/libglom/utils.cc b/glom/libglom/utils.cc
index bdea5e2..8fb91d6 100644
--- a/glom/libglom/utils.cc
+++ b/glom/libglom/utils.cc
@@ -25,9 +25,12 @@
 #include <libglom/data_structure/layout/report_parts/layoutitem_fieldsummary.h>
 #include <libglom/data_structure/glomconversions.h>
 
+#include <giomm/file.h>
+#include <glibmm/convert.h>
+#include <glibmm/fileutils.h>
+#include <glibmm/miscutils.h>
 #include <glibmm/i18n.h>
 
-#include <giomm/file.h>
 
 #include <string.h> // for strchr
 #include <sstream> //For stringstream
@@ -1348,4 +1351,74 @@ Glib::ustring Utils::get_list_of_layout_items_for_display(const sharedptr<const
     return Glib::ustring();
 }
 
+std::string Utils::get_temp_file_path(const std::string& prefix, const std::string& extension)
+{
+  //Get a temporary file path:
+  std::string filepath;
+  try
+  {
+    const std::string prefix_pattern = prefix + "XXXXXX" + extension;
+    const int filehandle = Glib::file_open_tmp(filepath, prefix);
+    ::close(filehandle);
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": Glib::file_open_tmp() failed" << std::endl;
+    return filepath;
+  }
+  
+  if(filepath.empty())
+  {
+    std::cerr << G_STRFUNC << ": Glib::file_open_tmp() returned an empty filepath" << std::endl;
+  }
+
+  return filepath;
+}
+
+Glib::ustring Utils::get_temp_file_uri(const std::string& prefix, const std::string& extension)
+{
+  try
+  {
+    const std::string filepath = get_temp_file_path(prefix, extension);
+    return Glib::filename_to_uri(filepath);
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": Exception from filename_to_uri(): " << ex.what() << std::endl;
+    return std::string();
+  }
+}
+
+std::string Utils::get_temp_directory_path(const std::string& prefix)
+{
+  std::string result;
+
+  const std::string pattern = Glib::build_filename(
+    Glib::get_tmp_dir(), prefix + "XXXXXX");
+
+  //We must copy the pattern, because mkdtemp() modifies it:
+  char* c_pattern = g_strdup(pattern.c_str());
+  
+  const char* filepath = g_mkdtemp(c_pattern);
+  if(filepath)
+    result = filepath;
+
+  return result;
+}
+
+Glib::ustring Utils::get_temp_directory_uri(const std::string& prefix)
+{
+  try
+  {
+    const std::string filepath = get_temp_directory_path(prefix);
+    return Glib::filename_to_uri(filepath);
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": Exception from filename_to_uri(): " << ex.what() << std::endl;
+    return Glib::ustring();
+  }
+}
+
+
 } //namespace Glom
diff --git a/glom/libglom/utils.h b/glom/libglom/utils.h
index f9cf8d6..07f22d1 100644
--- a/glom/libglom/utils.h
+++ b/glom/libglom/utils.h
@@ -210,6 +210,12 @@ Glib::ustring get_list_of_layout_items_for_display(const LayoutGroup::type_list_
  */
 Glib::ustring get_list_of_layout_items_for_display(const sharedptr<const LayoutGroup>& layout_group);
 
+std::string get_temp_file_path(const std::string& prefix = std::string(), const std::string& extension = std::string());
+Glib::ustring get_temp_file_uri(const std::string& prefix = std::string(), const std::string& extension = std::string());
+
+std::string get_temp_directory_path(const std::string& prefix);
+Glib::ustring get_temp_directory_uri(const std::string& prefix);
+
 } //namespace Utils
 
 } //namespace Glom
diff --git a/glom/utility_widgets/imageglom.cc b/glom/utility_widgets/imageglom.cc
index 14c2008..9ae46dc 100644
--- a/glom/utility_widgets/imageglom.cc
+++ b/glom/utility_widgets/imageglom.cc
@@ -581,28 +581,12 @@ static void make_file_read_only(const Glib::ustring& uri)
 
 Glib::ustring ImageGlom::save_to_temp_file(bool show_progress)
 {
-  Glib::ustring uri;
-  
-  //Get a temporary file path:
-  std::string filepath;
-  try
-  {
-    const int filehandle = Glib::file_open_tmp(filepath);
-    ::close(filehandle);
-  }
-  catch(const Glib::Error& ex)
-  {
-    std::cerr << G_STRFUNC << ": Glib::file_open_tmp() failed" << std::endl;
-  }
-  
-  if(filepath.empty())
+  Glib::ustring uri = Utils::get_temp_file_uri("glom_image");
+  if(uri.empty())
   {
-    std::cerr << G_STRFUNC << ": Glib::file_open_tmp() returned an empty filepath" << std::endl;
-    return uri;
+    std::cerr << ": uri is empty." << std::endl;
   }
   
-  uri = Glib::filename_to_uri(filepath);
-  
   bool saved = false;
   if(show_progress)
     saved = save_file(uri);
diff --git a/tests/import/utils.cc b/tests/import/utils.cc
index e24020d..7c2cd8e 100644
--- a/tests/import/utils.cc
+++ b/tests/import/utils.cc
@@ -1,4 +1,5 @@
 #include <tests/import/utils.h>
+#include <libglom/utils.h>
 #include <glibmm/convert.h>
 #include <glibmm/fileutils.h>
 #include <glibmm/main.h>
@@ -17,18 +18,15 @@ bool check(const std::string& name, bool test, std::stringstream& report)
   return test;
 }
 
-// Returns the file name of the temporary created file, which will contain the buffer's contents.
-static std::string create_file_from_buffer(const char* input, guint input_size)
+// Returns the file URI of the temporary created file, which will contain the buffer's contents.
+static Glib::ustring create_file_from_buffer(const char* input, guint input_size)
 {
-  // Use Glib's file utilities to get a unique temporary filename:
-  std::string tmp_filename;
-  const int tmp_file_handle = Glib::file_open_tmp(tmp_filename, "glom_testdata");
-  if(-1 < tmp_file_handle)
-    close(tmp_file_handle);
-
-  std::string file_uri;
-  //TODO: Catch exception.
-  file_uri = Glib::filename_to_uri(tmp_filename);
+  const std::string file_uri = Glom::Utils::get_temp_file_uri("glom_import_testdata");
+  if(file_uri.empty())
+  {
+    std::cerr << G_STRFUNC << ": file_uri was empty." << std::endl;
+    return std::string();
+  }
 
   Glib::RefPtr<Gio::File> file = Gio::File::create_for_uri(file_uri);
 
@@ -92,7 +90,7 @@ bool run_parser_from_buffer(const FuncConnectParserSignals& connect_parser_signa
 
   connect_parser_signals(parser);
 
-  const std::string file_uri = create_file_from_buffer(input, input_size);
+  const Glib::ustring file_uri = create_file_from_buffer(input, input_size);
   parser.set_file_and_start_parsing(file_uri);
   if (Glom::CsvParser::STATE_PARSING != parser.get_state())
     return false;
diff --git a/tests/test_document_autosave.cc b/tests/test_document_autosave.cc
index 3e73b34..316513f 100644
--- a/tests/test_document_autosave.cc
+++ b/tests/test_document_autosave.cc
@@ -20,6 +20,7 @@
 
 #include <libglom/document/document.h>
 #include <libglom/init.h>
+#include <libglom/utils.h>
 #include <giomm/file.h>
 #include <glibmm/convert.h>
 #include <glibmm/miscutils.h>
@@ -57,12 +58,8 @@ int main()
 {
   Glom::libglom_init();
 
-  //For instance, /tmp/testfile.glom");
-  const std::string temp_filename = "testglom_document_autosave";
-  const std::string temp_filepath = Glib::build_filename(Glib::get_tmp_dir(),
-    temp_filename);
-  file_uri = Glib::filename_to_uri(temp_filepath);
-
+  file_uri = Glom::Utils::get_temp_file_uri("testglom_document_autosave", ".glom");
+  
   //Make sure that the file does not exist yet:
   cleanup();
 
diff --git a/tests/test_selfhosting_new_empty.cc b/tests/test_selfhosting_new_empty.cc
index 7b688b8..77cb2f1 100644
--- a/tests/test_selfhosting_new_empty.cc
+++ b/tests/test_selfhosting_new_empty.cc
@@ -52,8 +52,8 @@ static bool test(Glom::Document::HostingMode hosting_mode)
   //Save a copy, specifying the path to file in a directory:
   //For instance, /tmp/testfileglom/testfile.glom");
   const std::string temp_filename = "testglom";
-  const std::string temp_filepath_dir = Glib::build_filename(Glib::get_tmp_dir(),
-    temp_filename);
+  const std::string temp_filepath_dir = 
+    Glom::Utils::get_temp_directory_path(temp_filename);
   const std::string temp_filepath = Glib::build_filename(temp_filepath_dir, temp_filename);
 
   //Make sure that the file does not exist yet:
diff --git a/tests/test_selfhosting_utils.cc b/tests/test_selfhosting_utils.cc
index 876e1b3..ae7646d 100644
--- a/tests/test_selfhosting_utils.cc
+++ b/tests/test_selfhosting_utils.cc
@@ -186,8 +186,8 @@ bool test_create_and_selfhost(const std::string& example_filename, Glom::Documen
   //Save a copy, specifying the path to file in a directory:
   //For instance, /tmp/testfileglom/testfile.glom");
   const std::string temp_filename = "testglom";
-  temp_filepath_dir = Glib::build_filename(Glib::get_tmp_dir(),
-    temp_filename);
+  temp_filepath_dir = 
+    Glom::Utils::get_temp_directory_path(temp_filename);
   const std::string temp_filepath = Glib::build_filename(temp_filepath_dir, temp_filename);
 
   //Make sure that the file does not exist yet:



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]