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

  1. // locale standard header
  2.  
  3. #if     _MSC_VER > 1000 /*IFSTRIP=IGN*/
  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. #ifdef __FORCE_INSTANCE
  71. template class _CRTIMP2 collate<char>;
  72. template class _CRTIMP2 collate<wchar_t>;
  73. #else        // __FORCE_INSTANCE
  74. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  75. extern template class _CRTIMP2 collate<char>;
  76. extern template class _CRTIMP2 collate<wchar_t>;
  77. #pragma warning(default:4231) /* restore previous warning */
  78. #endif        // __FORCE_INSTANCE
  79. #endif        // _DLL
  80.  
  81. template<class _E>
  82.     locale::id collate<_E>::id;
  83.         // TEMPLATE CLASS collate_byname
  84. template<class _E>
  85.     class collate_byname : public collate<_E> {
  86. public:
  87.     explicit collate_byname(const char *_S, size_t _R = 0)
  88.         : collate<_E>(_Locinfo(_S), _R) {}
  89. _PROTECTED:
  90.     virtual ~collate_byname()
  91.         {}
  92.     };
  93.         // STRUCT messages_base
  94. struct _CRTIMP2 messages_base : public locale::facet {
  95.     typedef int catalog;
  96.     explicit messages_base(size_t _R = 0)
  97.         : locale::facet(_R) {}
  98.     };
  99.         // TEMPLATE CLASS messages
  100. template<class _E>
  101.     class messages : public messages_base {
  102. public:
  103.     typedef _E char_type;
  104.     typedef basic_string<_E, char_traits<_E>,
  105.         allocator<_E> > string_type;
  106.     catalog open(const string& _X, const locale& _L) const
  107.         {return (do_open(_X, _L)); }
  108.     string_type get(catalog _C, int _S, int _M,
  109.         const string_type& _D) const
  110.         {return (do_get(_C, _S, _M, _D)); }
  111.     void close(catalog _C) const
  112.         {do_close(_C); }
  113.     static locale::id id;
  114.     explicit messages(size_t _R = 0)
  115.         : messages_base(_R) {_Init(_Locinfo()); }
  116.     messages(const _Locinfo& _Lobj, size_t _R = 0)
  117.         : messages_base(_R) {_Init(_Lobj); }
  118.     static size_t __cdecl _Getcat()
  119.         {return (_LC_MESSAGE); }
  120. _PROTECTED:
  121.     ~messages()
  122.         {delete[] _No;
  123.         delete[] _Yes; }
  124. protected:
  125.     void _Init(const _Locinfo& _Lobj)
  126.         {_No = _MAKLOCSTR(_E, _Lobj._Getno());
  127.         _Yes = _MAKLOCSTR(_E, _Lobj._Getyes()); }
  128.     virtual catalog do_open(const string&, const locale&) const
  129.         {return (0); }
  130.     virtual string_type do_get(catalog, int,
  131.         int _M, const string_type& _D) const
  132.         {if (_M == 0)
  133.             return (_No);
  134.         else if (_M == 1)
  135.             return (_Yes);
  136.         else
  137.             return (_D); }
  138.     virtual void do_close(catalog) const
  139.         {}
  140. private:
  141.     _E *_No, *_Yes;
  142.     };
  143.  
  144. #ifdef    _DLL
  145. #ifdef __FORCE_INSTANCE
  146. template class _CRTIMP2 messages<char>;
  147. template class _CRTIMP2 messages<wchar_t>;
  148. #else        // __FORCE_INSTANCE
  149. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  150. extern template class _CRTIMP2 messages<char>;
  151. extern template class _CRTIMP2 messages<wchar_t>;
  152. #pragma warning(default:4231) /* restore previous warning */
  153. #endif        // __FORCE_INSTANCE
  154. #endif        // _DLL
  155.  
  156. template<class _E>
  157.     locale::id messages<_E>::id;
  158.         // TEMPLATE CLASS messages_byname
  159. template<class _E>
  160.     class messages_byname : public messages<_E> {
  161. public:
  162.     explicit messages_byname(const char *_S, size_t _R = 0)
  163.         : messages<_E>(_Locinfo(_S), _R) {}
  164. _PROTECTED:
  165.     virtual ~messages_byname()
  166.         {}
  167.     };
  168.         // locale SUPPORT TEMPLATES
  169.  #define _HAS(loc, fac)    has_facet(loc, (fac *)0)
  170. template<class _F> inline
  171.     bool has_facet(const locale& _L, const _F *) //    _THROW0()
  172.     {size_t _Id = _F::id;
  173.     const locale::facet *_Pf = (_F *)0;
  174.     return (_L._Getfacet(_Id) != 0
  175.         || _L._Iscloc() && _F::_Getcat() != (size_t)(-1)); }
  176. typedef collate<char> _Collchar;
  177. inline bool locale::operator()(const string& _X,
  178.     const string& _Y) const
  179.     {const _Collchar& _Fac = _USE(*this, _Collchar);
  180.     return (_Fac.compare(_X.begin(), _X.end(),
  181.         _Y.begin(), _Y.end()) < 0); }
  182.         // ctype TEMPLATE FUNCTIONS
  183. template<class _E> inline
  184.     bool (isalnum)(_E _C, const locale& _L)
  185.     {return (_USE(_L, ctype<_E>).is(ctype_base::alnum, _C)); }
  186. template<class _E> inline
  187.     bool (isalpha)(_E _C, const locale& _L)
  188.     {return (_USE(_L, ctype<_E>).is(ctype_base::alpha, _C)); }
  189. template<class _E> inline
  190.     bool (iscntrl)(_E _C, const locale& _L)
  191.     {return (_USE(_L, ctype<_E>).is(ctype_base::cntrl, _C)); }
  192. template<class _E> inline
  193.     bool (isgraph)(_E _C, const locale& _L)
  194.     {return (_USE(_L, ctype<_E>).is(ctype_base::graph, _C)); }
  195. template<class _E> inline
  196.     bool (islower)(_E _C, const locale& _L)
  197.     {return (_USE(_L, ctype<_E>).is(ctype_base::lower, _C)); }
  198. template<class _E> inline
  199.     bool (isprint)(_E _C, const locale& _L)
  200.     {return (_USE(_L, ctype<_E>).is(ctype_base::print, _C)); }
  201. template<class _E> inline
  202.     bool (ispunct)(_E _C, const locale& _L)
  203.     {return (_USE(_L, ctype<_E>).is(ctype_base::punct, _C)); }
  204. template<class _E> inline
  205.     bool (isspace)(_E _C, const locale& _L)
  206.     {return (_USE(_L, ctype<_E>).is(ctype_base::space, _C)); }
  207. template<class _E> inline
  208.     bool (isupper)(_E _C, const locale& _L)
  209.     {return (_USE(_L, ctype<_E>).is(ctype_base::upper, _C)); }
  210. template<class _E> inline
  211.     bool (isxdigit)(_E _C, const locale& _L)
  212.     {return (_USE(_L, ctype<_E>).is(ctype_base::xdigit, _C)); }
  213. template<class _E> inline
  214.     _E (tolower)(_E _C, const locale& _L)
  215.     {return (_USE(_L, ctype<_E>).tolower(_C)); }
  216. template<class _E> inline
  217.     _E (toupper)(_E _C, const locale& _L)
  218.     {return (_USE(_L, ctype<_E>).toupper(_C)); }
  219.  
  220. _STD_END
  221.  
  222. #ifdef  _MSC_VER
  223. #pragma pack(pop)
  224. #endif  /* _MSC_VER */
  225.  
  226. #endif /* _LOCALE_ */
  227.  
  228. /*
  229.  * Copyright (c) 1995 by P.J. Plauger.  ALL RIGHTS RESERVED. 
  230.  * Consult your license regarding permissions and restrictions.
  231.  */
  232.