views:

189

answers:

3

I have a library which needs to parse double numbers which always use a point '.' as decimal separator. Unfortunately for this case, strtod() respects the locale which might use a different separator and thus parsing can fail. I can't setlocale() - it isn't thread-safe. So I'm searching for a clean locale-independent strtod implementation now. I have found multiple implementations so far, but all of them look hacky or just like bad code. Can someone recommend a well-tested, working, clean (ANSI) C implementation for me?

+1  A: 

Any decent CRT implementation does something about this problem. Review the <locale.h> header file for the workaround it provides. MSVC has _configthreadlocale(_ENABLE_PER_THREAD_LOCALE) for example. A CRT also common exposes a strtod() version that takes a locale argument, _strtod_l() in MSVC.

Hans Passant
It should be portable and I don't feel like adding a bunch of #ifdefs or anything similar to the code. I don't want to do it this way.
bert
+1  A: 

Grab some known implementation (that doesn't depend on atof), such as the one distributed with ruby: ruby_1_8/missing/strtod.c.

przemoc
Thank you. That's exactly what I've been searching for. A well-tested and clean implementation.
bert
A: 

There's also gdtoa available on netlib, BSD style license: http://www.netlib.org/fp/gdtoa.tgz

Spudd86