caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Matthieu Dubuget <matthieu.dubuget@gmail.com>
To: caml-list <caml-list@inria.fr>
Subject: [Caml-list] Is this C-binding correct?
Date: Mon, 08 Apr 2013 15:22:11 +0200	[thread overview]
Message-ID: <5162C483.2040409@gmail.com> (raw)

Hello,

I'm playing with some access datafile.
I just need to read some data from a small database.
There is a C library mdbtools, that allow to read them.
This C library is using glib.
Here is the C binding I wrote. It seems to works, but I'm not sure
this interface is correct.
Especially the "datas = new_datas;" line.

Thanks for your advices

Matt

The ML part:

external ex : string -> string -> string array list = "mdb_extract"

And the C one:

#include "mdbtools.h"

#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/memory.h>

__declspec(dllexport) value mdb_extract(value base, value tbl){
CAMLparam2(base, tbl);
CAMLlocal3(datas, new_datas, my_line);
MdbHandle *mdb;
MdbTableDef *table;
char **bound_values;
int *bound_lens;
char *va;
size_t length;
int i;

mdb = mdb_open(String_val(base), MDB_NOFLAGS);
table = mdb_read_table_by_name(mdb, String_val(tbl), MDB_TABLE);
mdb_read_columns(table);
mdb_rewind_table(table);
bound_values = (char **) g_malloc(table->num_cols * sizeof(char *));
bound_lens = (int *) g_malloc(table->num_cols * sizeof(int));
for (i=0;i<table->num_cols;i++){
bound_values[i] = (char *)g_malloc0(MDB_BIND_SIZE);
mdb_bind_column(table, i+1, bound_values[i], &bound_lens[i]);
};

/* Liste vide */
datas = Val_int(0);

while (mdb_fetch_row(table)){

/* Allocation d’un tableau */
my_line = caml_alloc(table->num_cols, 0);

for (i=0; i<table->num_cols; i++){
MdbColumn *col = g_ptr_array_index(table->columns,i);
if (col->col_type == MDB_OLE){
va = mdb_ole_read_full(mdb, col, &length);
} else {
va = bound_values[i];
};

Store_field(my_line, i, caml_copy_string(va));

if (col->col_type == MDB_OLE)
free(va);
}

new_datas = caml_alloc(2,0);
Store_field(new_datas, 0, my_line);
Store_field(new_datas, 1, datas);
datas = new_datas;
}

for (i=0;i<table->num_cols;i++) {
g_free(bound_values[i]);
}

g_free(bound_values);
g_free(bound_lens);

mdb_free_tabledef(table);
mdb_close(mdb);
CAMLreturn(datas);
}


             reply	other threads:[~2013-04-08 13:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-08 13:22 Matthieu Dubuget [this message]
2013-04-08 13:42 ` [Caml-list] " Matthieu Dubuget

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5162C483.2040409@gmail.com \
    --to=matthieu.dubuget@gmail.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).