comp.lang.ada
 help / color / mirror / Atom feed
* PSTR to Ada String.
@ 2006-10-25 20:50 Jade
  2006-10-26  7:56 ` Gerd
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Jade @ 2006-10-25 20:50 UTC (permalink / raw)


Hi
I am using GNAT's ada bindings and with functions like gethostname, etc
they choose to have a return value as a WIN32.PSTR.
However, all my ada functions require either ada standard strings or
characters.  I was wondering if there is way of converting PSTR to an
ada string.

Thanks,

Jade.




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

* Re: PSTR to Ada String.
  2006-10-25 20:50 PSTR to Ada String Jade
@ 2006-10-26  7:56 ` Gerd
  2006-10-26  8:13 ` Philippe Bertin
  2006-10-26  9:18 ` jca.miranda
  2 siblings, 0 replies; 6+ messages in thread
From: Gerd @ 2006-10-26  7:56 UTC (permalink / raw)


Here an example how it can be done:

  function AddrToPSTR is new Unchecked_Conversion(System.Address,
Win32.PSTR);

  procedure GetHostName (Name : out STRING; Len : out Integer; RC : out
INTEGER) is
    Erg : Win32.INT;
    HostName : STRING (1..255);
  begin
    -- The result (excluding the \0) must fit into "Name"
    Erg := gethostname (AddrToPSTR (HostName'ADDRESS), Name'LENGTH +
1);

    if Erg = SOCKET_ERROR
    then
      RC := INTEGER(WSAGetLastError);
    else
      RC := NO_ERROR;

      Name := (others => ' ');

      -- Should work correct, as "gethostname" should never return more
characters
      -- than "Name" can get.
      for i in HostName'FIRST .. HostName'LAST
      loop
        if HostName (i) = ASCII.NUL
        then
          Len := i - 1;
          return;
        else
          Name (Name'FIRST + i - 1) := HostName (i);
        end if;
      end loop;
    end if;
  end GetHostName;


Jade schrieb:

> Hi
> I am using GNAT's ada bindings and with functions like gethostname, etc
> they choose to have a return value as a WIN32.PSTR.
> However, all my ada functions require either ada standard strings or
> characters.  I was wondering if there is way of converting PSTR to an
> ada string.
> 
> Thanks,
> 
> Jade.




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

* Re: PSTR to Ada String.
  2006-10-25 20:50 PSTR to Ada String Jade
  2006-10-26  7:56 ` Gerd
@ 2006-10-26  8:13 ` Philippe Bertin
  2006-10-26 21:22   ` tmoran
  2006-10-26  9:18 ` jca.miranda
  2 siblings, 1 reply; 6+ messages in thread
From: Philippe Bertin @ 2006-10-26  8:13 UTC (permalink / raw)


Jade,

You may want to have a look at GtkAda, which is an Ada binding to the
(C-written) gtk library. There's good examples in there on how to
convert from and to C string (and a whole lot of other C- types in
general)

Kind regards,

PhB

Jade wrote:
...
> I was wondering if there is way of converting PSTR to an
> ada string.




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

* Re: PSTR to Ada String.
  2006-10-25 20:50 PSTR to Ada String Jade
  2006-10-26  7:56 ` Gerd
  2006-10-26  8:13 ` Philippe Bertin
@ 2006-10-26  9:18 ` jca.miranda
  2006-10-30 13:22   ` Jade
  2 siblings, 1 reply; 6+ messages in thread
From: jca.miranda @ 2006-10-26  9:18 UTC (permalink / raw)



Jade a écrit :

> Hi
> I am using GNAT's ada bindings and with functions like gethostname, etc
> they choose to have a return value as a WIN32.PSTR.
> However, all my ada functions require either ada standard strings or
> characters.  I was wondering if there is way of converting PSTR to an
> ada string.
>
> Thanks,
>
> Jade.

PSTR is simply a renaming for char * if I remember well.
So you should be able to make simple conversions thanks to
Ada.Interfaces.C To_C and To_Ada functions.

Rgds

Juan

P.S. : Hello Philippe! ^^ (Sorry for the noise added)




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

* Re: PSTR to Ada String.
  2006-10-26  8:13 ` Philippe Bertin
@ 2006-10-26 21:22   ` tmoran
  0 siblings, 0 replies; 6+ messages in thread
From: tmoran @ 2006-10-26 21:22 UTC (permalink / raw)


>You may want to have a look at GtkAda, which is an Ada binding to the
>(C-written) gtk library. There's good examples in there on how to ...
  Or at Claw, whose Claw.Sockets does:
    function gethostname(Name : Claw.Win32.Lpcstr;
                         Length : Claw.Int) return Claw.Int;
    pragma Import(Stdcall, gethostname, "gethostname");
    ...
    Local_Name : Interfaces.C.Char_Array(0 .. 200);
    ...
    Err := gethostname(Local_Name(0)'unchecked_access,
                       Claw.Int(Local_Name'length));
    if Err = Socket_Error then

> function AddrToPSTR is new Unchecked_Conversion(System.Address,
> Win32.PSTR);
   Locks you into certain compilers.  (A design requirement of Claw
was portability across 4 Ada->Windows compilers.)



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

* Re: PSTR to Ada String.
  2006-10-26  9:18 ` jca.miranda
@ 2006-10-30 13:22   ` Jade
  0 siblings, 0 replies; 6+ messages in thread
From: Jade @ 2006-10-30 13:22 UTC (permalink / raw)


Thanks everyone for the great suggestions.
Converting PSTR to ada strings is something I have to do often and you
made my life a lot easier.

Cheers,
Jade.




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

end of thread, other threads:[~2006-10-30 13:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-10-25 20:50 PSTR to Ada String Jade
2006-10-26  7:56 ` Gerd
2006-10-26  8:13 ` Philippe Bertin
2006-10-26 21:22   ` tmoran
2006-10-26  9:18 ` jca.miranda
2006-10-30 13:22   ` Jade

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