I think you won't be able to use with-out-parameters which seems to work
really well with value types, but maybe not so well for your example.  Part
of the problem might be the way c-string automatically encodes and decodes
using your encoding (utf8 by default I think).  Factor strings are not the
same as char*, so they have have to be converted into and out of "raw
memory" using an encoding when calling functions that take c-strings.

Here's an example using strcpy for convenience, and raw memory (using char*
instead of c-string).  We will try and preserve the dst input pointer
instead of using the return value.

     FUNCTION: char* strcpy ( char* dst, char* src ) ;

We will make a word that takes a Factor string, encodes it into a utf8
byte-array, allocates a new byte-array destination, then calls strcpy,
drops the return value, and uses the destination byte-array to decode back
into a Factor string.

     : do-strcpy ( src -- dst )
         utf8 string>alien
         [ length <byte-array> dup ] [ strcpy drop ] bi
         utf8 alien>string ;

     IN: scratchpad "hello" do-strcpy .
     "hello"

You can use byte-arrays, or raw memory allocated with malloc (make sure to
&free with destructors, or free manually).







On Fri, Nov 28, 2014 at 8:14 AM, Andrea Ferretti <ferrettiand...@gmail.com>
wrote:

> I am trying to interface with C code. Everything seems to work more or
> less fine, until I have to deal with output parameters, that is,
> parameters in C functions which are meant to be passed buffers which
> are filled by the function.
>
> I have made a very basic example which only copies a string and I have
> made a shared library called "libexample.so" which exports
>
>   void example_cp(char *in, char *out);
>
> I have verified that I can call the shared library from C.
>
> Now in factor I have defined an interface like this
>
>   USING: alien alien.c-types alien.syntax alien.libraries ;
>   IN: example-alien
>
>   <<
>     "libexample" "/home/papillon/esperimenti/ssl/libexample.so" cdecl
> add-library
>   >>
>
>   LIBRARY: libexample
>   FUNCTION: void example_cp ( c-string in, c-string out ) ;
>
> In the listener I try to use example_cp, but I am not sure how to pass
> a preallocated char buffer. If I try
>
>   { { c-string } } [ "hello world" swap example_cp ] with-out-parameters
>
> I get "index out of bounds: 0" which seems reasonable, since I pass an
> empty c-string. But if I try something like
>
>   { { c-string initial: "hello earth" } } [ "hello world" swap
> example_cp ] with-out-parameters
>
> I get "local-allocation-error".
>
> What is the correct way to pass a preallocated buffer?
>
>
> ------------------------------------------------------------------------------
> Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
> from Actuate! Instantly Supercharge Your Business Reports and Dashboards
> with Interactivity, Sharing, Native Excel Exports, App Integration & more
> Get technology previously reserved for billion-dollar corporations, FREE
>
> http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to