[glom/glom-1-20] Correct parsing of example data.



commit 252c71179b8f0070a64068b2ba7ee5f34ce065e0
Author: Murray Cumming <murrayc murrayc com>
Date:   Tue Dec 13 10:41:45 2011 +0100

    Correct parsing of example data.
    
    * glom/libglom/data_structure/field.[h|cc]: to_file_format(),
    from_file_format() documentation: Mention that this uses CSV format.
    from_file_format(): Unescape double quotes, because we escape quotes
    as that in to_file_format().
    This avoids reading them as double quotes when opening an example file.
    This will also correct the interpretation of text default values.
    * Makefile_tests.am:
    * tests/test_field_file_format.cc: Add a test of this for text and image
    data, checking that what we write is what we read.

 ChangeLog                            |   14 +++++++
 Makefile_tests.am                    |    6 +++
 glom/libglom/data_structure/field.cc |   22 +++++++++--
 glom/libglom/data_structure/field.h  |    3 +
 tests/test_field_file_format.cc      |   68 ++++++++++++++++++++++++++++++++++
 5 files changed, 109 insertions(+), 4 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index d9e1649..c06bb7e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,19 @@
 2011-12-13  Murray Cumming  <murrayc murrayc com>
 
+	Correct parsing of example data.
+
+	* glom/libglom/data_structure/field.[h|cc]: to_file_format(),
+	from_file_format() documentation: Mention that this uses CSV format.
+	from_file_format(): Unescape double quotes, because we escape quotes
+	as that in to_file_format().
+	This avoids reading them as double quotes when opening an example file.
+	This will also correct the interpretation of text default values.
+	* Makefile_tests.am:
+	* tests/test_field_file_format.cc: Add a test of this for text and image
+	data, checking that what we write is what we read.
+
+2011-12-13  Murray Cumming  <murrayc murrayc com>
+
 	test_selfhosting_new_then_image: Move some code to test_utils.
 
 	* Makefile_tests.am:
diff --git a/Makefile_tests.am b/Makefile_tests.am
index d5a0ec5..17042dd 100644
--- a/Makefile_tests.am
+++ b/Makefile_tests.am
@@ -26,6 +26,7 @@ check_PROGRAMS =						\
 	tests/test_document_load_and_change		\
 	tests/test_document_change			\
 	tests/test_document_autosave			\
+	tests/test_field_file_format			\
 	tests/test_parsing_time			\
 	tests/test_signal_reemit			\
 	tests/python/test_load_python_library\
@@ -49,6 +50,7 @@ TESTS =	tests/test_document_load	\
 	tests/test_document_load_and_change	\
 	tests/test_document_change	\
 	tests/test_document_autosave	\
+	tests/test_field_file_format \
 	tests/test_parsing_time	\
 	tests/test_signal_reemit \
 	tests/test_dtd_file_validation.sh \
@@ -114,6 +116,10 @@ tests_test_document_autosave_SOURCES = tests/test_document_autosave.cc
 tests_test_document_autosave_LDADD = $(tests_ldadd)
 tests_test_document_autosave_CPPFLAGS = $(tests_cppflags)
 
+tests_test_field_file_format_SOURCES = tests/test_field_file_format.cc $(sources_test_utils)
+tests_test_field_file_format_LDADD = $(tests_ldadd)
+tests_test_field_file_format_CPPFLAGS = $(tests_cppflags) $(glom_test_image_defines)
+
 tests_test_parsing_time_SOURCES = tests/test_parsing_time.cc
 tests_test_parsing_time_LDADD = $(tests_ldadd)
 tests_test_parsing_time_CPPFLAGS = $(tests_cppflags)
