AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
AnCH CUtils library documentation

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() {
// C style write function (a socket, a C library callback, ...): here it collects data
std::string sink;
buffer.size = 1024; // internal buffer size
buffer.write = [&sink](const char* data, std::size_t size) {
sink.append(data, size);
return size; // number of bytes written
};
{
out << "answer=" << 42 << std::endl; // flushed through write function
}
// C style read function: returns 0 at end of data
std::string source = "1 2 3";
std::size_t offset = 0;
input.size = 2;
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;
in >> a >> b >> c;
std::cout << sink << a + b + c << std::endl;
return 0;
}