comp.lang.ada
 help / color / mirror / Atom feed
From: "Alexei Polkhanov" <alexp@ica.net>
Subject: Re: Fill string with multiple lines from standard_input
Date: Fri, 16 Aug 2002 10:33:00 -0700
Date: 2002-08-16T10:33:00-07:00	[thread overview]
Message-ID: <XEa79.2120$8Y5.5100@news.bc.tac.net> (raw)
In-Reply-To: ajebhb$ndl$01$1@news.t-online.com

Did you consider using POSIX library like Florist ? Like this code I use in
order to read
configuration parameters:

----------------------------------------------------------------------------
----
    -- Read configuration parameters from file
    ------------------------------------------------------------------------
--------
    procedure Read_Configuration(File : in String; Options : in out
Service_Options)
    is
        ConfigFile         : POSIX.IO.File_Descriptor;
        ConfigPathname     : POSIX.Pathname :=
           POSIX.To_POSIX_String(File & ASCII.NUL);

        LineNum            : Integer := 0;
        Total              : POSIX.IO_Count := 0; -- total characters read
    begin

        declare

            Use  POSIX;

            Buffer          : POSIX.IO.IO_Buffer(1..Read_Block_Size); --
read buffer
            LineBuffer      : POSIX.IO.IO_Buffer(1..256); -- temporary
buffer to skim line
            Last            : POSIX.IO_Count; -- last character in read
buffer

            CopyFromPosition: Integer := 0;
            PasteAtPosition : Integer := 1;
            CharactersToCopy: Integer := 0;

            -- copy characters from read buffer into line buffer
            procedure AddCharactersToLine is
            begin
                LineBuffer(PasteAtPosition..(PasteAtPosition +
CharactersToCopy - 1 )) :=
                    Buffer(CopyFromPosition..(CopyFromPosition +
CharactersToCopy - 1 ));
            end AddCharactersToLine;

            ParamName : Token;
            ParamValue: Token;

        begin
            ConfigFile := POSIX.IO.Open(ConfigPathname, POSIX.IO.Read_Only);

            while true loop

                 POSIX.IO.Read(ConfigFile, Buffer, Last);

                 CopyFromPosition := 0;
                 CharactersToCopy := 0;

                 for BufferPos in 1..Last loop
                     if Buffer(Integer(BufferPos)) = LF then

                         if CharactersToCopy > 0 then
                             AddCharactersToLine;

                             declare
                                Line : String :=
POSIX.To_String(LineBuffer(LineBuffer'First..CharactersToCopy));
                             begin
                                Split(Line, ParamName, ParamValue);
                                UpdateValue(Line, ParamName, ParamValue);

                                -- if ParamName.exists and ParamValue.exists
then
                                --   Put_Line(Integer'Image(LineNum) & ": "
                                --      &
Line(ParamName.first..ParamName.last)
                                --      & " =[" &
Line(ParamValue.first..ParamValue.last) & "]");
                                -- else
                                --   null;
                                -- end if;

                             end;

                             CharactersToCopy := 0;
                             CopyFromPosition := 0;
                             PasteAtPosition := 1;

                         else
                             null;
                         end if;

                         LineNum := LineNum + 1;
                      else
                             if CopyFromPosition = 0 then
                                 CopyFromPosition := Integer(BufferPos);
                             else
                                 null;
                             end if;
                             CharactersToCopy := CharactersToCopy + 1;
                      end if;
                  end loop;

                  if CharactersToCopy > 0 then
                      AddCharactersToLine;
                      PasteAtPosition := CharactersToCopy;
                  else
                      null;
                  end if;

                  Total := Total + Last;

              end loop;

            exception
                -- when Ada.Io_Exceptions.End_Error =>
                --    POSIX.IO.Close(ConfigFile);
                when others =>
                    POSIX.IO.Close(ConfigFile);
        end;

        exception
            when others =>
                Put_Line("Exception rethrown");

    end Read_Configuration;


Cheers,

---
Alexei Polkhanov,
Sr. Software Engineer,
Incognito Software Inc,
Vancouver, Canada

"Vincent Smeets" <No@Spam.org> wrote in message
news:ajebhb$ndl$01$1@news.t-online.com...
> Hallo,
>
> How can I fill a string with multiple lines from the standard_input? I
want
> to have all the characters read from the input till a end of file in a
> string.
>
> In C I can do it like this:
>     char buffer[1024];
>     ssize_t length;
>
>     length = read (1, buffer, sizeof (buffer));
>     if (length < 0) {
>         perror ("read");
>         exit (1);
>     }
>
> I can do it with Get_Immediate:
>     Buffer : String (1 .. 1024);
>     Length : Natural;
>
>     Length := 0;
>     while not Ent_Of_File and Length < Buffer'Last loop
>         Length := Length + 1;
>         Get_Immediate (Buffer (Length));
>     end loop;
> This works but is very slow with GNAT 3.13p on Windows 2000.
>
> I haven't tried sequential IO, but that is also character by character. I
> want something to read a buffer at once. I have been thinking about using
> streams. Something like:
>     Buffer : String (1 .. 1024);
>
>     String'Read (
>         Text_IO.Text_Streams.Stream (Text_IO.Standard_Input),
>         Buffer);
> But here I don't know how much of the Buffer has been filled.
>
> The last non portable design is to use a pragma import for the C "read"
> function. It will work but I don't like it because it isn't portable.
>
> Is the a nice Ada95 way to do this?
>
>





      parent reply	other threads:[~2002-08-16 17:33 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-14 19:34 Fill string with multiple lines from standard_input Vincent Smeets
2002-08-14 20:37 ` Stephen Leake
2002-08-15 17:57   ` Vincent Smeets
2002-08-15 20:47     ` Stephen Leake
2002-08-19 19:39       ` Vincent Smeets
2002-08-20 15:01         ` Stephen Leake
2002-08-20 18:26           ` Vincent Smeets
2002-08-20 19:46             ` tmoran
2002-08-21 12:50             ` Jean-Pierre Rosen
2002-08-21 19:19             ` Randy Brukardt
2002-08-25  0:34               ` AG
2002-08-21 14:37         ` Kevin Cline
2002-08-21 19:47           ` Robert Dewar
2002-08-22  7:40             ` Kevin Cline
2002-08-23 19:49               ` Peter Richtmyer
2002-08-22 20:36             ` Kevin Cline
2002-08-25  0:45               ` AG
2002-08-26  0:47                 ` Kevin Cline
2002-08-15  2:30 ` SteveD
2002-08-15 18:02   ` Vincent Smeets
2002-08-16  0:25     ` Robert Dewar
2002-08-15 19:07   ` Vincent Smeets
2002-08-15 21:49     ` chris.danx
2002-08-16 19:30       ` Ted Dennison
2002-08-17  2:26         ` Randy Brukardt
2002-08-17 10:24         ` Robert Dewar
2002-08-19 13:40           ` Ted Dennison
2002-08-20  0:03             ` Robert Dewar
2002-08-16 17:33 ` Alexei Polkhanov [this message]
replies disabled

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