diff --git a/glom/libglom/data_structure/field.cc b/glom/libglom/data_structure/field.cc
index 0db4ee6..dd5126a 100644
--- a/glom/libglom/data_structure/field.cc
+++ b/glom/libglom/data_structure/field.cc
@@ -328,6 +328,7 @@ Glib::ustring Field::to_file_format(const Gnome::Gda::Value& value, glom_field_t
     Glib::ustring result = (str) ? 
       Glib::ustring(Glib::ScopedPtr<char>(str).get()) : Glib::ustring();
 
+    //Correction for text representations of image (binary) data:
     //Avoid arbitrary newlines in this text.
     //See libgda bug: https://bugzilla.gnome.org/show_bug.cgi?id=597390
     result = Utils::string_replace(result, "\n", "\\012");
@@ -369,13 +370,26 @@ Gnome::Gda::Value Field::from_file_format(const Glib::ustring& str, bool& succes
 Gnome::Gda::Value Field::from_file_format(const Glib::ustring& str, glom_field_type glom_type, bool& success)
 {
   success = true;
-  
+
+  //Unescape "" to ", because to_file_format() escaped ", as specified by the CSV RFC:
+  Glib::ustring string_unescaped;
+  if(glom_type == TYPE_IMAGE)
+  {
+    string_unescaped = str; //binary data does not have quote characters so we do not bother to escape or unescape it.
+  }
+  else
+  {
+    string_unescaped = 
+      Utils::string_replace(str, GLOM_QUOTE_FOR_FILE_FORMAT GLOM_QUOTE_FOR_FILE_FORMAT, GLOM_QUOTE_FOR_FILE_FORMAT);
+  }
+
+
   if(glom_type == TYPE_IMAGE)
   {
-    if(str.empty())
+    if(string_unescaped.empty())
       return  Gnome::Gda::Value();
 
-    GdaBinary* gdabinary = gda_string_to_binary(str.c_str());
+    GdaBinary* gdabinary = gda_string_to_binary(string_unescaped.c_str());
     if(!success || !gdabinary)
       return Gnome::Gda::Value();
     else
@@ -389,7 +403,7 @@ Gnome::Gda::Value Field::from_file_format(const Glib::ustring& str, glom_field_t
   else
   {
     NumericFormat format_ignored; //Because we use ISO format.
-    return Conversions::parse_value(glom_type, str, format_ignored, success, true);
+    return Conversions::parse_value(glom_type, string_unescaped, format_ignored, success, true);
   }
 }
 
diff --git a/glom/libglom/data_structure/field.h b/glom/libglom/data_structure/field.h
index 3974400..67aca32 100644
--- a/glom/libglom/data_structure/field.h
+++ b/glom/libglom/data_structure/field.h
@@ -181,9 +181,12 @@ public:
    * a default value or for example data.
    * This does not add quotes for text fields so the caller may need to do that.
    * Note that this does not do any extra escaping such as an XML file might need.
+   * However, this will escape data as per the CSV RFC.
    */
   Glib::ustring to_file_format(const Gnome::Gda::Value& value) const;
 
+  /** See to_file_format(const Gnome::Gda::Value& value).
+   */
   static Glib::ustring to_file_format(const Gnome::Gda::Value& value, glom_field_type glom_type);
 
   /** Parse the value from the canonical file format. See to_file_format()
diff --git a/tests/test_field_file_format.cc b/tests/test_field_file_format.cc
new file mode 100644
index 0000000..579edfe
--- /dev/null
+++ b/tests/test_field_file_format.cc
@@ -0,0 +1,68 @@
+/* Glom
+ *
+ * Copyright (C) 2011 Openismus GmbH
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+71 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "tests/test_utils.h"
+#include <libglom/init.h>
+#include <libglom/data_structure/field.h>
+#include <iostream>
+
+static void test_text_field()
+{
+  Glom::sharedptr<Glom::Field> field = Glom::sharedptr<Glom::Field>::create();
+  field->set_glom_type(Glom::Field::TYPE_TEXT);
+
+  //TODO: Test an image too:
+  const Gnome::Gda::Value value_original("text with \" double quote and ' single quote");
+  const Glib::ustring str = field->to_file_format(value_original);
+  g_assert(!str.empty());
+
+  bool converted = false;
+  const Gnome::Gda::Value value = field->from_file_format(str, converted);
+  g_assert(converted);
+  g_assert(value == value_original);
+}
+
+static void test_image_field()
+{
+  Glom::sharedptr<Glom::Field> field = Glom::sharedptr<Glom::Field>::create();
+  field->set_glom_type(Glom::Field::TYPE_IMAGE);
+
+  //TODO: Test an image too:
+  const Gnome::Gda::Value value_original = get_value_for_image();
+  const Glib::ustring str = field->to_file_format(value_original);
+  g_assert(!str.empty());
+
+  bool converted = false;
+  const Gnome::Gda::Value value = field->from_file_format(str, converted);
+  g_assert(converted);
+  g_assert(value == value_original);
+}
+
+int main()
+{
+  Glom::libglom_init();
+
+  test_text_field();
+  test_image_field();
+
+  Glom::libglom_deinit();
+
+  return EXIT_SUCCESS;
+}



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