[grisbi-cvs] grisbi/src gsb_real_cunit.c, NONE, 1.1 gsb_real_cunit.h, NONE, 1.1 Makefile.am, 1.153, 1.154 gsb_real.c, 1.34, 1.35 gsb_real.h, 1.14, 1.15 main.c, 1.141, 1.142

Pierre Biava pbiava at users.sourceforge.net
Mon May 11 20:54:29 CEST 2009


Update of /cvsroot/grisbi/grisbi/src
In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv4265/src

Modified Files:
	Makefile.am gsb_real.c gsb_real.h main.c 
Added Files:
	gsb_real_cunit.c gsb_real_cunit.h 
Log Message:
correction of gsb_real and adding Cunit

Index: gsb_real.h
===================================================================
RCS file: /cvsroot/grisbi/grisbi/src/gsb_real.h,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- gsb_real.h	10 May 2009 13:06:56 -0000	1.14
+++ gsb_real.h	11 May 2009 18:54:27 -0000	1.15
@@ -28,6 +28,9 @@
 gsb_real gsb_real_double_to_real ( gdouble number );
 gsb_real gsb_real_get_from_string ( const gchar *string );
 gchar *gsb_real_get_string ( gsb_real number );
+gchar *gsb_real_raw_format_string ( gsb_real number,
+                        struct lconv *conv,
+                        const gchar *currency_symbol );
 gchar *gsb_real_get_string_with_currency ( gsb_real number,
                         gint currency_number,
                         gboolean show_symbol );

Index: gsb_real.c
===================================================================
RCS file: /cvsroot/grisbi/grisbi/src/gsb_real.c,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -d -r1.34 -r1.35
--- gsb_real.c	10 May 2009 13:06:56 -0000	1.34
+++ gsb_real.c	11 May 2009 18:54:27 -0000	1.35
@@ -41,6 +41,11 @@
 
 gsb_real null_real = { 0 , 0 };
 
+glong gsb_real_power_10[] = { 1, 10, 100, 1000, 10000, 100000,
+                            1000000, 10000000, 100000000, 1000000000 };
+
+#define sizeofarray(x) (sizeof(x)/sizeof(*x))
+
 /*START_STATIC*/
 static gchar *gsb_real_format_string ( gsb_real number,
                         gint currency_number,
@@ -96,136 +101,158 @@
 }
 
 
-
-
 /**
  * Return the real in a formatted string with an optional currency
- * symbol, according to the locale regarding decimal separator,
+ * symbol, according to the given locale regarding decimal separator,
  * thousands separator and positive or negative sign.
  * 
- * \param number		Number to format.
- * \param currency_number 	the currency we want to adapt the number, 0 for no adaptation
- * \param show_symbol 		TRUE to add the currency symbol in the string
+ * \param number		    Number to format.
+ * \param conv      		the locale obtained with localeconv(), or built manually
+ * \param currency_symbol 	the currency symbol
  *
  * \return		A newly allocated string of the number (this
  *			function will never return NULL) 
  */
-gchar *gsb_real_format_string ( gsb_real number,
-                        gint currency_number,
-                        gboolean show_symbol )
+gchar *gsb_real_raw_format_string (gsb_real number,
+                        struct lconv *conv,
+                        const gchar *currency_symbol )
 {
-    struct lconv * conv = localeconv ( );
-    gchar *string, *exponent = NULL, *mantissa;
-    glong num;
-    gint nbre_char;
-    const gchar *currency_symbol = NULL;
-    char buf[G_ASCII_DTOSTR_BUF_SIZE];
-
-    /* as we use localeconv, all the currencies in grisbi will be formatted as the locale of 
-     * the system i think it's ok like that, and to adapt the view according with the 
-     * currency and not the current locale is much more complicated */
-    if (currency_number && show_symbol)
-        currency_symbol = gsb_data_currency_get_code (currency_number);
-    
-    /* First of all if number = 0 I return 0 with the symbol of the currency if necessary */
-    if (number.mantissa == 0)
-    {
-        if (currency_symbol && conv -> p_cs_precedes)
-            return g_strdup_printf ( "%s %s", currency_symbol, "0" );
-        else if (currency_symbol && ! conv -> p_cs_precedes)
-            return g_strdup_printf ( "%s %s", "0", currency_symbol );
-        else
-            return g_strdup ("0");
-    }
-
-    /* first we need to adapt the exponent to the currency */
-    /* if the exponent of the real is not the same of the currency, need to adapt it */
-    if ( currency_number
-      &&
-      number.exponent != gsb_data_currency_get_floating_point (currency_number) )
-        number = gsb_real_adjust_exponent ( number, gsb_data_currency_get_floating_point (
-                        currency_number) );
-
-    /* on traite la conversion avec g_ascii_dtostr */
-    num = labs(number.mantissa);
-
-    mantissa = g_ascii_dtostr ( buf, sizeof (buf), num );
-    if ( ! mantissa )
-            return g_strdup ("Error");
+    if ( (number.exponent < 0)
+    || (number.exponent >= sizeofarray (gsb_real_power_10))
+    || (number.mantissa == 0x80000000) )
+        return g_strdup("###ERR###");
 
-    nbre_char = g_utf8_strlen ( mantissa, -1);
+    gchar format[40];
+    gchar *result = NULL;
+    const gchar *cs_start = (currency_symbol && conv->p_cs_precedes) ? currency_symbol : "";
+    const gchar *cs_start_space = (currency_symbol && conv->p_cs_precedes && conv->p_sep_by_space) ? " " : "";
+    const gchar *sign = (number.mantissa < 0) ? conv->negative_sign : conv->positive_sign;
+    const gchar *mon_decimal_point = conv->mon_decimal_point && *conv->mon_decimal_point ? conv->mon_decimal_point : ".";
+    const gchar *cs_end_space = (currency_symbol && !conv->p_cs_precedes && conv->p_sep_by_space) ? " " : "";
+    const gchar *cs_end = (currency_symbol && !conv->p_cs_precedes) ? currency_symbol : "";
 
-    /* on extrait la partie entière et la partie décimale */
-    if ( nbre_char > number.exponent )
+    ldiv_t units = ldiv ( labs (number.mantissa), gsb_real_power_10[number.exponent] );
+    if ( units.quot < 1000 )
     {
-        exponent = g_strdup ( mantissa + (nbre_char - number.exponent ) );
-        mantissa = g_strndup ( mantissa, (nbre_char - number.exponent) );
+        g_snprintf (format, sizeof(format), "%s%d%s",
+                                           "%s%s%s%d%s%0",
+                                           number.exponent,
+                                           "d%s%s" );
+        result = g_strdup_printf ( format, 
+                        cs_start,
+                        cs_start_space,
+                        sign,
+                        units.quot,
+                        mon_decimal_point,
+                        units.rem,
+                        cs_end_space,
+                        cs_end );
     }
     else
     {
-        if ( nbre_char < number.exponent )
+        gchar *mon_thousands_sep = g_locale_to_utf8 ( conv->mon_thousands_sep, -1,
+                                                        NULL, NULL, NULL );
+        div_t thousands = div ( units.quot, 1000 );
+        if ( thousands.quot < 1000 )
         {
-            exponent = g_strnfill ( number.exponent - nbre_char, '0' );
-            exponent = g_strconcat ( exponent, mantissa, NULL );
+            g_snprintf ( format, sizeof (format), "%s%d%s",
+                                                "%s%s%s%d%s%03d%s%0",
+                                                number.exponent,
+                                                "d%s%s");
+            result = g_strdup_printf ( format, 
+                            cs_start,
+                            cs_start_space,
+                            sign,
+                            thousands.quot,
+                            mon_thousands_sep,
+                            thousands.rem,
+                            mon_decimal_point,
+                            units.rem,
+                            cs_end_space,
+                            cs_end );
         }
         else
-            exponent = g_strdup ( mantissa );
-        mantissa = g_strdup ( "0" );
-    }
-
-    /* on insère le séparateur des milliers */
-    gchar *mon_thousands_sep_utf8;
-
-    if ( nbre_char > 3 && (mon_thousands_sep_utf8 = g_locale_to_utf8 (
-                        conv->mon_thousands_sep, -1, NULL, NULL, NULL )) )
-    {
-        gchar *reverse;
-        gchar *ptr_char, *ptr_fin = NULL;
-        gchar *dest = NULL;
-        gchar *ch;
-
-        reverse = g_utf8_strreverse ( mantissa, -1 );
-        dest = g_malloc0 ( 30 * sizeof (gunichar));
-
-        nbre_char = g_utf8_strlen ( reverse, -1);
-        ptr_char = reverse;
-        ptr_fin = dest;
-        while ( (nbre_char = g_utf8_strlen (ptr_char, -1)) > 0)
         {
-            ch = g_strndup (ptr_char, 3);
-            ptr_fin = g_stpcpy ( ptr_fin, ch );
-            g_free ( ch );
-            ptr_fin = g_stpcpy (ptr_fin, mon_thousands_sep_utf8 );
-
-            nbre_char = g_utf8_strlen ( ptr_char, -1 );
-            if ( nbre_char > 3 )
-                ptr_char = g_utf8_offset_to_pointer ( ptr_char, 3 );
+            div_t millions = div ( thousands.quot, 1000 );
+            if ( millions.quot < 1000 )
+            {
+                g_snprintf ( format, sizeof (format), "%s%d%s",
+                                                    "%s%s%s%d%s%03d%s%03d%s%0",
+                                                    number.exponent,
+                                                    "d%s%s");
+                result = g_strdup_printf ( format, 
+                                cs_start,
+                                cs_start_space,
+                                sign,
+                                millions.quot,
+                                mon_thousands_sep,
+                                millions.rem,
+                                mon_thousands_sep,
+                                thousands.rem,
+                                mon_decimal_point,
+                                units.rem,
+                                cs_end_space,
+                                cs_end);
+            }
             else
-                ptr_char = g_utf8_offset_to_pointer ( ptr_char, nbre_char );
+            {
+                div_t billions = div ( millions.quot, 1000 );
+                g_snprintf ( format, sizeof (format), "%s%d%s",
+                                                    "%s%s%s%d%s%03d%s%03d%s%03d%s%0",
+                                                    number.exponent,
+                                                    "d%s%s" );
+                result = g_strdup_printf ( format, 
+                                cs_start,
+                                cs_start_space,
+                                sign,
+                                billions.quot,
+                                mon_thousands_sep,
+                                billions.rem,
+                                mon_thousands_sep,
+                                millions.rem,
+                                mon_thousands_sep,
+                                thousands.rem,
+                                mon_decimal_point,
+                                units.rem,
+                                cs_end_space,
+                                cs_end );
+            }
         }
-
-        mantissa = g_utf8_strreverse ( dest, -1 );
-        g_free ( reverse );
-        g_free ( dest );
+        g_free ( mon_thousands_sep );
     }
-    
-    /* Add the sign at the end of the string just before to reverse it to avoid
-       to have to insert it at the begin just after... */
-    string = g_strdup_printf ( "%s%s%s%s%s%s%s%s", 
-                    ( currency_symbol && conv -> p_cs_precedes ? currency_symbol : "" ),
-                    ( currency_symbol && conv -> p_sep_by_space ? " " : "" ),
-                    number.mantissa < 0 ? conv -> negative_sign : conv -> positive_sign,
-                    mantissa,
-                    ( * conv -> mon_decimal_point ? conv -> mon_decimal_point : "." ),
-                    strlen (exponent) ? exponent : "0",
-                    ( currency_symbol && ! conv -> p_cs_precedes && conv -> p_sep_by_space ? 
-                    " " : "" ),
-                    ( currency_symbol && ! conv -> p_cs_precedes ? currency_symbol : "" ) );
+    return result;
+}
 
