comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <matthewjheaney@earthlink.net>
Subject: Re: Importing C structs?
Date: Thu, 31 Jul 2003 05:21:59 GMT
Date: 2003-07-31T05:21:59+00:00	[thread overview]
Message-ID: <XH1Wa.298$jp.33@newsread4.news.pas.earthlink.net> (raw)
In-Reply-To: pan.2003.07.30.20.09.17.183172.1022@nospam.net


"Freejack" <freejack@nospam.net> wrote in message
news:pan.2003.07.30.20.09.17.183172.1022@nospam.net...
>
> However, I'm fuzzy on importing C structs. In a nutshell...here is the
> function I'm attempting to import...
>
> DB *
>   dbopen(const char *file, int flags, int mode, DBTYPE type,
> const void *openinfo);

type DB_Type is limited null record;
type DB_Access is access all DB_Type;
for DB_Access'Storage_Size use 0;
pragma Convention (C, DB_Access);

function dbopen
  (file : Interfaces.C.chars_ptr;
   flags : Interfaces.C.int;
   mode : Interfaces.C.int;
   dbtype : dbtype_type; --???
   openinfo : System.Address) return DB_Access;

pragma Convention (C, dbopen);
pragma Import (C, dbopen, "dbopen");


> Where "int mode" corresponds to the C "open" declaration and DBTYPE is a
> C struct. I'm using the dbopen man page for the Sleepycat Berkely DB.

I don't have the man pages available to me.  What exactly is DBTYPE?  Is
this a struct?  Is is really passed by value?

> Now I'm using
>
> pragma Import( C, dbopen );
>
> And then casting everything as either a C.integer or a character pointer.
>  It compiles this way, but it sucks as a way to do it.

Your pragma is OK, but I don't know why you're casting.

You can probably substitute type Standard.Integer for C.int, but this isn't
guaranteed to work.  (It does on GNAT, because GNAT guarantees that the Ada
representation matches the C representation.)

The example above is a thin binding.  You can always wrap that in
higher-level ("thick") call, inlined as desired.

> Specifically, would I cast DBTYPE as a seperate record type, and then
> provide a seperate function to translate each item into the type expected
> by the C struct? Or can import the DBTYPE struct whole hog into my Ada
> specification?

Import the struct.  But do post what the C struct looks like.





      parent reply	other threads:[~2003-07-31  5:21 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-07-30 20:03 Importing C structs? Freejack
2003-07-30 20:52 ` chris
2003-07-30 21:26   ` Freejack
2003-07-30 23:50   ` tmoran
2003-07-31 10:53     ` chris
2003-08-04 14:33     ` Andreas Almroth
2003-08-04 15:16       ` Samuel Tardieu
2003-08-04 20:07         ` Randy Brukardt
2003-07-31 17:14   ` Warren W. Gay VE3WWG
2003-08-12  0:02     ` chris
2003-07-31 18:17   ` Xenos
2003-07-31 19:16     ` Simon Wright
2003-07-31 20:17       ` Samuel Tardieu
2003-07-31 20:40       ` Xenos
2003-07-30 23:14 ` Ching Bon Lam
2003-07-31  0:07   ` tmoran
2003-07-31  5:35   ` Matthew Heaney
2003-07-31  7:46     ` Freejack
2003-07-31  9:27       ` Martin Dowie
2003-07-31 21:41         ` Freejack
2003-08-01  7:39           ` Martin Dowie
2003-07-31 17:34       ` Matthew Heaney
2003-07-31 11:29     ` Ching Bon Lam
2003-07-31 16:59       ` Matthew Heaney
2003-07-31 17:32         ` Warren W. Gay VE3WWG
2003-07-31 17:13       ` Matthew Heaney
2003-07-31 17:40       ` Randy Brukardt
2003-07-31  5:21 ` Matthew Heaney [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