comp.lang.ada
 help / color / mirror / Atom feed
* Please evaluate tiny binding that does not use Interfaces.C
@ 2017-08-18  2:23 patrick
  2017-08-18  5:18 ` Per Sandberg
  0 siblings, 1 reply; 4+ messages in thread
From: patrick @ 2017-08-18  2:23 UTC (permalink / 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 ;
 }

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

end of thread, other threads:[~2017-08-18 15:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-18  2:23 Please evaluate tiny binding that does not use Interfaces.C patrick
2017-08-18  5:18 ` Per Sandberg
2017-08-18 13:05   ` patrick
2017-08-18 15:41     ` patrick

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