ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
writer.c File Reference

Write ANSI ASCII frames to file descriptor. More...

Go to the source code of this file.

Data Structures

struct  terminal_fd_writer_s
 

Typedefs

typedef struct terminal_fd_writer_s terminal_fd_writer_t
 

Functions

asciichat_error_t terminal_fd_writer_create (int fd, terminal_fd_writer_t **out)
 Create a terminal FD writer to a file descriptor.
 
asciichat_error_t terminal_fd_writer_write (terminal_fd_writer_t *writer, const char *frame)
 Write a frame to the file descriptor.
 
void terminal_fd_writer_destroy (terminal_fd_writer_t *writer)
 Destroy terminal FD writer.
 

Detailed Description

Write ANSI ASCII frames to file descriptor.

Definition in file writer.c.

Typedef Documentation

◆ terminal_fd_writer_t

Function Documentation

◆ terminal_fd_writer_create()

asciichat_error_t terminal_fd_writer_create ( int  fd,
terminal_fd_writer_t **  out 
)

Create a terminal FD writer to a file descriptor.

Parameters
fdFile descriptor to write to (e.g., STDOUT_FILENO, open file)
outPointer to store writer context
Returns
ASCIICHAT_OK on success

Definition at line 19 of file writer.c.

19 {
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}
#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_OK
Definition error_codes.h:51
@ ERROR_INVALID_PARAM
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:548
FILE * platform_fdopen(const char *name, int fd, const char *mode)
Convert file descriptor to stream (fdopen replacement)

References ASCIICHAT_OK, ERROR_INVALID_PARAM, terminal_fd_writer_s::fp, log_debug, platform_fdopen(), SAFE_CALLOC, SET_ERRNO, and SET_ERRNO_SYS.

◆ terminal_fd_writer_destroy()

void terminal_fd_writer_destroy ( terminal_fd_writer_t *  writer)

Destroy terminal FD writer.

Parameters
writerWriter context to destroy

Definition at line 62 of file writer.c.

62 {
63 if (!writer)
64 return;
65 if (writer->fp) {
66 fclose(writer->fp);
67 writer->fp = NULL;
68 }
69 SAFE_FREE(writer);
70}
#define SAFE_FREE(ptr)
Definition common.h:376

References terminal_fd_writer_s::fp, and SAFE_FREE.

◆ terminal_fd_writer_write()

asciichat_error_t terminal_fd_writer_write ( terminal_fd_writer_t *  writer,
const char *  frame 
)

Write a frame to the file descriptor.

Writes the frame data followed by a newline. Frame should contain ANSI-colored ASCII art, typically from rendering output.

Parameters
writerWriter context
frameFrame data to write (NULL is no-op)
Returns
ASCIICHAT_OK on success

Definition at line 37 of file writer.c.

37 {
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}
#define NS_PER_SEC_INT
Definition time.h:157
#define log_debug_every(interval_us, fmt,...)
Rate-limited DEBUG logging.
Definition log/log.h:702
int frame
Definition splash.c:99

References ASCIICHAT_OK, ERROR_INVALID_PARAM, terminal_fd_writer_s::fp, frame, log_debug_every, NS_PER_SEC_INT, SET_ERRNO, and SET_ERRNO_SYS.