From: patrick@spellingbeewinnars.org
Subject: Please evaluate tiny binding that does not use Interfaces.C
Date: Thu, 17 Aug 2017 19:23:58 -0700 (PDT)
Date: 2017-08-17T19:23:58-07:00 [thread overview]
Message-ID: <75d36301-5649-403b-b06f-7166196929c5@googlegroups.com> (raw)
Hi Everyone
It seems like everyone uses Interfaces.C when creating a C binding. I realize that this makes the binding more portable.
However, is there something to be said for small, easy to read code too?
It seems that most bindings attempt to recreate C structs with records and then create pointers to those records. Meanwhile, the binding does not access any members of that C struct directly. Those structs often have other structs as members too and the process goes on and on.
I realize that void pointers are dangerous but C is dangerous and I am wondering if my use is within good practices?
My code is small and contains no exception handling and likely has lots of other things missing like a gpr files and such but is the basic interfacing between C and Ada okay? Do you see any pitfalls, aside from the fact that it is calling C ?
I personally find this very easy to write and read, I am hoping that this will work out. Please let me know what you think
-----------------------------------------------------------------
build.sh
gnatmake -c terminal_app.adb
gnatmake -c ncurses_glue
gcc-4.6 -c ncurses_glue.c
gnatbind terminal_app.ali ncurses_glue.ali
gnatmake -c b~terminal_app.adb
gcc-4.6 terminal_app.o b~terminal_app.o ncurses_glue.o -o terminal_app -gnatwa -lncursesw -lgnat -gnato
-----------------------------------------------------------------
-- test to see if library without interfaces.c will work out
--
--
with ncurses_glue ;
with ada.strings.fixed ;
procedure terminal_app is
package ng renames ncurses_glue ;
package sf renames ada.strings.fixed ;
stdscr : access ng.WINDOW ;
ret : integer ;
str : string := "test 㐴 " & ASCII.NUL ;
begin
stdscr := ng.initscr_plus ;
ret := ng.addstr(str) ;
ret := ng.refresh ;
delay 2.0 ;
ret := ng.endwin ;
end terminal_app ;
-----------------------------------------------------------------
package ncurses_glue is
type null_record is null record ;
type WINDOW is access null_record ;
function initscr_plus
return Access WINDOW;
pragma Import (C, initscr_plus, "initscr_plus");
function addstr (arg1 : string)
return integer ;
pragma Import (C, addstr, "addstr");
function refresh
return integer;
pragma Import (C, refresh, "refresh") ;
function endwin
return integer;
pragma Import (C, endwin, "endwin") ;
end ncurses_glue ;
-----------------------------------------------------------------
#include <ncursesw/ncurses.h>
#include <locale.h>
void * initscr_plus() {
setlocale(LC_CTYPE,"");
initscr() ;
return stdscr ;
}
next reply other threads:[~2017-08-18 2:23 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-18 2:23 patrick [this message]
2017-08-18 5:18 ` Please evaluate tiny binding that does not use Interfaces.C Per Sandberg
2017-08-18 13:05 ` patrick
2017-08-18 15:41 ` patrick
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox