home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / include / locale < prev    next >
Text File  |  1998-06-16  |  7KB  |  222 lines

  1. // locale standard header
  2.  
  3. #if     _MSC_VER > 1000
  4. #pragma once
  5. #endif
  6.  
  7. #ifndef _LOCALE_
  8. #define _LOCALE_
  9. #include <string>
  10. #include <xlocmon>
  11. #include <xlocnum>
  12. #include <xloctime>
  13.  
  14. #ifdef  _MSC_VER
  15. #pragma pack(push,8)
  16. #endif  /* _MSC_VER */
  17. _STD_BEGIN
  18.         // TEMPLATE CLASS collate
  19. template<class _E>
  20.     class collate : public locale::facet {
  21. public:
  22.     typedef _E char_type;
  23.     typedef basic_string<_E, char_traits<_E>,
  24.         allocator<_E> > string_type;
  25.     int compare(const _E *_F1, const _E *_L1,
  26.         const _E *_F2, const _E *_L2) const
  27.         {return (do_compare(_F1, _L1, _F2, _L2)); }
  28.     string_type transform(const _E *_F, const _E *_L) const
  29.         {return (do_transform(_F, _L)); }
  30.     long hash(const _E *_F, const _E *_L) const
  31.         {return (do_hash(_F, _L)); }
  32.     static locale::id id;
  33.     explicit collate(size_t _R = 0)
  34.         : locale::facet(_R) {_Init(_Locinfo()); }
  35.     collate(const _Locinfo& _Lobj, size_t _R = 0)
  36.         : locale::facet(_R) {_Init(_Lobj); }
  37.     static size_t __cdecl _Getcat()
  38.         {return (_LC_COLLATE); }
  39. _PROTECTED:
  40.     ~collate()
  41.         {}
  42. protected:
  43.     void _Init(const _Locinfo& _Lobj)
  44.         {_Coll = _Lobj._Getcoll(); }
  45.     virtual int do_compare(const _E *_F1, const _E *_L1,
  46.         const _E *_F2, const _E *_L2) const
  47.         {return (_Strcoll(_F1, _L1, _F2, _L2, &_Coll)); }
  48.     virtual string_type do_transform(const _E *_F,
  49.         const _E *_L) const
  50.         {size_t _I, _N;
  51.         string_type _Str;
  52.         for (_N = _L - _F; ; )
  53.             {_Str.append(_N, '\0');
  54.             if ((_I = _Strxfrm(_Str.begin(), _Str.end(),
  55.                 _F, _L, &_Coll)) <= _Str.size())
  56.                 break;
  57.             _N = _Str.size() < _I ? _I - _Str.size() : 1; }
  58.         _Str.resize(_I);
  59.         return (_Str); }
  60.     virtual long do_hash(const _E *_F, const _E *_L) const
  61.         {unsigned long _V = 0;
  62.         for (; _F != _L; ++_F)
  63.             _V = (_V << 8 | _V >> 24) + *_F;
  64.         return ((long)_V); }
  65. private:
  66.     _Locinfo::_Collvec _Coll;
  67.     };
  68.  
  69. #ifdef    _DLL
  70. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  71. extern template class _CRTIMP collate<char>;
  72. extern template class _CRTIMP collate<wchar_t>;
  73. #pragma warning(default:4231) /* restore previous warning */
  74. #endif        // _DLL
  75.  
  76. template<class _E>
  77.     locale::id collate<_E>::id;
  78.         // TEMPLATE CLASS collate_byname
  79. template<class _E>
  80.     class collate_byname : public collate<_E> {
  81. public:
  82.     explicit collate_byname(const char *_S, size_t _R = 0)
  83.         : collate<_E>(_Locinfo(_S), _R) {}
  84. _PROTECTED:
  85.     virtual ~collate_byname()
  86.         {}
  87.     };
  88.         // STRUCT messages_base
  89. struct _CRTIMP messages_base : public locale::facet {
  90.     typedef int catalog;
  91.     explicit messages_base(size_t _R = 0)
  92.         : locale::facet(_R) {}
  93.     };
  94.         // TEMPLATE CLASS messages
  95. template<class _E>
  96.     class messages : public messages_base {
  97. public:
  98.     typedef _E char_type;
  99.     typedef basic_string<_E, char_traits<_E>,
  100.         allocator<_E> > string_type;
  101.     catalog open(const string& _X, const locale& _L) const
  102.         {return (do_open(_X, _L)); }
  103.     string_type get(catalog _C, int _S, int _M,
  104.         const string_type& _D) const
  105.         {return (do_get(_C, _S, _M, _D)); }
  106.     void close(catalog _C) const
  107.         {do_close(_C); }
  108.     static locale::id id;
  109.     explicit messages(size_t _R = 0)
  110.         : messages_base(_R) {_Init(_Locinfo()); }
  111.     messages(const _Locinfo& _Lobj, size_t _R = 0)
  112.         : messages_base(_R) {_Init(_Lobj); }
  113.     static size_t __cdecl _Getcat()
  114.         {return (_LC_MESSAGE); }
  115. _PROTECTED:
  116.     ~messages()
  117.         {delete[] _No;
  118.         delete[] _Yes; }
  119. protected:
  120.     void _Init(const _Locinfo& _Lobj)
  121.         {_No = _MAKLOCSTR(_E, _Lobj._Getno());
  122.         _Yes = _MAKLOCSTR(_E, _Lobj._Getyes()); }
  123.     virtual catalog do_open(const string&, const locale&) const
  124.         {return (0); }
  125.     virtual string_type do_get(catalog, int,
  126.         int _M, const string_type& _D) const
  127.         {if (_M == 0)
  128.             return (_No);
  129.         else if (_M == 1)
  130.             return (_Yes);
  131.         else
  132.             return (_D); }
  133.     virtual void do_close(catalog) const
  134.         {}
  135. private:
  136.     _E *_No, *_Yes;
  137.     };
  138.  
  139. #ifdef    _DLL
  140. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  141. extern template class _CRTIMP messages<char>;
  142. extern template class _CRTIMP messages<wchar_t>;
  143. #pragma warning(default:4231) /* restore previous warning */
  144. #endif        // _DLL
  145.  
  146. template<class _E>
  147.     locale::id messages<_E>::id;
  148.         // TEMPLATE CLASS messages_byname
  149. template<class _E>
  150.     class messages_byname : public messages<_E> {
  151. public:
  152.     explicit messages_byname(const char *_S, size_t _R = 0)
  153.         : messages<_E>(_Locinfo(_S), _R) {}
  154. _PROTECTED:
  155.     virtual ~messages_byname()
  156.         {}
  157.     };
  158.         // locale SUPPORT TEMPLATES
  159.  #define _HAS(loc, fac)    has_facet(loc, (fac *)0)
  160. template<class _F> inline
  161.     bool has_facet(const locale& _L, const _F *) //    _THROW0()
  162.     {size_t _Id = _F::id;
  163.     const locale::facet *_Pf = (_F *)0;
  164.     return (_L._Getfacet(_Id) != 0
  165.         || _L._Iscloc() && _F::_Getcat() != (size_t)(-1)); }
  166. typedef collate<char> _Collchar;
  167. inline bool locale::operator()(const string& _X,
  168.     const string& _Y) const
  169.     {const _Collchar& _Fac = _USE(*this, _Collchar);
  170.     return (_Fac.compare(_X.begin(), _X.end(),
  171.         _Y.begin(), _Y.end()) < 0); }
  172.         // ctype TEMPLATE FUNCTIONS
  173. template<class _E> inline
  174.     bool (isalnum)(_E _C, const locale& _L)
  175.     {return (_USE(_L, ctype<_E>).is(ctype_base::alnum, _C)); }
  176. template<class _E> inline
  177.     bool (isalpha)(_E _C, const locale& _L)
  178.     {return (_USE(_L, ctype<_E>).is(ctype_base::alpha, _C)); }
  179. template<class _E> inline
  180.     bool (iscntrl)(_E _C, const locale& _L)
  181.     {return (_USE(_L, ctype<_E>).is(ctype_base::cntrl, _C)); }
  182. template<class _E> inline
  183.     bool (isgraph)(_E _C, const locale& _L)
  184.     {return (_USE(_L, ctype<_E>).is(ctype_base::graph, _C)); }
  185. template<class _E> inline
  186.     bool (islower)(_E _C, const locale& _L)
  187.     {return (_USE(_L, ctype<_E>).is(ctype_base::lower, _C)); }
  188. template<class _E> inline
  189.     bool (isprint)(_E _C, const locale& _L)
  190.     {return (_USE(_L, ctype<_E>).is(ctype_base::print, _C)); }
  191. template<class _E> inline
  192.     bool (ispunct)(_E _C, const locale& _L)
  193.     {return (_USE(_L, ctype<_E>).is(ctype_base::punct, _C)); }
  194. template<class _E> inline
  195.     bool (isspace)(_E _C, const locale& _L)
  196.     {return (_USE(_L, ctype<_E>).is(ctype_base::space, _C)); }
  197. template<class _E> inline
  198.     bool (isupper)(_E _C, const locale& _L)
  199.     {return (_USE(_L, ctype<_E>).is(ctype_base::upper, _C)); }
  200. template<class _E> inline
  201.     bool (isxdigit)(_E _C, const locale& _L)
  202.     {return (_USE(_L, ctype<_E>).is(ctype_base::xdigit, _C)); }
  203. template<class _E> inline
  204.     _E (tolower)(_E _C, const locale& _L)
  205.     {return (_USE(_L, ctype<_E>).tolower(_C)); }
  206. template<class _E> inline
  207.     _E (toupper)(_E _C, const locale& _L)
  208.     {return (_USE(_L, ctype<_E>).toupper(_C)); }
  209.  
  210. _STD_END
  211.  
  212. #ifdef  _MSC_VER
  213. #pragma pack(pop)
  214. #endif  /* _MSC_VER */
  215.  
  216. #endif /* _LOCALE_ */
  217.  
  218. /*
  219.  * Copyright (c) 1995 by P.J. Plauger.  ALL RIGHTS RESERVED. 
  220.  * Consult your license regarding permissions and restrictions.
  221.  */
  222.