ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
writer.c
Go to the documentation of this file.
1
9
12#include <ascii-chat/log/log.h>
13#include <stdlib.h>
14#include <stdio.h>
15#include <string.h>
16#include <libplatform/compiler.h>
17#include <libplatform/system.h>
18#include <libplatform/terminal.h>
20
21typedef struct terminal_fd_writer_s {
22 FILE *fp; // File pointer created from FD
24
26 if (fd < 0 || !out) {
27 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid writer parameters: fd=%d, output=%p", fd, (void *)out);
28 }
29
30 FILE *fp = platform_fdopen("terminal_fd_writer", fd, "w");
31 if (!fp) {
32 return SET_ERRNO_SYS(ERROR_INVALID_PARAM, "fdopen failed for FD %d", fd);
33 }
34
35 terminal_fd_writer_t *writer = SAFE_CALLOC(1, sizeof(*writer), terminal_fd_writer_t *);
36 writer->fp = fp;
37
38 log_debug("created with fd=%d", fd);
39 *out = writer;
40 return ASCIICHAT_OK;
41}
42
44 if (!writer) {
45 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid writer context");
46 }
47
48 if (!frame) {
49 return ASCIICHAT_OK; // No-op for NULL frame
50 }
51
52 // Write frame and newline
53 asciichat_error_t err = stdout_writer_write(writer->fp, frame, strlen(frame));
54 if (err != ASCIICHAT_OK)
55 return err;
56 err = stdout_writer_write(writer->fp, "\n", 1);
57 if (err != ASCIICHAT_OK)
58 return err;
59
60 log_debug_every(NS_PER_SEC_INT, "wrote frame (%zu bytes)", strlen(frame));
61 return ASCIICHAT_OK;
62}
63
65 if (!writer)
66 return;
67 if (writer->fp) {
68 fclose(writer->fp);
69 writer->fp = NULL;
70 }
71 SAFE_FREE(writer);
72}
#define SET_ERRNO(code,...)
#define SET_ERRNO_SYS(code,...)
#define SAFE_FREE(ptr)
Definition common.h:377
#define SAFE_CALLOC(count, size, cast)
Definition common.h:275
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_INVALID_PARAM
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:595
#define NS_PER_SEC_INT
Definition time.h:169
📝 Logging API with multiple log levels and terminal output control
#define log_debug_every(interval_us, fmt,...)
Rate-limited DEBUG logging.
Definition log/log.h:749
static bool size_t gotel_query_t * out
Definition queries.h:8
asciichat_error_t stdout_writer_write(FILE *stream, const void *data, size_t size)
🔤 String Manipulation and Shell Escaping Utilities
asciichat_error_t terminal_fd_writer_create(int fd, terminal_fd_writer_t **out)
Create a terminal FD writer to a file descriptor.
Definition writer.c:25
struct terminal_fd_writer_s terminal_fd_writer_t
asciichat_error_t terminal_fd_writer_write(terminal_fd_writer_t *writer, const char *frame)
Write a frame to the file descriptor.
Definition writer.c:43
void terminal_fd_writer_destroy(terminal_fd_writer_t *writer)
Destroy terminal FD writer.
Definition writer.c:64
Write ANSI ASCII frames to file descriptor.