โŒ About FreshRSS

Normal view

There are new articles available, click to refresh the page.
Before yesterdayNews from the Ada programming language world

Ada: subprogram that ignores initial blank spaces

I'm interested in finding out if there is a way to create a "Get" subprogram for strings that works like "Get" for Integers or Float. As I understand the Ada get procedure for Integers ignores any type in of blank spaces before the integer and only collects the integer value.

Let's say we want to collect a string of five characters that should be stored in the variable "S" but the user type in 10 blank spaces and then the string so it would look something like this: Put in a string of 5 characters: buses I would like to create a "Get" that ignore these blank spaces and only gives my program the string value of 5 characters.

This is my main program.

S : String (1 .. 5);
begin
Put("Put in a string of 5 characters: ");
Get(S);
Put_Line(S);

I've read something about a function End_Of_Line. I understand that i need to create some kind of subprogram that collects the string and skips all initial blank spaces but I have not come up with a working solution.

Update: I tried to create my own get but got a little bit stuck. The get procedure should read the next character if the previous is ' ' but if it reads "the correct" string, how should the code look like?

       procedure Get(Item : in out String) is
      
      Ch : Character;
   begin
      
      loop
     Get(Ch);
     if Ch = ' ' and (not End_Of_Line) then
        Get(Ch);
     else 
       exit;
     end if;
      end loop;
      
   end Get;

Bug in GNAT Get(FRom => SomeString, Item => SomeInteger, Last => Last)?

Get(TheFile, IntValue); works great with strings formatted like 16#12# to read hex values. Shouldn't Get from a string function the same way?

I tried this, but passing 16#12# only yields 16. Pure hex, eg F8 results in an exception

with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure hex is
   IntValue  : Integer;
   Last : Positive;
begin
   Put("Enter a hexadecimal string: ");
   Get(HexString);

   -- Convert the hexadecimal string to an integer
   Get(From => HexString, Item => IntValue, Last => Last);

   Put ("The integer value is: ");
   Put (IntValue, Width => 0);
   New_Line;
end hex;

String from integer8

I'm using a library in ada which contains many types :

type Int8 is range -8 ** 7 .. 2 ** 7 - 1;
subtype T_Name_String is Int8;
type T_Name_String_Fixed20 is array (range 1..20) of T_Name_String ;

And there is a record with :

type The_Record is record
   name : T_Name_String_Fixed20;
   -- and others
end record;

I can't change that, I don't know why there are using Int8 for ada strings but I'm not able to initiate the field name. I've try :

-- First try:
MyRecord.name = "hello               ";
-- Error : expected type T_Name_String_Fixed20 found a string type

-- Second try
Ada.Strings.Fixed.Move(Target => MyRecord.name;
                       Source => "hello               "
โŒ
โŒ