-    g_free ( exponent );
-    g_free ( mantissa );
 
-    return string;
+/**
+ * Return the real in a formatted string with an optional currency
+ * symbol, according to the locale regarding decimal separator,
+ * thousands separator and positive or negative sign.
+ * 
+ * \param number		Number to format.
+ * \param currency_number 	the currency we want to adapt the number, 0 for no adaptation
+ * \param show_symbol 		TRUE to add the currency symbol in the string
+ *
+ * \return		A newly allocated string of the number (this
+ *			function will never return NULL) 
+ */
+gchar *gsb_real_format_string ( gsb_real number,
+                        gint currency_number,
+                        gboolean show_symbol )
+{
+    gint floating_point;
+
+    const gchar *currency_symbol = (currency_number && show_symbol)
+                                   ? gsb_data_currency_get_code (currency_number)
+                                   : NULL;
+
+    /* first we need to adapt the exponent to the currency */
+    /* if the exponent of the real is not the same of the currency, need to adapt it */
+    floating_point = gsb_data_currency_get_floating_point ( currency_number );
+    if ( currency_number && number.exponent != floating_point )
+        number = gsb_real_adjust_exponent ( number, floating_point );
+    
+    return gsb_real_raw_format_string ( number, localeconv(), currency_symbol );
 }
 
 

