Recently,I was trying to wrap libgit2's api.I have generated a wrapper with sed and c2nim.But it's very buggy and unusable.
libgit2 have plenty of definition in its head files like:
typedef struct git_commit git_commit;
And c2nim will ignore these,leaves a lots of undefined error.
In c code,it will use like this:
#include <stdlib.h>
#include <stdio.h>
#include "git2.h"
int main(){
git_repository *repo = NULL;
int er=git_repository_open(&repo, "./tmp/");
if(er!=0){
exit(1);
}
git_oid oid;
int error = git_oid_fromstr(&oid,"1da5570cc4153a7adfd01caaec666c6ae3a611d5");
if(error!=0){
exit(1);
}
git_commit *commit;
int err = git_commit_lookup(&commit, repo, &oid);
const char *message = git_commit_message(commit);
printf("%s",message);
}
But the nim version,I was confused with argument passing and variable define like git_commit.
#Notice the git_commit is not defined,and in
#https://libgit2.github.com/libgit2/#HEAD/group/commit/git_commit_lookup
#it was defined as double pointer.
proc git_commit_lookup*(commit:ptr ptr git_commit; repo: pointer; id: pointer) {. importc: "git_commit_lookup",dynlib:"libgit2.so",cdecl.}
proc git_repository_open(ou:pointer,path:cstring):cint {. importc: "git_repository_open",dynlib:"libgit2.so" .}
proc git_oid_fromstr*(ou:ptr git_oid,str:cstring):cint {.importc:"git_oid_fromstr",dynlib:"libgit2.so".}
var
repo:pointer
var err= git_repository_open(repo,"./tmp")
if err!=0:
quit(1)
var
id:git_oid
sha:cstring="1da5570cc4153a7adfd01caaec666c6ae3a611d5"
err=git_oid_fromstr(id.addr,sha)
if err!=0:
quit(1)
var
theCommit:ptr git_commit #type is not defined and I don't know what should I do.
discard git_commit_lookup(co.addr,repo,id.addr) #It panic!
typedef struct git_commit{} git_commit; I think then c2nim process it, and with some luck the result is useful.
What about typedef struct __git_commit git_commit;
Unfortunately, that is common, e.g. in htslib:
I use this:
#mangle __faidx_t foo_faidx_t
And it generates:
type
foo_faidx_t* {.importc: "__faidx_t", header: "htslib/faidx.h", bycopy.} = object
And I add "struct" manually, to this:
type
foo_faidx_t* {.importc: "struct __faidx_t", header: "htslib/faidx.h", bycopy.} = object