File : a-textio-c.adb


   1 ------------------------------------------------------------------------------
   2 --                                                                          --
   3 --                         GNAT RUN-TIME COMPONENTS                         --
   4 --                                                                          --
   5 --                          A D A . T E X T _ I O                           --
   6 --                                                                          --
   7 --                                 B o d y                                  --
   8 --                                                                          --
   9 --          Copyright (C) 1992-2015, Free Software Foundation, Inc.         --
  10 --                                                                          --
  11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  12 -- terms of the  GNU General Public License as published  by the Free Soft- --
  13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
  14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  16 -- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
  17 --                                                                          --
  18 --                                                                          --
  19 --                                                                          --
  20 --                                                                          --
  21 --                                                                          --
  22 -- You should have received a copy of the GNU General Public License and    --
  23 -- a copy of the GCC Runtime Library Exception along with this program;     --
  24 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
  25 -- <http://www.gnu.org/licenses/>.                                          --
  26 --                                                                          --
  27 -- GNAT was originally developed  by the GNAT team at  New York University. --
  28 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
  29 --                                                                          --
  30 ------------------------------------------------------------------------------
  31 
  32 --  Version for use with C run time
  33 
  34 package body Ada.Text_IO is
  35 
  36    --------------
  37    -- New_Line --
  38    --------------
  39 
  40    procedure New_Line is
  41    begin
  42       Put (ASCII.LF);
  43    end New_Line;
  44 
  45    ---------
  46    -- Put --
  47    ---------
  48 
  49    procedure Put (Item : Character) is
  50       function Putchar (C : Integer) return Integer;
  51       pragma Import (C, Putchar);
  52 
  53       Ignore : Integer;
  54 
  55    begin
  56       Ignore := Putchar (Character'Pos (Item));
  57    end Put;
  58 
  59    procedure Put (Item : String) is
  60    begin
  61       for J in Item'Range loop
  62          Put (Item (J));
  63       end loop;
  64    end Put;
  65 
  66    ---------
  67    -- Put --
  68    ---------
  69 
  70    procedure Put (X : Integer) is
  71       Neg_X : Integer;
  72       S     : String (1 .. Integer'Width);
  73       First : Natural := S'Last + 1;
  74       Val   : Integer;
  75 
  76    begin
  77       --  Work on negative values to avoid overflows
  78 
  79       Neg_X := (if X < 0 then X else -X);
  80 
  81       loop
  82          --  Cf RM 4.5.5 Multiplying Operators.  The rem operator will return
  83          --  negative values for negative values of Neg_X.
  84 
  85          Val := Neg_X rem 10;
  86          Neg_X := (Neg_X - Val) / 10;
  87          First := First - 1;
  88          S (First) := Character'Val (Character'Pos ('0') - Val);
  89          exit when Neg_X = 0;
  90       end loop;
  91 
  92       if X < 0 then
  93          First := First - 1;
  94          S (First) := '-';
  95       end if;
  96 
  97       Put (S (First .. S'Last));
  98    end Put;
  99 
 100    --------------
 101    -- Put_Line --
 102    --------------
 103 
 104    procedure Put_Line (Item : String) is
 105    begin
 106       Put (Item);
 107       New_Line;
 108    end Put_Line;
 109 
 110 end Ada.Text_IO;