ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
reader.c File Reference

For terminal fd reader, this is the live record that ties fp, frame_height, frame_width, line_buffer together. The owning code creates it, updates it while work is active, and releases its handles and pointed-to storage before freeing the record. More...

Go to the source code of this file.

Data Structures

struct  terminal_fd_reader_s

Macros

#define LINE_BUFFER_SIZE   16384

Typedefs

typedef struct terminal_fd_reader_s terminal_fd_reader_t

Functions

asciichat_error_t terminal_fd_reader_create (int fd, int frame_height, terminal_fd_reader_t **out)
 Create a terminal FD reader from a file descriptor.
asciichat_error_t terminal_fd_reader_next (terminal_fd_reader_t *reader, char **out_frame)
 Read next frame from file descriptor.
void terminal_fd_reader_destroy (terminal_fd_reader_t *reader)
 Destroy terminal FD reader.

Detailed Description

For terminal fd reader, this is the live record that ties fp, frame_height, frame_width, line_buffer together. The owning code creates it, updates it while work is active, and releases its handles and pointed-to storage before freeing the record.

Read ANSI ASCII frames from file descriptor, chunked by height

Definition in file reader.c.

Macro Definition Documentation

◆ LINE_BUFFER_SIZE

#define LINE_BUFFER_SIZE   16384

Definition at line 31 of file reader.c.

Referenced by terminal_fd_reader_create(), and terminal_fd_reader_next().

Typedef Documentation

◆ terminal_fd_reader_t

Function Documentation

◆ terminal_fd_reader_create()

asciichat_error_t terminal_fd_reader_create ( int fd,
int frame_height,
terminal_fd_reader_t ** out )

Create a terminal FD reader from a file descriptor.

Parameters
fdFile descriptor to read from (e.g., STDIN_FILENO, open file)
frame_heightNumber of lines per frame (required - used for frame boundaries)
outPointer to store reader context
Returns
ASCIICHAT_OK on success

Width is auto-detected from the first frame's line length.

Definition at line 33 of file reader.c.

33 {
34 if (fd < 0 || frame_height <= 0 || !out) {
35 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid reader parameters: fd=%d, height=%d, output=%p", fd, frame_height, (void *)out);
36 }
37
38 FILE *fp = platform_fdopen("terminal_fd_reader", fd, "r");
39 if (!fp) {
40 return SET_ERRNO_SYS(ERROR_INVALID_PARAM, "fdopen failed for FD %d", fd);
41 }
42
43 terminal_fd_reader_t *reader = SAFE_CALLOC(1, sizeof(*reader), terminal_fd_reader_t *);
44 reader->fp = fp;
45 reader->frame_height = frame_height;
46 reader->frame_width = 0; // Will be detected from first frame
47 reader->line_buffer = SAFE_MALLOC(LINE_BUFFER_SIZE, char *);
49 reader->eof_reached = false;
50 reader->first_frame_processed = false;
51
52 log_debug("created with fd=%d, frame height %d (width auto-detect)", fd, frame_height);
53 *out = reader;
54 return ASCIICHAT_OK;
55}
#define SET_ERRNO(code,...)
#define SET_ERRNO_SYS(code,...)
#define SAFE_MALLOC(size, cast)
#define SAFE_CALLOC(count, size, cast)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
static bool size_t gotel_query_t * out
Definition queries.h:10
struct terminal_fd_reader_s terminal_fd_reader_t
#define LINE_BUFFER_SIZE
Definition reader.c:31
size_t line_buffer_size
Definition reader.c:26
bool first_frame_processed
Definition reader.c:28

References ASCIICHAT_OK, terminal_fd_reader_s::eof_reached, ERROR_INVALID_PARAM, terminal_fd_reader_s::first_frame_processed, terminal_fd_reader_s::fp, terminal_fd_reader_s::frame_height, terminal_fd_reader_s::frame_width, terminal_fd_reader_s::line_buffer, LINE_BUFFER_SIZE, terminal_fd_reader_s::line_buffer_size, log_debug, out, SAFE_CALLOC, SAFE_MALLOC, SET_ERRNO, and SET_ERRNO_SYS.

◆ terminal_fd_reader_destroy()

void terminal_fd_reader_destroy ( terminal_fd_reader_t * reader)

Destroy terminal FD reader.

Parameters
readerReader context to destroy

Definition at line 146 of file reader.c.

146 {
147 if (!reader)
148 return;
149 if (reader->fp) {
150 fclose(reader->fp);
151 reader->fp = NULL;
152 }
153 SAFE_FREE(reader->line_buffer);
154 SAFE_FREE(reader);
155}
#define SAFE_FREE(ptr)

References terminal_fd_reader_s::fp, terminal_fd_reader_s::line_buffer, and SAFE_FREE.

◆ terminal_fd_reader_next()

