comp.lang.ada
 help / color / mirror / Atom feed
* Newbie, returning strings in Ada
@ 2003-02-16 11:33 Santiago A.
  2003-02-16 11:58 ` Simon Wright
  2003-02-16 19:23 ` Jeffrey Carter
  0 siblings, 2 replies; 3+ messages in thread
From: Santiago A. @ 2003-02-16 11:33 UTC (permalink / raw)


Hello:
 I'm a newbie in Ada, and I'm trying to test AdaSockets. Using the
example listener.adb", I try to read a line from a socket but the
program has a strange behavior. After a little investigation I think
it has little to do with sockets, I'm afraid it's about something I
haven't understood about Ada strings. So I decided to to post it in
ada group instead AddSockets developers, if it's not the case, sorry
;-)

I use get_line function of AdaSockets, this reads character by
character from the socket, and add them to a string, if the char is LF
then returns. Everything works fine until It returns, then Depending
upon how I call the function, the programa stops.

This is the original procedure found in AdaSockets body:

     
   function Get_Line (
          Socket : Socket_Fd'Class ) 
      return String is 
       Result : String (1 .. 1024);  
       Index  : Positive           := Result'First;  
       Char   : Character;  
    begin
       loop
          Char := Get_Char (Socket);
          if Char = Lf then
             return Result (1 .. Index - 1);
          elsif Char /= Cr then
             Result (Index) := Char;
             Index := Index + 1;
             if Index > Result'Last then
                return Result & Get_Line (Socket);
             end if;
          end if;
       end loop;
    end Get_Line;

1) This case works fine:
      Put_Line (Sock, get_line(Socket) );

   where put_line spec is:  

      procedure Put_Line (Socket : in Socket_FD'Class;
                         Str : in String)

2) This case works fine too:

        var U_Line:Unbounded_String;
          ....
        U_Line:=To_Unbounded_String(Get_Line(Sock));


3) This case fails 

        var F_Line:string:="1234567890";   
         .....
        F_Line:=Get_Line(Sock);

I type two characters in the client side and press enter. Everything
seems to work fine, the server reads the first character, reads the
second,reads the LF, enters in the return sentence and... never
returns to the caller.

Is there any problem in a function that returns a string with lenght 2
into a variable string(10)?

Must the returned string and the variable that will hold the result be
of the same length?

By the way, I'm working with gnat-3.15p on Windows 2000.

Thanks



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Newbie, returning strings in Ada
  2003-02-16 11:33 Newbie, returning strings in Ada Santiago A.
@ 2003-02-16 11:58 ` Simon Wright
  2003-02-16 19:23 ` Jeffrey Carter
  1 sibling, 0 replies; 3+ messages in thread
From: Simon Wright @ 2003-02-16 11:58 UTC (permalink / raw)


general@ciberpiula.net (Santiago A.) writes:

> 1) This case works fine:
>       Put_Line (Sock, get_line(Socket) );
> 
>    where put_line spec is:  
> 
>       procedure Put_Line (Socket : in Socket_FD'Class;
>                          Str : in String)
> 
> 2) This case works fine too:
> 
>         var U_Line:Unbounded_String;
>           ....
>         U_Line:=To_Unbounded_String(Get_Line(Sock));
> 
> 
> 3) This case fails 
> 
>         var F_Line:string:="1234567890";   
>          .....
>         F_Line:=Get_Line(Sock);
> 
> I type two characters in the client side and press enter. Everything
> seems to work fine, the server reads the first character, reads the
> second,reads the LF, enters in the return sentence and... never
> returns to the caller.
> 
> Is there any problem in a function that returns a string with lenght 2
> into a variable string(10)?
> 
> Must the returned string and the variable that will hold the result be
> of the same length?

I don't think you can have actually written

       var F_Line:string:="1234567890";

!!!

Anyway, you are quite right, the returned string and the variable that
will hold the result must be of the same length.

Fortunately you can write

  loop
     declare
        f_line : constant string := get_line (sock);
     begin
        -- use f_line

because each time you enter the declare block the compiler creates a
new f_line of the length required to hold the actual string returned
by this particular call to get_line.

(the "constant" is just a programming style thing, declare things
constant if you don't need them to be variables).



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Newbie, returning strings in Ada
  2003-02-16 11:33 Newbie, returning strings in Ada Santiago A.
  2003-02-16 11:58 ` Simon Wright
@ 2003-02-16 19:23 ` Jeffrey Carter
  1 sibling, 0 replies; 3+ messages in thread
From: Jeffrey Carter @ 2003-02-16 19:23 UTC (permalink / raw)


Santiago A. wrote:
> 2) This case works fine too:
> 
>         var U_Line:Unbounded_String;
>           ....
>         U_Line:=To_Unbounded_String(Get_Line(Sock));
> 
> 
> 3) This case fails 
> 
>         var F_Line:string:="1234567890";   
>          .....
>         F_Line:=Get_Line(Sock);

This is not Ada. Perhaps you are using Pascal; "var" is not part of 
Ada's syntax.

If you were using Ada, you might write

declare
    F_Line : String := "1234567890";
begin
    F_Line := Get_Line (Sock);
    ...
end;

The assignment to F_Line will usually raise Constraint_Error. The only 
time this will not raise an exception is when Get_Line returns a String 
of length 10, or if you have run-time checks suppressed. I suggest you 
not suppress run-time checking.

Using an Unbounded_String works because it's an abstraction that may 
hold a string of any length. An object of type String is always the same 
length.

> Is there any problem in a function that returns a string with lenght 2
> into a variable string(10)?

Yes.

> 
> Must the returned string and the variable that will hold the result be
> of the same length?

Yes. However, you can store the result in an object created for that 
purpose:

declare
    F_Line : [constant] String := Get_Line (Sock);
begin
    ...
end;

If you leave off "constant", then you can modify the value in F_Line. If 
you don't do that, though, it's best to mark the object as a constant.

-- 
Jeff Carter
"If I could find a sheriff who so offends the citizens of Rock
Ridge that his very appearance would drive them out of town ...
but where would I find such a man? Why am I asking you?"
Blazing Saddles




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-02-16 19:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-16 11:33 Newbie, returning strings in Ada Santiago A.
2003-02-16 11:58 ` Simon Wright
2003-02-16 19:23 ` Jeffrey Carter

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