From dfecdec31638ca6c7deafc031ed8758accc955a1 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 20 Mar 2011 08:56:03 +0100 Subject: [PATCH] Aggiunte le funzioni SolipaUtils::chk_codice_fiscale e SolipaUtils::chk_partita_iva. --- .gitignore | 1 + src/utils.c | 138 +++++++++++++++++++++++++++++++++++++ src/utils.h | 3 + tests/Makefile.am | 1 + tests/utils_codfisc_piva.c | 32 +++++++++ 5 files changed, 175 insertions(+) create mode 100644 tests/utils_codfisc_piva.c diff --git a/.gitignore b/.gitignore index 64023a7..f4d398b 100644 --- a/.gitignore +++ b/.gitignore @@ -50,6 +50,7 @@ Rules-quot tests/allegato tests/mail tests/utils +tests/utils_codfisc_piva tests/utils_format_money tests/utils_round *.csv diff --git a/src/utils.c b/src/utils.c index ca68a6d..af4d2b1 100644 --- a/src/utils.c +++ b/src/utils.c @@ -769,6 +769,144 @@ const gchar return ret; } +gboolean +solipa_chk_codice_fiscale (const gchar *codice_fiscale, gboolean empty_good) +{ + gchar *cf; + + guint s; + guint i; + guint c; + + guint setdisp[] = { 1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 2, 4, 18, 20, + 11, 3, 6, 8, 12, 14, 16, 10, 22, 25, 24, 23 }; + + if (codice_fiscale == NULL) + { + cf = g_strdup (""); + } + else + { + cf = g_strdup (codice_fiscale); + } + + cf = g_strstrip (cf); + + if (g_strcmp0 (cf, "") == 0) + { + return empty_good; + } + + if (g_utf8_strlen (cf, -1) != 16) + { + g_warning ("Lunghezza (%d) errata.", g_utf8_strlen (cf, -1)); + return FALSE; + } + + cf = g_ascii_strup (cf, -1); + + for (i = 0; i < 16; i++) + { + if (!isdigit (cf[i]) && !('A' <= cf[i] && cf[i] <= 'Z')) + { + g_warning ("Caratteri non validi (%c).", cf[i]); + return FALSE; + } + } + + s = 0; + for (i = 1; i <= 13; i += 2) + { + if (isdigit (cf[i])) + { + s += cf[i] - '0'; + } + else + { + s += cf[i] - 'A'; + } + } + + for (i = 0; i <= 14; i += 2) + { + c = cf[i]; + if (isdigit (c)) + { + c = c - '0' + 'A'; + } + s += setdisp[c - 'A']; + } + + if ((s % 26 + 'A') != cf[15]) + { + return FALSE; + } + + return TRUE; +} + +gboolean +solipa_chk_partita_iva (const gchar *partita_iva, gboolean empty_good) +{ + gchar *piva; + + guint i; + guint c; + guint s; + + if (partita_iva == NULL) + { + piva = g_strdup (""); + } + else + { + piva = g_strdup (partita_iva); + } + + piva = g_strstrip (piva); + + if (g_strcmp0 (piva, "") == 0) + { + return empty_good; + } + + if (g_utf8_strlen (piva, -1) != 11) + { + return FALSE; + } + + for (i = 0; i < 11; i++) + { + if (!isdigit(piva[i])) + { + return FALSE; + } + } + + s = 0; + for (i = 0; i <= 9; i += 2) + { + s += piva[i] - '0'; + } + + for (i = 1; i <= 9; i += 2) + { + c = 2 * (piva[i] - '0'); + if (c > 9) + { + c = c - 9; + } + s += c; + } + + if ((10 - s % 10) % 10 != piva[10] - '0') + { + return FALSE; + } + + return TRUE; +} + /** * This function is copied from * http://bugzilla.gnome.org/show_bug.cgi?id=524831. diff --git a/src/utils.h b/src/utils.h index ed5fab1..24092c4 100644 --- a/src/utils.h +++ b/src/utils.h @@ -64,6 +64,9 @@ struct tm *solipa_gtk_tree_model_get_value_tm (GtkTreeModel *model, GtkTreeIter const gchar *solipa_tm_to_sql (struct tm *tm_data, const gchar *format); +gboolean solipa_chk_codice_fiscale (const gchar *codice_fiscale, gboolean empty_good); +gboolean solipa_chk_partita_iva (const gchar *partita_iva, gboolean empty_good); + gchar *g_mkdtemp (gchar *tmpl); diff --git a/tests/Makefile.am b/tests/Makefile.am index 457715f..535ef49 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -12,5 +12,6 @@ LDADD = $(top_builddir)/src/libsolipa.la noinst_PROGRAMS = allegato \ mail \ utils \ + utils_codfisc_piva \ utils_format_money \ utils_round diff --git a/tests/utils_codfisc_piva.c b/tests/utils_codfisc_piva.c new file mode 100644 index 0000000..b7b60fd --- /dev/null +++ b/tests/utils_codfisc_piva.c @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2010-2011 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 ("Codice Fiscale %s %s.", argv[1], solipa_chk_codice_fiscale (argv[1], TRUE) ? "corretto" : "non corretto"); + + g_message ("Partita IVA %s %s.", argv[2], solipa_chk_partita_iva (argv[2], TRUE) ? "corretta" : "non corretta"); + + return 0; +} -- 2.49.0