comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: Hex ouput
Date: Sat, 25 Oct 2003 00:39:08 GMT
Date: 2003-10-25T00:39:08+00:00	[thread overview]
Message-ID: <3F99C629.90903@spam.com> (raw)
In-Reply-To: <3F99407E.5070305@psu.edu>

[-- Attachment #1: Type: text/plain, Size: 791 bytes --]

Robert Spooner wrote:
> 
> I would like to get a copy of the body of your package. It looks like a 
> more simple approach than using picture strings - which I've been using 
> for a home project of controlling an amateur radio transceiver.

See attached. The test program produces a lot of lines on standard 
output, so you'll probably want to redirect it to a file, or pipe it 
into a more program. Please let me know if it works well for you, and 
any problems you encounter.

> Nice signature line. I've found that trying to do safe programming in 
> C++ involves a lot of abstraction inversion because the granularity on 
> safety is so coarse.

Only one of many, chosen randomly each day. Only a few have to do with S/W.

-- 
Jeff Carter
"We use a large, vibrating egg."
Annie Hall
44

[-- Attachment #2: images.ads --]
[-- Type: text/plain, Size: 823 bytes --]

with Ada.Text_IO;
package Images is
   subtype Field       is Ada.Text_IO.Field;
   subtype Number_Base is Ada.Text_IO.Number_Base;
   
   generic -- Signed_Image
      type Number is range <>;
   function Signed_Image (Value : Number; Width : Field := 0; Zero_Filled : Boolean := False; Base : Number_Base := 10)
   return String;
   -- Returns an image of Value, at least Width characters wide, in base Base, padded with blanks or zeroes,
   -- according to Zero_Filled
   
   generic -- Modular_Image
      type Number is mod <>;
   function Modular_Image (Value : Number; Width : Field := 0; Zero_Filled : Boolean := False; Base : Number_Base := 10)
   return String;
   -- Returns an image of Value, at least Width characters wide, in base Base, padded with blanks or zeroes,
   -- according to Zero_Filled
end Images;

[-- Attachment #3: images.adb --]
[-- Type: text/plain, Size: 2573 bytes --]

with Ada.Strings.Fixed;
package body Images is
   use Ada.Strings.Fixed;
   use Ada.Text_IO;
   
   function Adjust (Image : String; Width : Field; Negative : Boolean; Zero_Filled : Boolean) return String is
   -- Apply Width, Negative, & Zero_Filled to Image
   
      Blank : constant Character := ' ';
      Zero  : constant Character := '0';
      Minus : constant Character := '-';
   begin -- Adjust
      if Zero_Filled then
         if Negative then
            return Minus & (1 .. Width - Image'Length - 1 => Zero) & Image;
         else
            return (1 .. Width - Image'Length => Zero) & Image;
         end if;
      else
         if Negative then
            return (1 .. Width - Image'Length - 1 => Blank) & Minus & Image;
         else
            return (1 .. Width - Image'Length => Blank) & Image;
         end if;
      end if;
   end Adjust;
   
   function Signed_Image (Value : Number; Width : Field := 0; Zero_Filled : Boolean := False; Base : Number_Base := 10)
   return String is
      package Number_IO is new Integer_IO (Number);
      use Number_IO;
      
      Image : String (1 .. 100);
      Start : Positive;
      Stop  : Positive;
      
      Negative : constant Boolean := Value < 0;
   begin -- Signed_Image
      Put (To => Image, Item => Value, Base => Base);
      
      case Base is
      when 10 =>
         Start := Index_Non_Blank (Image);
         Stop  := Image'Last;
         
         if Negative then
            Start := Start + 1;
         end if;
      when 2 .. 9 | 11 .. 16 =>
         Start := 1 + Index (Image, "#");
         Stop  := Image'Last - 1;
      when others =>
         raise Program_Error;
      end case;
      
      return Adjust (Image (Start .. Stop), Width, Negative, Zero_Filled);
   end Signed_Image;

   function Modular_Image (Value : Number; Width : Field := 0; Zero_Filled : Boolean := False; Base : Number_Base := 10)
   return String is
      package Number_IO is new Modular_IO (Number);
      use Number_IO;
      
      Image : String (1 .. 100);
      Start : Positive;
      Stop  : Positive;
   begin -- Modular_Image
      Put (To => Image, Item => Value, Base => Base);
      
      case Base is
      when 10 =>
         Start := Index_Non_Blank (Image);
         Stop  := Image'Last;
      when 2 .. 9 | 11 .. 16 =>
         Start := 1 + Index (Image, "#");
         Stop  := Image'Last - 1;
      when others =>
         raise Program_Error;
      end case;
      
      return Adjust (Image (Start .. Stop), Width, False, Zero_Filled);
   end Modular_Image;
end Images;

[-- Attachment #4: test_images.adb --]
[-- Type: text/plain, Size: 1757 bytes --]

with Ada.Text_IO;
with Images;

use Ada.Text_IO;
procedure Test_Images is
   type Mod_Int is mod 2 ** Integer'Size;
   
   function Image is new Images.Signed_Image  (Integer);
   function Image is new Images.Modular_Image (Mod_Int);
   
   Middle : constant Mod_Int := Mod_Int'Last / 2;
begin -- Test_Images
   All_Fills : for Fill in Boolean loop
      All_Bases : for Base in Images.Number_Base loop
         Put_Line ('>' & Image (Integer'First,  0, Fill, Base) & '<');
         Put_Line ('>' & Image (Integer'(0),    0, Fill, Base) & '<');
         Put_Line ('>' & Image (Integer'Last,   0, Fill, Base) & '<');
         Put_Line ('>' & Image (Integer'First, 17, Fill, Base) & '<');
         Put_Line ('>' & Image (Integer'(0),   17, Fill, Base) & '<');
         Put_Line ('>' & Image (Integer'Last,  17, Fill, Base) & '<');
         Put_Line ('>' & Image (Integer'First, 35, Fill, Base) & '<');
         Put_Line ('>' & Image (Integer'(0),   35, Fill, Base) & '<');
         Put_Line ('>' & Image (Integer'Last,  35, Fill, Base) & '<');
         
         Put_Line ('>' & Image (Mod_Int'First,  0, Fill, Base) & '<');
         Put_Line ('>' & Image (Middle,         0, Fill, Base) & '<');
         Put_Line ('>' & Image (Mod_Int'Last,   0, Fill, Base) & '<');
         Put_Line ('>' & Image (Mod_Int'First, 17, Fill, Base) & '<');
         Put_Line ('>' & Image (Middle,        17, Fill, Base) & '<');
         Put_Line ('>' & Image (Mod_Int'Last,  17, Fill, Base) & '<');
         Put_Line ('>' & Image (Mod_Int'First, 35, Fill, Base) & '<');
         Put_Line ('>' & Image (Middle,        35, Fill, Base) & '<');
         Put_Line ('>' & Image (Mod_Int'Last,  35, Fill, Base) & '<');
      end loop All_Bases;
   end loop All_Fills;
end Test_Images;

  reply	other threads:[~2003-10-25  0:39 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-22 16:56 Hex ouput Xenos
2003-10-22 17:19 ` Ed Falis
2003-10-23 13:34   ` Xenos
2003-10-23 14:14     ` Hard copy of Ada RM (was: Hex ouput) Ed Falis
2003-10-23 14:57       ` Xenos
2003-10-24  6:24       ` Dirk Craeynest
2003-10-23 15:39     ` Hex ouput Jerry Petrey
2003-10-23 19:51       ` Xenos
2003-10-23 21:18         ` Stephen Leake
2003-10-24  7:36         ` Dmitry A. Kazakov
2003-10-24  1:09       ` Jeffrey Carter
2003-10-24 15:08         ` Robert Spooner
2003-10-25  0:39           ` Jeffrey Carter [this message]
2003-10-26  1:16             ` Jeffrey Carter
2003-10-23 23:49     ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox