www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Further questions on interfacing to Postgres libpq

reply Joe <jma freedomcircle.com> writes:
An example test program that I'm using to learn D to C 
interfacing (specifically calling the libpq library) has a call 
to a C function declared as follows:

     void PQprint(FILE *fout,      /* output stream */
                  const PGresult *res,
                  const PQprintOpt *po);

PQprintOpt is a struct whose first six members are declared as 
'pqbool' which is in turn declared as "typedef char pqbool;" in 
the distributed Postgres header file. I've defined an "alias 
pqbool = char;" in the D file, which is pretty straightforward.

The second of the six members has the name "align", which is a D 
keyword. So I renamed it "align_" and I presume that won't cause 
any problems.

To deal with the first argument to PQprint, I added "import 
core.stdc.stdio : FILE;".  The question is how to pass the D 
"stdout" as that argument.  The D compiler tells me I can't pass 
it as is (as was done in C), because in D it's of type "File".
Jan 26 2018
parent rikki cattermole <rikki cattermole.co.nz> writes:
On 27/01/2018 5:11 AM, Joe wrote:
 An example test program that I'm using to learn D to C interfacing 
 (specifically calling the libpq library) has a call to a C function 
 declared as follows:
 
      void PQprint(FILE *fout,      /* output stream */
                   const PGresult *res,
                   const PQprintOpt *po);
 
 PQprintOpt is a struct whose first six members are declared as 'pqbool' 
 which is in turn declared as "typedef char pqbool;" in the distributed 
 Postgres header file. I've defined an "alias pqbool = char;" in the D 
 file, which is pretty straightforward.
Use ubyte, not char. char has a bunch of logic related to Unicode surrounding it which is clearly not the intent.
 The second of the six members has the name "align", which is a D 
 keyword. So I renamed it "align_" and I presume that won't cause any 
 problems.
You're good, layout+size just has to match not names of fields.
 To deal with the first argument to PQprint, I added "import 
 core.stdc.stdio : FILE;".  The question is how to pass the D "stdout" as 
 that argument.  The D compiler tells me I can't pass it as is (as was 
 done in C), because in D it's of type "File".
https://dlang.org/phobos/std_stdio.html#.File.getFP
Jan 26 2018