129 lines
2.8 KiB
C
129 lines
2.8 KiB
C
#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* recv_msg_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* recv_msg_pipe_io(pipe_io* pipeio) {
|
|
if (!check_pipe_io(pipeio)) {
|
|
return NULL;
|
|
}
|
|
return recv_msg_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);
|
|
}
|