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.

285 lines
8.7 KiB

  1. // xloctime internal header (from <locale>)
  2. #ifndef _XLOCTIME_
  3. #define _XLOCTIME_
  4. #include <ctime>
  5. #include <xiosbase>
  6. #ifdef _MSC_VER
  7. #pragma pack(push,8)
  8. #endif /* _MSC_VER */
  9. _STD_BEGIN
  10. // STRUCT time_base
  11. struct _CRTIMP2 time_base : public locale::facet {
  12. enum _Dateorder {no_order, dmy, mdy, ymd, ydm};
  13. _BITMASK(_Dateorder, dateorder);
  14. time_base(size_t _R = 0)
  15. : locale::facet(_R) {}
  16. };
  17. _BITMASK_OPS(time_base::_Dateorder);
  18. // TEMPLATE CLASS time_get
  19. template<class _E,
  20. class _II = istreambuf_iterator<_E, char_traits<_E> > >
  21. class time_get : public time_base {
  22. public:
  23. typedef _E char_type;
  24. typedef _II iter_type;
  25. static locale::id id;
  26. dateorder date_order() const
  27. {return (do_date_order()); }
  28. _II get_time(_II _F, _II _L, ios_base& _X,
  29. ios_base::iostate& _St, tm *_Tp) const
  30. {return (do_get_time(_F, _L, _X, _St, _Tp)); }
  31. _II get_date(_II _F, _II _L, ios_base& _X,
  32. ios_base::iostate& _St, tm *_Tp) const
  33. {return (do_get_date(_F, _L, _X, _St, _Tp)); }
  34. _II get_weekday(_II _F, _II _L, ios_base& _X,
  35. ios_base::iostate& _St, tm *_Tp) const
  36. {return (do_get_weekday(_F, _L, _X, _St, _Tp)); }
  37. _II get_monthname(_II _F, _II _L, ios_base& _X,
  38. ios_base::iostate& _St, tm *_Tp) const
  39. {return (do_get_monthname(_F, _L, _X, _St, _Tp)); }
  40. _II get_year(_II _F, _II _L, ios_base& _X,
  41. ios_base::iostate& _St, tm *_Tp) const
  42. {return (do_get_year(_F, _L, _X, _St, _Tp)); }
  43. explicit time_get(size_t _R = 0)
  44. : time_base(_R) {_Init(_Locinfo()); }
  45. time_get(const _Locinfo& _Lobj, size_t _R = 0)
  46. : time_base(_R) {_Init(_Lobj); }
  47. static size_t __cdecl _Getcat()
  48. {return (_LC_TIME); }
  49. _PROTECTED:
  50. virtual ~time_get()
  51. {delete[] _Days;
  52. delete[] _Months; }
  53. protected:
  54. void _Init(const _Locinfo& _Lobj)
  55. {_Days = _MAKLOCSTR(_E, _Lobj._Getdays());
  56. _Months = _MAKLOCSTR(_E, _Lobj._Getmonths()); }
  57. virtual dateorder do_date_order() const
  58. {return (mdy); }
  59. virtual _II do_get_time(_II _F, _II _L, ios_base& _X,
  60. ios_base::iostate& _St, tm *_Tp) const
  61. {const _E _Colon = _WIDEN(_E, ':');
  62. _St |= _Getint(_F, _L, 0, 23, _Tp->tm_hour);
  63. if (_St != ios_base::goodbit || *_F != _Colon)
  64. _St |= ios_base::failbit;
  65. else
  66. _St |= _Getint(++_F, _L, 0, 59, _Tp->tm_min);
  67. if (_St != ios_base::goodbit || *_F != _Colon)
  68. _St |= ios_base::failbit;
  69. else
  70. _St |= _Getint(++_F, _L, 0, 59, _Tp->tm_sec);
  71. return (_F); }
  72. virtual _II do_get_date(_II _F, _II _L, ios_base& _X,
  73. ios_base::iostate& _St, tm *_Tp) const // Dec 2, 1979
  74. {_F = get_monthname(_F, _L, _X, _St, _Tp);
  75. if (_St != ios_base::goodbit || *_F != _WIDEN(_E, ' '))
  76. _St |= ios_base::failbit;
  77. else
  78. _St |= _Getint(++_F, _L, 1, 31, _Tp->tm_mday);
  79. if (_St != ios_base::goodbit || *_F != _WIDEN(_E, ','))
  80. _St |= ios_base::failbit;
  81. else if (++_F == _L || *_F != _WIDEN(_E, ' '))
  82. _St |= ios_base::failbit;
  83. else
  84. _F = get_year(++_F, _L, _X, _St, _Tp);
  85. if (_F == _L)
  86. _St |= ios_base::eofbit;
  87. return (_F); }
  88. virtual _II do_get_weekday(_II _F, _II _L, ios_base& _X,
  89. ios_base::iostate& _St, tm *_Tp) const
  90. {int _N = _Getloctxt(_F, _L, (size_t)0, _Days);
  91. if (_N < 0)
  92. _St |= ios_base::failbit;
  93. else
  94. _Tp->tm_wday = _N >> 1;
  95. return (_F); }
  96. virtual _II do_get_monthname(_II _F, _II _L, ios_base& _X,
  97. ios_base::iostate& _St, tm *_Tp) const
  98. {int _N = _Getloctxt(_F, _L, (size_t)0, _Months);
  99. if (_N < 0)
  100. _St |= ios_base::failbit;
  101. else
  102. _Tp->tm_mon = _N >> 1;
  103. return (_F); }
  104. virtual _II do_get_year(_II _F, _II _L, ios_base& _X,
  105. ios_base::iostate& _St, tm *_Tp) const
  106. {int _Ans;
  107. _St |= _Getint(_F, _L, 0, 2035, _Ans);
  108. if (_St & ios_base::failbit)
  109. ;
  110. else if (1900 <= _Ans)
  111. _Ans -= 1900;
  112. else if (2035 - 1900 < _Ans)
  113. _St |= ios_base::failbit;
  114. if (!(_St & ios_base::failbit))
  115. _Tp->tm_year = _Ans;
  116. return (_F); }
  117. private:
  118. static ios_base::iostate __cdecl _Getint(_II& _F, _II& _L,
  119. int _Lo, int _Hi, int& _V)
  120. {char _Ac[_MAX_INT_DIG], *_Ep;
  121. char *_P = _Ac;
  122. if (_F == _L)
  123. ;
  124. else if (*_F == _WIDEN(_E, '+'))
  125. *_P++ = '+', ++_F;
  126. else if (*_F == _WIDEN(_E, '-'))
  127. *_P++ = '-', ++_F;
  128. bool _Sd = false;
  129. while (_F != _L && *_F == _WIDEN(_E, '0'))
  130. _Sd = true, ++_F;
  131. if (_Sd)
  132. *_P++ = '0';
  133. for (char *const _Pe = &_Ac[_MAX_INT_DIG - 1]; _F != _L
  134. && '0' <= (*_P = (char)_NARROW(_E, *_F)) && *_P <= '9';
  135. _Sd = true, ++_F)
  136. if (_P < _Pe)
  137. ++_P;
  138. if (!_Sd)
  139. _P = _Ac;
  140. *_P = '\0';
  141. errno = 0;
  142. const long _Ans = strtol(_Ac, &_Ep, 10);
  143. ios_base::iostate _St = ios_base::goodbit;
  144. if (_F == _L)
  145. _St |= ios_base::eofbit;
  146. if (_Ep == _Ac || errno != 0 || _Ans < _Lo || _Hi < _Ans)
  147. _St |= ios_base::failbit;
  148. else
  149. _V = _Ans;
  150. return (_St); }
  151. _E *_Days, *_Months;
  152. };
  153. template<class _E, class _II>
  154. locale::id time_get<_E, _II>::id;
  155. // TEMPLATE CLASS time_get_byname
  156. template<class _E,
  157. class _II = istreambuf_iterator<_E, char_traits<_E> > >
  158. class time_get_byname : public time_get<_E, _II> {
  159. public:
  160. explicit time_get_byname(const char *_S, size_t _R = 0)
  161. : time_get<_E, _II>(_Locinfo(_S), _R) {}
  162. _PROTECTED:
  163. virtual ~time_get_byname()
  164. {}
  165. };
  166. // TEMPLATE CLASS time_put
  167. template<class _E,
  168. class _OI = ostreambuf_iterator<_E, char_traits<_E> > >
  169. class time_put : public time_base {
  170. public:
  171. typedef _E char_type;
  172. typedef _OI iter_type;
  173. _OI put(_OI _F, ios_base& _X, const tm *_Tp,
  174. const _E *_Ff, const _E *_Lf) const
  175. {const _E _Pct = _WIDEN(_E, '%');
  176. for (; _Ff != _Lf; ++_Ff)
  177. if (*_Ff != _Pct)
  178. *_F++ = *_Ff;
  179. else if (++_Ff == _Lf)
  180. {*_F++ = _Pct;
  181. break; }
  182. else
  183. {char _C = (char)_NARROW(_E, *_Ff);
  184. char _Q = '\0';
  185. if (_C != 'E' && _C != 'O' && _C != 'Q'
  186. && _C != '#')
  187. ;
  188. else if (++_Ff == _Lf)
  189. {*_F++ = _Pct, *_F++ = _C;
  190. break; }
  191. else
  192. _Q = _C, _C = (char)_NARROW(_E, *_Ff);
  193. _F = do_put(_F, _X, _Tp, _C, _Q); }
  194. return (_F); }
  195. _OI put(_OI _F, ios_base& _X, const tm *_Tp,
  196. char _C, char _M = 0) const
  197. {return (do_put(_F, _X, _Tp, _C, _M)); }
  198. static locale::id id;
  199. explicit time_put(size_t _R = 0)
  200. : time_base(_R) {_Init(_Locinfo()); }
  201. time_put(const _Locinfo& _Lobj, size_t _R = 0)
  202. : time_base(_R) {_Init(_Lobj); }
  203. static size_t __cdecl _Getcat()
  204. {return (_LC_TIME); }
  205. _PROTECTED:
  206. virtual ~time_put()
  207. {}
  208. protected:
  209. void _Init(const _Locinfo& _Lobj)
  210. {_Tnames = _Lobj._Gettnames(); }
  211. virtual _OI do_put(_OI _F, ios_base& _X, const tm *_Tp,
  212. char _C, char _M = 0) const
  213. {char _Fmt[5] = {"!%x\0"};
  214. size_t _I, _N;
  215. string _Str;
  216. if (_M == (_E)0)
  217. _Fmt[2] = _C;
  218. else
  219. _Fmt[2] = _M, _Fmt[3] = _C;
  220. for (_N = 16; ; _N *= 2)
  221. {_Str.append(_N, '\0');
  222. if (0 < (_I = _Strftime(_Str.begin(), _Str.size(),
  223. _Fmt, _Tp, _Tnames._Getptr())))
  224. break; }
  225. for (char *_S = _Str.begin(); 0 < --_I; ++_F)
  226. *_F = _WIDEN(_E, *++_S);
  227. return (_F); }
  228. private:
  229. _Locinfo::_Timevec _Tnames;
  230. };
  231. template<class _E, class _OI>
  232. locale::id time_put<_E, _OI>::id;
  233. // TEMPLATE CLASS time_put_byname
  234. template<class _E,
  235. class _OI = ostreambuf_iterator<_E, char_traits<_E> > >
  236. class time_put_byname : public time_put<_E, _OI> {
  237. public:
  238. explicit time_put_byname(const char *_S, size_t _R = 0)
  239. : time_put<_E, _OI>(_Locinfo(_S), _R) {}
  240. _PROTECTED:
  241. virtual ~time_put_byname()
  242. {}
  243. };
  244. #ifdef _DLL
  245. #ifdef __FORCE_INSTANCE
  246. template class _CRTIMP2 time_get<char,
  247. istreambuf_iterator<char, char_traits<char> > >;
  248. template class _CRTIMP2 time_get<wchar_t,
  249. istreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
  250. template class _CRTIMP2 time_put<char,
  251. ostreambuf_iterator<char, char_traits<char> > >;
  252. template class _CRTIMP2 time_put<wchar_t,
  253. ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
  254. #else // __FORCE_INSTANCE
  255. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  256. extern template class _CRTIMP2 time_get<char,
  257. istreambuf_iterator<char, char_traits<char> > >;
  258. extern template class _CRTIMP2 time_get<wchar_t,
  259. istreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
  260. extern template class _CRTIMP2 time_put<char,
  261. ostreambuf_iterator<char, char_traits<char> > >;
  262. extern template class _CRTIMP2 time_put<wchar_t,
  263. ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
  264. #pragma warning(default:4231) /* restore previous warning */
  265. #endif // __FORCE_INSTANCE
  266. #endif // _DLL
  267. _STD_END
  268. #ifdef _MSC_VER
  269. #pragma pack(pop)
  270. #endif /* _MSC_VER */
  271. #endif /* _XLOCTIME_ */
  272. /*
  273. * Copyright (c) 1995 by P.J. Plauger. ALL RIGHTS RESERVED.
  274. * Consult your license regarding permissions and restrictions.
  275. */