soci-dtocstr.h 1.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
//
// Copyright (C) 2014 Vadim Zeitlin.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef SOCI_PRIVATE_SOCI_DTOCSTR_H_INCLUDED
#define SOCI_PRIVATE_SOCI_DTOCSTR_H_INCLUDED

11
#include "soci/soci-platform.h"
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include "soci/error.h"

#include <stdlib.h>
#include <stdio.h>

namespace soci
{

namespace details
{

// Locale-independent, i.e. always using "C" locale, function for converting
// floating point number to string.
//
// The resulting string will contain the floating point number in "C" locale,
// i.e. will always use point as decimal separator independently of the current
// locale.
inline
std::string double_to_cstring(double d)
{
    // See comments in cstring_to_double() in soci-cstrtod.h, we're dealing
    // with the same issues here.

    static size_t const bufSize = 32;
    char buf[bufSize];
    snprintf(buf, bufSize, "%.20g", d);

    // Replace any commas which can be used as decimal separator with points.
    for (char* p = buf; *p != '\0'; p++ )
    {
        if (*p == ',')
        {
            *p = '.';

            // There can be at most one comma in this string anyhow.
            break;
        }
    }

    return buf;
}

} // namespace details

} // namespace soci

#endif // SOCI_PRIVATE_SOCI_DTOCSTR_H_INCLUDED