home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / src / num_get.cpp < prev    next >
C/C++ Source or Header  |  2001-03-28  |  5KB  |  174 lines

  1. /*
  2.  * Copyright (c) 1999
  3.  * Silicon Graphics Computer Systems, Inc.
  4.  *
  5.  * Copyright (c) 1999 
  6.  * Boris Fomitchev
  7.  *
  8.  * This material is provided "as is", with absolutely no warranty expressed
  9.  * or implied. Any use is at your own risk.
  10.  *
  11.  * Permission to use or copy this software for any purpose is hereby granted 
  12.  * without fee, provided the above notices are retained on all copies.
  13.  * Permission to modify the code and to distribute modified code is granted,
  14.  * provided the above notices are retained, and a notice that the code was
  15.  * modified is included with the above copyright notice.
  16.  *
  17.  */ 
  18. # include "stlport_prefix.h"
  19. #include <stl/_num_get.h>
  20. #include <stl/_istream.h>
  21. #include <stl/_algo.h>
  22.  
  23. _STLP_BEGIN_NAMESPACE
  24.  
  25. //----------------------------------------------------------------------
  26. // num_get
  27.  
  28.  
  29. static char narrow_digits[11]  = "0123456789";
  30. static char narrow_xdigits[13] = "aAbBcCdDeEfF";
  31.  
  32. # ifndef _STLP_NO_WCHAR_T 
  33.  
  34. void  _STLP_CALL
  35. _Initialize_get_digit(wchar_t* digits, wchar_t* xdigits,
  36.                        const ctype<wchar_t>& ct)
  37. {
  38.   ct.widen(narrow_digits + 0,  narrow_digits + 10,  digits);
  39.   ct.widen(narrow_xdigits + 0, narrow_xdigits + 10, xdigits);
  40. }
  41.  
  42. // Return either the digit corresponding to c, or a negative number if
  43. // if c isn't a digit.  We return -1 if c is the separator character, and
  44. // -2 if it's some other non-digit.
  45. int _STLP_CALL __get_digit(wchar_t c,
  46.                            const wchar_t* digits, const wchar_t* xdigits,
  47.                            wchar_t separator)
  48. {
  49.   // Test if it's the separator.
  50.   if (c == separator)
  51.     return -1;
  52.  
  53.   const wchar_t* p;
  54.  
  55.   // Test if it's a decimal digit.
  56.   p = find(digits, digits + 10, c);
  57.   if (p != digits + 10)
  58.     return (int)(p - digits);
  59.  
  60.   // Test if it's a hex digit.
  61.   p = find(xdigits, xdigits + 12, c);
  62.   if (p != xdigits + 12)
  63.     return (int)(10 + (xdigits - p) / 2);
  64.   else
  65.     return -2;                  // It's not a digit and not the separator.
  66. }
  67.  
  68. # endif /* _STLP_NO_WCHAR_T */
  69.  
  70. // __valid_grouping compares two strings, one representing the
  71. // group sizes encountered when reading an integer, and the other
  72. // representing the valid group sizes as returned by the numpunct
  73. // grouping() member function.  Both are interpreted right-to-left.
  74. // The grouping string is treated as if it were extended indefinitely
  75. // with its last value.  For a grouping to be valid, each term in
  76. // the first string must be equal to the corresponding term in the
  77. // second, except for the last, which must be less than or equal.
  78.  
  79. // boris : this takes reversed first string !
  80. bool  _STLP_CALL
  81. __valid_grouping(const char * first1, const char * last1, 
  82.                  const char * first2, const char * last2)
  83. {
  84.   if (first1 == last1 || first2 == last2) return true;
  85.  
  86.   --last1; --last2;
  87.  
  88.   while (first1 != last1) {
  89.     if (*last1 != *first2)
  90.       return false;
  91.     --last1;
  92.     if (first2 != last2) ++first2;
  93.   }
  94.  
  95.   return *last1 <= *first2;
  96. }
  97.  
  98. // this needed for some compilers to make sure sumbols are extern
  99. extern const unsigned char __digit_val_table[];
  100. extern const char __narrow_atoms[];
  101.  
  102.  
  103. const unsigned char __digit_val_table[128] = 
  104. {
  105.   0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  106.   0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  107.   0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  108.    0, 1, 2, 3, 4, 5, 6, 7, 8, 9,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  109.   0xFF,10,11,12,13,14,15,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  110.   0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  111.   0xFF,10,11,12,13,14,15,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  112.   0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  113. };     
  114.  
  115. const char __narrow_atoms[5] = {'+', '-', '0', 'x', 'X'};
  116.  
  117. // index is actually a char
  118.  
  119. # ifndef _STLP_NO_WCHAR_T
  120.  
  121. // Similar, except return the character itself instead of the numeric
  122. // value.  Used for floating-point input.
  123. bool  _STLP_CALL __get_fdigit(wchar_t& c, const wchar_t* digits)
  124. {
  125.   const wchar_t* p = find(digits, digits + 10, c);
  126.   if (p != digits + 10) {
  127.     c = (char)('0' + (p - digits));
  128.     return true;
  129.   }
  130.   else
  131.     return false;
  132. }
  133.  
  134. bool  _STLP_CALL __get_fdigit_or_sep(wchar_t& c, wchar_t sep,
  135.                                      const wchar_t * digits)
  136. {
  137.   if (c == sep) {
  138.     c = (char)',';
  139.     return true;
  140.   }
  141.   else
  142.     return __get_fdigit(c, digits);
  143. }
  144.  
  145. //----------------------------------------------------------------------
  146. // Force instantiation of of num_get<>
  147.  
  148. #if !defined(_STLP_NO_FORCE_INSTANTIATE)
  149. template class _STLP_CLASS_DECLSPEC  istreambuf_iterator<wchar_t, char_traits<wchar_t> >;
  150. template class num_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
  151. // template class num_get<wchar_t, const wchar_t*>;
  152. #endif
  153.  
  154. # endif /* _STLP_NO_WCHAR_T */
  155.  
  156. //----------------------------------------------------------------------
  157. // Force instantiation of of num_get<>
  158.  
  159. #if !defined(_STLP_NO_FORCE_INSTANTIATE)
  160. template class _STLP_CLASS_DECLSPEC istreambuf_iterator<char, char_traits<char> >;
  161. // template class num_get<char, const char*>;
  162. template class num_get<char, istreambuf_iterator<char, char_traits<char> > >;
  163. #endif
  164.  
  165. // basic_streambuf<char, char_traits<char> >* _STLP_CALL _M_get_istreambuf(basic_istream<char, char_traits<char> >& ) ;
  166.  
  167. _STLP_END_NAMESPACE
  168.  
  169. // Local Variables:
  170. // mode:C++
  171. // End:
  172.  
  173.  
  174.