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
6#ifndef E8CAA280_1C10_4862_B560_47F74D754175
7#define E8CAA280_1C10_4862_B560_47F74D754175
8
9#include "platform.h"
10
11#include <stdbool.h>
12#include <stdio.h>
13#include <sys/types.h>
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19#include "cstr.h"
20
21// Read a line from stdin with a prompt
22bool readline(const char* prompt, char* buffer, size_t buffer_len);
23
24// getpassword() reads a password from the terminal with echo disabled.
25// uses termios on Linux and Windows API on Windows.
26// The password is stored in the buffer and the function returns the number
27// of characters read. The buffer is null-terminated.
28int getpassword(const char* prompt, char* buffer, size_t buffer_len);
29
30// stream_t type wraps common FILE* operations to be work with strings as a
31// files. Any type can implement read, write, seek, eof, flush, read_char.
32typedef struct stream* stream_t;
33
34// Create a stream from a file pointer
35stream_t create_file_stream(FILE* fp);
36
37// Read from file stream into ptr.
38size_t file_stream_read(stream_t s, void* ptr, size_t size, size_t count);
39
40// Free a stream created with create_string_stream or create_file_stream
41// and close the underlying file FILE* streams.
42// If the underlying FILE* is stdin/stdout/stderr, they are not closed.
43void stream_destroy(stream_t stream);
44
45// Read a character from the stream until a delimiter is found or the buffer
46// is full. Returns the number of characters read or -1 on error. The buffer
47// is null-terminated. Create implementation for reading from a file using
48// create_file_stream helper function and for reading from a string using
49// create_string_stream.
50ssize_t read_until(stream_t stream, int delim, char* buffer, size_t buffer_size);
51
52typedef struct string_stream {
53 cstr* str;
54 size_t pos;
55} string_stream;
56
57// Create and initialize a stream from a string. Free the stream with
58// free_string_stream.
59stream_t create_string_stream(size_t initial_capacity);
60
61// copy the string to the stream. The position is not incremented.
62int string_stream_write(stream_t stream, const char* str);
63
64// Return the data in the string stream.
65const char* string_stream_data(stream_t stream);
66
67// Copy the contents of one stream to another.
68// Inspired by the golang io.Copy function.
69// Both streams must be valid. Invalid streams are checked with
70// is_valid_stream if the program is compiled with assertions enabled.
71// Compile with -DNDEBUG to disable assertions. Returns the number of bytes
72// copied or -1 on error.
73unsigned long io_copy(stream_t writer, stream_t reader);
74
75// Copy contents of one reader into writer, writing up to n bytes into the
76// writer.
77unsigned long io_copy_n(stream_t writer, stream_t reader, size_t n);
78
79#ifdef __cplusplus
80}
81#endif
82
83#endif /* E8CAA280_1C10_4862_B560_47F74D754175 */
High-performance C string with Small String Optimization (SSO).
Cross-platform compatibility definitions.
A dynamically resizable C string with SSO.
Definition cstr.h:104