caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Is this C-binding correct?
@ 2013-04-08 13:22 Matthieu Dubuget
  2013-04-08 13:42 ` [Caml-list] " Matthieu Dubuget
  0 siblings, 1 reply; 2+ messages in thread
From: Matthieu Dubuget @ 2013-04-08 13:22 UTC (permalink / raw)
  To: caml-list

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);
}


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Caml-list] Re: Is this C-binding correct?
  2013-04-08 13:22 [Caml-list] Is this C-binding correct? Matthieu Dubuget
@ 2013-04-08 13:42 ` Matthieu Dubuget
  0 siblings, 0 replies; 2+ messages in thread
From: Matthieu Dubuget @ 2013-04-08 13:42 UTC (permalink / raw)
  To: caml-list

Matthieu Dubuget a écrit :
> Thanks for your advices
>

This second try, using another function seems safer to me. But… is it?

#include "mdbtools.h"

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


value add_line(value datas,  MdbHandle *mdb, MdbTableDef *table, char 
**bound_values){
   CAMLparam1(datas);
   CAMLlocal2(my_line,new_datas);
   int i;
   MdbColumn *col;
   char *va;
   size_t length;

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

     for (i=0; i<table->num_cols; i++){
       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);
     CAMLreturn(new_datas);
}


  __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;
   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))
     datas = add_line(datas, mdb, table, bound_values);
   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);
}


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-04-08 13:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-04-08 13:22 [Caml-list] Is this C-binding correct? Matthieu Dubuget
2013-04-08 13:42 ` [Caml-list] " Matthieu Dubuget

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).