Refactor file handling code into modular RAII'd stuff.

Signed-off-by: Peter Atashian <retep998@gmail.com>