Introduction
AnCH CUtils library provides a gateway between C and C++: C++ streams over C style read and write functions.
Prerequisites
AnCH CUtils is a standalone library.
Installation
CMake target anch::cutils:
find_package(Anch REQUIRED)
target_link_libraries(my-app PRIVATE anch::cutils)
See installation.
Conception
An cbuffer describes a buffer size and C style read and write functions (they return the number of bytes read or written ; read returns 0 at end of data). CIStream, COStream and CIOStream are standard streams over them: partial writes are retried and write errors set the stream bad bit. anch-network sockets are built on them.
Examples
#include "cutils/iostream.hpp"
#include <iostream>
#include <string>
int
main() {
std::string sink;
buffer.
write = [&sink](
const char* data, std::size_t size) {
sink.append(data, size);
return size;
};
{
out << "answer=" << 42 << std::endl;
}
std::string source = "1 2 3";
std::size_t offset = 0;
input.
read = [&](
char* data, std::size_t size) {
std::size_t nb = std::min(size, source.size() - offset);
source.copy(data, nb, offset);
offset += nb;
return nb;
};
int a, b, c;
std::cout << sink << a + b + c << std::endl;
return 0;
}