Some simple tools for input and output operations
All tools are header only. Add the include directory to your header search path.
Provided C++11 tools:
- Input parsing helpers
- I/O operator for C arrays
- I/O operator for
std::array
- I/O operator for
std::vector
- Mask all non-printable and non-ascii characters in a string
Provided C++17 tools:
- Convert sequences to strings
- Variadic output via
std::ostringstream
- Variadic output via
std::ostringstream
with delimiter
The stream in the C++ standard library don't support char8_t
, char16_t
and char32_t
!
https://stackoverflow.com/questions/8168997/using-char16-t-and-char32-t-in-i-o
We don't use wchar_t
in our code. Because of that we support char
only in the io_tools library functions.
The library is header-only but you can install it via cmake
.
If you want to install it for all users:
cmake /path/to/io_tools
make
make install
If you want to install it without root permissions:
cmake -DCMAKE_INSTALL_PREFIX=$HOME/.local /path/to/io_tools
make
make install
When you installed the library, you can use find_package(io_tools)
in your CMakeLists.txt
. It will define ${io_tools_INCLUDE_DIR}
which you can use in include_directories
to add the installed library to your header search path.
If you installed it with CMAKE_INSTALL_PREFIX
, you have to use CMAKE_PREFIX_PATH
by configuring your own project, otherwise find_package(io_tools)
will not find io_tools
.
cmake -DCMAKE_PREFIX_PATH=$HOME/.local /path/to/your/project
make
cmake -DIO_TOOLS_BUILD_TESTS=ON /path/to/io_tools
make
Instead of IO_TOOLS_BUILD_TESTS
which builds all tests, you can test for specific C++ standards by:
IO_TOOLS_BUILD_TESTS_CPP11
IO_TOOLS_BUILD_TESTS_CPP14
IO_TOOLS_BUILD_TESTS_CPP17
Returns if the next character in is
is expected
. Returns false
if not std::ios_base::goodbit
was set in is
.
The streams iostate
is never changed by is_next
!
Same as io_tools::is_next
but the character is extracted from the stream if it matchs.
Same as io_tools::extract_if_is
but the std::ios_base::failbit
is set on is
if extraction failed.
Note that expected
can be any type with overloaded input operator.
This dosn't work with arrays of size 0
!
For both input and output streams the I/O operators are overloaded. In include io_tools/io_c_array.hpp
/io_tools/io_std_array.hpp
.
Array format as string is: '{' element *(',' element) '}'
.
For input operations the element count must match the array size.
Then you need to import the overloads into your scope:
using namespace ::io_tools::io_c_array;
using namespace ::io_tools::io_std_array;
#include <io_tools/io_c_array.hpp>
#include <sstream>
void output_example(){
using namespace ::io_tools::io_c_array;
int array[2] = {1, 2};
std::ostringstream os;
os << array; // os.str() == "{1,2}"
}
void input_example(){
using namespace ::io_tools::io_c_array;
int array[2] = {};
std::istringstream is("{1,2}");
is >> array; // array[0] == 1 && array[1] == 2
}
#include <io_tools/io_c_array.hpp>
#include <sstream>
void output_example(){
using namespace ::io_tools::io_std_array;
std::array< int, 2 > array{{1, 2}};
std::ostringstream os;
os << array; // os.str() == "{1,2}"
}
void input_example(){
using namespace ::io_tools::io_std_array;
std::array< int, 2 > array{};
std::istringstream is("{1,2}");
is >> array; // array[0] == 1 && array[1] == 2
}
For both input and output streams the I/O operators are overloaded. In include io_tools/io_std_vector.hpp
.
Array format as string is: '{' element *(',' element) '}'
.
The std::vector
size is automatically adjusted during the input operation.
Then you need to import the overloads into your scope:
using namespace ::io_tools::io_std_vector;
#include <io_tools/io_std_vector.hpp>
#include <sstream>
void output_example(){
using namespace ::io_tools::io_std_vector;
std::vector< int > array{1, 2};
std::ostringstream os;
os << array; // os.str() == "{1,2}"
}
void input_example(){
using namespace ::io_tools::io_std_vector;
std::vector< int > array;
std::istringstream is("{1,2}");
is >> array; // array.size() == 2 && array[0] == 1 && array[1] == 2
}
This tools prints every element of a sequence into a string separated by delimiter
. A sequence is an object for which you can iterate from begin to end. The print is implemented as output on an std::ostringstream
.
#include <io_tools/range_to_string.hpp>
#include <iostream>
#include <vector>
int main(){
std::vector< int > s1{1, 2, 3};
std::cout << range_to_string(s1) << '\n'; // "1, 2, 3"
int s1[3] = {1, 2, 3};
std::cout << range_to_string(s2, "x") << '\n'; // "1x2x3"
}
Prints every given argument on an std::ostringstream
. By default std::boolalpha
is enabled.
#include <io_tools/make_string.hpp>
#include <iostream>
int main(){
std::cout << io_tools::make_string(0,3, "test", 5, '\n'); // "0.3test5\n"
}
Prints every given argument on an std::ostringstream
separated by delimiter
. By default std::boolalpha
is enabled.
#include <io_tools/make_string.hpp>
#include <iostream>
int main(){
std::cout << io_tools::make_string_separated_by(
"-:-", 0,3, "test", 5, '\n'); // "0.3-:-test-:-5-:-\n"
}
This functions replaces any non-printable and non-ascii character in the string with its hex code or escape sequence version. The hex code is in uppercase and starts with \x
. (E.g. \x0F
) The following escape sequences are used instead of the hex code: \a
, \b
, \f
, \n
, \r
, \t
and \v
. Backslashes are replaced by two backslashes. (\
-> \\
)
https://en.cppreference.com/w/cpp/language/escape
This is done byte wise and works also for UTF-8 encoded text because UTF-8 encoded characters are required that any byte in the character is bigger then 127 which makes any byte non-ascii.
#include <io_tools/mask_non_print.hpp>
#include <iostream>
int main(){
std::cout << io_tools::mask_non_print("»test«\n"); // "\\xC2\\xBBtest\\xC2\\xAB\\n"
}
This tool is not well designed and should be replaced by a better one.
Don't use it!
TODO
TODO
TODO
This software was originally developed privately by Benjamin Buch. All changes are released under the Boost Software License - Version 1.0 and published on GitHub.