From 05e397f142816003a08cc77a6cd39bf84df061d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 4 Apr 2019 16:25:47 +0200 Subject: [PATCH] Implement small C header only client library --- clients/c_cpp/pipe_io.h | 128 ++++++++++++++++++++++++++++++++++++++++ clients/c_cpp/test.c | 11 ++++ 2 files changed, 139 insertions(+) create mode 100644 clients/c_cpp/pipe_io.h create mode 100644 clients/c_cpp/test.c diff --git a/clients/c_cpp/pipe_io.h b/clients/c_cpp/pipe_io.h new file mode 100644 index 0000000..9566ae7 --- /dev/null +++ b/clients/c_cpp/pipe_io.h @@ -0,0 +1,128 @@ +#pragma once +#include +#include +#include +#include + + +typedef struct pipe_reader { + FILE* stream; + char* line_buf; + size_t buf_size; +} pipe_reader; + +pipe_reader new_pipe_reader(const char* pipe_path) { + pipe_reader preader; + preader.stream = fopen(pipe_path, "r"); + preader.line_buf = NULL; + preader.buf_size = 0; + return preader; +} + +bool check_pipe_reader(const pipe_reader* preader) { + if (!preader || preader->stream == NULL) { + return false; + } + return true; +} + +const char* read_line_pipe_reader(pipe_reader* preader) { + if (!check_pipe_reader(preader)) { + return NULL; + } + ssize_t read = getline(&preader->line_buf, &preader->buf_size, preader->stream); + if (read > 0) { + preader->line_buf[read - 1] = '\0'; + return preader->line_buf; + } + return NULL; +} + +void close_pipe_reader(pipe_reader* preader) { + if (!check_pipe_reader(preader)) { + return; + } + fclose(preader->stream); + preader->stream = NULL; + if (preader->line_buf) { + free(preader->line_buf); + preader->line_buf = NULL; + } +} + + +typedef struct pipe_writer { + FILE* stream; +} pipe_writer; + +pipe_writer new_pipe_writer(const char* pipe_path) { + pipe_writer pwriter; + pwriter.stream = fopen(pipe_path, "w"); + return pwriter; +} + +bool check_pipe_writer(const pipe_writer* pwriter) { + if (!pwriter || pwriter->stream == NULL) { + return false; + } + return true; +} + +bool send_msg_pipe_writer(pipe_writer* pwriter, const char* msg) { + if (!check_pipe_writer(pwriter)) { + return false; + } + fprintf(pwriter->stream, "%s\n", msg); + fflush(pwriter->stream); + return true; +} + +void close_pipe_writer(pipe_writer* pwriter) { + if (!check_pipe_writer(pwriter)) { + return; + } + fclose(pwriter->stream); + pwriter->stream = NULL; +} + + +typedef struct pipe_io { + pipe_reader preader; + pipe_writer pwriter; +} pipe_io; + +pipe_io new_pipe_io(const char* in_pipe_path, const char* out_pipe_path) { + pipe_io pipeio; + pipeio.preader = new_pipe_reader(in_pipe_path); + pipeio.pwriter = new_pipe_writer(out_pipe_path); + return pipeio; +} + +bool check_pipe_io(const pipe_io* pipeio) { + if (!pipeio) { + return false; + } + return (check_pipe_reader(&pipeio->preader) && check_pipe_writer(&pipeio->pwriter)); +} + +const char* read_line_pipe_io(pipe_io* pipeio) { + if (!check_pipe_io(pipeio)) { + return NULL; + } + return read_line_pipe_reader(&pipeio->preader); +} + +bool send_msg_pipe_io(pipe_io* pipeio, const char* msg) { + if (!check_pipe_io(pipeio)) { + return false; + } + return send_msg_pipe_writer(&pipeio->pwriter, msg); +} + +void close_pipe_io(pipe_io* pipeio) { + if (!check_pipe_io(pipeio)) { + return; + } + close_pipe_reader(&pipeio->preader); + close_pipe_writer(&pipeio->pwriter); +} diff --git a/clients/c_cpp/test.c b/clients/c_cpp/test.c new file mode 100644 index 0000000..020ec3a --- /dev/null +++ b/clients/c_cpp/test.c @@ -0,0 +1,11 @@ + #include "pipe_io.h" + + +int main() { + pipe_io pipeio = new_pipe_io("in", "out"); + const char* line; + while ((line = read_line_pipe_io(&pipeio))) { + send_msg_pipe_io(&pipeio, line); + } + close_pipe_io(&pipeio); +}