On Wed, Mar 9, 2016 at 1:05 PM, Jeffrey Sarnoff
<[email protected]> wrote:
> I am bound to the api, at least for calling into the application.  I don't
> know how to do either of the things you recommend.  Could you point me to an
> example?

See below. Note that the `open_memstream` API may not be widely
available on non-Linux. (Apparently it's aded to POSIX though...)
The reason you have to deal directly with C FILE* is that the C FILE*
API is not designed to be extensible (or at least there's no portable
API to do so). Therefore, you can't convert a opaque julia IO object
to a C FILE* and let C libraries write through it instead...

```
julia> p_cstdout = unsafe_load(cglobal(:stdout, Ptr{Void}))
Ptr{Void} @0x00007f6dcb81f600

julia> ccall(:fwrite, Csize_t, (Cstring, Csize_t, Csize_t, Ptr{Void}),
"aaa\n", 4, 1, p_cstdout)
aaa
0x0000000000000001

julia> ptr = Ref{Ptr{Void}}()
Base.RefValue{Ptr{Void}}(Ptr{Void} @0x000000000000000e)

julia> sz = Ref{Csize_t}()
Base.RefValue{UInt64}(0x00007f6bc942f5f0)

julia> strstm = ccall(:open_memstream, Ptr{Void}, (Ptr{Ptr{Void}},
Ptr{Csize_t}), ptr, sz)
Ptr{Void} @0x0000000002638f70

julia> ccall(:fwrite, Csize_t, (Cstring, Csize_t, Csize_t, Ptr{Void}),
"aaa\n", 4, 1, strstm)
0x0000000000000001

julia> ccall(:fwrite, Csize_t, (Cstring, Csize_t, Csize_t, Ptr{Void}),
"aaa\n", 4, 1, strstm)
0x0000000000000001

julia> sz[]
0x00007f6bc942f5f0

julia> ccall(:fclose, Cint, (Ptr{Void},), strstm)
0

julia> sz[]
0x0000000000000008

julia> pointer_to_array(Ptr{UInt8}(ptr[]), sz[], true)
8-element Array{UInt8,1}:
 0x61
 0x61
 0x61
 0x0a
 0x61
 0x61
 0x61
 0x0a

julia> ary = ans
8-element Array{UInt8,1}:
 0x61
 0x61
 0x61
 0x0a
 0x61
 0x61
 0x61
 0x0a

julia> bytestring(ary)
"aaa\naaa\n"
```

>
> On Wednesday, March 9, 2016 at 12:58:20 PM UTC-5, Yichao Yu wrote:
>>
>>
>> On Mar 9, 2016 12:38 PM, "Jeffrey Sarnoff" <[email protected]> wrote:
>> >
>> > I am trying to wrap this so it will (a) print to STDOUT (b) (if
>> > possible) print to a string:
>> > void arf_fprint(FILE * file, const arf_t x)ΒΆ
>> >
>> > Prints x as an integer mantissa and exponent to the stream file.
>> >
>> >
>> > arf_t is made with  arf(x), that works.
>> >
>> >
>> > this:
>> >
>> > function arf_fprint(x::arb)
>> >
>> >            a = arf(x); s=STDOUT
>> >
>> >            ccall((:arf_fprint, :libarb), Void, (Ptr{s},
>> > Ptr{arf_struct},), &s, &a)
>>
>> Julia does not use c FILE*, if you are bound to this api, you should
>> probably fetch the c stream with cglobal or construct a string stream with
>> platform dependent api.
>>
>> >
>> >        end
>> >
>> > does this:
>> > ERROR: TypeError: Ptr: in parameter, expected Type{T}, got Base.TTY
>> >  in arf_fprint at none:3
>> >

Reply via email to