On 21 Mar 2005 at 17:54, Waldek Hebisch wrote:
Prof A Olowofoyeku wrote:
Hi
I have run into some problems with the translation of libplot.
<nip> > or something else? > > The issue here seems to be the precise equivalent of the C "FILE" > struct. I believe that the library wants to write to these "FILE *" > stuff. >
C "FILE" is an opaque pointer. Opaque means that all operations on it should go trough C library. It is completly different than Pascal file. AFAICS GPC has no support for C "FILE". In practice you can write:
type CFile = pointer;
You may use some tricks to ensure that all operations on CFile go trough library (like wrapping it iside a record or declaring it restricted). To really use it you need declarations for libc functions. Minimally:
function fopen(name : CString; mode CString): CFile; external name 'fopen'; function fclose(f : CFile) : CInteger; external name 'fclose';
[...]
It is the libplot library itself that wants to write to these file pointers. It turns out that was I needed was a way to pass to the libplot routines pointers to the actual libc "stdin", "stdout" and "stderr", as opposed to Pascal implementations. I achieved it by writing a small C wrapper ("plotc.c"), with these:
#include <stdio.h> FILE *libc_stdin (void); FILE *libc_stdin (void) {return stdin;}
FILE *libc_stdout (void); FILE *libc_stdout (void) {return stdout;}
FILE *libc_stderr (void); FILE *libc_stderr (void) {return stderr;}
Then in the Pascal unit, I have "{$L plotc.c}", and then I declare these: Type TPlotFile = Pointer; Function libc_stdin : TPlotFile; external name 'libc_stdin'; Function libc_stdout : TPlotFile; external name 'libc_stdout'; Function libc_stderr : TPlotFile; external name 'libc_stderr';
So in the calling program I can pass the libc_std* to libplot routines, and everyone seems happy ...
It probably makes more sense to use external variables than external functions, but trying to make the libc_std* stuff into variables caused GPFs. I am not too bothered to try to sort it out, as the functions work perfectly well.
Best regards, The Chief -------- Prof. Abimbola A. Olowofoyeku (The African Chief) web: http://www.greatchief.plus.com/