#import mapi.h

nkm1 t4nk074 at openmailbox.org
Wed Mar 21 18:42:43 UTC 2018


On Wednesday, 21 March 2018 at 16:22:45 UTC, Martin Tschierschke 
wrote:
> Is there an step by step introduction how to convert a C header 
> of an external lib into the right extern(C){} block?
>
> A blog post or tutorial, or chapter in a D book? (I have those 
> from Packt Publishing)
>
> (Especially I am trying to get this used with D:
> Montetdb C-API 
> https://www.monetdb.org/Documentation/Manuals/SQLreference/Programming/MAPI
> With: 
> https://github.com/snaga/monetdb/blob/master/clients/mapilib/mapi.h)
>
> The page: https://dlang.org/spec/interfaceToC.html is known, 
> but not detailed enough for me.

The easiest thing to do is to write a wrapper in C. The wrapper 
will include all necessary headers and provide some easy to use 
functions that you can call from D. Of course, you'll need to use 
a C compiler to compile it.
Anyway, this header looks very straightforwar, no particular 
tricks with the preprocessor. It should be something like this 
(untested, obviously):

import core.stdc.stdio : FILE;

enum
{
     MAPI_AUTO      = 0,  /* automatic type detection */
     MAPI_TINY      = 1,
     MAPI_UTINY     = 2,
     MAPI_SHORT     = 3,
     MAPI_USHORT    = 4,
     MAPI_INT       = 5,
     MAPI_UINT      = 6,
     MAPI_LONG      = 7,
     MAPI_ULONG     = 8,
     MAPI_LONGLONG  = 9,
     MAPI_ULONGLONG = 10,
     MAPI_CHAR      = 11,
     MAPI_VARCHAR   = 12,
     MAPI_FLOAT     = 13,
     MAPI_DOUBLE    = 14,
     MAPI_DATE      = 15,
     MAPI_TIME      = 16,
     MAPI_DATETIME  = 17,
     MAPI_NUMERIC   = 18,
}

enum int PLACEHOLDER = '?';

enum
{
     MAPI_SEEK_SET = 0,
     MAPI_SEEK_CUR = 1,
     MAPI_SEEK_END = 2,
}

enum
{
     MAPI_TRACE      = 1,
     MAPI_TRACE_LANG = 2,
}

alias MapiMsg = int;

enum
{
     MOK         =  0,
     MERROR      = -1,
     MTIMEOUT    = -2,
     MMORE       = -3,
     MSERVER     = -4,
}

enum
{
     LANG_MAL    = 0,
     LANG_SQL    = 2,
     LANG_JAQL   = 3,
}

/* prompts for MAPI protocol */
enum int PROMPTBEG = '\001'; /* start prompt bracket */

  /* prompt: ready for new query */
const(char)* PROMPT1 = "\001\001\n".ptr;

/* prompt: more data needed */
const(char)* PROMTP2 = "\001\002\n".ptr;

/*
  * The table field information is extracted from the table headers
  * obtained from the server. This list may be extended in the 
future.
  * The type of both the 'param' and 'binding'
  * variables refer to their underlying C-type. They are used for
  * automatic type coercion between back-end and application.
  */
struct MapiStruct;
alias Mapi = MapiStruct*;

/* this definition is a straight copy from 
sql/include/sql_query.h */
enum
{
     Q_PARSE     = 0,
     Q_TABLE     = 1,
     Q_UPDATE    = 2,
     Q_SCHEMA    = 3,
     Q_TRANS     = 4,
     Q_PREPARE   = 5,
     Q_BLOCK     = 6,
}

struct MapiStatement;
alias MapiHdl = MapiStatement*;

alias mapi_uint64 = ulong;
alias mapi_int64 = long;

/* three structures used for communicating date/time information 
*/
/* these structs are deliberately compatible with the ODBC 
versions
    SQL_DATE_STRUCT, SQL_TIME_STRUCT, and SQL_TIMESTAMP_STRUCT */

/* used by MAPI_DATE */
struct MapiDate
{
     short year;
     ushort month;
     ushort day;
}

/* used by MAPI_TIME */
struct MapiTime
{
     ushort hour;
     ushort minute;
     ushort second;
}

/* used by MAPI_DATETIME */
struct MapiDateTime
{
     short year;
     ushort month;
     ushort day;
     ushort hour;
     ushort minute;
     ushort second;
     uint fraction;  /* in 1000 millionths of a second (10e-9) */
}

/* connection-oriented functions */
extern (C)
{
     Mapi mapi_mapi(const char *host,
                    int port,
                    const char *username,
                    const char *password,
                    const char *lang,
                    const char *dbname);


     // and so on...
}


More information about the Digitalmars-d-learn mailing list