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 File Reference

terminal fd writer keeps the values that move through the subsystem: fp. Some members are pointers into caller- or subsystem-owned memory, so lifetime follows the API that populated them rather than the struct copy itself. 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

terminal fd writer keeps the values that move through the subsystem: fp. Some members are pointers into caller- or subsystem-owned memory, so lifetime follows the API that populated them rather than the struct copy itself.

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 25 of file writer.c.

25 {
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}
#define SET_ERRNO(code,...)
#define SET_ERRNO_SYS(code,...)
#define SAFE_CALLOC(count, size, cast)
Definition common.h:275
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_INVALID_PARAM
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:595
static bool size_t gotel_query_t * out
Definition queries.h:8
struct terminal_fd_writer_s terminal_fd_writer_t

References ASCIICHAT_OK, ERROR_INVALID_PARAM, terminal_fd_writer_s::fp, log_debug, out, 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 64 of file writer.c.

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

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 43 of file writer.c.

43 {
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}
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
#define NS_PER_SEC_INT
Definition time.h:169
#define log_debug_every(interval_us, fmt,...)
Rate-limited DEBUG logging.
Definition log/log.h:749
asciichat_error_t stdout_writer_write(FILE *stream, const void *data, size_t size)

References ASCIICHAT_OK, ERROR_INVALID_PARAM, terminal_fd_writer_s::fp, log_debug_every, NS_PER_SEC_INT, SET_ERRNO, and stdout_writer_write().