solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
Loading...
Searching...
No Matches
stdstreams.h
Go to the documentation of this file.
1
19#ifndef E8CAA280_1C10_4862_B560_47F74D754175
20#define E8CAA280_1C10_4862_B560_47F74D754175
21
22#include "platform.h"
23
24#include <stdbool.h>
25#include <stdio.h>
26#include <sys/types.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/* -----------------------------------------------------------------------
33 * Debug / release safety guards — zero overhead in release builds.
34 * --------------------------------------------------------------------- */
35#ifndef NDEBUG
36#include <assert.h>
37#define STREAM_ASSERT(x) assert(x)
38#else
39#define STREAM_ASSERT(x) ((void)0)
40#endif
41
42/* -----------------------------------------------------------------------
43 * Terminal helpers
44 * --------------------------------------------------------------------- */
45
57bool readline(const char* prompt, char* buffer, size_t buffer_len);
58
69int getpassword(const char* prompt, char* buffer, size_t buffer_len);
70
71/* -----------------------------------------------------------------------
72 * Stream type definitions
73 * --------------------------------------------------------------------- */
74
81typedef struct stream* stream_t;
82
91typedef struct string_stream {
92 char* data;
93 size_t size;
94 size_t capacity;
95 size_t pos;
97
98/* -----------------------------------------------------------------------
99 * Stream result type — mirrors POSIX read/write semantics
100 * --------------------------------------------------------------------- */
101typedef ssize_t stream_result_t;
102
103/* -----------------------------------------------------------------------
104 * Lifecycle
105 * --------------------------------------------------------------------- */
106
117
127stream_t create_string_stream(size_t initial_capacity);
128
137void stream_destroy(stream_t stream);
138
139/* -----------------------------------------------------------------------
140 * Seeking
141 * --------------------------------------------------------------------- */
142
151int stream_seek(stream_t stream, long offset, int whence);
152
153/* -----------------------------------------------------------------------
154 * File-stream helpers
155 * --------------------------------------------------------------------- */
156
167size_t file_stream_read(stream_t s, void* restrict ptr, size_t size, size_t count);
168
169/* -----------------------------------------------------------------------
170 * String-stream helpers
171 * --------------------------------------------------------------------- */
172
183int string_stream_write(stream_t stream, const char* str);
184
196int string_stream_write_len(stream_t stream, const char* str, size_t n);
197
205const char* string_stream_data(stream_t stream);
206
207/* -----------------------------------------------------------------------
208 * Delimited read
209 * --------------------------------------------------------------------- */
210
223ssize_t read_until(stream_t stream, int delim, char* buffer, size_t buffer_size);
224
225/* -----------------------------------------------------------------------
226 * Layer 1 — type-specialised fast paths
227 * --------------------------------------------------------------------- */
228
238unsigned long string_stream_copy_fast(stream_t dst, stream_t src);
239
240/* -----------------------------------------------------------------------
241 * Layer 2 — generic copy
242 * --------------------------------------------------------------------- */
243
253unsigned long io_copy(stream_t writer, stream_t reader);
254
263unsigned long io_copy_n(stream_t writer, stream_t reader, size_t n);
264
265#ifdef __cplusplus
266}
267#endif
268
269#endif /* E8CAA280_1C10_4862_B560_47F74D754175 */
Cross-platform compatibility definitions.
size_t file_stream_read(stream_t s, void *restrict ptr, size_t size, size_t count)
Read up to count objects of size bytes from a file stream into ptr, rewinding to the beginning first.
Definition stdstreams.c:161
void stream_destroy(stream_t stream)
Destroy a stream and release all connected resources.
Definition stdstreams.c:503
int stream_seek(stream_t stream, long offset, int whence)
Seek within a stream exactly matching POSIX fseek(3) semantics.
Definition stdstreams.c:125
const char * string_stream_data(stream_t stream)
Fetch a read-only view of the underlying strictly NUL-terminated string data.
Definition stdstreams.c:302
bool readline(const char *prompt, char *buffer, size_t buffer_len)
Read a line from stdin, optionally printing a prompt first.
Definition stdstreams.c:35
struct stream * stream_t
Opaque stream handle.
Definition stdstreams.h:81
int string_stream_write(stream_t stream, const char *str)
Append the NUL-terminated string str to stream.
Definition stdstreams.c:271
int getpassword(const char *prompt, char *buffer, size_t buffer_len)
Read a password from the terminal with echo disabled.
Definition stdstreams.c:53
int string_stream_write_len(stream_t stream, const char *str, size_t n)
Append the NUL-terminated string str to stream. This is faster than string_stream_write when the leng...
Definition stdstreams.c:287
unsigned long io_copy_n(stream_t writer, stream_t reader, size_t n)
Copy tightly capped chunk up to n bytes.
Definition stdstreams.c:451
stream_t create_file_stream(FILE *fp)
Wrap an existing FILE* in a standardized stream context.
Definition stdstreams.c:145
unsigned long string_stream_copy_fast(stream_t dst, stream_t src)
Direct memory-bound string to string copy without generic dispatch.
Definition stdstreams.c:394
unsigned long io_copy(stream_t writer, stream_t reader)
Copy contents universally from reader into writer.
Definition stdstreams.c:427
stream_t create_string_stream(size_t initial_capacity)
Allocate a new in-memory string stream.
Definition stdstreams.c:307
ssize_t read_until(stream_t stream, int delim, char *buffer, size_t buffer_size)
Read from stream into buffer until delim is found.
Definition stdstreams.c:344
String stream internal state structure.
Definition stdstreams.h:91
size_t capacity
Definition stdstreams.h:94