ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
writer.c
Go to the documentation of this file.
1
10#include <stdlib.h>
11#include <stdio.h>
12#include <string.h>
14
15typedef struct terminal_fd_writer_s {
16 FILE *fp; // File pointer created from FD
18
20 if (fd < 0) {
21 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid fd: %d", fd);
22 }
23
24 FILE *fp = platform_fdopen("terminal_fd_writer", fd, "w");
25 if (!fp) {
26 return SET_ERRNO_SYS(ERROR_INVALID_PARAM, "fdopen failed for FD %d", fd);
27 }
28
29 terminal_fd_writer_t *writer = SAFE_CALLOC(1, sizeof(*writer), terminal_fd_writer_t *);
30 writer->fp = fp;
31
32 log_debug("terminal_fd_writer: created with fd=%d", fd);
33 *out = writer;
34 return ASCIICHAT_OK;
35}
36
38 if (!writer) {
39 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid writer context");
40 }
41
42 if (!frame) {
43 return ASCIICHAT_OK; // No-op for NULL frame
44 }
45
46 // Write frame and newline
47 int ret = fprintf(writer->fp, "%s\n", frame);
48 if (ret < 0) {
49 return SET_ERRNO_SYS(ERROR_INVALID_PARAM, "fprintf failed");
50 }
51
52 // Flush to ensure output is visible immediately
53 int flush_ret = fflush(writer->fp);
54 if (flush_ret != 0) {
55 return SET_ERRNO_SYS(ERROR_INVALID_PARAM, "fflush failed");
56 }
57
58 log_debug_every(NS_PER_SEC_INT, "terminal_fd_writer: wrote frame (%zu bytes)", strlen(frame));
59 return ASCIICHAT_OK;
60}
61
63 if (!writer)
64 return;
65 if (writer->fp) {
66 fclose(writer->fp);
67 writer->fp = NULL;
68 }
69 SAFE_FREE(writer);
70}
Platform abstraction layer umbrella header providing unified cross-platform API.
#define SAFE_FREE(ptr)
Definition common.h:376
#define SAFE_CALLOC(count, size, cast)
Definition common.h:274
#define SET_ERRNO_SYS(code, context_msg,...)
Set error code with custom message and system error context, returning the error code.
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:49
@ ASCIICHAT_OK
Definition error_codes.h:51
@ ERROR_INVALID_PARAM
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:548
#define NS_PER_SEC_INT
Definition time.h:157
FILE * platform_fdopen(const char *name, int fd, const char *mode)
Convert file descriptor to stream (fdopen replacement)
📝 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:702
Cross-platform memory allocation utilities.
int frame
Definition splash.c:99
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:19
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:37
void terminal_fd_writer_destroy(terminal_fd_writer_t *writer)
Destroy terminal FD writer.
Definition writer.c:62
Write ANSI ASCII frames to file descriptor.