home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_USR08B.LHA / gerlib / examples / add / normal / cina.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  2.1 KB  |  147 lines

  1. /*-- Rev Header - do NOT edit!
  2.  *
  3.  *  Filename : cina.cc
  4.  *  Purpose  : (Erst einmal) Ersatz von istream, e.g. cin
  5.  *
  6.  *  Program  : -
  7.  *  Author   : Gerhard Müller
  8.  *  Copyright: (c) by Gerhard Müller
  9.  *  Creation :
  10.  *
  11.  *  compile  : make
  12.  *
  13.  *  Compile version  : 0.1
  14.  *  Ext. Version     : 0.1
  15.  *
  16.  *  REVISION HISTORY
  17.  *
  18.  *  Date                     Comment
  19.  *  ------------------------ -------------------------------------------------
  20.  *  Fri Sep 17 00:39:53 1993 Seems to work quite well.
  21.  *
  22.  *-- REV_END --
  23.  */
  24.  
  25.     /*
  26.      * C++-Includes, C++-Definitionen
  27.      *
  28.      */
  29.  
  30.  
  31. #include "cina.h"
  32.  
  33. cina& cina::operator>>(long& return_val)
  34. {
  35.     look_tie();
  36.  
  37.     int base = 10;
  38.     int ndigits = 0;
  39.     int neg;
  40.     int val;
  41.  
  42.     register int ch = skip_ws(*this);
  43.  
  44.     if (ch == EOF)
  45.         goto eof_fail;
  46.  
  47.     neg = 0;
  48.  
  49.     if (ch == '+')
  50.     {
  51.         ch = skip_ws(*this);
  52.     }
  53.     else if (ch == '-')
  54.     {
  55.         neg = 1;
  56.         ch = skip_ws(*this);
  57.     }
  58.  
  59.     if (ch == EOF) goto eof_fail;
  60.  
  61. //    if (!(stream.flags() & ios::basefield)) {
  62.  
  63.     if (ch == '0')
  64.     {
  65.         ch = FGetC(_fh);
  66.  
  67.         if (ch == EOF)
  68.         {
  69.             val = 0;
  70.             goto ok;
  71.         }
  72.  
  73.         if (ch == 'x' || ch == 'X')
  74.         {
  75.             base = 16;
  76.             ch = FGetC(_fh);
  77.             if (ch == EOF) goto eof_fail;
  78.         }
  79.         else
  80.         {
  81.             UnGetC(_fh,ch);
  82.             base = 8;
  83.             ch = '0';
  84.         }
  85.     }
  86.  
  87. //    }
  88. //  else if ((stream.flags() & ios::basefield) == ios::hex)
  89. //    base = 16;
  90. //  else if ((stream.flags() & ios::basefield) == ios::oct)
  91. //    base = 8;
  92.  
  93.  
  94.     val = 0;
  95.  
  96.     for (;;)
  97.     {
  98.         if (ch == EOF)
  99.             break;
  100.  
  101.         int digit;
  102.  
  103.         if (ch >= '0' && ch <= '9')
  104.             digit = ch - '0';
  105.         else if (ch >= 'A' && ch <= 'F')
  106.                 digit = ch - 'A' + 10;
  107.              else if (ch >= 'a' && ch <= 'f')
  108.                       digit = ch - 'a' + 10;
  109.                   else
  110.                       digit = 999;
  111.  
  112.         if (digit >= base)
  113.         {
  114.             UnGetC(_fh,ch);
  115.  
  116.             if (ndigits == 0)
  117.                 goto fail;
  118.             else
  119.             goto ok;
  120.         }
  121.  
  122.         ndigits++;
  123.         val = base * val + digit;
  124.         ch = FGetC(_fh);
  125.     }
  126. ok:
  127.     if(neg) val=-val;
  128.     return_val=val;
  129.     return *this;
  130.  
  131. fail:
  132.     return_val=0;
  133.     set(ios::failbit);
  134.     return *this;
  135.  
  136. eof_fail:
  137.     return_val=0;
  138.     set(ios::failbit|ios::eofbit);
  139.     return *this;
  140. }
  141.  
  142.  
  143.  
  144.  
  145. cina cin;
  146.  
  147.