asciichat_error_t terminal_fd_reader_next ( terminal_fd_reader_t * reader,
char ** out_frame )

Read next frame from file descriptor.

Reads frame_height lines from the FD and returns them as a single frame string. Returns NULL in out_frame when EOF is reached.

Parameters
readerReader context
out_framePointer to store frame data (NULL when EOF, must free with SAFE_FREE)
Returns
ASCIICHAT_OK on success (including EOF)

Definition at line 57 of file reader.c.

57 {
58 if (!reader || !out_frame) {
59 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
60 }
61
62 if (reader->eof_reached) {
63 *out_frame = NULL;
64 return ASCIICHAT_OK; // EOF, but no error
65 }
66
67 // Allocate buffer for complete frame (frame_height lines)
68 // Estimate: 16KB per line should be enough for ANSI + ASCII
69 size_t frame_capacity = reader->frame_height * LINE_BUFFER_SIZE;
70 char *frame_data = SAFE_MALLOC(frame_capacity, char *);
71 size_t frame_pos = 0;
72
73 // Read exactly frame_height lines from FD
74 for (int line_num = 0; line_num < reader->frame_height; line_num++) {
75 // Read one line from FD
76 char *line = fgets(reader->line_buffer, (int)reader->line_buffer_size, reader->fp);
77
78 if (!line) {
79 // EOF reached
80 reader->eof_reached = true;
81
82 // If we read some lines but not a complete frame, that's ok - return partial
83 if (frame_pos > 0) {
84 log_debug("EOF reached after %d/%d lines, returning partial frame", line_num, reader->frame_height);
85 // Remove trailing newline from last line if present
86 if (frame_pos > 0 && frame_data[frame_pos - 1] == '\n') {
87 frame_pos--;
88 }
89 frame_data[frame_pos] = '\0';
90 *out_frame = frame_data;
91 return ASCIICHAT_OK;
92 }
93
94 SAFE_FREE(frame_data);
95 *out_frame = NULL;
96 return ASCIICHAT_OK; // EOF
97 }
98
99 // Append line to frame
100 size_t line_len = strlen(line);
101 if (frame_pos + line_len >= frame_capacity) {
102 log_warn("frame buffer overflow, line %d too large", line_num);
103 // Realloc and continue
104 frame_capacity *= 2;
105 char *new_frame = SAFE_MALLOC(frame_capacity, char *);
106 memcpy(new_frame, frame_data, frame_pos);
107 SAFE_FREE(frame_data);
108 frame_data = new_frame;
109 }
110
111 memcpy(frame_data + frame_pos, line, line_len);
112 frame_pos += line_len;
113 }
114
115 // Remove trailing newline from last line (we'll add it back in display)
116 if (frame_pos > 0 && frame_data[frame_pos - 1] == '\n') {
117 frame_pos--;
118 }
119 frame_data[frame_pos] = '\0';
120
121 // Detect frame width from first frame (count visible chars in first line)
122 if (!reader->first_frame_processed && frame_pos > 0) {
123 reader->first_frame_processed = true;
124
125 // Find first line by looking for newline
126 char *first_newline = strchr(frame_data, '\n');
127 if (first_newline) {
128 // Null-terminate first line temporarily for width calculation
129 char saved_char = *first_newline;
130 *first_newline = '\0';
131 reader->frame_width = display_width(frame_data);
132 *first_newline = saved_char; // Restore
133 } else {
134 // Only one line in frame, use it for width
135 reader->frame_width = display_width(frame_data);
136 }
137
138 log_info("detected frame width %d from first frame", reader->frame_width);
139 }
140
141 *out_frame = frame_data;
142 log_debug_every(NS_PER_SEC_INT, "read frame (%zu bytes, %dx%d)", frame_pos, reader->frame_width, reader->frame_height);
143 return ASCIICHAT_OK;
144}
#define log_warn(...)
Log a WARN message.
Definition log/log.h:591
#define log_info(...)
Log an INFO message.
Definition log/log.h:578
#define NS_PER_SEC_INT
Definition time.h:173
int display_width(const char *text)
Calculate the visible display width of text with ANSI escape codes.
#define log_debug_every(interval_us, fmt,...)
Rate-limited DEBUG logging.
Definition log/log.h:719

References ASCIICHAT_OK, display_width(), terminal_fd_reader_s::eof_reached, ERROR_INVALID_PARAM, terminal_fd_reader_s::first_frame_processed, terminal_fd_reader_s::fp, terminal_fd_reader_s::frame_height, terminal_fd_reader_s::frame_width, terminal_fd_reader_s::line_buffer, LINE_BUFFER_SIZE, terminal_fd_reader_s::line_buffer_size, log_debug, log_debug_every, log_info, log_warn, NS_PER_SEC_INT, SAFE_FREE, SAFE_MALLOC, and SET_ERRNO.