comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Ada equivalent to &#1057;++ header sentence.
Date: Mon, 23 Aug 2004 22:32:09 -0500
Date: 2004-08-23T22:32:09-05:00	[thread overview]
Message-ID: <L-ednYtKD_8fK7fcRVn-qw@megapath.net> (raw)
In-Reply-To: mailman.5.1093005464.28011.comp.lang.ada@ada-france.org

"Stephen Leake" <stephen_leake@acm.org> wrote in message
news:mailman.5.1093005464.28011.comp.lang.ada@ada-france.org...
> rhezusfactor@yahoo.com (Johnswort) writes:
...
> > Presumingly, LPSTR may come from Win32.
>
> Yes, it stands for "Long Pointer to String". You might be tempted to
> define it in Ada as:
>
> type LPSTR is access String; --  WRONG!
>
> But since an Ada string is an Ada array, and a C string is just a
> pointer to Char, this is wrong. You can use Interfaces.C.Pointers, but
> that gets very cumbersome. Use System.Address, and make sure you
> append ASCII.Nul to the end of any strings passed to C.

I was with you until this point, but I don't agree with the advice to use
System.Address. Using it in Ada 95 (other than with an address clause)
represents a design mistake in my view.

Claw defines LPStr as
    type LPStr is access all Interfaces.C.Char;

and uses the Interfaces.C To_C and To_Ada functions to do the needed
conversions. Note that Interfaces.C.Char_Array has aliased components, so
that
    Name(Name'First)'Access
passes the first character of a Char_Array, (and thus the whole array in C
terms).

This has the advantage of being more strongly typed - it's still possible to
pass a char array that's not null terminated, but you can't accidentially
pass an array of integers or (more commonly) some record type.
Rule of thumb: Never, ever write weakly typed code when it's reasonable to
write strongly typed code.

An alternative is to use Interfaces.C.Strings (there's no reason to mess
with Interfaces.C.Pointers for this), but it tends to make you do some
storage management that's usually unnecessary. (We also didn't have it
working in Janus/Ada when we started Claw, which is probably really why we
avoided it.)

                    Randy.






      parent reply	other threads:[~2004-08-24  3:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-20  8:22 Ada equivalent to &#1057;++ header sentence Johnswort
2004-08-20 11:17 ` Jeff C,
2004-08-20 12:37 ` Stephen Leake
2004-08-20 12:53   ` Frank J. Lhota
2004-08-20 19:11   ` Johnswort
2004-08-24  3:32   ` Randy Brukardt [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