Re: an admittedly lazy programmer question...

  • From: "J.-F. Cap" <jfdotcap.ml@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 29 May 2012 10:19:11 +0200

2012/5/28 William Adams <william_a_adams@xxxxxxx>
>
>
> I am doing some interop, and the C function looks like this:
>
>
>
> void somefunc(FILE *f)
>
>
>
> Meaning, it expects to the a FILE structure.  Typically because it's going to 
> do the reading from the file on its own.  I really don't like these kinds of 
> interfaces, but there you have it.
>
>
>
> Question:
>
>
>
> Would LuaJIT ffi do anything for me to translate from Luas io to this FILE 
> structure?  Can I simply do:
>
>
>
> fh = io.open(file)
>
> somefunc(fh)
>
>
>
> And have it work?  Assuming I've defined FILE in some FFI place.
>
>
>
> Alternatively, I could redefine the function signature:
>
>
>
> void somefunc(void *f)
>
>
>
> But still, the question remains, is the io.open returning something I could 
> feed to this function in some way?
>
>
>
> -- William
>
>
>
> ===============================
>
> - Shaping clay is easier than digging it out of the ground.
>
>
> http://williamaadams.wordpress.com
> http://www.thingiverse.com/WilliamAAdams
> https://github.com/Wiladams


This works here (i386 GNU/Linux).



ffi.cdef[[

struct _IO_FILE;
typedef struct _IO_FILE FILE;

size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);

typedef struct {
  FILE *fp;             /* File handle. */
  uint32_t type;        /* File type. */
} IOFileUD;

]]

local function fwrite(file,offset,data)
             local f=assert(type(file)=='userdata' and ffi.cast('IOFileUD 
*',file))
             local fp=f.fp
             ffi.C.fwrite(...,...,...,fp)
          end

...

local file=io.open(....)

fwrite(file, ....)

file:close()



JF

Other related posts: