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.

739 lines
30 KiB

  1. // xlocnum internal header (from <locale>)
  2. #ifndef _XLOCNUM_
  3. #define _XLOCNUM_
  4. #include <cerrno>
  5. #include <cfloat>
  6. #include <climits>
  7. #include <cstdio>
  8. #include <cstdlib>
  9. #include <xiosbase>
  10. #ifdef _MSC_VER
  11. #pragma pack(push,8)
  12. #endif /* _MSC_VER */
  13. _STD_BEGIN
  14. // TEMPLATE CLASS numpunct
  15. template<class _E>
  16. class numpunct : public locale::facet {
  17. public:
  18. typedef basic_string<_E, char_traits<_E>, allocator<_E> >
  19. string_type;
  20. typedef _E char_type;
  21. static locale::id id;
  22. _E decimal_point() const
  23. {return (do_decimal_point()); }
  24. _E thousands_sep() const
  25. {return (do_thousands_sep()); }
  26. string grouping() const
  27. {return (do_grouping()); }
  28. string_type falsename() const
  29. {return (do_falsename()); }
  30. string_type truename() const
  31. {return (do_truename()); }
  32. explicit numpunct(size_t _R = 0)
  33. : locale::facet(_R) {_Init(_Locinfo()); }
  34. numpunct(const _Locinfo& _Lobj, size_t _R = 0)
  35. : locale::facet(_R) {_Init(_Lobj); }
  36. static size_t __cdecl _Getcat()
  37. {return (_LC_NUMERIC); }
  38. _PROTECTED:
  39. virtual ~numpunct()
  40. {delete[] _Gr;
  41. delete[] _Nf;
  42. delete[] _Nt; }
  43. protected:
  44. void _Init(const _Locinfo& _Lobj)
  45. {const lconv *_P = _Lobj._Getlconv();
  46. _Dp = _WIDEN(_E, _P->decimal_point[0]);
  47. _Ks = _WIDEN(_E, _P->thousands_sep[0]);
  48. _Gr = _MAKLOCSTR(char, _P->grouping);
  49. _Nf = _MAKLOCSTR(_E, _Lobj._Getfalse());
  50. _Nt = _MAKLOCSTR(_E, _Lobj._Gettrue()); }
  51. virtual _E do_decimal_point() const
  52. {return (_Dp); }
  53. virtual _E do_thousands_sep() const
  54. {return (_Ks); }
  55. virtual string do_grouping() const
  56. {return (string(_Gr)); }
  57. virtual string_type do_falsename() const
  58. {return (string_type(_Nf)); }
  59. virtual string_type do_truename() const
  60. {return (string_type(_Nt)); }
  61. private:
  62. char *_Gr;
  63. _E _Dp, _Ks, *_Nf, *_Nt;
  64. };
  65. typedef numpunct<char> _Npc;
  66. typedef numpunct<wchar_t> _Npwc;
  67. // TEMPLATE CLASS numpunct_byname
  68. template<class _E>
  69. class numpunct_byname : public numpunct<_E> {
  70. public:
  71. explicit numpunct_byname(const char *_S, size_t _R = 0)
  72. : numpunct<_E>(_Locinfo(_S), _R) {}
  73. _PROTECTED:
  74. virtual ~numpunct_byname()
  75. {}
  76. };
  77. template<class _E>
  78. locale::id numpunct<_E>::id;
  79. #define _VIRTUAL virtual
  80. template<class _E> inline
  81. bool (isdigit)(_E _C, const locale& _L)
  82. {return (_USE(_L, ctype<_E>).is(ctype_base::digit, _C)); }
  83. // TEMPLATE CLASS num_get
  84. template<class _E,
  85. class _II = istreambuf_iterator<_E, char_traits<_E> > >
  86. class num_get : public locale::facet {
  87. public:
  88. typedef numpunct<_E> _Mypunct;
  89. typedef basic_string<_E, char_traits<_E>, allocator<_E> >
  90. _Mystr;
  91. static size_t __cdecl _Getcat()
  92. {return (_LC_NUMERIC); }
  93. static locale::id id;
  94. _PROTECTED:
  95. virtual ~num_get()
  96. {}
  97. protected:
  98. void _Init(const _Locinfo& _Lobj)
  99. {}
  100. public:
  101. explicit num_get(size_t _R = 0)
  102. : locale::facet(_R) {_Init(_Locinfo()); }
  103. num_get(const _Locinfo& _Lobj, size_t _R = 0)
  104. : locale::facet(_R) {_Init(_Lobj); }
  105. typedef _E char_type;
  106. typedef _II iter_type;
  107. _II get(_II _F, _II _L, ios_base& _X, ios_base::iostate& _St,
  108. _Bool& _V) const
  109. {return (do_get(_F, _L, _X, _St, _V)); }
  110. _II get(_II _F, _II _L, ios_base& _X, ios_base::iostate& _St,
  111. unsigned short& _V) const
  112. {return (do_get(_F, _L, _X, _St, _V)); }
  113. _II get(_II _F, _II _L, ios_base& _X, ios_base::iostate& _St,
  114. unsigned int& _V) const
  115. {return (do_get(_F, _L, _X, _St, _V)); }
  116. _II get(_II _F, _II _L, ios_base& _X, ios_base::iostate& _St,
  117. long& _V) const
  118. {return (do_get(_F, _L, _X, _St, _V)); }
  119. _II get(_II _F, _II _L, ios_base& _X, ios_base::iostate& _St,
  120. unsigned long& _V) const
  121. {return (do_get(_F, _L, _X, _St, _V)); }
  122. #ifdef _WIN64
  123. _II get(_II _F, _II _L, ios_base& _X, ios_base::iostate& _St,
  124. __int64& _V) const
  125. {return (do_get(_F, _L, _X, _St, _V)); }
  126. _II get(_II _F, _II _L, ios_base& _X, ios_base::iostate& _St,
  127. unsigned __int64& _V) const
  128. {return (do_get(_F, _L, _X, _St, _V)); }
  129. #endif // _WIN64
  130. _II get(_II _F, _II _L, ios_base& _X, ios_base::iostate& _St,
  131. float& _V) const
  132. {return (do_get(_F, _L, _X, _St, _V)); }
  133. _II get(_II _F, _II _L, ios_base& _X, ios_base::iostate& _St,
  134. double& _V) const
  135. {return (do_get(_F, _L, _X, _St, _V)); }
  136. _II get(_II _F, _II _L, ios_base& _X, ios_base::iostate& _St,
  137. long double& _V) const
  138. {return (do_get(_F, _L, _X, _St, _V)); }
  139. _II get(_II _F, _II _L, ios_base& _X, ios_base::iostate& _St,
  140. void *& _V) const
  141. {return (do_get(_F, _L, _X, _St, _V)); }
  142. protected:
  143. _VIRTUAL _II do_get(_II _F, _II _L, ios_base& _X,
  144. ios_base::iostate& _St, _Bool& _V) const
  145. {int _Ans = -1;
  146. if (_X.flags() & ios_base::boolalpha)
  147. {const _Mypunct& _Fac = _USE(_X.getloc(), _Mypunct);
  148. _Mystr _Str(1, (_E)0);
  149. _Str += _Fac.falsename();
  150. _Str += (_E)0;
  151. _Str += _Fac.truename();
  152. _Ans = _Getloctxt(_F, _L, (size_t)2, _Str.c_str()); }
  153. else
  154. {char _Ac[_MAX_INT_DIG], *_Ep;
  155. errno = 0;
  156. const unsigned long _Ulo = strtoul(_Ac, &_Ep,
  157. _Getifld(_Ac, _F, _L, _X.flags(), _X.getloc()));
  158. if (_Ep != _Ac && errno == 0 && _Ulo <= 1)
  159. _Ans = _Ulo; }
  160. if (_F == _L)
  161. _St |= ios_base::eofbit;
  162. if (_Ans < 0)
  163. _St |= ios_base::failbit;
  164. else
  165. _V = _Ans != 0;
  166. return (_F); }
  167. _VIRTUAL _II do_get(_II _F, _II _L, ios_base& _X,
  168. ios_base::iostate& _St, unsigned short& _V) const
  169. {char _Ac[_MAX_INT_DIG], *_Ep;
  170. errno = 0;
  171. int _Base =
  172. _Getifld(_Ac, _F, _L, _X.flags(), _X.getloc());
  173. char *_S = _Ac[0] == '-' ? _Ac + 1 : _Ac;
  174. const unsigned long _Ans = strtoul(_S, &_Ep, _Base);
  175. if (_F == _L)
  176. _St |= ios_base::eofbit;
  177. if (_Ep == _S || errno != 0 || USHRT_MAX < _Ans)
  178. _St |= ios_base::failbit;
  179. else
  180. _V = (unsigned short)(_Ac[0] == '-' ? -(long)_Ans : _Ans);
  181. return (_F); }
  182. _VIRTUAL _II do_get(_II _F, _II _L, ios_base& _X,
  183. ios_base::iostate& _St, unsigned int& _V) const
  184. {char _Ac[_MAX_INT_DIG], *_Ep;
  185. errno = 0;
  186. int _Base =
  187. _Getifld(_Ac, _F, _L, _X.flags(), _X.getloc());
  188. char *_S = _Ac[0] == '-' ? _Ac + 1 : _Ac;
  189. const unsigned long _Ans = strtoul(_S, &_Ep, _Base);
  190. if (_F == _L)
  191. _St |= ios_base::eofbit;
  192. if (_Ep == _S || errno != 0 || UINT_MAX < _Ans)
  193. _St |= ios_base::failbit;
  194. else
  195. _V = _Ac[0] == '-' ? -(long)_Ans : _Ans;
  196. return (_F); }
  197. _VIRTUAL _II do_get(_II _F, _II _L, ios_base& _X,
  198. ios_base::iostate& _St, long& _V) const
  199. {char _Ac[_MAX_INT_DIG], *_Ep;
  200. errno = 0;
  201. const long _Ans = strtol(_Ac, &_Ep,
  202. _Getifld(_Ac, _F, _L, _X.flags(), _X.getloc()));
  203. if (_F == _L)
  204. _St |= ios_base::eofbit;
  205. if (_Ep == _Ac || errno != 0)
  206. _St |= ios_base::failbit;
  207. else
  208. _V = _Ans;
  209. return (_F); }
  210. _VIRTUAL _II do_get(_II _F, _II _L, ios_base& _X,
  211. ios_base::iostate& _St, unsigned long& _V) const
  212. {char _Ac[_MAX_INT_DIG], *_Ep;
  213. errno = 0;
  214. const unsigned long _Ans = strtoul(_Ac, &_Ep,
  215. _Getifld(_Ac, _F, _L, _X.flags(), _X.getloc()));
  216. if (_F == _L)
  217. _St |= ios_base::eofbit;
  218. if (_Ep == _Ac || errno != 0)
  219. _St |= ios_base::failbit;
  220. else
  221. _V = _Ans;
  222. return (_F); }
  223. #ifdef _WIN64
  224. _VIRTUAL _II do_get(_II _F, _II _L, ios_base& _X,
  225. ios_base::iostate& _St, __int64& _V) const
  226. {char _Ac[_MAX_INT_DIG];
  227. errno = 0;
  228. _Getifld(_Ac, _F, _L, _X.flags(), _X.getloc());
  229. const __int64 _Ans = _atoi64(_Ac);
  230. if (_F == _L)
  231. _St |= ios_base::eofbit;
  232. if (errno != 0)
  233. _St |= ios_base::failbit;
  234. else
  235. _V = _Ans;
  236. return (_F); }
  237. _VIRTUAL _II do_get(_II _F, _II _L, ios_base& _X,
  238. ios_base::iostate& _St, unsigned __int64& _V) const
  239. {char _Ac[_MAX_INT_DIG];
  240. errno = 0;
  241. _Getifld(_Ac, _F, _L, _X.flags(), _X.getloc());
  242. const unsigned __int64 _Ans = _atoi64(_Ac);
  243. if (_F == _L)
  244. _St |= ios_base::eofbit;
  245. if (errno != 0)
  246. _St |= ios_base::failbit;
  247. else
  248. _V = _Ans;
  249. return (_F); }
  250. #endif // _WIN64
  251. _VIRTUAL _II do_get(_II _F, _II _L, ios_base& _X,
  252. ios_base::iostate& _St, float& _V) const
  253. {char _Ac[_MAX_EXP_DIG + _MAX_SIG_DIG + 32], *_Ep;
  254. errno = 0;
  255. const float _Ans = _Stof(_Ac, &_Ep,
  256. _Getffld(_Ac, _F, _L, _X.getloc()));
  257. if (_F == _L)
  258. _St |= ios_base::eofbit;
  259. if (_Ep == _Ac || errno != 0)
  260. _St |= ios_base::failbit;
  261. else
  262. _V = _Ans;
  263. return (_F); }
  264. _VIRTUAL _II do_get(_II _F, _II _L, ios_base& _X,
  265. ios_base::iostate& _St, double& _V) const
  266. {char _Ac[_MAX_EXP_DIG + _MAX_SIG_DIG + 32], *_Ep;
  267. errno = 0;
  268. const double _Ans = _Stod(_Ac, &_Ep,
  269. _Getffld(_Ac, _F, _L, _X.getloc()));
  270. if (_F == _L)
  271. _St |= ios_base::eofbit;
  272. if (_Ep == _Ac || errno != 0)
  273. _St |= ios_base::failbit;
  274. else
  275. _V = _Ans;
  276. return (_F); }
  277. _VIRTUAL _II do_get(_II _F, _II _L, ios_base& _X,
  278. ios_base::iostate& _St, long double& _V) const
  279. {char _Ac[_MAX_EXP_DIG + _MAX_SIG_DIG + 32], *_Ep;
  280. errno = 0;
  281. const long double _Ans = _Stold(_Ac, &_Ep,
  282. _Getffld(_Ac, _F, _L, _X.getloc()));
  283. if (_F == _L)
  284. _St |= ios_base::eofbit;
  285. if (_Ep == _Ac || errno != 0)
  286. _St |= ios_base::failbit;
  287. else
  288. _V = _Ans;
  289. return (_F); }
  290. _VIRTUAL _II do_get(_II _F, _II _L, ios_base& _X,
  291. ios_base::iostate& _St, void *& _V) const
  292. {union _Pvlo {
  293. void *_Pv;
  294. unsigned long _Lo[1 +
  295. (sizeof (void *) - 1) / sizeof (unsigned long)];
  296. } _U;
  297. const int _NL = sizeof (_U._Lo) / sizeof (unsigned long);
  298. for (int _I = 0; ; ++_F)
  299. {char _Ac[_MAX_INT_DIG], *_Ep;
  300. errno = 0;
  301. _U._Lo[_I] = strtoul(_Ac, &_Ep,
  302. _Getifld(_Ac, _F, _L,
  303. ios_base::hex, _X.getloc()));
  304. if (_F == _L)
  305. _St |= ios_base::eofbit;
  306. if (_Ep == _Ac || errno != 0)
  307. {_St |= ios_base::failbit;
  308. break; }
  309. if (_NL <= ++_I)
  310. break;
  311. if (_F == _L || *_F != _WIDEN(_E, ':'))
  312. {_St |= ios_base::failbit;
  313. break; }}
  314. if (!(_St & ios_base::failbit))
  315. _V = _U._Pv;
  316. return (_F); }
  317. private:
  318. static int __cdecl _Getifld(char *_Ac, _II& _F, _II& _L,
  319. ios_base::fmtflags _Bfl, const locale& _Loc)
  320. {const _E _E0 = _WIDEN(_E, '0');
  321. const _Mypunct& _Fac = _USE(_Loc, _Mypunct);
  322. const string _Gr = _Fac.grouping();
  323. const _E _Ks = _Fac.thousands_sep();
  324. char *_P = _Ac;
  325. if (_F == _L)
  326. ;
  327. else if (*_F == _WIDEN(_E, '+'))
  328. *_P++ = '+', ++_F;
  329. else if (*_F == _WIDEN(_E, '-'))
  330. *_P++ = '-', ++_F;
  331. _Bfl &= ios_base::basefield;
  332. int _Base = _Bfl == ios_base::oct ? 8
  333. : _Bfl == ios_base::hex ? 16
  334. : _Bfl == ios_base::_Fmtzero ? 0 : 10;
  335. bool _Sd = false, _Snz = false;
  336. if (_F != _L && *_F == _E0)
  337. {_Sd = true, ++_F;
  338. if (_F != _L && (*_F == _WIDEN(_E, 'x')
  339. || *_F == _WIDEN(_E, 'X'))
  340. && (_Base == 0 || _Base == 16))
  341. _Base = 16, _Sd = false, ++_F;
  342. else if (_Base == 0)
  343. _Base = 8; }
  344. int _Dlen = _Base == 0 || _Base == 10 ? 10
  345. : _Base == 8 ? 8 : 16 + 6;
  346. string _Grin(1, _Sd ? '\1' : '\0');
  347. size_t _I = 0;
  348. for (char *const _Pe = &_Ac[_MAX_INT_DIG - 1];
  349. _F != _L; ++_F)
  350. if (memchr("0123456789abcdefABCDEF",
  351. *_P = (char)_NARROW(_E, *_F), _Dlen) != 0)
  352. {if ((_Snz || *_P != '0') && _P < _Pe)
  353. ++_P, _Snz = true;
  354. _Sd = true;
  355. if (_Grin[_I] != CHAR_MAX)
  356. ++_Grin[_I]; }
  357. else if (_Grin[_I] == '\0' || _Ks == (_E)0
  358. || *_F != _Ks)
  359. break;
  360. else
  361. _Grin.append(1, '\0'), ++_I;
  362. if (_I == 0)
  363. ;
  364. else if ('\0' < _Grin[_I])
  365. ++_I;
  366. else
  367. _Sd = false;
  368. for (const char *_Pg = _Gr.c_str(); _Sd && 0 < _I; --_I)
  369. if (*_Pg == CHAR_MAX)
  370. break;
  371. else if (0 < --_I && *_Pg != _Grin[_I]
  372. || 0 == _I && *_Pg < _Grin[_I])
  373. _Sd = false;
  374. else if ('\0' < _Pg[1])
  375. ++_Pg;
  376. if (_Sd && !_Snz)
  377. *_P++ = '0';
  378. else if (!_Sd)
  379. _P = _Ac;
  380. *_P = '\0';
  381. return (_Base);
  382. }
  383. static int __cdecl _Getffld(char *_Ac, _II& _F, _II &_L,
  384. const locale& _Loc)
  385. {const _E _E0 = _WIDEN(_E, '0');
  386. const _Mypunct& _Fac = _USE(_Loc, _Mypunct);
  387. char *_P = _Ac;
  388. if (_F == _L)
  389. ;
  390. else if (*_F == _WIDEN(_E, '+'))
  391. *_P++ = '+', ++_F;
  392. else if (*_F == _WIDEN(_E, '-'))
  393. *_P++ = '-', ++_F;
  394. bool _Sd = false;
  395. for (; _F != _L && *_F == _E0; _Sd = true, ++_F)
  396. ;
  397. if (_Sd)
  398. *_P++ = '0';
  399. int _Ns = 0;
  400. int _Pten = 0;
  401. for (; _F != _L && isdigit(*_P = (char)_NARROW(_E, *_F), _Loc);
  402. _Sd = true, ++_F)
  403. if (_Ns < _MAX_SIG_DIG)
  404. ++_P, ++_Ns;
  405. else
  406. ++_Pten;
  407. if (_F != _L && *_F == _Fac.decimal_point())
  408. *_P++ = localeconv()->decimal_point[0], ++_F;
  409. if (_Ns == 0)
  410. {for (; _F != _L && *_F == _E0; _Sd = true, ++_F)
  411. --_Pten;
  412. if (_Pten < 0)
  413. *_P++ = '0', ++_Pten; }
  414. for (; _F != _L && isdigit(*_P = (char)_NARROW(_E, *_F), _Loc);
  415. _Sd = true, ++_F)
  416. if (_Ns < _MAX_SIG_DIG)
  417. ++_P, ++_Ns;
  418. if (_Sd && _F != _L
  419. && (*_F == _WIDEN(_E, 'e') || *_F == _WIDEN(_E, 'E')))
  420. {*_P++ = 'e', ++_F;
  421. _Sd = false, _Ns = 0;
  422. if (_F == _L)
  423. ;
  424. else if (*_F == _WIDEN(_E, '+'))
  425. *_P++ = '+', ++_F;
  426. else if (*_F == _WIDEN(_E, '-'))
  427. *_P++ = '-', ++_F;
  428. for (; _F != _L && *_F == _E0; _Sd = true, ++_F)
  429. ;
  430. if (_Sd)
  431. *_P++ = '0';
  432. for (; _F != _L && isdigit(*_P = (char)_NARROW(_E, *_F), _Loc);
  433. _Sd = true, ++_F)
  434. if (_Ns < _MAX_EXP_DIG)
  435. ++_P, ++_Ns; }
  436. if (!_Sd)
  437. _P = _Ac;
  438. *_P = '\0';
  439. return (_Pten);
  440. };
  441. };
  442. template<class _E, class _II>
  443. locale::id num_get<_E, _II>::id;
  444. // TEMPLATE CLASS num_put
  445. template<class _E,
  446. class _OI = ostreambuf_iterator<_E, char_traits<_E> > >
  447. class num_put : public locale::facet {
  448. public:
  449. typedef numpunct<_E> _Mypunct;
  450. typedef basic_string<_E, char_traits<_E>, allocator<_E> >
  451. _Mystr;
  452. static size_t __cdecl _Getcat()
  453. {return (_LC_NUMERIC); }
  454. static locale::id id;
  455. _PROTECTED:
  456. virtual ~num_put()
  457. {}
  458. protected:
  459. void _Init(const _Locinfo& _Lobj)
  460. {}
  461. public:
  462. explicit num_put(size_t _R = 0)
  463. : locale::facet(_R) {_Init(_Locinfo()); }
  464. num_put(const _Locinfo& _Lobj, size_t _R = 0)
  465. : locale::facet(_R) {_Init(_Lobj); }
  466. typedef _E char_type;
  467. typedef _OI iter_type;
  468. _OI put(_OI _F, ios_base& _X, _E _Fill,
  469. _Bool _V) const
  470. {return (do_put(_F, _X, _Fill, _V)); }
  471. _OI put(_OI _F, ios_base& _X, _E _Fill,
  472. long _V) const
  473. {return (do_put(_F, _X, _Fill, _V)); }
  474. _OI put(_OI _F, ios_base& _X, _E _Fill,
  475. unsigned long _V) const
  476. {return (do_put(_F, _X, _Fill, _V)); }
  477. #ifdef _WIN64
  478. _OI put(_OI _F, ios_base& _X, _E _Fill,
  479. __int64 _V) const
  480. {return (do_put(_F, _X, _Fill, _V)); }
  481. _OI put(_OI _F, ios_base& _X, _E _Fill,
  482. unsigned __int64 _V) const
  483. {return (do_put(_F, _X, _Fill, _V)); }
  484. #endif // _WIN64
  485. _OI put(_OI _F, ios_base& _X, _E _Fill,
  486. double _V) const
  487. {return (do_put(_F, _X, _Fill, _V)); }
  488. _OI put(_OI _F, ios_base& _X, _E _Fill,
  489. long double _V) const
  490. {return (do_put(_F, _X, _Fill, _V)); }
  491. _OI put(_OI _F, ios_base& _X, _E _Fill,
  492. const void *_V) const
  493. {return (do_put(_F, _X, _Fill, _V)); }
  494. protected:
  495. virtual _OI do_put(_OI _F, ios_base& _X, _E _Fill,
  496. _Bool _V) const
  497. {const _Mypunct& _Fac = _USE(_X.getloc(), _Mypunct);
  498. _Mystr _Str;
  499. if (!(_X.flags() & ios_base::boolalpha))
  500. _Str.append(1, _WIDEN(_E, _V ? '1' : '0'));
  501. else if (_V)
  502. _Str = _Fac.truename();
  503. else
  504. _Str = _Fac.falsename();
  505. size_t _M = _X.width() <= 0 || (size_t)(_X.width()) <= _Str.size()
  506. ? 0 : _X.width() - _Str.size();
  507. ios_base::fmtflags _Afl =
  508. _X.flags() & ios_base::adjustfield;
  509. if (_Afl != ios_base::left)
  510. _F = _Rep(_F, _Fill, _M), _M = 0;
  511. _F = _Put(_F, _Str.c_str(), _Str.size());
  512. _X.width(0);
  513. return (_Rep(_F, _Fill, _M)); }
  514. virtual _OI do_put(_OI _F, ios_base& _X, _E _Fill,
  515. long _V) const
  516. {char _Buf[2 * _MAX_INT_DIG], _Fmt[6];
  517. return (_Iput(_F, _X, _Fill, _Buf,
  518. sprintf(_Buf, _Ifmt(_Fmt, 'd', _X.flags()), _V))); }
  519. virtual _OI do_put(_OI _F, ios_base& _X, _E _Fill,
  520. unsigned long _V) const
  521. {char _Buf[2 * _MAX_INT_DIG], _Fmt[6];
  522. return (_Iput(_F, _X, _Fill, _Buf,
  523. sprintf(_Buf, _Ifmt(_Fmt, 'u', _X.flags()), _V))); }
  524. #ifdef _WIN64
  525. virtual _OI do_put(_OI _F, ios_base& _X, _E _Fill,
  526. __int64 _V) const
  527. {char _Buf[2 * _MAX_INT_DIG], _Fmt[6];
  528. return (_Iput(_F, _X, _Fill, _Buf,
  529. sprintf(_Buf, _Ifmt(_Fmt, "Id", _X.flags()), _V))); }
  530. virtual _OI do_put(_OI _F, ios_base& _X, _E _Fill,
  531. unsigned __int64 _V) const
  532. {char _Buf[2 * _MAX_INT_DIG], _Fmt[6];
  533. return (_Iput(_F, _X, _Fill, _Buf,
  534. sprintf(_Buf, _Ifmt(_Fmt, "Iu", _X.flags()), _V))); }
  535. #endif // _WIN64
  536. virtual _OI do_put(_OI _F, ios_base& _X, _E _Fill,
  537. double _V) const
  538. {char _Buf[_MAX_EXP_DIG + _MAX_SIG_DIG + 32 + DBL_MAX_10_EXP], _Fmt[8];
  539. streamsize _Prec = _X.precision() <= 0
  540. && !(_X.flags() & ios_base::fixed) ? 6
  541. : _X.precision();
  542. int _Mpr = (int)(_MAX_SIG_DIG < _Prec ? _MAX_SIG_DIG : _Prec);
  543. return (_Fput(_F, _X, _Fill, _Buf, _Prec - _Mpr,
  544. sprintf(_Buf, _Ffmt(_Fmt, 0, _X.flags()),
  545. _Mpr, _V))); }
  546. virtual _OI do_put(_OI _F, ios_base& _X, _E _Fill,
  547. long double _V) const
  548. {char _Buf[_MAX_EXP_DIG + _MAX_SIG_DIG + 32 + DBL_MAX_10_EXP], _Fmt[8];
  549. streamsize _Prec = _X.precision() <= 0
  550. && !(_X.flags() & ios_base::fixed) ? 6
  551. : _X.precision();
  552. int _Mpr = (int)(_MAX_SIG_DIG < _Prec ? _MAX_SIG_DIG : _Prec);
  553. return (_Fput(_F, _X, _Fill, _Buf, _Prec - _Mpr,
  554. sprintf(_Buf, _Ffmt(_Fmt, 'L', _X.flags()),
  555. _Mpr, _V))); }
  556. virtual _OI do_put(_OI _F, ios_base& _X, _E _Fill,
  557. const void *_V) const
  558. {
  559. const int _NL = 1
  560. + (sizeof (void *) - 1) / sizeof (unsigned long);
  561. char _Buf[(_NL + 1) * (_MAX_INT_DIG + 1)];
  562. int _N = sprintf(_Buf, "%p", _V);
  563. size_t _M = _X.width() <= 0 || _X.width() <= _N
  564. ? 0 : _X.width() - _N;
  565. ios_base::fmtflags _Afl =
  566. _X.flags() & ios_base::adjustfield;
  567. if (_Afl != ios_base::left)
  568. _F = _Rep(_F, _Fill, _M), _M = 0;
  569. _F = _Putc(_F, _Buf, _N);
  570. _X.width(0);
  571. return (_Rep(_F, _Fill, _M)); }
  572. static char *_Ffmt(char *_Fmt, char _Spec,
  573. ios_base::fmtflags _Fl)
  574. {char *_S = _Fmt;
  575. *_S++ = '%';
  576. if (_Fl & ios_base::showpos)
  577. *_S++ = '+';
  578. if (_Fl & ios_base::showpoint)
  579. *_S++ = '#';
  580. *_S++ = '.';
  581. *_S++ = '*';
  582. if (_Spec != 0)
  583. *_S++ = _Spec; // 'L' for long double only
  584. ios_base::fmtflags _Ffl = _Fl & ios_base::floatfield;
  585. *_S++ = _Ffl == ios_base::fixed ? 'f'
  586. : _Ffl == ios_base::scientific ? 'e' : 'g';
  587. *_S = '\0';
  588. return (_Fmt); }
  589. static _OI __cdecl _Fput(_OI _F, ios_base& _X, _E _Fill,
  590. const char *_S, size_t _Nz, size_t _N)
  591. {size_t _M = _X.width() <= 0 || ((size_t)_X.width()) <= _N + _Nz
  592. ? 0 : _X.width() - _N - _Nz;
  593. ios_base::fmtflags _Afl =
  594. _X.flags() & ios_base::adjustfield;
  595. if (_Afl != ios_base::left && _Afl != ios_base::internal)
  596. _F = _Rep(_F, _Fill, _M), _M = 0;
  597. else if (_Afl == ios_base::internal)
  598. {if (0 < _N && (*_S == '+' || *_S == '-'))
  599. _F = _Putc(_F, _S, 1), ++_S, --_N;
  600. _F = _Rep(_F, _Fill, _M), _M = 0; }
  601. const char *_P = (const char *)memchr(_S,
  602. localeconv()->decimal_point[0], _N);
  603. if (_P != 0)
  604. {const _Mypunct& _Fac = _USE(_X.getloc(), _Mypunct);
  605. size_t _Nf = (size_t) (_P - _S + 1);
  606. _F = _Putc(_F, _S, _Nf - 1);
  607. _F = _Rep(_F, _Fac.decimal_point(), 1);
  608. _S += _Nf, _N -= _Nf; }
  609. if ((_P = (const char *)memchr(_S, 'e', _N)) != 0)
  610. {size_t _Nm = (size_t) (_P - _S + 1);
  611. _F = _Putc(_F, _S, _Nm - 1);
  612. _F = _Rep(_F, _WIDEN(_E, '0'), _Nz), _Nz = 0;
  613. _F = _Putc(_F, _X.flags() & ios_base::uppercase
  614. ? "E" : "e", 1);
  615. _S += _Nm, _N -= _Nm; }
  616. _F = _Putc(_F, _S, _N);
  617. _F = _Rep(_F, _WIDEN(_E, '0'), _Nz);
  618. _X.width(0);
  619. return (_Rep(_F, _Fill, _M)); }
  620. static char *__cdecl _Ifmt(char *_Fmt, char _Spec,
  621. ios_base::fmtflags _Fl)
  622. {char *_S = _Fmt;
  623. *_S++ = '%';
  624. if (_Fl & ios_base::showpos)
  625. *_S++ = '+';
  626. if (_Fl & ios_base::showbase)
  627. *_S++ = '#';
  628. *_S++ = 'l';
  629. ios_base::fmtflags _Bfl = _Fl & ios_base::basefield;
  630. *_S++ = _Bfl == ios_base::oct ? 'o'
  631. : _Bfl != ios_base::hex ? _Spec // 'd' or 'u'
  632. : _Fl & ios_base::uppercase ? 'X' : 'x';
  633. *_S = '\0';
  634. return (_Fmt); }
  635. #ifdef _WIN64
  636. static char *__cdecl _Ifmt(char *_Fmt, const char *_Spec,
  637. ios_base::fmtflags _Fl)
  638. {char *_S = _Fmt;
  639. *_S++ = '%';
  640. if (_Fl & ios_base::showpos)
  641. *_S++ = '+';
  642. if (_Fl & ios_base::showbase)
  643. *_S++ = '#';
  644. *_S++ = _Spec[0];
  645. ios_base::fmtflags _Bfl = _Fl & ios_base::basefield;
  646. *_S++ = _Bfl == ios_base::oct ? 'o'
  647. : _Bfl != ios_base::hex ? _Spec[1] // 'd' or 'u'
  648. : _Fl & ios_base::uppercase ? 'X' : 'x';
  649. *_S = '\0';
  650. return (_Fmt); }
  651. #endif // _WIN64
  652. static _OI __cdecl _Iput(_OI _F, ios_base& _X, _E _Fill,
  653. char *_S, size_t _N)
  654. {const size_t _Np = *_S == '+' || *_S == '-' ? 1
  655. : *_S == '0' && (_S[1] == 'x' || _S[1] == 'X') ? 2
  656. : 0;
  657. const _Mypunct& _Fac = _USE(_X.getloc(), _Mypunct);
  658. const string _Gr = _Fac.grouping();
  659. const _E _Ks = _Fac.thousands_sep();
  660. bool _Grp = '\0' < *_Gr.c_str();
  661. if (_Grp)
  662. {const char *_Pg = _Gr.c_str();
  663. size_t _I = _N;
  664. for (_Grp = false; *_Pg != CHAR_MAX && '\0' < *_Pg
  665. && *_Pg < (char)(_I - _Np); _Grp = true)
  666. {_I -= *_Pg;
  667. memmove(&_S[_I + 1], &_S[_I], _N + 1 - _I);
  668. _S[_I] = ',', ++_N;
  669. if ('\0' < _Pg[1])
  670. ++_Pg; }}
  671. size_t _M = _X.width() <= 0 || (size_t)(_X.width()) <= _N
  672. ? 0 : _X.width() - _N;
  673. ios_base::fmtflags _Afl =
  674. _X.flags() & ios_base::adjustfield;
  675. if (_Afl != ios_base::left && _Afl != ios_base::internal)
  676. _F = _Rep(_F, _Fill, _M), _M = 0;
  677. else if (_Afl == ios_base::internal)
  678. {_F = _Putc(_F, _S, _Np), _S += _Np, _N -= _Np;
  679. _F = _Rep(_F, _Fill, _M), _M = 0; }
  680. if (!_Grp)
  681. _F = _Putc(_F, _S, _N);
  682. else
  683. for (; ; ++_S, --_N)
  684. {size_t _Nd = strcspn(_S, ",");
  685. _F = _Putc(_F, _S, _Nd);
  686. _S += _Nd, _N -= _Nd;
  687. if (_N == 0)
  688. break;
  689. if (_Ks != (_E)0)
  690. _F = _Rep(_F, _Ks, 1); }
  691. _X.width(0);
  692. return (_Rep(_F, _Fill, _M)); }
  693. static _OI _Put(_OI _F, const _E *_S, size_t _N)
  694. {for (; 0 < _N; --_N, ++_F, ++_S)
  695. *_F = *_S;
  696. return (_F); }
  697. static _OI _Putc(_OI _F, const char *_S, size_t _N)
  698. {for (; 0 < _N; --_N, ++_F, ++_S)
  699. *_F = _WIDEN(_E, *_S);
  700. return (_F); }
  701. static _OI _Rep(_OI _F, _E _C, size_t _N)
  702. {for (; 0 < _N; --_N, ++_F)
  703. *_F = _C;
  704. return (_F); }
  705. };
  706. template<class _E, class _OI>
  707. locale::id num_put<_E, _OI>::id;
  708. #ifdef _DLL
  709. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  710. extern template class _CRTIMP numpunct<char>;
  711. extern template class _CRTIMP numpunct<wchar_t>;
  712. extern template class _CRTIMP num_get<char,
  713. istreambuf_iterator<char, char_traits<char> > >;
  714. extern template class _CRTIMP num_get<wchar_t,
  715. istreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
  716. extern template class _CRTIMP num_put<char,
  717. ostreambuf_iterator<char, char_traits<char> > >;
  718. extern template class _CRTIMP num_put<wchar_t,
  719. ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
  720. #pragma warning(default:4231) /* restore previous warning */
  721. #endif // _DLL
  722. _STD_END
  723. #ifdef _MSC_VER
  724. #pragma pack(pop)
  725. #endif /* _MSC_VER */
  726. #endif /* _XLOCNUM_ */
  727. /*
  728. * Copyright (c) 1995 by P.J. Plauger. ALL RIGHTS RESERVED.
  729. * Consult your license regarding permissions and restrictions.
  730. */