Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IO->FILE* for interfacing with C functions #5053

Merged
merged 1 commit into from
Dec 8, 2013
Merged

Add IO->FILE* for interfacing with C functions #5053

merged 1 commit into from
Dec 8, 2013

Conversation

timholy
Copy link
Sponsor Member

@timholy timholy commented Dec 7, 2013

In particular, I've found fdopen useful when interfacing with C libraries that do I/O. For example, suppose you need to parse the magic bytes of the file to check that it's of the type you expect (which you might do in Julia), then hand off I/O to a C function.

Rather than implementing this functionality multiple times in different packages, it seemed like it might be time to consider standardizing such functionality. Alternatively, we could have a CIO.jl package.

This has only been tested on Linux, although I tried to write it in a way that should work on all platforms.

It might be prettiest to make ccall automatically convert an IOStream to a FILE*, if possible. However, there might be performance issues arising from the fseek, so it might be better not to make this too easy.

Finally, the main reason this is a WIP: if the stream is opened in mode "w", fdopen segfaults. HOWEVER, that seems to be because of this odd behavior:

julia> s = open("/tmp/test.dat", "w")
IOStream(<file /tmp/test.dat>)

julia> isreadable(s)
true

That seems like a bug to me. If you work around it with

julia> FILEp = fdopen(fd(s), "w")
Ptr{Void} @0x00000000048a3950

then there is no segfault.

@StefanKarpinski
Copy link
Sponsor Member

I'm not thrilled with the idea of adding three exports for this, but yes, we obviously need to be able to do this.

@timholy
Copy link
Sponsor Member Author

timholy commented Dec 7, 2013

I hear you. I suppose one option would be to keep this as an unexported interface. ccalls are kindof black-diamond territory, anyway.

@ihnorton
Copy link
Member

ihnorton commented Dec 7, 2013

👍 was just looking for a way to do this for libpng.

@timholy
Copy link
Sponsor Member Author

timholy commented Dec 7, 2013

Can you guess what prompted this? :-)

@StefanKarpinski
Copy link
Sponsor Member

How about having a CFileHandle type and providing a conversion from an open IO object to that. Then you can override seek and tell methods for that type.

@timholy
Copy link
Sponsor Member Author

timholy commented Dec 7, 2013

That's prettier. It will still be 2 exports, you're OK with that?

@StefanKarpinski
Copy link
Sponsor Member

That's only one export: CFileHandle. What's the other one?

@timholy
Copy link
Sponsor Member Author

timholy commented Dec 7, 2013

Duh. Of course you're right.

@StefanKarpinski
Copy link
Sponsor Member

Calling it CFILE or C_FILE might be better.

@timholy
Copy link
Sponsor Member Author

timholy commented Dec 7, 2013

It's a pointer to a FILE struct, not a FILE struct. CptrFILE? CFILEptr?

But I like the idea of having FILE in the name, it makes it greppable.

@timholy
Copy link
Sponsor Member Author

timholy commented Dec 7, 2013

OK, I went with CFILE. The OS is allocating one, so it's not as misleading as I initially suggested.

Once #5058 is deemed suitable, this should be safe and ready to merge.

timholy added a commit that referenced this pull request Dec 8, 2013
Add IO->FILE* for interfacing with C functions
@timholy timholy merged commit b516081 into JuliaLang:master Dec 8, 2013
@timholy timholy deleted the fdopen branch December 8, 2013 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants