home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adav313.zip / gnat-3_13p-os2-bin-20010916.zip / emx / gnatlib / s-valrea.adb < prev    next >
Text File  |  2000-07-19  |  11KB  |  328 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --                      S Y S T E M . V A L _ R E A L                       --
  6. --                                                                          --
  7. --                                 S p e c                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.17 $
  10. --                                                                          --
  11. --          Copyright (C) 1992-1999 Free Software Foundation, Inc.          --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT was originally developed  by the GNAT team at  New York University. --
  32. -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
  33. --                                                                          --
  34. ------------------------------------------------------------------------------
  35.  
  36. with System.Powten_Table; use System.Powten_Table;
  37. with System.Val_Util;     use System.Val_Util;
  38.  
  39. package body System.Val_Real is
  40.  
  41.    ---------------
  42.    -- Scan_Real --
  43.    ---------------
  44.  
  45.    function Scan_Real
  46.      (Str  : String;
  47.       Ptr  : access Integer;
  48.       Max  : Integer)
  49.       return Long_Long_Float
  50.    is
  51.       P : Integer;
  52.       --  Local copy of string pointer
  53.  
  54.       Base   : Long_Long_Float;
  55.       --  Base value
  56.  
  57.       Uval : Long_Long_Float;
  58.       --  Accumulated float result
  59.  
  60.       subtype Digs is Character range '0' .. '9';
  61.       --  Used to check for decimal digit
  62.  
  63.       Scale : Integer := 0;
  64.       --  Power of Base to multiply result by
  65.  
  66.       Start : Positive;
  67.       --  Position of starting non-blank character
  68.  
  69.       Minus : Boolean;
  70.       --  Set to True if minus sign is present, otherwise to False
  71.  
  72.       Bad_Base : Boolean := False;
  73.       --  Set True if Base out of range or if out of range digit
  74.  
  75.       After_Point : Natural := 0;
  76.       --  Set to 1 after the point
  77.  
  78.       procedure Scanf;
  79.       --  Scans integer literal value starting at current character position.
  80.       --  For each digit encountered, Uval is multiplied by 10.0, and the new
  81.       --  digit value is incremented. In addition Scale is decremented for each
  82.       --  digit encountered if we are after the point (After_Point = 1). The
  83.       --  longest possible syntactically valid numeral is scanned out, and on
  84.       --  return P points past the last character. On entry, the current
  85.       --  character is known to be a digit, so a numeral is definitely present.
  86.  
  87.       procedure Scanf is
  88.          Digit : Natural;
  89.  
  90.       begin
  91.          loop
  92.             Digit := Character'Pos (Str (P)) - Character'Pos ('0');
  93.             Uval := Uval * 10.0 + Long_Long_Float (Digit);
  94.             P := P + 1;
  95.             Scale := Scale - After_Point;
  96.  
  97.             --  Done if end of input field
  98.  
  99.             if P > Max then
  100.                return;
  101.  
  102.             --  Check next character
  103.  
  104.             elsif Str (P) not in Digs then
  105.                if Str (P) = '_' then
  106.                   Scan_Underscore (Str, P, Ptr, Max, False);
  107.                else
  108.                   return;
  109.                end if;
  110.             end if;
  111.          end loop;
  112.       end Scanf;
  113.  
  114.    --  Start of processing for System.Scan_Real
  115.  
  116.    begin
  117.       Scan_Sign (Str, Ptr, Max, Minus, Start);
  118.       P := Ptr.all;
  119.       Ptr.all := Start;
  120.  
  121.       --  If digit, scan numeral before point
  122.  
  123.       if Str (P) in Digs then
  124.          Uval := 0.0;
  125.          Scanf;
  126.  
  127.       --  Initial point, allowed only if followed by digit (RM 3.5(47))
  128.  
  129.       elsif Str (P) = '.'
  130.         and then P < Max
  131.         and then Str (P + 1) in Digs
  132.       then
  133.          Uval := 0.0;
  134.  
  135.       --  Any other initial character is an error
  136.  
  137.       else
  138.          raise Constraint_Error;
  139.       end if;
  140.  
  141.       --  Deal with based case
  142.  
  143.       if P < Max and then (Str (P) = ':' or else Str (P) = '#') then
  144.          declare
  145.             Base_Char : constant Character := Str (P);
  146.             Digit     : Natural;
  147.             Fdigit    : Long_Long_Float;
  148.  
  149.          begin
  150.             --  Set bad base if out of range, and use safe base of 16.0,
  151.             --  to guard against division by zero in the loop below.
  152.  
  153.             if Uval < 2.0 or else Uval > 16.0 then
  154.                Bad_Base := True;
  155.                Uval := 16.0;
  156.             end if;
  157.  
  158.             Base := Uval;
  159.             Uval := 0.0;
  160.             P := P + 1;
  161.  
  162.             --  Special check to allow initial point (RM 3.5(49))
  163.  
  164.             if Str (P) = '.' then
  165.                After_Point := 1;
  166.                P := P + 1;
  167.             end if;
  168.  
  169.             --  Loop to scan digits of based number. On entry to the loop we
  170.             --  must have a valid digit. If we don't, then we have an illegal
  171.             --  floating-point value, and we raise Constraint_Error, note that
  172.             --  Ptr at this stage was reset to the proper (Start) value.
  173.  
  174.             loop
  175.                if P > Max then
  176.                   raise Constraint_Error;
  177.  
  178.                elsif Str (P) in Digs then
  179.                   Digit := Character'Pos (Str (P)) - Character'Pos ('0');
  180.  
  181.                elsif Str (P) in 'A' .. 'F' then
  182.                   Digit :=
  183.                     Character'Pos (Str (P)) - (Character'Pos ('A') - 10);
  184.  
  185.                elsif Str (P) in 'a' .. 'f' then
  186.                   Digit :=
  187.                     Character'Pos (Str (P)) - (Character'Pos ('a') - 10);
  188.  
  189.                else
  190.                   raise Constraint_Error;
  191.                end if;
  192.  
  193.                P := P + 1;
  194.                Fdigit := Long_Long_Float (Digit);
  195.  
  196.                if Fdigit >= Base then
  197.                   Bad_Base := True;
  198.                else
  199.                   Scale := Scale - After_Point;
  200.                   Uval := Uval * Base + Fdigit;
  201.                end if;
  202.  
  203.                if P > Max then
  204.                   raise Constraint_Error;
  205.  
  206.                elsif Str (P) = '_' then
  207.                   Scan_Underscore (Str, P, Ptr, Max, True);
  208.  
  209.                else
  210.                   --  Skip past period after digit. Note that the processing
  211.                   --  here will permit either a digit after the period, or the
  212.                   --  terminating base character, as allowed in (RM 3.5(48))
  213.  
  214.                   if Str (P) = '.' and then After_Point = 0 then
  215.                      P := P + 1;
  216.                      After_Point := 1;
  217.  
  218.                      if P > Max then
  219.                         raise Constraint_Error;
  220.                      end if;
  221.                   end if;
  222.  
  223.                   exit when Str (P) = Base_Char;
  224.                end if;
  225.             end loop;
  226.  
  227.             --  Based number successfully scanned out (point was found)
  228.  
  229.             Ptr.all := P + 1;
  230.          end;
  231.  
  232.       --  Non-based case, check for being at decimal point now. Note that
  233.       --  in Ada 95, we do not insist on a decimal point being present
  234.  
  235.       else
  236.          Base := 10.0;
  237.          After_Point := 1;
  238.  
  239.          if P <= Max and then Str (P) = '.' then
  240.             P := P + 1;
  241.  
  242.             --  Scan digits after point if any are present (RM 3.5(46))
  243.  
  244.             if P <= Max and then Str (P) in Digs then
  245.                Scanf;
  246.             end if;
  247.          end if;
  248.  
  249.          Ptr.all := P;
  250.       end if;
  251.  
  252.       --  At this point, we have Uval containing the digits of the value as
  253.       --  an integer, and Scale indicates the negative of the number of digits
  254.       --  after the point. Base contains the base value (an integral value in
  255.       --  the range 2.0 .. 16.0). Test for exponent, must be at least one
  256.       --  character after the E for the exponent to be valid.
  257.  
  258.       Scale := Scale + Scan_Exponent (Str, Ptr, Max, Real => True);
  259.  
  260.       --  At this point the exponent has been scanned if one is present and
  261.       --  Scale is adjusted to include the exponent value. Uval contains the
  262.       --  the integral value which is to be multiplied by Base ** Scale.
  263.  
  264.       --  If base is not 10, use exponentiation for scaling
  265.  
  266.       if Base /= 10.0 then
  267.          Uval := Uval * Base ** Scale;
  268.  
  269.       --  For base 10, use power of ten table, repeatedly if necessary.
  270.  
  271.       elsif Scale > 0 then
  272.  
  273.          while Scale > Maxpow loop
  274.             Uval := Uval * Powten (Maxpow);
  275.             Scale := Scale - Maxpow;
  276.          end loop;
  277.  
  278.          if Scale > 0 then
  279.             Uval := Uval * Powten (Scale);
  280.          end if;
  281.  
  282.       elsif Scale < 0 then
  283.  
  284.          while (-Scale) > Maxpow loop
  285.             Uval := Uval / Powten (Maxpow);
  286.             Scale := Scale + Maxpow;
  287.          end loop;
  288.  
  289.          if Scale < 0 then
  290.             Uval := Uval / Powten (-Scale);
  291.          end if;
  292.       end if;
  293.  
  294.       --  Here is where we check for a bad based number
  295.  
  296.       if Bad_Base then
  297.          raise Constraint_Error;
  298.  
  299.       --  If OK, then deal with initial minus sign, note that this processing
  300.       --  is done even if Uval is zero, so that -0.0 is correctly interpreted.
  301.  
  302.       else
  303.          if Minus then
  304.             return -Uval;
  305.          else
  306.             return Uval;
  307.          end if;
  308.       end if;
  309.  
  310.    end Scan_Real;
  311.  
  312.    ----------------
  313.    -- Value_Real --
  314.    ----------------
  315.  
  316.    function Value_Real (Str : String) return Long_Long_Float is
  317.       V : Long_Long_Float;
  318.       P : aliased Integer := Str'First;
  319.  
  320.    begin
  321.       V := Scan_Real (Str, P'Access, Str'Last);
  322.       Scan_Trailing_Blanks (Str, P);
  323.       return V;
  324.  
  325.    end Value_Real;
  326.  
  327. end System.Val_Real;
  328.