--- NEW FILE: gsb_real_cunit.c ---
(This appears to be a binary file; contents omitted.)

Index: Makefile.am
===================================================================
RCS file: /cvsroot/grisbi/grisbi/src/Makefile.am,v
retrieving revision 1.153
retrieving revision 1.154
diff -u -d -r1.153 -r1.154
--- Makefile.am	17 Jan 2009 07:23:53 -0000	1.153
+++ Makefile.am	11 May 2009 18:54:27 -0000	1.154
@@ -1,12 +1,12 @@
 # Process this file with automake to produce Makefile.in
 
 localedir = $(datadir)/locale
-INCLUDES = -I$(top_srcdir) -I$(includedir) -I../intl -DLOCALEDIR=\"$(localedir)\" -DPIXMAPS_DIR="\"$(datadir)/pixmaps/@PACKAGE@\"" -DPLUGINS_DIR="\"$(prefix)/lib/@PACKAGE@\"" -DLOGO_PATH="\"$(datadir)/pixmaps/@PACKAGE@/grisbi-logo.png\"" -DDATA_PATH="\"$(datadir)/@PACKAGE@\"" -DHELP_PATH="\"$(datadir)/doc/@PACKAGE@\""  -Wall
+INCLUDES = -I$(top_srcdir) -I$(includedir) -I../intl -DHAVE_CUNIT -DLOCALEDIR=\"$(localedir)\" -DPIXMAPS_DIR="\"$(datadir)/pixmaps/@PACKAGE@\"" -DPLUGINS_DIR="\"$(prefix)/lib/@PACKAGE@\"" -DLOGO_PATH="\"$(datadir)/pixmaps/@PACKAGE@/grisbi-logo.png\"" -DDATA_PATH="\"$(datadir)/@PACKAGE@\"" -DHELP_PATH="\"$(datadir)/doc/@PACKAGE@\""  -Wall
 bin_PROGRAMS = grisbi
 
 # TODO: This manual setting of -Wl is a hack, I cannot get it from
 # configure right now because of no doc.
-grisbi_LDFLAGS = @lt_prog_compiler_wl@@export_dynamic_flag_spec@
+grisbi_LDFLAGS = @lt_prog_compiler_wl@@export_dynamic_flag_spec@ -lcunit
 
 config_dir = config
 
@@ -97,6 +97,7 @@
 	gsb_payment_method_config.c	\
 	gsb_plugins.c		\
 	gsb_real.c		\
+	gsb_real_cunit.c	\
 	gsb_reconcile.c		\
 	gsb_reconcile_config.c	\
 	gsb_reconcile_list.c	\
