views:

1119

answers:

5

I'm parsing GPS status entries in fixed NMEA sentences, where fraction part of geographical minutes comes always after period. However, on systems where locale defines comma as decimal separator, atof function ignores period and whole fraction part.

What is the best method to deal with this issue? Long/latitude string in stored in character array, if it matters.

Example Code:

m_longitude = atof((char *)pField);

where

pField[] = "01000.3897";

Cross-platform project, compiled for Windows XP and CE.

Comment to solution:

Accepted answer is more elegant, but this answer (and comment) is also worth knowing as a quick fix

+2  A: 

A nasty solution I've done once is to sprintf() 0.0f and grab the second character from the output. Then in the input string replace '.' by that character. This solves the comma case, but would also work if a locale defined other decimal separators.

MSalters
localeconv (in <locale.h>) returns a pointer to struct whose decimal_point member contains that value. Note that the pointer is valid until the next localeconv() or setlocale()
AProgrammer
+1  A: 

Any reason why you can't do a setlocale "C" before the atof and restore the locale afterwards? Maybe I misunderstood the question...

danio
Definitely. I can't risk any impact on other parts of the system and changing locale for sure can affect other processes.
tomash
the setlocale call only affects the locale of the current process. If you have other threads which are doing locale-dependent things they would need to be synchronized.
danio
AFAIK under windows CE locales are global, not replicated per process
tomash
A: 

You could iterate through all the characters in the array and swap any non-numbers with a . character, which should work as long as the coordinates are in a number-single_delimiter_character_-number format.

suszterpatt
Misundrestanding. There always will be single period, but sometimes atof will expect comma and ignore fraction part after period.
tomash
Right. In that case, I'd go with MSalters's solution: print a float, get the delimiter, then replace the `.` with it.
suszterpatt
+5  A: 

You could always use (modulo error-checking):

#include <sstream>
...

float longitude = 0.0f;
std::istringstream istr(pField);

istr >> longitude;

The standard iostreams use the global locale by default (which in turn should be initialized to the classic (US) locale). Thus the above should work in general unless someone previously has changed the global locale to something else, even if you're running on a non-english platform. To be absolutely sure that the desired locale is used, create a specific locale and "imbue" the stream with that locale before reading from it:

#include <sstream>
#include <locale>

...
float longitude = 0.0f;
std::istringstream istr(pField);

istr.imbue(std::locale("C"));
istr >> longitude;

As a side note, I've usually used regular expressions to validate NMEA fields, extract the different parts of the field as captures, and then convert the different parts using the above method. The portion before the decimal point in an NMEA longitude field actually is formatted as "DDDMM.mmm.." where DDD correspond to degrees, MM.mmm to minutes (but I guess you already knew that).

Cwan
It use the global C++ locale. Modifying the global C++ locale modifies the C locale if it has a name -- if it hasn't the effect on the C locale is implementation defined.
AProgrammer
@AProgrammer: Did you actually read and understand my reply before commenting/downvoting?
Cwan
@AProgrammer: Ok, re-reading my reply it might not have been very clear. Nevertheless I never suggested changing the global locale, just mentioned that if someone else did, it will have effect on the sample code.
Cwan
@Cwe: yes I read it (I even did a +1 -- mainly for mentioning that the data isn't decimal). I confirmed what you where writing -- you expressed a doubt in your first formulation -- and added information about the interaction between C++ and C global locale.
AProgrammer
A: 

Do you really need to get locale behavior for numerics? If not

setlocale(LC_ALL|~LC_NUMERIC, "");

or the equivalent use of std::locale constructor.

AProgrammer