comp.lang.ada
 help / color / mirror / Atom feed
* Interfaces.C
@ 1994-11-01 15:50 Ken Thomas
  1994-11-01 22:45 ` Interfaces.C Dale Stanbrough
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Ken Thomas @ 1994-11-01 15:50 UTC (permalink / raw)


I am attempting to write an Ada9X interface for a C function
with specification

void foo(int *a, int l)

the aim being to pass an integer array to the C-function. What is the
correct usage of the package Interfaces.C and its children?

Ken Thomas
University of Southampton
U.K.
kst@uk.ac.soton.ecs




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

* Re: Interfaces.C
  1994-11-01 15:50 Interfaces.C Ken Thomas
@ 1994-11-01 22:45 ` Dale Stanbrough
  1994-11-02 13:34 ` Interfaces.C Robb Nebbe
  1994-11-06  1:15 ` Interfaces.C Dale Stanbrough
  2 siblings, 0 replies; 7+ messages in thread
From: Dale Stanbrough @ 1994-11-01 22:45 UTC (permalink / raw)


Subject: Interfaces.C
From: Ken Thomas, kst@ecs.soton.ac.uk
Date: 1 Nov 1994 15:50:18 -0000
In article <395o3q$2j1@wynkyn.ecs.soton.ac.uk> Ken Thomas, kst@ecs.soton.ac.uk
writes:
>I am attempting to write an Ada9X interface for a C function
>with specification
>
>void foo(int *a, int l)
>
>the aim being to pass an integer array to the C-function. What is the
>correct usage of the package Interfaces.C and its children?
>
>Ken Thomas
>University of Southampton
>U.K.
>kst@uk.ac.soton.ecs

-- presuming l is the length of the array...

type list is array(integer range <>) of interfaces.C.int;

