From fa2840e72b9ff58c363ee279bcfd1fbcc6d6049e Mon Sep 17 00:00:00 2001 From: Enrico Forestieri Date: Fri, 17 Nov 2006 19:27:42 +0000 Subject: [PATCH] Add locale facet in order to input numbers from wide streams on systems where sizeof(wchar_t) == 2 * src/support/docstring.C: - New class ascii_num_get_facet for inputting numbers from idocstreams. - Add the ascii_num_get_facet facet to the locale initializer. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@15959 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/support/docstring.C | 44 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/src/support/docstring.C b/src/support/docstring.C index b3e8a76b1e..2be4134c5c 100644 --- a/src/support/docstring.C +++ b/src/support/docstring.C @@ -443,6 +443,47 @@ protected: }; +/// Facet for inputting ascii representations of numbers from idocstreams. +/// Here we simply need defining the virtual do_get functions. +class ascii_num_get_facet : public std::num_get > > +{ + typedef std::istreambuf_iterator > iter_type; +public: + ascii_num_get_facet(size_t refs = 0) : std::num_get(refs) {} + + /// Facet for converting ascii representation of numbers to a value. + class string_num_get_facet : public std::num_get::iterator> + { + public: + string_num_get_facet() : std::num_get::iterator>(1) {} + }; + +private: + bool isNumpunct(lyx::char_type const c) const + { + /// Only account for the standard numpunct "C" locale facet. + return c < 0x80 && (c == '-' || c == '+' || isdigit(c) + || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F') + || c == 'x' || c == 'X'); + } + +protected: + iter_type + do_get(iter_type iit, iter_type eit, std::ios_base & b, + std::ios_base::iostate & err, long & v) const + { + std::string s; + s.resize(64); + for (int i = 0; iit != eit && isNumpunct(*iit); ++i, ++iit) + s[i] = static_cast(*iit); + string_num_get_facet f; + f.get(s.begin(), s.end(), b, err, v); + + return iit; + } +}; + + /// class to add our facets to the global locale class locale_initializer { public: @@ -451,7 +492,8 @@ public: std::locale global; std::locale const loc1(global, new ascii_ctype_facet); std::locale const loc2(loc1, new ascii_num_put_facet); - std::locale::global(loc2); + std::locale const loc3(loc2, new ascii_num_get_facet); + std::locale::global(loc3); } }; -- 2.39.2