bfg: factored out stream opening code

Will create wrapper methods around stream classes that check stream
state and throw proper exceptions with messages from strerror.
Mrmpf: will switch to C API. C++ streams are so horrible for file I/O, it's no fun anymore.