procedure foo(a:in out list) is
	procedure C_foo(a:in system'address; l:interfaces.c.int);
	pragma import(C, C_foo, "foo");
begin
	C_foo(a(a'first)'address, interfaces.c.int(a'length));
end;

or...
-- has the advantage of isolating the nasty C bits.

type list is array(integer range <>) of some_integer_type;

procedure foo(a:in out list) is

	procedure C_foo(a:in system'address; l:interfaces.c.int);
	pragma import(C, C_foo, "foo");
	temp	:array(a'range) of interfaces.c.int;

begin
	for i in a'range loop
		temp(i) := interfaces.c.int(a(i));
	end loop;

	C_foo(temp(temp'first)'address, interfaces.c.int(temp'length));

	for i in temp'range loop
		a(i) := some_integer_type(temp(i));
	end loop;
end;


Dale
-------------------------------------------------------------
Dale Stanbrough, RMIT, Melbourne, Australia, dale@rmit.edu.au
GNU Ada 94 (GNAT) => the best $0 you'll ever spend.
Available for DOS, Linux, OS/2, Sun Sparc, Sun Solaris, ...
Coming to a GNU supported platform near you soon...



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

* Re: Interfaces.C
  1994-11-01 15:50 Interfaces.C Ken Thomas
  1994-11-01 22:45 ` Interfaces.C Dale Stanbrough
@ 1994-11-02 13:34 ` Robb Nebbe
  1994-11-06  1:15 ` Interfaces.C Dale Stanbrough
  2 siblings, 0 replies; 7+ messages in thread
From: Robb Nebbe @ 1994-11-02 13:34 UTC (permalink / raw)


In article <395o3q$2j1@wynkyn.ecs.soton.ac.uk>, kst@ecs.soton.ac.uk (Ken Thomas) writes:
|> I am attempting to write an Ada9X interface for a C function
|> with specification
|> 
|> void foo(int *a, int l)
|> 
|> the aim being to pass an integer array to the C-function. What is the
|> correct usage of the package Interfaces.C and its children?
|> 

If you just want to pass parameters to the C function you would write:

  type Int_Array is array( Natural range <> ) of C.int
  -- the range could be anything but C will treat it as starting at 0

  procedure foo( a : in Int_Array; l : in C.int );
  pragma Import( C, foo );


The array a will be passed by reference and l by value automatically.
The rules for how parameters are passed to a C program are defined
in Ada9x making it easy to abstract away from C's low-level calling
interface.

- Robb Nebbe



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

* Re: Interfaces.C
  1994-11-01 15:50 Interfaces.C Ken Thomas
  1994-11-01 22:45 ` Interfaces.C Dale Stanbrough
  1994-11-02 13:34 ` Interfaces.C Robb Nebbe
@ 1994-11-06  1:15 ` Dale Stanbrough
  2 siblings, 0 replies; 7+ messages in thread
From: Dale Stanbrough @ 1994-11-06  1:15 UTC (permalink / raw)


In article <1994Nov2.142450@lglsun.epfl.ch> Robb Nebbe, nebbe@lglsun.epfl.ch
writes:

>If you just want to pass parameters to the C function you would write:
>
>  type Int_Array is array( Natural range <> ) of C.int
>  -- the range could be anything but C will treat it as starting at 0
>
>  procedure foo( a : in Int_Array; l : in C.int );
>  pragma Import( C, foo );
>
>

But this says something different to void foo(int *a, int l).
A better version would be 

	procedure foo( a : in out Int_Array; l :in C.int);

Will this still be passed by reference?

Dale
-------------------------------------------------------------
Dale Stanbrough, RMIT, Melbourne, Australia, dale@rmit.edu.au
GNU Ada 94 (GNAT) => the best $0 you'll ever spend.
Available for DOS, Linux, OS/2, Sun Sparc, Sun Solaris, ...
Coming to a GNU supported platform near you soon...



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

* Interfaces.C
@ 1996-08-10  0:00 David Krovich
  1996-08-11  0:00 ` Interfaces.C David C. Hoos, Sr.
  1996-08-14  0:00 ` Interfaces.C Mike Fair
  0 siblings, 2 replies; 7+ messages in thread
From: David Krovich @ 1996-08-10  0:00 UTC (permalink / raw)



	
	I'm looking for source code examples for using the interfaces.C
package.  Any pointers would be appreciated.
	
	I've already printed out the Interfaces to Other Languages
section from the Ada 95 Reference manual, so please don't just point
me towards that.

	Thanks.

---------------------------------------------------------------------
------David Krovich-----------------------Computer Science Major-----
----dkrovich@cs.wvu.edu------------------West Virginia University----
---------------------------------------------------------------------







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

* Re: Interfaces.C
  1996-08-10  0:00 Interfaces.C David Krovich
@ 1996-08-11  0:00 ` David C. Hoos, Sr.
  1996-08-14  0:00 ` Interfaces.C Mike Fair
  1 sibling, 0 replies; 7+ messages in thread
From: David C. Hoos, Sr. @ 1996-08-11  0:00 UTC (permalink / raw)



Hi David,
here is a quick and dirty example.  Of course, Ada itself has better
facilities for printing, than the c-library's printf, but this illustrates
some c interfaces.  Hoe this helps -- the example even includes pointers ;)

with Interfaces.C;
with System;
with Text_IO;
procedure demo_interfaces_c is
  ada_format_string : constant string := "This is a C format string with a
long float" &
      " format specification %lf" & ascii.LF;
  function printf (
      format_string_address : system.address;
      long_float_value : long_float
      ) return integer;
  pragma interface (C, printf);
  return_value : integer;
  c_format_string : Interfaces.C.char_array (0 ..
ada_format_string'length); 
begin
  c_format_string := Interfaces.C.To_C (ada_format_string);
  return_value := printf (c_format_string'Address, long_float
(3.141592654));
  Text_IO.Put_Line ("printf return value =>" &
integer'image(return_value));
end demo_interfaces_c;

-- 
David C. Hoos, Sr.,
http://www.dbhwww.com
http://www.ada95.com

David Krovich <dkrovich@cs.wvu.edu> wrote in article
<DvwyEs.2vw@cerc.wvu.edu>...
> 	
> 	I'm looking for source code examples for using the interfaces.C
> package.  Any pointers would be appreciated.
> 	
> 	I've already printed out the Interfaces to Other Languages
> section from the Ada 95 Reference manual, so please don't just point
> me towards that.
> 
> 	Thanks.
> 
> ---------------------------------------------------------------------
> ------David Krovich-----------------------Computer Science Major-----
> ----dkrovich@cs.wvu.edu------------------West Virginia University----
> ---------------------------------------------------------------------
> 
> 
> 
> 




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

* Re: Interfaces.C
  1996-08-10  0:00 Interfaces.C David Krovich
  1996-08-11  0:00 ` Interfaces.C David C. Hoos, Sr.
@ 1996-08-14  0:00 ` Mike Fair
  1 sibling, 0 replies; 7+ messages in thread
From: Mike Fair @ 1996-08-14  0:00 UTC (permalink / raw)
  To: David Krovich


David Krovich wrote:
> 
> 
>         I'm looking for source code examples for using the interfaces.C
> package.  Any pointers would be appreciated.
> 

I have done several thin bindings to C code in Ada95
I have one for the GRX graphics library
and just starting working on a Berkley Sockets binding.

The GRX binding was actually approaching a fairly robust
thick binding.

My sockets binding currently will support
Socket()
listen()
bind()
accpet()
send()
recv()

I have only tested small programs with it but so far it works
very well.
It currently has 
package Sockets and
package Sockets.Internet

I would be more than happy to provide the source to you.
If anyone else has already done a Sockets Ada binding
please drop me a line, I do not know much about it I am
just learning as I go.

-- Mike --




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

end of thread, other threads:[~1996-08-14  0:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1994-11-01 15:50 Interfaces.C Ken Thomas
1994-11-01 22:45 ` Interfaces.C Dale Stanbrough
1994-11-02 13:34 ` Interfaces.C Robb Nebbe
1994-11-06  1:15 ` Interfaces.C Dale Stanbrough
  -- strict thread matches above, loose matches on Subject: below --
1996-08-10  0:00 Interfaces.C David Krovich
1996-08-11  0:00 ` Interfaces.C David C. Hoos, Sr.
1996-08-14  0:00 ` Interfaces.C Mike Fair

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