Implement small C header only client library

This commit is contained in:
Kristóf Tóth 2019-04-04 16:25:47 +02:00
parent 1425dc1fa0
commit 05e397f142
2 changed files with 139 additions and 0 deletions

128
clients/c_cpp/pipe_io.h Normal file
View File

@ -0,0 +1,128 @@
#pragma once
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
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);
}

11
clients/c_cpp/test.c Normal file
View File

@ -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);
}