tags:

views:

71

answers:

3

Are there any widely used I/O stream abstraction layers for plain C?

By an I/O stream abstraction layer I mean any layer that at least allows the creation of custom read/write functions. For C++, there's the standard iostream and boost::iostreams. For glibc users, there's a possibility to use custom streams. These won't do any good if the requirement is to write portable C code.

It is better to use a library that is either widely used or easy to embed to the source code, or both.

SDL_RWops is an undocumented but widely used feature of SDL, and zziplib can use it. However, it does not make much sense to add a dependency to SDL just to get this feature.

GLib contains a GIOChannel abstraction, but the library is again quite large and they say that "support for Windows is only partially complete".

However, the above are not quite satisfactory for small libraries, such as decoders, file format readers and signal processors: they contain lots of unnecessary stuff and the LGPL licensing prevents embedding the relevant parts to non-GPL code.

+2  A: 

I think you answered your own question.

No, there are no widely used steam abstraction libraries. Those that exist are usually a small part of libraries that most people don't want to depend on, or are very specialized.

What are your requirements? What is it you are looking for out of an abstraction library? I have to admit that every time I've looked at one, or started writing one, I usually end up back at the standard POSIX interfaces... what more abstraction could one want?

Chris Arguin
Currently I'm writing a file format reader library that must be as reusable and versatile as possible, i.e. the users may want to write custom input streams to support loading files from memory, network, archives, etc. I just hate having to reinvent the wheel, i.e. writing my own little SDL_RWops. If zlib or libpng didn't provide an interface to specify user's own fread and memory allocation functions, people would be use them less.I guess I'm either going to write my own or do the library in C++ using iostreams and provide a C interface.
ahnurmi
Although I disagree - sometimes more abstraction is needed - today "there are no widely used stream abstraction libraries" seems to be the correct answer.
ahnurmi
+1  A: 

The BIO abstraction in OpenSSL sounds like it fits the bill.

caf
Interesting. It's a quite difficult dependency; it has an Apache-like license and it contains networking stuff which may not be needed by all applications. Looks like it's well designed, though.
ahnurmi
+1  A: 

There is libslack (GNU GPL) which may provide some of the functionality you are after and this MIT License-ed input stream wrapper:

http://attractivechaos.wordpress.com/2008/10/11/a-generic-buffered-stream-wrapper/

fullgo
libslack seems to be Unix-only and GPL licensed. I couldn't figure out at a glance what that kstream actually does, and the example was unix-specific. But thanks, both are quite interesting.
ahnurmi