[glom] Export: Correct problems in the binary data for images.



commit c50cd3fe2b8028fda43520e34cc23f031792bd19
Author: Murray Cumming <murrayc murrayc com>
Date:   Mon Oct 5 10:35:49 2009 +0200

    Export: Correct problems in the binary data for images.
    
    	* glom/libglom/data_structure/field.cc: to_file_format(): Escape
    	newlines and quotes in binary data text, to workaround libgda bug
    	https://bugzilla.gnome.org/show_bug.cgi?id=597390
    	* glom/frame_glom.cc: export_data_to_stream(): Double check that
    	image data contains no newlines or quotes.
    	* glom/libglom/utils.cc: string_replace(): Avoid an endless loop
    	when the search_for input parameter is empty.

 ChangeLog                            |   14 +++++++++++++-
 glom/frame_glom.cc                   |   28 +++++++++++++++++++++++-----
 glom/import_csv/csv_parser.cc        |    5 ++++-
 glom/libglom/data_structure/field.cc |   12 ++++++++++--
 glom/libglom/utils.cc                |   11 +++++++++--
 5 files changed, 59 insertions(+), 11 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index e0b280d..7c5a2da 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,18 @@
+2009-10-05  Murray Cumming  <murrayc murrayc com>
+
+	Export: Correct problems in the binary data for images.
+
+	* glom/libglom/data_structure/field.cc: to_file_format(): Escape 
+	newlines and quotes in binary data text, to workaround libgda bug 
+	https://bugzilla.gnome.org/show_bug.cgi?id=597390
+	* glom/frame_glom.cc: export_data_to_stream(): Double check that 
+	image data contains no newlines or quotes.
+	* glom/libglom/utils.cc: string_replace(): Avoid an endless loop 
+	when the search_for input parameter is empty.
+
 2009-10-04  Murray Cumming  <murrayc murrayc com>
 
-  Fix regression: Actually show field choices again in combo boxes.
+	Fix regression: Actually show field choices again in combo boxes.
   
 	* glom/libglom/utils.cc: get_choice_values(): Move the list_values.push_back() 
 	out of the ifdef block so we actually show field choices again in the combo 
diff --git a/glom/frame_glom.cc b/glom/frame_glom.cc
index 87e8307..28041de 100644
--- a/glom/frame_glom.cc
+++ b/glom/frame_glom.cc
@@ -823,6 +823,28 @@ void Frame_Glom::export_data_to_stream(std::ostream& the_stream, const FoundSet&
 
             const Glib::ustring field_text = field->to_file_format(value);
 
+            if(layout_item->get_glom_type() == Field::TYPE_IMAGE) //This is too much data.
+            {
+              // Some extra debug checks, 
+              // though we believe that all these problems are now fixed in File::to_file_format():
+
+              const char* newline_to_find = "\r\n";
+              size_t pos = field_text.find_first_of(newline_to_find);
+              if(pos != std::string::npos)
+              {
+                std::cerr << "export: binary data field text contains an unexpected newline: " << field_text << std::endl;
+                continue;
+              }
+
+              const char* quote_to_find = """";
+              pos = field_text.find_first_of(newline_to_find);
+              if(pos != std::string::npos)
+              {
+                std::cerr << "export: binary data field text contains an unexpected quote: " << field_text << std::endl;
+                continue;
+              }
+            }
+            
             if(layout_item->get_glom_type() == Field::TYPE_TEXT)
             {
               //The CSV RFC says text may be quoted and should be if it has newlines:
@@ -831,11 +853,7 @@ void Frame_Glom::export_data_to_stream(std::ostream& the_stream, const FoundSet&
             else
               row_string += field_text;
 
-            if(layout_item->get_glom_type() == Field::TYPE_IMAGE) //This is too much data.
-            {
-              if(!Conversions::value_is_empty(value))
-                std::cout << "  field name=" << layout_item->get_name() << ", image value not empty=" << std::endl;
-            }
+           
             //std::cout << "  field name=" << layout_item->get_name() << ", value=" << layout_item->m_field.sql(value) << std::endl;
           //}
         }
diff --git a/glom/import_csv/csv_parser.cc b/glom/import_csv/csv_parser.cc
index 363cddd..481c83d 100644
--- a/glom/import_csv/csv_parser.cc
+++ b/glom/import_csv/csv_parser.cc
@@ -66,7 +66,7 @@ CsvParser::CsvParser(const std::string& encoding_charset)
 
 void CsvParser::set_file_and_start_parsing(const std::string& file_uri)
 {
-  // TODO: Also fail on file:///?
+  // TODO: Check URI validity?
   g_return_if_fail(!file_uri.empty());
 
   m_file = Gio::File::create_for_uri(file_uri);
@@ -281,7 +281,10 @@ bool CsvParser::on_idle_parse()
 
   const char* inbuffer = &m_raw[m_input_position];
   char* inbuf = const_cast<char*>(inbuffer);
+
+  g_return_val_if_fail(m_input_position <= m_raw.size(), true);
   gsize inbytes = m_raw.size() - m_input_position;
+
   char outbuffer[CONVERT_BUFFER_SIZE];
   char* outbuf = outbuffer;
   gsize outbytes = CONVERT_BUFFER_SIZE;
diff --git a/glom/libglom/data_structure/field.cc b/glom/libglom/data_structure/field.cc
index fb77c09..f09b11e 100644
--- a/glom/libglom/data_structure/field.cc
+++ b/glom/libglom/data_structure/field.cc
@@ -300,8 +300,16 @@ Glib::ustring Field::to_file_format(const Gnome::Gda::Value& value, glom_field_t
     else
     {
       gchar* str = gda_binary_to_string(gdabinary, 0);
-      return (str) ? Glib::ustring(Glib::ScopedPtr<char>(str).get())
-        : Glib::ustring();
+      Glib::ustring result = (str) ? 
+        Glib::ustring(Glib::ScopedPtr<char>(str).get()) : Glib::ustring();
+
+      //Avoid arbitrary newlines in this text.
+      //See libgda bug: https://bugzilla.gnome.org/show_bug.cgi?id=597390
+      result = Utils::string_replace(result, "\n", "");
+
+      //Escape any quotes in this text:
+      //See libgda bug: https://bugzilla.gnome.org/show_bug.cgi?id=597390
+      return Utils::string_replace(result, "\"", "\\042");
     }
   }
   
diff --git a/glom/libglom/utils.cc b/glom/libglom/utils.cc
index 3713007..da80edf 100644
--- a/glom/libglom/utils.cc
+++ b/glom/libglom/utils.cc
@@ -134,17 +134,24 @@ Glib::ustring Utils::trim_whitespace(const Glib::ustring& text)
 
 Glib::ustring Utils::string_replace(const Glib::ustring& src, const Glib::ustring search_for, const Glib::ustring& replace_with)
 {
+  if(search_for.empty())
+  {
+    std::cerr << "Utils::string_replace(): search_for was empty." << std::endl;
+    return src;
+  }
+
   //std::cout << "debug: Utils::string_replace(): src=" << src << ", search_for=" << search_for << ", replace_with=" << replace_with << std::endl;
 
   std::string result = src;
 
   std::string::size_type pos = 0;
-  std::string::size_type len_search = search_for.size();
-  std::string::size_type len_replace = replace_with.size();
+  const std::string::size_type len_search = search_for.size();
+  const std::string::size_type len_replace = replace_with.size();
 
   std::string::size_type pos_after_prev = 0;
   while((pos = result.find(search_for, pos_after_prev)) != std::string::npos)
   {
+    //std::cout << "  debug: pos=" << pos << ", found=" << search_for << ", in string: " << result.substr(pos_after_prev, 20) << std::endl;
     //std::cout << "  debug: before: result =" << result << ", pos_after_prev=pos_after_prev" << std::endl;
     result.replace(pos, len_search, replace_with);
     //std::cout << "  after: before: result = result" << std::endl;



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