home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / Rw / numeral.cc < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  16.0 KB  |  562 lines

  1. #ifndef __NUMERAL_CC
  2. #define __NUMERAL_CC
  3. #pragma option push -b -a8 -pc -Vx- -Ve- -w-inl -w-aus -w-sig
  4. /***************************************************************************
  5.  *
  6.  * numeral.cc - Definitions for the Standard Library numeric facets
  7.  *
  8.  ***************************************************************************
  9.  *
  10.  * Copyright (c) 1994-1999 Rogue Wave Software, Inc.  All Rights Reserved.
  11.  *
  12.  * This computer software is owned by Rogue Wave Software, Inc. and is
  13.  * protected by U.S. copyright laws and other laws and by international
  14.  * treaties.  This computer software is furnished by Rogue Wave Software,
  15.  * Inc. pursuant to a written license agreement and may be used, copied,
  16.  * transmitted, and stored only in accordance with the terms of such
  17.  * license and with the inclusion of the above copyright notice.  This
  18.  * computer software or any other copies thereof may not be provided or
  19.  * otherwise made available to any other person.
  20.  *
  21.  * U.S. Government Restricted Rights.  This computer software is provided
  22.  * with Restricted Rights.  Use, duplication, or disclosure by the
  23.  * Government is subject to restrictions as set forth in subparagraph (c)
  24.  * (1) (ii) of The Rights in Technical Data and Computer Software clause
  25.  * at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
  26.  * Commercial Computer Software รป Restricted Rights at 48 CFR 52.227-19,
  27.  * as applicable.  Manufacturer is Rogue Wave Software, Inc., 5500
  28.  * Flatiron Parkway, Boulder, Colorado 80301 USA.
  29.  *
  30.  **************************************************************************/
  31.  
  32. #ifndef __STD_RW_LOCNUMRW__
  33. #include <rw/numbrw>
  34. #endif
  35.  
  36. #ifndef _RWSTD_NO_NAMESPACE
  37. namespace __rwstd {
  38. #endif
  39.  
  40. // -----------------------------------------------
  41. // Template numpunct_data<charT> member functions.
  42. // -----------------------------------------------
  43.  
  44. template <class charT>
  45. void numpunct_data<charT>::__initfacetbase (const locale*) {
  46.   tf_defs_[0].s=tn_.c_str();
  47.   tf_defs_[0].v=1;
  48.   tf_defs_[1].s=fn_.c_str();
  49.   tf_defs_[1].v=0;
  50.  
  51.   tf_map_.num_defs_=2 ;
  52.   tf_map_.defs_=tf_defs_;
  53. }
  54.  
  55. template <class charT>
  56. numpunct_init<charT>*
  57. _RWSTDExportTemplate fixup_numpunct_init
  58.     (numpunct_init<char> *init,charT*)
  59. {
  60.   if (init->del_)
  61.     delete[] (char*) init;
  62.   return NULL;
  63. }
  64.  
  65. template <class charT>
  66. numpunct_init<charT>*
  67. numpunct_data<charT>::get_init_by_name_
  68.     (const char *name)
  69. {
  70.   return fixup_numpunct_init(get_named_init_(name),(charT*)0);
  71. }
  72. #ifndef _RWSTD_NO_NAMESPACE
  73. } namespace std {
  74. #endif
  75.  
  76. // ----------------------------------------------------
  77. // Facet num_get<charT,InputIterator> member templates.
  78. // ----------------------------------------------------
  79. template <class charT, class InputIterator>
  80. locale::id num_get<charT, InputIterator>::id;
  81.  
  82. template <class charT, class InputIterator>
  83. num_get<charT,InputIterator>::~num_get() { }
  84.  
  85. #ifndef _RWSTD_NO_BOOL
  86.  
  87. template <class charT, class InputIterator>
  88. InputIterator num_get<charT,InputIterator>::do_get
  89.     (InputIterator in, InputIterator end, ios_base& io,
  90.      ios_base::iostate& err, bool& value) const
  91. {
  92.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  93.   long v=reader.to_ulong(reader.get_int_digits());
  94.   err=ios_base::goodbit; // aka 0
  95.  
  96.   if (!reader.error)
  97.     if (v==0)
  98.       value=false;
  99.     else if (v==1 && !reader.negative)
  100.       value=true;
  101.     else
  102.       err=ios_base::failbit;
  103.   else if (reader.advanced)
  104.     err=ios_base::failbit;
  105.   else {
  106.     const numpunct<charT>& nump =
  107.         _RWSTD_STATIC_CAST(const numpunct<charT>&,reader.punct);
  108.     int k=reader.get_keyword(reader.get_tf_map(nump));
  109.     if (k<0)
  110.       err=ios_base::failbit;
  111.     else
  112.       value=k;
  113.   }
  114.  
  115.   if (reader.reached_end)
  116.     err|=ios_base::eofbit;
  117.  
  118.   return in;
  119. }
  120.  
  121. #endif // _RWSTD_NO_BOOL
  122.  
  123. template <class charT, class InputIterator>
  124. InputIterator num_get<charT,InputIterator>::do_get
  125.     (InputIterator in, InputIterator end, ios_base& io,
  126.      ios_base::iostate& err, void*& value) const
  127. {
  128.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  129.   void *v=reader.to_pointer(reader.get_pointer_digits());
  130.   err=ios_base::goodbit;
  131.  
  132.   if (reader.error)
  133.     err=ios_base::failbit;
  134.   else
  135.     value=v;
  136.  
  137.   if (reader.reached_end)
  138.     err|=ios_base::eofbit;
  139.  
  140.   return in;
  141. }
  142.  
  143. template <class charT, class InputIterator>
  144. InputIterator num_get<charT,InputIterator>::do_get
  145.     (InputIterator in, InputIterator end, ios_base& io,
  146.      ios_base::iostate& err, long& value) const
  147. {
  148.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  149.   long v=reader.to_ulong(reader.get_int_digits());
  150.   err=ios_base::goodbit;
  151.  
  152.   if (!reader.error)
  153.     if (reader.negative) {
  154.       if ((v=-v)>0)
  155.         reader.error=reader.overflow;
  156.     } else
  157.       if (v<0 && reader.radix==10)
  158.         reader.error=reader.overflow;
  159.  
  160.   if (reader.error)
  161.     err=ios_base::failbit;
  162.   else
  163.     value=v;
  164.  
  165.   if (reader.reached_end)
  166.     err|=ios_base::eofbit;
  167.     
  168.   return in;
  169. }
  170.  
  171. #ifdef _RWSTD_LONG_LONG
  172.  
  173. template <class charT, class InputIterator>
  174. InputIterator num_get<charT,InputIterator>::do_get
  175.     (InputIterator in, InputIterator end, ios_base& io,
  176.      ios_base::iostate& err, _RWSTD_LONG_LONG& value) const
  177. {
  178.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  179.   _RWSTD_LONG_LONG v=reader.to_ulong_ulong(reader.get_int_digits());
  180.   err=ios_base::goodbit;
  181.  
  182.   if (!reader.error)
  183.     if (reader.negative) {
  184.       if ((v=-v)>0)
  185.         reader.error=reader.overflow;
  186.     } else
  187.       if (v<0 && reader.radix==10)
  188.         reader.error=reader.overflow;
  189.  
  190.   if (reader.error)
  191.     err=ios_base::failbit;
  192.   else
  193.     value=v;
  194.  
  195.   if (reader.reached_end)
  196.     err|=ios_base::eofbit;
  197.     
  198.   return in;
  199. }
  200.  
  201. template <class charT, class InputIterator>
  202. InputIterator num_get<charT,InputIterator>::do_get
  203.     (InputIterator in, InputIterator end, ios_base& io,
  204.      ios_base::iostate& err, unsigned _RWSTD_LONG_LONG& value) const
  205. {
  206.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  207.   unsigned _RWSTD_LONG_LONG v=reader.to_ulong_ulong(reader.get_int_digits());
  208.   err=ios_base::goodbit;
  209.  
  210.   if (reader.error || (reader.negative && v!=0))
  211.     err=ios_base::failbit;
  212.   else
  213.     value=v;
  214.  
  215.   if (reader.reached_end)
  216.     err|=ios_base::eofbit;
  217.  
  218.   return in;
  219. }
  220.  
  221. #endif // _RWSTD_LONG_LONG
  222.  
  223. template <class charT, class InputIterator>
  224. InputIterator num_get<charT,InputIterator>::do_get
  225.     (InputIterator in, InputIterator end, ios_base& io,
  226.      ios_base::iostate& err, unsigned short& value) const
  227. {
  228.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  229.   unsigned long v=reader.to_ulong(reader.get_int_digits());
  230.   err=ios_base::goodbit;
  231.  
  232.   if (reader.error
  233.       || v > (unsigned long) numeric_limits<unsigned short>::max()
  234.       || (reader.negative && v!=0))
  235.     err=ios_base::failbit;
  236.   else
  237.     value=v;
  238.  
  239.   if (reader.reached_end)
  240.     err|=ios_base::eofbit;
  241.  
  242.   return in;
  243. }
  244.  
  245. template <class charT, class InputIterator>
  246. InputIterator num_get<charT,InputIterator>::do_get
  247.     (InputIterator in, InputIterator end, ios_base& io,
  248.      ios_base::iostate& err, unsigned int& value) const
  249. {
  250.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  251.   unsigned long v=reader.to_ulong(reader.get_int_digits());
  252.   err=ios_base::goodbit;
  253.  
  254.   if (reader.error
  255.       || v > (unsigned long) numeric_limits<unsigned int>::max()
  256.       || (reader.negative && v!=0))
  257.     err=ios_base::failbit;
  258.   else
  259.     value=v;
  260.  
  261.   if (reader.reached_end)
  262.     err|=ios_base::eofbit;
  263.  
  264.   return in;
  265. }
  266.  
  267. template <class charT, class InputIterator>
  268. InputIterator num_get<charT,InputIterator>::do_get
  269.     (InputIterator in, InputIterator end, ios_base& io,
  270.      ios_base::iostate& err, unsigned long& value) const
  271. {
  272.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  273.   unsigned long v=reader.to_ulong(reader.get_int_digits());
  274.   err=ios_base::goodbit;
  275.  
  276.   if (reader.error || (reader.negative && v!=0))
  277.     err=ios_base::failbit;
  278.   else
  279.     value=v;
  280.  
  281.   if (reader.reached_end)
  282.     err|=ios_base::eofbit;
  283.  
  284.   return in;
  285. }
  286.  
  287. template <class charT, class InputIterator>
  288. InputIterator num_get<charT,InputIterator>::do_get
  289.     (InputIterator in, InputIterator end, ios_base& io,
  290.      ios_base::iostate& err, float& value) const
  291. {
  292.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  293.   float v=reader.to_float(reader.get_float_digits());
  294.   err=ios_base::goodbit;
  295.  
  296.   if (reader.error)
  297.     err=ios_base::failbit;
  298.   else
  299.     value=v;
  300.  
  301.   if (reader.reached_end)
  302.     err|=ios_base::eofbit;
  303.  
  304.   return in;
  305. }
  306.  
  307. template <class charT, class InputIterator>
  308. InputIterator num_get<charT,InputIterator>::do_get
  309.     (InputIterator in, InputIterator end, ios_base& io,
  310.      ios_base::iostate& err, double& value) const
  311. {
  312.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  313.   double v=reader.to_double(reader.get_float_digits());
  314.   err=ios_base::goodbit;
  315.  
  316.   if (reader.error)
  317.     err=ios_base::failbit;
  318.   else
  319.     value=v;
  320.  
  321.   if (reader.reached_end)
  322.     err|=ios_base::eofbit;
  323.  
  324.   return in;
  325. }
  326.  
  327. template <class charT, class InputIterator>
  328. InputIterator num_get<charT,InputIterator>::do_get
  329.     (InputIterator in, InputIterator end, ios_base& io,
  330.      ios_base::iostate& err, long double& value) const
  331. {
  332.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  333.   long double v=reader.to_long_double(reader.get_float_digits());
  334.   err=ios_base::goodbit;
  335.  
  336.   if (reader.error)
  337.     err=ios_base::failbit;
  338.   else
  339.     value=v;
  340.  
  341.   if (reader.reached_end)
  342.     err|=ios_base::eofbit;
  343.  
  344.   return in;
  345. }
  346.  
  347. // -----------------------------------------------------
  348. // Facet num_put<charT,OutputIterator> member templates.
  349. // -----------------------------------------------------
  350.  
  351. template <class charT, class OutputIterator>
  352. locale::id num_put<charT, OutputIterator>::id;
  353.  
  354. template <class charT, class OutputIterator>
  355. num_put<charT,OutputIterator>::~num_put() { }
  356.  
  357. #ifndef _RWSTD_NO_BOOL
  358.  
  359. template <class charT, class OutputIterator>
  360. OutputIterator num_put<charT,OutputIterator>::do_put
  361.     (OutputIterator out, ios_base& io, charT fill, bool value) const
  362. {
  363.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  364.   if (io.flags() & ios_base::boolalpha) {
  365.     const numpunct<charT>& nump =
  366.         _RWSTD_STATIC_CAST(const numpunct<charT>&,writer.punct);
  367.     writer.put_keyword(writer.get_tf_string(nump,value),fill);
  368.   } else {
  369.     writer.digitize((unsigned long) (value? 1 : 0));
  370.     writer.put_digits(fill);
  371.   }
  372.   return out;
  373. }
  374.  
  375. #endif // _RWSTD_NO_BOOL
  376.  
  377. template <class charT, class OutputIterator>
  378. OutputIterator num_put<charT,OutputIterator>::do_put
  379.     (OutputIterator out, ios_base& io, charT fill, const void* value) const
  380. {
  381.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  382.   writer.digitize(value);
  383.   writer.put_digits(fill);
  384.   return out;
  385. }
  386.  
  387. template <class charT, class OutputIterator>
  388. OutputIterator num_put<charT,OutputIterator>::do_put
  389.     (OutputIterator out, ios_base& io, charT fill, long value) const
  390. {
  391.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  392.   writer.digitize(value);
  393.   writer.put_digits(fill);
  394.   return out;
  395. }
  396.  
  397. template <class charT, class OutputIterator>
  398. OutputIterator num_put<charT,OutputIterator>::do_put
  399.     (OutputIterator out, ios_base& io, charT fill, unsigned long value) const
  400. {
  401.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  402.   writer.digitize(value);
  403.   writer.put_digits(fill);
  404.   return out;
  405. }
  406.  
  407. #ifdef _RWSTD_LONG_LONG
  408.  
  409. template <class charT, class OutputIterator>
  410. OutputIterator num_put<charT,OutputIterator>::do_put
  411.     (OutputIterator out, ios_base& io, charT fill, 
  412.      _RWSTD_LONG_LONG val) const
  413. {
  414.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  415.   writer.digitize(val);
  416.   writer.put_digits(fill);
  417.   return out;
  418. }
  419.  
  420. template <class charT, class OutputIterator>
  421. OutputIterator num_put<charT,OutputIterator>::do_put
  422.     (OutputIterator out, ios_base& io, charT fill, 
  423.      unsigned _RWSTD_LONG_LONG val) const
  424. {
  425.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  426.   writer.digitize(val);
  427.   writer.put_digits(fill);
  428.   return out;
  429. }
  430.  
  431. #endif // _RWSTD_LONG_LONG
  432.  
  433. template <class charT, class OutputIterator>
  434. OutputIterator num_put<charT,OutputIterator>::do_put
  435.     (OutputIterator out, ios_base& io, charT fill, double value) const
  436. {
  437.   __RWSTD::digit_writer<charT,OutputIterator> digits(out,io);
  438.   digits.digitize(value);
  439.   digits.put_digits(fill);
  440.   return out;
  441. }
  442.  
  443. #ifndef _RWSTD_NO_LONG_DOUBLE
  444. template <class charT, class OutputIterator>
  445. OutputIterator num_put<charT,OutputIterator>::do_put
  446.     (OutputIterator out, ios_base& io, charT fill, long double value) const
  447. {
  448.   __RWSTD::digit_writer<charT,OutputIterator> digits(out,io);
  449.   digits.digitize(value);
  450.   digits.put_digits(fill);
  451.   return out;
  452. }
  453. #endif // _RWSTD_NO_LONG_DOUBLE
  454.  
  455. #ifndef _RWSTD_STRICT_ANSI
  456. // Rogue wave extensions
  457. template <class charT, class OutputIterator>
  458. OutputIterator num_put<charT,OutputIterator>::do_put
  459.     (OutputIterator out, ios_base& io, charT fill, short value) const
  460. {
  461.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  462.   writer.digitize(value);
  463.   writer.put_digits(fill);
  464.   return out;
  465. }
  466.  
  467. template <class charT, class OutputIterator>
  468. OutputIterator num_put<charT,OutputIterator>::do_put
  469.     (OutputIterator out, ios_base& io, charT fill, unsigned short value) const
  470. {
  471.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  472.   writer.digitize(value);
  473.   writer.put_digits(fill);
  474.   return out;
  475. }
  476.  
  477. template <class charT, class OutputIterator>
  478. OutputIterator num_put<charT,OutputIterator>::do_put
  479.     (OutputIterator out, ios_base& io, charT fill, int value) const
  480. {
  481.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  482.   writer.digitize(value);
  483.   writer.put_digits(fill);
  484.   return out;
  485. }
  486.  
  487. template <class charT, class OutputIterator>
  488. OutputIterator num_put<charT,OutputIterator>::do_put
  489.     (OutputIterator out, ios_base& io, charT fill, unsigned int value) const
  490. {
  491.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  492.   writer.digitize(value);
  493.   writer.put_digits(fill);
  494.   return out;
  495. }
  496. #endif // _RWSTD_STRICT_ANSI
  497.  
  498. // ---------------------------------------
  499. // Facet numpunct<charT> member templates.
  500. // ---------------------------------------
  501.  
  502. template <class charT>
  503. locale::id numpunct<charT>::id;
  504.  
  505. template <class charT>
  506. numpunct<charT>::~numpunct() { }
  507.  
  508. template <class charT>
  509. charT numpunct<charT>::do_decimal_point () const { return this->dp_; }
  510.  
  511. template <class charT>
  512. charT numpunct<charT>::do_thousands_sep () const { return this->ts_; }
  513.  
  514. template <class charT>
  515. string numpunct<charT>::do_grouping () const { return this->gr_; }
  516.  
  517. template <class charT>
  518. _TYPENAME numpunct<charT>::string_type
  519. numpunct<charT>::do_falsename () const { return this->fn_; }
  520.  
  521. template <class charT>
  522. _TYPENAME numpunct<charT>::string_type
  523. numpunct<charT>::do_truename () const { return this->tn_; }
  524.  
  525. template <class charT>
  526. void numpunct<charT>::__initfacet (const locale* loc) {
  527.   this->dp_=do_decimal_point();
  528.   this->ts_=do_thousands_sep();
  529.   this->gr_=do_grouping();
  530.   this->fn_=do_falsename();
  531.   this->tn_=do_truename();
  532.   this->__initfacetbase(loc);
  533. }
  534.  
  535. // ----------------------------------------------------------------------
  536. // Numeric punctuation by-name member templates: numpunct_byname<charT>
  537. // ----------------------------------------------------------------------
  538.  
  539. template <class charT>
  540. numpunct_byname<charT>::numpunct_byname (const char *n, size_t refs):
  541. #if defined (_MSC_VER) && !defined (__BORLANDC__)
  542.     numpunct<charT>(refs,get_init_by_name_(n))
  543. #else
  544. #  if !defined (_RWSTD_NO_NAMESPACE)
  545.     numpunct<charT>(refs,__RWSTD::numpunct_data<charT>::get_init_by_name_(n))
  546. #  else
  547.     numpunct<charT>(refs,numpunct_data<charT>::get_init_by_name_(n))
  548. #  endif
  549. #endif // _MSC_VER
  550. { }
  551.  
  552. template <class charT>
  553. numpunct_byname<charT>::~numpunct_byname()
  554. { }
  555.  
  556. #ifndef _RWSTD_NO_NAMESPACE
  557. }
  558. #endif
  559.  
  560. #pragma option pop
  561. #endif /* __NUMERAL_CC */
  562.