A boost-iostream filter for outputting multi-line strings with consistent indentation.
This little helper addresses two use cases for outputting text via C++ std::iostreams:
- Properly indented output of nested data structures using C++ stream insertion operators (i.e.
operator<<(struct&)) wether for ad-hoc debug output or text file formats like JSON and XML. - To a lesser extend the ability to output text-files or multi-line strings, i.e. strings containing new-lines (
\ncharacters) to a single, fixed indentation level.
The implementation is based on the Boost.Iostreams library and consists of a filter that replaces new-line characters with a new-line followed by a number of spaces according to indentation size (i.e. the number of spaces per indentation level) and the filter's current indentation level. The indentation size is specified at filter-instantiation time, the indentation size is controlled via a token-protocol using the zero-character (\0) as escape character, followed by either the character i or d for incrementing and decrementing the indentation level. The protocol tokens can be inserted into the character stream via four manipulators:
incmanipulator increases the filter's indentation level by onedecmanipulator decreases the filter's indentation level by oneincnincreases the filter's indentation level likeincfollowed by a new-line (\n)decndecreases the filter's indentation level likedecfollowed by a new-line (\n)
The filter class and the manipulator functions are header-file only, i.e. compilation is not required. To build the project's example and unit-tests, CMake is required. The CMakeLists.txt defines two path variables, one for the path to the boost include directory and one for the path to the boost link/library directory.
The example shows how the classes of a typical 3d scene graph could be written to an output stream for debugging purposes.