From nobody Wed May 1 22:32:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 149813529716367.52590592553952; Thu, 22 Jun 2017 05:41:37 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A04FC63319; Thu, 22 Jun 2017 12:41:35 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 731E058830; Thu, 22 Jun 2017 12:41:35 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 277091853E31; Thu, 22 Jun 2017 12:41:35 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v5MCb0aR024588 for ; Thu, 22 Jun 2017 08:37:00 -0400 Received: by smtp.corp.redhat.com (Postfix) id ED05719178; Thu, 22 Jun 2017 12:37:00 +0000 (UTC) Received: from caroline.brq.redhat.com (unknown [10.43.2.67]) by smtp.corp.redhat.com (Postfix) with ESMTP id 75AC55C7C2 for ; Thu, 22 Jun 2017 12:36:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com A04FC63319 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com A04FC63319 From: Martin Kletzander To: libvir-list@redhat.com Date: Thu, 22 Jun 2017 14:36:53 +0200 Message-Id: <82ef66c97478892c8b411307be66b93167bb0c1d.1498135013.git.mkletzan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH] util: Extract locale-related fixes into separate functions X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Thu, 22 Jun 2017 12:41:36 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Martin Kletzander --- src/util/virstring.c | 96 ++++++++++++++++++++++++++++++++----------------= ---- 1 file changed, 60 insertions(+), 36 deletions(-) diff --git a/src/util/virstring.c b/src/util/virstring.c index feea5be05198..6125725364f3 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -522,6 +522,7 @@ virStrToLong_ullp(char const *s, char **end_ptr, int ba= se, #if HAVE_NEWLOCALE static locale_t virLocale; +static locale_t virLocaleOld; static int virLocaleOnceInit(void) @@ -533,7 +534,58 @@ virLocaleOnceInit(void) } VIR_ONCE_GLOBAL_INIT(virLocale); -#endif + +static int +virLocaleSet(void) +{ + if (virLocaleInitialize() < 0) + return -1; + virLocaleOld =3D uselocale(virLocale); + return 0; +} + +static void +virLocaleRevert(void) +{ + uselocale(virLocaleOld); +} + +static void +virLocaleFixupRadix(char **strp ATTRIBUTE_UNUSED) +{ +} + +#else /* !HAVE_NEWLOCALE */ + +static int +virLocaleSet(void) +{ + return 0; +} + +static void +virLocaleRevert(void) +{ +} + +static void +virLocaleFixupRadix(char **strp) +{ + char *radix, *tmp; + struct lconv *lc; + + lc =3D localeconv(); + radix =3D lc->decimal_point; + tmp =3D strstr(*strp, radix); + if (tmp) { + *tmp =3D '.'; + if (strlen(radix) > 1) + memmove(tmp + 1, tmp + strlen(radix), strlen(*strp) - (tmp - *= strp)); + } +} + +#endif /* !HAVE_NEWLOCALE */ + /** * virStrToDouble @@ -552,17 +604,11 @@ virStrToDouble(char const *s, int err; errno =3D 0; -#if HAVE_NEWLOCALE - locale_t old_loc; - if (virLocaleInitialize() < 0) + if (virLocaleSet() < 0) return -1; - - old_loc =3D uselocale(virLocale); -#endif val =3D strtod(s, &p); /* exempt from syntax-check */ -#if HAVE_NEWLOCALE - uselocale(old_loc); -#endif + virLocaleRevert(); + err =3D (errno || (!end_ptr && *p) || p =3D=3D s); if (end_ptr) *end_ptr =3D p; @@ -584,36 +630,14 @@ virDoubleToStr(char **strp, double number) { int ret =3D -1; -#if HAVE_NEWLOCALE - - locale_t old_loc; - - if (virLocaleInitialize() < 0) - goto error; + if (virLocaleSet() < 0) + return -1; - old_loc =3D uselocale(virLocale); ret =3D virAsprintf(strp, "%lf", number); - uselocale(old_loc); - -#else - - char *radix, *tmp; - struct lconv *lc; - - if ((ret =3D virAsprintf(strp, "%lf", number) < 0)) - goto error; - lc =3D localeconv(); - radix =3D lc->decimal_point; - tmp =3D strstr(*strp, radix); - if (tmp) { - *tmp =3D '.'; - if (strlen(radix) > 1) - memmove(tmp + 1, tmp + strlen(radix), strlen(*strp) - (tmp - *= strp)); - } + virLocaleRevert(); + virLocaleFixupRadix(strp); -#endif /* HAVE_NEWLOCALE */ - error: return ret; } --=20 2.13.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list