#pragma once
#include "common.hpp"
typedef struct { char buf[BUF_SIZE]; size_t len; } readline_ctx_t;
typedef bool (*readline_handler)(char*, size_t, intptr_t);
tristate_t readline_r(int fd, char sep, readline_ctx_t*, readline_handler, intptr_t=0); // for non-blocking i/o, the corresponding ctx has to be passed, otherwise, an internal one will be used
bool readline_r(char*, size_t, char sep, readline_handler, intptr_t=0);
int file_open(const char*, char mode, bool nonblock); ///< (non-)blocking open; mode 'w': replacing existent files (not truncating), creating dirs; mode 'r': for reading
off_t file_size(int); ///< file size or 0 upon error
bool set_blocking(int, bool);
bool has_data(int, msec_t); ///< select()s for reading on fd and returns false if the timeout elapsed
tristate_t separator_write(int, const char*, size_t, const char* =NULL, size_t=0, const char* =NULL, size_t=0); ///< write given string(s) with separators. depending on fd, it might block or return TRI_NONE.
tristate_t separator_read(int, bool (*)(int, char*, size_t), msec_t tout=0); ///< reads from fd and calls handler for each separated string. depending on fd, it will block or return TRI_NONE if there is nothing to read.