util: replace RUN_WITH_LOCALE with extended locale functions

There were two callers, one can use strtod_l() and the other strptime_l().

(David: fix up commit-msg and coding-style)
This commit is contained in:
Cristian Rodríguez
2015-01-18 22:08:44 +01:00
committed by David Herrmann
parent 43fcd650e5
commit 0193ad26ba
3 changed files with 29 additions and 47 deletions
+12 -6
View File
@@ -507,18 +507,24 @@ int safe_atolli(const char *s, long long int *ret_lli) {
int safe_atod(const char *s, double *ret_d) {
char *x = NULL;
double d = 0;
locale_t loc;
assert(s);
assert(ret_d);
RUN_WITH_LOCALE(LC_NUMERIC_MASK, "C") {
errno = 0;
d = strtod(s, &x);
loc = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0);
if (loc == (locale_t) 0)
return -errno;
errno = 0;
d = strtod_l(s, &x, loc);
if (!x || x == s || *x || errno) {
freelocale(loc);
return errno ? -errno : -EINVAL;
}
if (!x || x == s || *x || errno)
return errno ? -errno : -EINVAL;
freelocale(loc);
*ret_d = (double) d;
return 0;
}
-26
View File
@@ -942,32 +942,6 @@ int unlink_noerrno(const char *path);
_r_; \
})
struct _locale_struct_ {
locale_t saved_locale;
locale_t new_locale;
bool quit;
};
static inline void _reset_locale_(struct _locale_struct_ *s) {
PROTECT_ERRNO;
if (s->saved_locale != (locale_t) 0)
uselocale(s->saved_locale);
if (s->new_locale != (locale_t) 0)
freelocale(s->new_locale);
}
#define RUN_WITH_LOCALE(mask, loc) \
for (_cleanup_(_reset_locale_) struct _locale_struct_ _saved_locale_ = { (locale_t) 0, (locale_t) 0, false }; \
({ \
if (!_saved_locale_.quit) { \
PROTECT_ERRNO; \
_saved_locale_.new_locale = newlocale((mask), (loc), (locale_t) 0); \
if (_saved_locale_.new_locale != (locale_t) 0) \
_saved_locale_.saved_locale = uselocale(_saved_locale_.new_locale); \
} \
!_saved_locale_.quit; }) ; \
_saved_locale_.quit = true)
bool id128_is_valid(const char *s) _pure_;
int split_pair(const char *s, const char *sep, char **l, char **r);