[glom] Use the new Gnome::Gda::Numeric API.
- From: Murray Cumming <murrayc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glom] Use the new Gnome::Gda::Numeric API.
- Date: Sun, 13 Nov 2011 21:07:56 +0000 (UTC)
commit b486fdd4e22050db67d76bcbc969a3d67ceb4f9f
Author: Murray Cumming <murrayc murrayc com>
Date: Sun Nov 13 22:07:47 2011 +0100
Use the new Gnome::Gda::Numeric API.
* glom/libglom/data_structure/glomconversions.cc:
* glom/libglom/python_embed/pygdavalue_conversions.cc:
* tests/python/test_python_execute_func_date.cc: GdaNumeric now has
a real API so we can make our code simpler.
ChangeLog | 9 +++
glom/libglom/data_structure/glomconversions.cc | 63 +++-----------------
.../libglom/python_embed/pygdavalue_conversions.cc | 5 +-
tests/python/test_python_execute_func_date.cc | 4 +-
4 files changed, 21 insertions(+), 60 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index b1dd73c..dbf2f0e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2011-11-13 Murray Cumming <murrayc murrayc com>
+
+ Use the new Gnome::Gda::Numeric API.
+
+ * glom/libglom/data_structure/glomconversions.cc:
+ * glom/libglom/python_embed/pygdavalue_conversions.cc:
+ * tests/python/test_python_execute_func_date.cc: GdaNumeric now has
+ a real API so we can make our code simpler.
+
2011-11-11 Murray Cumming <murrayc murrayc com>
List view and Related Records: Disable buttons when appropriate.
diff --git a/glom/libglom/data_structure/glomconversions.cc b/glom/libglom/data_structure/glomconversions.cc
index 78766fc..b11c70f 100644
--- a/glom/libglom/data_structure/glomconversions.cc
+++ b/glom/libglom/data_structure/glomconversions.cc
@@ -331,19 +331,8 @@ double Conversions::get_double_for_gda_value_numeric(const Gnome::Gda::Value& va
}
}
- const GdaNumeric* gda_numeric = value.get_numeric();
- std::string text_in_c_locale;
- if(gda_numeric && gda_numeric->number) //A char* - I assume that it formatted as per the C locale. murrayc. TODO: Do we need to look at the other fields?
- text_in_c_locale = gda_numeric->number; //What formatting does this use?
-
- //Get an actual numeric value, so we can get a locale-specific text representation:
- std::stringstream the_stream;
- the_stream.imbue( std::locale::classic() ); //The C locale.
- the_stream.str(text_in_c_locale); //Avoid using << because Glib::ustring does implicit character conversion with that.
-
- double number = 0;
- the_stream >> number;
- return number;
+ const Gnome::Gda::Numeric numeric = value.get_numeric();
+ return numeric.get_double();
}
Glib::ustring Conversions::get_text_for_gda_value(Field::glom_field_type glom_type, const Gnome::Gda::Value& value, const std::locale& locale, const NumericFormat& numeric_format, bool iso_format)
@@ -532,25 +521,9 @@ Glib::ustring Conversions::get_text_for_gda_value(Field::glom_field_type glom_ty
Gnome::Gda::Value Conversions::parse_value(double number)
{
//This is just a way to get a NUMERIC Gnome::Gda::Value from a numeric type:
- //Try to parse the inputted number, according to the current locale.
-
- GdaNumeric gda_numeric = {0, 0, 0, 0};
-
- //Then generate a canonical representation of the number:
- std::stringstream clocale_stream;
- clocale_stream.imbue( std::locale::classic() ); //The C locale.
- clocale_stream << number;
- Glib::ustring number_canonical_text = clocale_stream.str(); //Avoid << because it does implicit character conversion (though that might not be a problem here. Not sure). murrayc
-
- //TODO: What about the precision and width values?
- /* From the postgres docs:
- * The scale of a numeric is the count of decimal digits in the fractional part, to the right of the decimal point.
- * The precision of a numeric is the total count of significant digits in the whole number, that is, the number of digits to both sides of the decimal point.
- * So the number 23.5141 has a precision of 6 and a scale of 4. Integers can be considered to have a scale of zero.
- */
- gda_numeric.number = g_strdup(number_canonical_text.c_str());
-
- return Gnome::Gda::Value(&gda_numeric);
+ Gnome::Gda::Numeric numeric;
+ numeric.set_double(number);
+ return Gnome::Gda::Value(numeric);
}
Gnome::Gda::Value Conversions::parse_value(Field::glom_field_type glom_type, const Glib::ustring& text, bool& success, bool iso_format)
@@ -636,31 +609,13 @@ Gnome::Gda::Value Conversions::parse_value(Field::glom_field_type glom_type, con
the_stream >> the_number; //TODO: Does this throw any exception if the text is an invalid number?
//std::cout << "debug: " << G_STRFUNC << ": text=" << text_to_parse << ", number=" << the_number << std::endl;
-
+
- GdaNumeric gda_numeric = {0, 0, 0, 0};
-
- //Then generate a canonical representation of the number:
-
- std::stringstream clocale_stream;
- clocale_stream.imbue( std::locale::classic() ); //The C locale.
-
- //Avoid the e syntax. Normally this happens afer 7 digits, with loss of precision. TODO: Handle more.
- clocale_stream << std::setprecision( NumericFormat::get_default_precision() );
- clocale_stream << the_number;
- const Glib::ustring number_canonical_text = clocale_stream.str(); //Avoid << because it does implicit character conversion (though that might not be a problem here. Not sure). murrayc
- //std::cout << " DEBUG: number_canonical_text=" << number_canonical_text << std::endl;
-
- //TODO: What about the precision and width values?
- /* From the postgres docs:
- * The scale of a numeric is the count of decimal digits in the fractional part, to the right of the decimal point.
- * The precision of a numeric is the total count of significant digits in the whole number, that is, the number of digits to both sides of the decimal point.
- * So the number 23.5141 has a precision of 6 and a scale of 4. Integers can be considered to have a scale of zero.
- */
- gda_numeric.number = g_strdup(number_canonical_text.c_str());
+ Gnome::Gda::Numeric numeric;
+ numeric.set_double(the_number);
success = true; //Can this ever fail?
- return Gnome::Gda::Value(&gda_numeric);
+ return Gnome::Gda::Value(numeric);
}
else if(glom_type == Field::TYPE_BOOLEAN)
{
diff --git a/glom/libglom/python_embed/pygdavalue_conversions.cc b/glom/libglom/python_embed/pygdavalue_conversions.cc
index 76d1ecd..42b95d1 100644
--- a/glom/libglom/python_embed/pygdavalue_conversions.cc
+++ b/glom/libglom/python_embed/pygdavalue_conversions.cc
@@ -7,6 +7,7 @@
#include <boost/python.hpp>
#include "pygdavalue_conversions.h"
+#include <libgdamm/numeric.h>
#include <libgda/gda-blob-op.h>
#include <iostream>
@@ -225,9 +226,7 @@ boost::python::object glom_pygda_value_as_boost_pyobject(const Glib::ValueBase&
ret = boost::python::object(g_value_get_int(boxed));
} else if (value_type == GDA_TYPE_NUMERIC) {
const GdaNumeric* val = gda_value_get_numeric(boxed);
- const gchar* number_as_text = val->number; /* Formatted according to the C locale, probably. */
- /* This would need a string _object_: ret = PyFloat_FromString(number_as_text, 0); */
- ret = boost::python::object(g_ascii_strtod(number_as_text, 0));
+ ret = boost::python::object(gda_numeric_get_double((GdaNumeric*)val));
} else if (value_type == G_TYPE_FLOAT) {
ret = boost::python::object(g_value_get_float(boxed));
} else if (value_type == GDA_TYPE_SHORT) {
diff --git a/tests/python/test_python_execute_func_date.cc b/tests/python/test_python_execute_func_date.cc
index 50b3e12..68b8954 100644
--- a/tests/python/test_python_execute_func_date.cc
+++ b/tests/python/test_python_execute_func_date.cc
@@ -67,10 +67,8 @@ void execute_func_with_date_input_value()
g_assert(value.get_value_type() == GDA_TYPE_NUMERIC);
//Check that the return value is of the expected value:
- g_assert(value.get_numeric());
- g_assert(value.get_numeric()->number);
//std::cout << "GdaNumeric number=" << value.get_numeric()->number << std::endl;
- g_assert(value.get_numeric()->number == std::string("1973"));
+ g_assert(value.get_numeric().get_double() == 1973);
//std::cout << "value=" << value.to_string() << std::endl;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]