| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- //
- // NumberFormatter.cpp
- //
- // $Id: //poco/1.4/Foundation/src/NumberFormatter.cpp#4 $
- //
- // Library: Foundation
- // Package: Core
- // Module: NumberFormatter
- //
- // Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
- // and Contributors.
- //
- // SPDX-License-Identifier: BSL-1.0
- //
- #include "Poco/NumberFormatter.h"
- #include "Poco/MemoryStream.h"
- #include <iomanip>
- #if !defined(POCO_NO_LOCALE)
- #include <locale>
- #endif
- #include <cstdio>
- #if defined(_MSC_VER) || defined(__MINGW32__)
- #define I64_FMT "I64"
- #elif defined(__APPLE__)
- #define I64_FMT "q"
- #else
- #define I64_FMT "ll"
- #endif
- namespace Poco {
- std::string NumberFormatter::format(bool value, BoolFormat format)
- {
- switch(format)
- {
- default:
- case FMT_TRUE_FALSE:
- if (value == true)
- return "true";
- return "false";
- case FMT_YES_NO:
- if (value == true)
- return "yes";
- return "no";
- case FMT_ON_OFF:
- if (value == true)
- return "on";
- return "off";
- }
- }
- void NumberFormatter::append(std::string& str, int value)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- intToStr(value, 10, result, sz);
- str.append(result, sz);
- }
- void NumberFormatter::append(std::string& str, int value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- intToStr(value, 10, result, sz, false, width);
- str.append(result, sz);
- }
- void NumberFormatter::append0(std::string& str, int value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- intToStr(value, 10, result, sz, false, width, '0');
- str.append(result, sz);
- }
- void NumberFormatter::appendHex(std::string& str, int value)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(static_cast<unsigned int>(value), 0x10, result, sz);
- str.append(result, sz);
- }
- void NumberFormatter::appendHex(std::string& str, int value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(static_cast<unsigned int>(value), 0x10, result, sz, false, width, '0');
- str.append(result, sz);
- }
- void NumberFormatter::append(std::string& str, unsigned value)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(value, 10, result, sz);
- str.append(result, sz);
- }
- void NumberFormatter::append(std::string& str, unsigned value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(value, 10, result, sz, false, width);
- str.append(result, sz);
- }
- void NumberFormatter::append0(std::string& str, unsigned int value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(value, 10, result, sz, false, width, '0');
- str.append(result, sz);
- }
- void NumberFormatter::appendHex(std::string& str, unsigned value)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(value, 0x10, result, sz);
- str.append(result, sz);
- }
- void NumberFormatter::appendHex(std::string& str, unsigned value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(value, 0x10, result, sz, false, width, '0');
- str.append(result, sz);
- }
- void NumberFormatter::append(std::string& str, long value)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- intToStr(value, 10, result, sz);
- str.append(result, sz);
- }
- void NumberFormatter::append(std::string& str, long value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- intToStr(value, 10, result, sz, false, width);
- str.append(result, sz);
- }
- void NumberFormatter::append0(std::string& str, long value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- intToStr(value, 10, result, sz, false, width, '0');
- str.append(result, sz);
- }
- void NumberFormatter::appendHex(std::string& str, long value)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(static_cast<unsigned long>(value), 0x10, result, sz);
- str.append(result, sz);
- }
- void NumberFormatter::appendHex(std::string& str, long value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(static_cast<unsigned long>(value), 0x10, result, sz, false, width, '0');
- str.append(result, sz);
- }
- void NumberFormatter::append(std::string& str, unsigned long value)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(value, 10, result, sz);
- str.append(result, sz);
- }
- void NumberFormatter::append(std::string& str, unsigned long value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(value, 10, result, sz, false, width, '0');
- str.append(result, sz);
- }
- void NumberFormatter::append0(std::string& str, unsigned long value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(value, 10, result, sz, false, width, '0');
- str.append(result, sz);
- }
- void NumberFormatter::appendHex(std::string& str, unsigned long value)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(value, 0x10, result, sz);
- str.append(result, sz);
- }
- void NumberFormatter::appendHex(std::string& str, unsigned long value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(value, 0x10, result, sz, false, width, '0');
- str.append(result, sz);
- }
- #if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
- void NumberFormatter::append(std::string& str, Int64 value)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- intToStr(value, 10, result, sz);
- str.append(result, sz);
- }
- void NumberFormatter::append(std::string& str, Int64 value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- intToStr(value, 10, result, sz, false, width, '0');
- str.append(result, sz);
- }
- void NumberFormatter::append0(std::string& str, Int64 value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- intToStr(value, 10, result, sz, false, width, '0');
- str.append(result, sz);
- }
- void NumberFormatter::appendHex(std::string& str, Int64 value)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(static_cast<UInt64>(value), 0x10, result, sz);
- str.append(result, sz);
- }
- void NumberFormatter::appendHex(std::string& str, Int64 value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(static_cast<UInt64>(value), 0x10, result, sz, false, width, '0');
- str.append(result, sz);
- }
- void NumberFormatter::append(std::string& str, UInt64 value)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(value, 10, result, sz);
- str.append(result, sz);
- }
- void NumberFormatter::append(std::string& str, UInt64 value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(value, 10, result, sz, false, width, '0');
- str.append(result, sz);
- }
- void NumberFormatter::append0(std::string& str, UInt64 value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(value, 10, result, sz, false, width, '0');
- str.append(result, sz);
- }
- void NumberFormatter::appendHex(std::string& str, UInt64 value)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(value, 0x10, result, sz);
- str.append(result, sz);
- }
- void NumberFormatter::appendHex(std::string& str, UInt64 value, int width)
- {
- char result[NF_MAX_INT_STRING_LEN];
- std::size_t sz = NF_MAX_INT_STRING_LEN;
- uIntToStr(value, 0x10, result, sz, false, width, '0');
- str.append(result, sz);
- }
- #endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
- void NumberFormatter::append(std::string& str, float value)
- {
- char buffer[NF_MAX_FLT_STRING_LEN];
- floatToStr(buffer, POCO_MAX_FLT_STRING_LEN, value);
- str.append(buffer);
- }
- void NumberFormatter::append(std::string& str, double value)
- {
- char buffer[NF_MAX_FLT_STRING_LEN];
- doubleToStr(buffer, POCO_MAX_FLT_STRING_LEN, value);
- str.append(buffer);
- }
- void NumberFormatter::append(std::string& str, double value, int precision)
- {
- std::string result;
- str.append(doubleToStr(result, value, precision));
- }
- void NumberFormatter::append(std::string& str, double value, int width, int precision)
- {
- std::string result;
- str.append(doubleToStr(result, value, precision, width));
- }
- void NumberFormatter::append(std::string& str, const void* ptr)
- {
- char buffer[24];
- #if defined(POCO_PTR_IS_64_BIT)
- #if defined(POCO_LONG_IS_64_BIT)
- std::sprintf(buffer, "%016lX", (UIntPtr) ptr);
- #else
- std::sprintf(buffer, "%016" I64_FMT "X", (UIntPtr) ptr);
- #endif
- #else
- std::sprintf(buffer, "%08lX", (UIntPtr) ptr);
- #endif
- str.append(buffer);
- }
- } // namespace Poco
|