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] Re: Is this C-binding correct?
Date: Mon, 08 Apr 2013 15:42:24 +0200	[thread overview]
Message-ID: <5162C940.9050507@gmail.com> (raw)
In-Reply-To: <5162C483.2040409@gmail.com>

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


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

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

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=5162C940.9050507@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).