From bc564dca05a794d9e480796a0a790580eb8fab80 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 19 Dec 2010 11:51:51 +0100 Subject: [PATCH] Aggiunta la funzione solipa_format_money (da finire). --- .gitignore | 1 + src/utils.c | 59 ++++++++++++++++++++++++++++++++++++++ src/utils.h | 2 ++ tests/Makefile.am | 3 +- tests/utils_format_money.c | 30 +++++++++++++++++++ 5 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tests/utils_format_money.c diff --git a/.gitignore b/.gitignore index d48b7cf..224f903 100644 --- a/.gitignore +++ b/.gitignore @@ -49,4 +49,5 @@ Rules-quot *.exe tests/mail tests/utils +tests/utils_format_money *.csv diff --git a/src/utils.c b/src/utils.c index 3b46acc..3031c20 100644 --- a/src/utils.c +++ b/src/utils.c @@ -394,3 +394,62 @@ solipa_gtktreemodel_to_csv_gui (Solipa *solipa, GtkTreeModel *model, } } +gchar +*solipa_format_money (gdouble number, gboolean with_currency_symbol) +{ + gchar *ret; + + GRegex *regex; + GError *error; + + gchar *str; + gssize str_len; + + /* TODO + * - get number of decimal from locale + * - get grouping char from locale + * - get currency symbol from locale + */ + + ret = g_strdup (""); + + error = NULL; + regex = g_regex_new ("(-?\\d+)(\\d\\d\\d)", 0, 0, &error); + if (error != NULL) + { + g_warning ("Error on creating regex: %s.", + error->message != NULL ? error->message : "no details"); + return ""; + } + + ret = g_strdup_printf ("%0.2f", number); + + while (TRUE) + { + error = NULL; + str_len = g_utf8_strlen (ret, -1); + str = g_regex_replace ((const GRegex *)regex, + ret, str_len, 0, + "\\1.\\2", 0, + &error); + if (error != NULL) + { + g_warning ("Error on regex replacing: %s.", + error->message != NULL ? error->message : "no details"); + g_regex_unref (regex); + return ""; + } + if (g_strcmp0 (ret, str) != 0) + { + ret = g_strdup (str); + } + else + { + break; + } + } + + g_regex_unref (regex); + + return ret; +} diff --git a/src/utils.h b/src/utils.h index 7c53b06..c0a6cf4 100644 --- a/src/utils.h +++ b/src/utils.h @@ -42,6 +42,8 @@ void solipa_gtktreemodel_to_csv_gui (Solipa *solipa, GtkTreeModel *model, gchar **columns_title, guint n_columns); +gchar *solipa_format_money (gdouble number, gboolean with_currency_symbol); + G_END_DECLS diff --git a/tests/Makefile.am b/tests/Makefile.am index 72b8782..f63d05b 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -10,4 +10,5 @@ LIBS = $(SOLIPA_LIBS) \ LDADD = $(top_builddir)/src/libsolipa.la noinst_PROGRAMS = mail \ - utils + utils \ + utils_format_money diff --git a/tests/utils_format_money.c b/tests/utils_format_money.c new file mode 100644 index 0000000..6a62da8 --- /dev/null +++ b/tests/utils_format_money.c @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2010 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +int +main (int argc, char *argv[]) +{ + gtk_init (&argc, &argv); + + g_message ("%s", solipa_format_money (g_strtod (argv[1], NULL), FALSE)); + + return 0; +} -- 2.49.0