@@ -226,13 +227,14 @@
 	gsb_payment_method_config.h	\
         gsb_plugins.h           \
 	gsb_real.h		\
+	gsb_real_cunit.h	\
 	gsb_reconcile.h		\
 	gsb_reconcile_config.h	\
 	gsb_reconcile_list.h	\
 	gsb_reconcile_sort_config.h	\
 	gsb_report.h		\
         gsb_status.h            \
-	gsb_select_icon.h \
+	gsb_select_icon.h 	\
 	gsb_scheduler.h		\
 	gsb_scheduler_list.h	\
 	gsb_transactions_list.h	\
@@ -266,16 +268,16 @@
 	parametres.h		\
 	parse_cmdline.h		\
 	print_config.h		\
-	print_dialog_config.h		\
+	print_dialog_config.h	\
 	print_report.h		\
-	print_transactions_list.h		\
+	print_transactions_list.h	\
 	qif.h			\
 	tip.h			\
 	structures.h		\
 	tiers_onglet.h		\
 	traitement_variables.h	\
 	transaction_list.h	\
-	transaction_list_select.h\
+	transaction_list_select.h	\
 	transaction_list_sort.h	\
 	transaction_model.h	\
 	utils.h			\

--- NEW FILE: gsb_real_cunit.h ---
#ifndef _GSB_REAL_CUNIT_H
#define _GSB_REAL_CUNIT_H (1)

#include <CUnit/Basic.h>


CU_pSuite gsb_real_cunit_create_suite();

#endif//_GSB_REAL_CUNIT_H

Index: main.c
===================================================================
RCS file: /cvsroot/grisbi/grisbi/src/main.c,v
retrieving revision 1.141
retrieving revision 1.142
diff -u -d -r1.141 -r1.142
--- main.c	27 Apr 2009 19:41:03 -0000	1.141
+++ main.c	11 May 2009 18:54:27 -0000	1.142
@@ -1,7 +1,7 @@
 /* *******************************************************************************/
 /*                                 GRISBI                                        */
-/* Programme de gestion financière personnelle                                   */
-/*           	  license : GPLv2                                             */
+/*              Programme de gestion financière personnelle                      */
+/*           	                license : GPLv2                                  */
 /*                                                                               */
 /*     Copyright (C)    2000-2008 Cédric Auger (cedric at grisbi.org)               */
 /*                      2003-2008 Benjamin Drieu (bdrieu at april.org)              */
@@ -33,7 +33,6 @@
 # include <config.h>
 #endif
 
-
 #include "include.h"
 
 /*START_INCLUDE*/
@@ -82,6 +81,40 @@
 extern gchar *nom_fichier_comptes;
 /*END_EXTERN*/
 
+#if HAVE_CUNIT
+
+#include <CUnit/Automated.h>
+#include <CUnit/Basic.h>
+
+#include "gsb_real_cunit.h"
+
+int gsb_cunit_run_tests()
+{
+    /* initialize the CUnit test registry */
+    if (CUE_SUCCESS != CU_initialize_registry())
+        return CU_get_error();
+
+    /* add a suite to the registry */
+    CU_pSuite pSuite = gsb_real_cunit_create_suite();
+    if(NULL == pSuite)
+    {
+        CU_cleanup_registry();
+        return CU_get_error();
+    }
+
+    /* Run all tests */
+#ifdef _WIN32
+    CU_automated_run_tests();
+#else//_WIN32
+	CU_basic_run_tests();
+#endif//_WIN32
+    CU_cleanup_registry();
+    return CU_get_error();
+}
+#endif//HAVE_CUNIT
+
+
+
 /**
  * Main function
  *
@@ -92,6 +125,10 @@
  */
 int main (int argc, char **argv)
 {
+#if HAVE_CUNIT
+    gsb_cunit_run_tests();
+#endif//HAVE_CUNIT
+
     GtkWidget * statusbar;
     gboolean first_use = FALSE;
     gchar *string;



More information about the cvs mailing list