-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Conversation
I'm not thrilled with the idea of adding three exports for this, but yes, we obviously need to be able to do this. |
I hear you. I suppose one option would be to keep this as an unexported interface. |
👍 was just looking for a way to do this for libpng. |
Can you guess what prompted this? :-) |
How about having a |
That's prettier. It will still be 2 exports, you're OK with that? |
That's only one export: |
Duh. Of course you're right. |
Calling it |
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. |
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. |
Add IO->FILE* for interfacing with C functions
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 anIOStream
to aFILE*
, if possible. However, there might be performance issues arising from thefseek
, 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:That seems like a bug to me. If you work around it with
then there is no segfault.