| | 235 | /************************************************************************/ |
|---|
| | 236 | /* CPLStrtofDelim() */ |
|---|
| | 237 | /************************************************************************/ |
|---|
| | 238 | |
|---|
| | 239 | /** |
|---|
| | 240 | * Converts ASCII string to floating point number using specified delimiter. |
|---|
| | 241 | * |
|---|
| | 242 | * This function converts the initial portion of the string pointed to |
|---|
| | 243 | * by nptr to single floating point representation. This function does the |
|---|
| | 244 | * same as standard strtof(3), but does not take locale in account. Instead of |
|---|
| | 245 | * locale defined decimal delimiter you can specify your own one. Also see |
|---|
| | 246 | * notes for CPLAtof() function. |
|---|
| | 247 | * |
|---|
| | 248 | * @param nptr Pointer to string to convert. |
|---|
| | 249 | * @param endptr If is not NULL, a pointer to the character after the last |
|---|
| | 250 | * character used in the conversion is stored in the location referenced |
|---|
| | 251 | * by endptr. |
|---|
| | 252 | * @param point Decimal delimiter. |
|---|
| | 253 | * |
|---|
| | 254 | * @return Converted value, if any. |
|---|
| | 255 | */ |
|---|
| | 256 | float CPLStrtofDelim(const char *nptr, char **endptr, char point) |
|---|
| | 257 | { |
|---|
| | 258 | #if defined(HAVE_STRTOF) |
|---|
| | 259 | /* -------------------------------------------------------------------- */ |
|---|
| | 260 | /* We are implementing a simple method here: copy the input string */ |
|---|
| | 261 | /* into the temporary buffer, replace the specified decimal delimiter */ |
|---|
| | 262 | /* with the one, taken from locale settings and use standard strtof() */ |
|---|
| | 263 | /* on that buffer. */ |
|---|
| | 264 | /* -------------------------------------------------------------------- */ |
|---|
| | 265 | |
|---|
| | 266 | struct lconv *poLconv = localeconv(); |
|---|
| | 267 | char *pszNumber = CPLStrdup( nptr ); |
|---|
| | 268 | double dfValue; |
|---|
| | 269 | int nError; |
|---|
| | 270 | |
|---|
| | 271 | if ( poLconv |
|---|
| | 272 | && poLconv->decimal_point |
|---|
| | 273 | && strlen(poLconv->decimal_point) > 0 ) |
|---|
| | 274 | { |
|---|
| | 275 | int i = 0; |
|---|
| | 276 | char byPoint = poLconv->decimal_point[0]; |
|---|
| | 277 | |
|---|
| | 278 | while ( pszNumber[i] ) |
|---|
| | 279 | { |
|---|
| | 280 | if ( pszNumber[i] == point ) |
|---|
| | 281 | { |
|---|
| | 282 | pszNumber[i] = byPoint; |
|---|
| | 283 | break; |
|---|
| | 284 | } |
|---|
| | 285 | i++; |
|---|
| | 286 | } |
|---|
| | 287 | } |
|---|
| | 288 | |
|---|
| | 289 | dfValue = strtof( pszNumber, endptr ); |
|---|
| | 290 | nError = errno; |
|---|
| | 291 | |
|---|
| | 292 | if ( endptr ) |
|---|
| | 293 | *endptr = (char *)nptr + (*endptr - pszNumber); |
|---|
| | 294 | |
|---|
| | 295 | CPLFree( pszNumber ); |
|---|
| | 296 | |
|---|
| | 297 | errno = nError; |
|---|
| | 298 | return dfValue; |
|---|
| | 299 | |
|---|
| | 300 | #else |
|---|
| | 301 | |
|---|
| | 302 | return (float)CPLStrtodDelim(nptr, endptr, point); |
|---|
| | 303 | |
|---|
| | 304 | #endif /* HAVE_STRTOF */ |
|---|
| | 305 | } |
|---|
| | 306 | |
|---|
| | 307 | /************************************************************************/ |
|---|
| | 308 | /* CPLStrtof() */ |
|---|
| | 309 | /************************************************************************/ |
|---|
| | 310 | |
|---|
| | 311 | /** |
|---|
| | 312 | * Converts ASCII string to floating point number. |
|---|
| | 313 | * |
|---|
| | 314 | * This function converts the initial portion of the string pointed to |
|---|
| | 315 | * by nptr to single floating point representation. This function does the |
|---|
| | 316 | * same as standard strtof(3), but does not take locale in account. That |
|---|
| | 317 | * means, the decimal delimiter is always '.' (decimal point). Use |
|---|
| | 318 | * CPLStrtofDelim() function if you want to specify custom delimiter. Also |
|---|
| | 319 | * see notes for CPLAtof() function. |
|---|
| | 320 | * |
|---|
| | 321 | * @param nptr Pointer to string to convert. |
|---|
| | 322 | * @param endptr If is not NULL, a pointer to the character after the last |
|---|
| | 323 | * character used in the conversion is stored in the location referenced |
|---|
| | 324 | * by endptr. |
|---|
| | 325 | * |
|---|
| | 326 | * @return Converted value, if any. |
|---|
| | 327 | */ |
|---|
| | 328 | float CPLStrtof(const char *nptr, char **endptr) |
|---|
| | 329 | { |
|---|
| | 330 | return CPLStrtofDelim(nptr, endptr, '.'); |
|---|
| | 331 | } |
|---|