From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,UTF8 X-Google-Thread: 103376,85ebe4a637a97a2f X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-07-07 10:46:10 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!skynet.be!skynet.be!louie!tlk!not-for-mail Sender: lbrenta@lbrenta Newsgroups: comp.lang.ada Subject: Re: binding References: From: Ludovic Brenta Date: 07 Jul 2003 19:40:41 +0200 Message-ID: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Organization: -= Belgacom Usenet Service =- NNTP-Posting-Host: 217.136.28.79 X-Trace: 1057599969 reader0.news.skynet.be 274 217.136.28.79:32996 X-Complaints-To: usenet-abuse@skynet.be Xref: archiver1.google.com comp.lang.ada:40113 Date: 2003-07-07T19:40:41+02:00 List-Id: "Cephus�" writes: > Hello all, I am new to Ada and I guess really programming alltogether. I > read a lot here about bindings and I was wondering if someone could give me > a simple definition of the term? thanks for your time... Before defining a binding, let me first define a library; if you already know what this is you can skip ahead. A library is a collection of subprograms that are packaged together so that people can reuse them in many different programs. For example, the GIMP ToolKit (http://www.gtk.org) is a library (actually a set of several libraries) that makes it easy to write programs with a GUI. You can write a library in Ada if you want (in fact there are quite a few libraries written in Ada), but there are other languages as well. In particular, many libraries are written in C, because this language is very simple and is a least common denominator amongst languages. That is the reason why GTK is written in C. Ada has special built-in support for "interfacing with foreign languages". This means that, from an Ada program, you can call subprograms that are written in other languages, in particular C. A "binding", therefore, is essentially a set of subprogram specifications that make library subprograms visible to you, the Ada programmer. Many bindings exist for many different libraries; for one example, see http://libre.act-europe.fr/GtkAda for an Ada binding to GTK. There are two kinds of bindings: "thin" and "thick". A "thin" binding provides no abstraction over the library; it just exposes the subprograms from the library. Usually, these subprograms are associated with data types; it is possible to define Ada data types that are equivalent to the C data types. By contrast, a "thick" binding presents a cleaner and more Ada-ish interface which you can use to call library subprograms. For example, a "thick" binding would use Ada exceptions, rather than error codes, to report problems; it would also utilise Ada's overloading features to make the binding simpler to use than the original library. C has neither exceptions nor overloading, so libraries written in C tend to be clumsier to use than Ada libraries or Ada thick bindings to them. Here is an example of a binding that I posted not long ago; it binds the function "popen" from the POSIX C library and makes it available to Ada programmers. This function, in C, has the following prototype: FILE * popen (const char *COMMAND, const char *MODE) meaning that it takes two C-style "strings" and returns a pointer to a FILE structure. If there is an error, popen returns a null pointer. I've provided a thick binding to this function below; instead of returning a special value (null), it raises an exception if something goes wrong. Also, the second parameter (MODE) is modelled by an Ada enumerated type because there is a finite set of acceptable values. Inside the Ada procedure Popen, there is a nested function called Popen_Native. This is a "thin" binding : it matches the C function exactly. So, as is usually the case, the "thick" binding is built on top of a "thin" binding; here, I hide the "thin" binding from the Ada programmer by nesting it into the "thick" binding. HTH with Interfaces.C; use Interfaces.C; with Interfaces.C_Streams; with System; with Ada.Text_IO; procedure Spawn_And_Read is type Popen_Mode is (Read, Write, Read_Write); Popen_Error : exception; procedure Popen (Command : in String; Mode : in Popen_Mode; Stream : out Interfaces.C_Streams.FILEs) -- Executes COMMAND in a subshell, and attaches STREAM to the -- subprocess' standard input and/or output. If MODE = Read, -- you can read the process' output from STREAM. If MODE = Write, -- you can write into the process' standard input with STREAM. -- if MODE = Read_Write, both operations are allowed. is function Popen_Native (Command, Mode : Interfaces.C.char_array) return Interfaces.C_Streams.FILEs; pragma Import (C, Popen_Native, "popen"); use type Interfaces.C_Streams.FILEs; begin case Mode is when Read => Stream := Popen_Native (To_C (Command), To_C ("r")); when Write => Stream := Popen_Native (To_C (Command), To_C ("w")); when Read_Write => Stream := Popen_Native (To_C (Command), To_C ("rw")); end case; if Stream = Interfaces.C_Streams.NULL_Stream then raise Popen_Error; end if; end Popen; procedure Pclose (Stream : Interfaces.C_Streams.FILEs); pragma Import (C, Pclose, "pclose"); Stream : Interfaces.C_Streams.FILEs; Buffer : String (1 .. 100); Bytes_Read : Interfaces.C_Streams.size_t; begin Popen (Command => "ls -lF /", Mode => Read, Stream => Stream); while Interfaces.C_Streams.Feof (Stream) = 0 loop Bytes_Read := Interfaces.C_Streams.fread (Buffer => Buffer'Address, Size => 1, Count => Buffer'Length, Stream => Stream); Ada.Text_IO.Put (Buffer (1 .. Integer (Bytes_Read))); end loop; Pclose (Stream); end Spawn_And_Read; -- Ludovic Brenta.