X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flocale_convert.cc;h=2c7c74eff57241021bd8d50a049075c2fb8b8ce1;hb=e1e7de8d73bedd0c741e7df0390068c67867e09f;hp=129ee50fd3bad985d8ca927d9aa3b40e2ec2010d;hpb=732f931e0559f29100b56f7f4a6e6be8820f0ff7;p=libdcp.git diff --git a/src/locale_convert.cc b/src/locale_convert.cc index 129ee50f..2c7c74ef 100644 --- a/src/locale_convert.cc +++ b/src/locale_convert.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2016 Carl Hetherington + Copyright (C) 2016-2021 Carl Hetherington This file is part of libdcp. @@ -31,13 +31,41 @@ files in the program, then also delete it here. */ + +/** @file src/locale_convert.cc + * @brief Methods to convert to/from string using the current locale + */ + + #include "locale_convert.h" #include #include + using std::string; using std::wstring; + +template<> +string +dcp::locale_convert (unsigned char x, int, bool) +{ + char buffer[64]; + snprintf (buffer, sizeof(buffer), "%hhd", x); + return buffer; +} + + +template<> +string +dcp::locale_convert (unsigned short int x, int, bool) +{ + char buffer[64]; + snprintf (buffer, sizeof(buffer), "%hd", x); + return buffer; +} + + template<> string dcp::locale_convert (int x, int, bool) @@ -47,6 +75,7 @@ dcp::locale_convert (int x, int, bool) return buffer; } + template<> string dcp::locale_convert (unsigned int x, int, bool) @@ -56,15 +85,21 @@ dcp::locale_convert (unsigned int x, int, bool) return buffer; } + template<> string dcp::locale_convert (long int x, int, bool) { char buffer[64]; +#ifdef LIBDCP_WINDOWS + __mingw_snprintf (buffer, sizeof(buffer), "%ld", x); +#else snprintf (buffer, sizeof(buffer), "%ld", x); +#endif return buffer; } + template<> string dcp::locale_convert (unsigned long int x, int, bool) @@ -74,6 +109,7 @@ dcp::locale_convert (unsigned long int x, int, bool) return buffer; } + template<> string dcp::locale_convert (long long int x, int, bool) @@ -87,6 +123,7 @@ dcp::locale_convert (long long int x, int, bool) return buffer; } + template<> string dcp::locale_convert (unsigned long long int x, int, bool) @@ -100,6 +137,7 @@ dcp::locale_convert (unsigned long long int x, int, bool) return buffer; } + template<> string dcp::locale_convert (float x, int precision, bool fixed) @@ -115,6 +153,7 @@ dcp::locale_convert (float x, int precision, bool fixed) return buffer; } + template<> string dcp::locale_convert (double x, int precision, bool fixed) @@ -130,6 +169,7 @@ dcp::locale_convert (double x, int precision, bool fixed) return buffer; } + template<> string dcp::locale_convert (string x, int, bool) @@ -137,6 +177,7 @@ dcp::locale_convert (string x, int, bool) return x; } + template<> string dcp::locale_convert (char* x, int, bool) @@ -144,6 +185,7 @@ dcp::locale_convert (char* x, int, bool) return x; } + template<> string dcp::locale_convert (char const * x, int, bool) @@ -151,6 +193,7 @@ dcp::locale_convert (char const * x, int, bool) return x; } + template<> string dcp::locale_convert (wchar_t const * x, int, bool) @@ -159,6 +202,7 @@ dcp::locale_convert (wchar_t const * x, int, bool) return string (s.begin(), s.end()); } + template<> string dcp::locale_convert (char x, int, bool) @@ -168,6 +212,7 @@ dcp::locale_convert (char x, int, bool) return s; } + template<> string dcp::locale_convert (boost::filesystem::path x, int, bool) @@ -175,6 +220,37 @@ dcp::locale_convert (boost::filesystem::path x, int, bool) return x.string(); } + +template<> +unsigned char +dcp::locale_convert (string x, int, bool) +{ + unsigned char y = 0; + sscanf (x.c_str(), "%hhu", &y); + return y; +} + + +template<> +unsigned short int +dcp::locale_convert (string x, int, bool) +{ + unsigned short int y = 0; + sscanf (x.c_str(), "%hu", &y); + return y; +} + + +template<> +unsigned int +dcp::locale_convert (string x, int, bool) +{ + unsigned int y = 0; + sscanf (x.c_str(), "%u", &y); + return y; +} + + template<> int dcp::locale_convert (string x, int, bool) @@ -184,8 +260,9 @@ dcp::locale_convert (string x, int, bool) return y; } + template<> -long int +long dcp::locale_convert (string x, int, bool) { long int y = 0; @@ -193,6 +270,49 @@ dcp::locale_convert (string x, int, bool) return y; } + +template<> +unsigned long +dcp::locale_convert (string x, int, bool) +{ + unsigned long y = 0; +#ifdef LIBDCP_WINDOWS + __mingw_sscanf (x.c_str(), "%lud", &y); +#else + sscanf (x.c_str(), "%lud", &y); +#endif + return y; +} + + +template<> +long long +dcp::locale_convert (string x, int, bool) +{ + long long y = 0; +#ifdef LIBDCP_WINDOWS + __mingw_sscanf (x.c_str(), "%lld", &y); +#else + sscanf (x.c_str(), "%lld", &y); +#endif + return y; +} + + +template<> +unsigned long long +dcp::locale_convert (string x, int, bool) +{ + unsigned long long y = 0; +#ifdef LIBDCP_WINDOWS + __mingw_sscanf (x.c_str(), "%llud", &y); +#else + sscanf (x.c_str(), "%llud", &y); +#endif + return y; +} + + template<> float dcp::locale_convert (string x, int, bool) @@ -202,6 +322,7 @@ dcp::locale_convert (string x, int, bool) return y; } + template<> double dcp::locale_convert (string x, int, bool)