Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
6.4 KiB

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