Leaked source code of windows server 2003
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.

238 lines
6.7 KiB

  1. // locale standard header
  2. #pragma once
  3. #ifndef _LOCALE_
  4. #define _LOCALE_
  5. #include <string>
  6. #include <xlocmes>
  7. #include <xlocmon>
  8. #include <xlocnum>
  9. #include <xloctime>
  10. #pragma pack(push,8)
  11. #pragma warning(push,3)
  12. _STD_BEGIN
  13. // TEMPLATE CLASS collate
  14. template<class _Elem>
  15. class collate
  16. : public locale::facet
  17. { // facet for ordering sequences of elements
  18. public:
  19. typedef _Elem char_type;
  20. typedef basic_string<_Elem, char_traits<_Elem>,
  21. allocator<_Elem> > string_type;
  22. int compare(const _Elem *_First1, const _Elem *_Last1,
  23. const _Elem *_First2, const _Elem *_Last2) const
  24. { // compare [_First1, _Last1) to [_First2, _Last2)
  25. return (do_compare(_First1, _Last1, _First2, _Last2));
  26. }
  27. string_type transform(const _Elem *_First, const _Elem *_Last) const
  28. { // transform [_First, _Last) to key string
  29. return (do_transform(_First, _Last));
  30. }
  31. long hash(const _Elem *_First, const _Elem *_Last) const
  32. { // compute hash code for [_First, _Last)
  33. return (do_hash(_First, _Last));
  34. }
  35. static locale::id id; // unique facet id
  36. explicit collate(size_t _Refs = 0)
  37. : locale::facet(_Refs)
  38. { // construct from current locale
  39. _Init(_Locinfo());
  40. }
  41. collate(const _Locinfo& _Lobj, size_t _Refs = 0)
  42. : locale::facet(_Refs)
  43. { // construct from specified locale
  44. _Init(_Lobj);
  45. }
  46. static size_t __cdecl _Getcat(const locale::facet **_Ppf = 0)
  47. { // return locale category mask and construct standard facet
  48. if (_Ppf != 0 && *_Ppf == 0)
  49. *_Ppf = _NEW_CRT collate<_Elem>;
  50. return (_X_COLLATE);
  51. }
  52. _PROTECTED:
  53. ~collate()
  54. { // destroy the object
  55. }
  56. protected:
  57. void _Init(const _Locinfo& _Lobj)
  58. { // initialize from _Lobj
  59. _Coll = _Lobj._Getcoll();
  60. }
  61. virtual int do_compare(const _Elem *_First1, const _Elem *_Last1,
  62. const _Elem *_First2, const _Elem *_Last2) const
  63. { // compare [_First1, _Last1) to [_First2, _Last2)
  64. return (_LStrcoll(_First1, _Last1, _First2, _Last2, &_Coll));
  65. }
  66. virtual string_type do_transform(const _Elem *_First,
  67. const _Elem *_Last) const
  68. { // transform [_First, _Last) to key string
  69. size_t _Count;
  70. string_type _Str;
  71. for (_Count = _Last - _First; ; )
  72. { // grow string if locale-specific strxfrm fails
  73. _Str.resize(_Count);
  74. if ((_Count = _LStrxfrm(&*_Str.begin(), &*_Str.end(),
  75. _First, _Last, &_Coll)) <= _Str.size())
  76. break;
  77. }
  78. _Str.resize(_Count);
  79. return (_Str);
  80. }
  81. virtual long do_hash(const _Elem *_First, const _Elem *_Last) const
  82. { // compute hash code for [_First, _Last)
  83. unsigned long _Val = 0;
  84. for (; _First != _Last; ++_First)
  85. _Val = (_Val << 8 | _Val >> 24) + *_First;
  86. return ((long)_Val);
  87. }
  88. private:
  89. _Locinfo::_Collvec _Coll; // used by _LStrcoll and _XStrxfrm
  90. };
  91. // STATIC collate::id OBJECT
  92. template<class _Elem>
  93. locale::id collate<_Elem>::id;
  94. #ifdef _DLL_CPPLIB
  95. template class _CRTIMP2 collate<char>;
  96. template class _CRTIMP2 collate<wchar_t>;
  97. #endif // _DLL_CPPLIB
  98. // TEMPLATE CLASS collate_byname
  99. template<class _Elem>
  100. class collate_byname
  101. : public collate<_Elem>
  102. { // collate for named locale
  103. public:
  104. explicit collate_byname(const char *_Locname, size_t _Refs = 0)
  105. : collate<_Elem>(_Locinfo(_Locname), _Refs)
  106. { // construct for named locale
  107. }
  108. _PROTECTED:
  109. virtual ~collate_byname()
  110. { // destroy the object
  111. }
  112. };
  113. // locale SUPPORT TEMPLATES
  114. #define _HAS(loc, fac) has_facet<fac>(loc)
  115. template<class _Facet> inline
  116. bool has_facet(const locale& _Loc) _THROW0()
  117. { // test if facet is in locale
  118. _Lockit _Lock(_LOCK_LOCALE); // the thread lock, make get atomic
  119. size_t _Id = _Facet::id;
  120. return (_Loc._Getfacet(_Id) != 0 || _Facet::_Getcat() != (size_t)(-1));
  121. }
  122. template<class _Facet> inline _DEPRECATED
  123. bool has_facet(const locale& _Loc, const _Facet *) _THROW0()
  124. { // test if facet is in locale -- retained, two arg version
  125. return (has_facet<_Facet>(_Loc));
  126. }
  127. // ctype TEMPLATE FUNCTIONS
  128. template<class _Elem> inline
  129. bool (isalnum)(_Elem _Ch, const locale& _Loc)
  130. { // test if character is alphanumeric, locale specific
  131. return (_USE(_Loc, ctype<_Elem>).is(ctype_base::alnum, _Ch));
  132. }
  133. template<class _Elem> inline
  134. bool (isalpha)(_Elem _Ch, const locale& _Loc)
  135. { // test if character is alphabetic, locale specific
  136. return (_USE(_Loc, ctype<_Elem>).is(ctype_base::alpha, _Ch));
  137. }
  138. template<class _Elem> inline
  139. bool (iscntrl)(_Elem _Ch, const locale& _Loc)
  140. { // test if character is control, locale specific
  141. return (_USE(_Loc, ctype<_Elem>).is(ctype_base::cntrl, _Ch));
  142. }
  143. template<class _Elem> inline
  144. bool (isdigit)(_Elem _Ch, const locale& _Loc)
  145. { // test if character is digit, locale specific
  146. return (_USE(_Loc, ctype<_Elem>).is(ctype_base::digit, _Ch));
  147. }
  148. template<class _Elem> inline
  149. bool (isgraph)(_Elem _Ch, const locale& _Loc)
  150. { // test if character is graphic, locale specific
  151. return (_USE(_Loc, ctype<_Elem>).is(ctype_base::graph, _Ch));
  152. }
  153. template<class _Elem> inline
  154. bool (islower)(_Elem _Ch, const locale& _Loc)
  155. { // test if character is lower case, locale specific
  156. return (_USE(_Loc, ctype<_Elem>).is(ctype_base::lower, _Ch));
  157. }
  158. template<class _Elem> inline
  159. bool (isprint)(_Elem _Ch, const locale& _Loc)
  160. { // test if character is printing, locale specific
  161. return (_USE(_Loc, ctype<_Elem>).is(ctype_base::print, _Ch));
  162. }
  163. template<class _Elem> inline
  164. bool (ispunct)(_Elem _Ch, const locale& _Loc)
  165. { // test if character is punctuation, locale specific
  166. return (_USE(_Loc, ctype<_Elem>).is(ctype_base::punct, _Ch));
  167. }
  168. template<class _Elem> inline
  169. bool (isspace)(_Elem _Ch, const locale& _Loc)
  170. { // test if character is whitespace, locale specific
  171. return (_USE(_Loc, ctype<_Elem>).is(ctype_base::space, _Ch));
  172. }
  173. template<class _Elem> inline
  174. bool (isupper)(_Elem _Ch, const locale& _Loc)
  175. { // test if character is upper case, locale specific
  176. return (_USE(_Loc, ctype<_Elem>).is(ctype_base::upper, _Ch));
  177. }
  178. template<class _Elem> inline
  179. bool (isxdigit)(_Elem _Ch, const locale& _Loc)
  180. { // test if character is hexadecimal digit, locale specific
  181. return (_USE(_Loc, ctype<_Elem>).is(ctype_base::xdigit, _Ch));
  182. }
  183. template<class _Elem> inline
  184. _Elem (tolower)(_Elem _Ch, const locale& _Loc)
  185. { // convert character to lower case, locale specific
  186. return (_USE(_Loc, ctype<_Elem>).tolower(_Ch));
  187. }
  188. template<class _Elem> inline
  189. _Elem (toupper)(_Elem _Ch, const locale& _Loc)
  190. { // convert character to upper case, locale specific
  191. return (_USE(_Loc, ctype<_Elem>).toupper(_Ch));
  192. }
  193. _STD_END
  194. #pragma warning(pop)
  195. #pragma pack(pop)
  196. #endif /* _LOCALE_ */
  197. /*
  198. * Copyright (c) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  199. * Consult your license regarding permissions and restrictions.
  200. V3.10:0009 */