#ifndef __JLANG__JWHITESPACESFACET__ #define __JLANG__JWHITESPACESFACET__ #include #include /** * \author mdejong */ namespace JLANG {} namespace JPP { using namespace JLANG; } namespace JLANG { /** * Auxiliary class to ensure that the mask set is defined prior to * the ctype<>() constructor call. */ class JMask { public: typedef std::ctype ctype; protected: ctype::mask mask_set[ctype::table_size]; }; /** * Auxiliary class to specify white space character(s) in currect locale. */ class JWhiteSpacesFacet : private JMask, public std::ctype { public: /** * Constructor. * * \param loc current locale * \param ws white space character */ JWhiteSpacesFacet(const std::locale& loc, const char ws) : JMask(), JMask::ctype(mask_set) { configure(loc, std::string(1, ws)); } /** * Constructor. * * \param loc current locale * \param ws white space character(s) */ JWhiteSpacesFacet(const std::locale& loc, const std::string& ws) : JMask(), JMask::ctype(mask_set) { configure(loc, ws); } private: /** * Configure. * * \param loc current locale * \param ws white space character(s) */ void configure(const std::locale& loc, const std::string& ws) { using namespace std; char buffer[table_size]; for (unsigned int i = 0; i != table_size; ++i) { buffer[i] = (char) i; } use_facet(loc).is(buffer, buffer + table_size, mask_set); for (unsigned int i = 0; i != table_size; ++i) { if (mask_set[i] & JMask::ctype::space) { mask_set[i] ^= JMask::ctype::space; } } for (string::const_iterator i = ws.begin(); i != ws.end(); ++i) { mask_set[(int) *i] |= JMask::ctype::space; } } }; } #endif