Z / libzlang 0.29.0
C99 runtime library and the Zlang 0.29 source-to-source compiler
Loading...
Searching...
No Matches
io.h
Go to the documentation of this file.
1#ifndef ZLANG_STANDARD_IO_IO_H
2#define ZLANG_STANDARD_IO_IO_H
3
6
7#if defined(ZLANG_STD_IO_WRITE_STRING)
8ZLANG_STATIC_INLINE bool zlang_std_io_write_string(zlang_string_t value) {
9 return zlang_print(value);
10}
11#endif
12
13#if defined(ZLANG_STD_IO_WRITE_BOOL)
14ZLANG_STATIC_INLINE bool zlang_std_io_write_bool(bool value) { return zlang_print_bool(value); }
15#endif
16
17#if defined(ZLANG_STD_IO_WRITE_I32)
18ZLANG_STATIC_INLINE bool zlang_std_io_write_i32(int32_t value) { return zlang_print_i32(value); }
19#endif
20
21#if defined(ZLANG_STD_IO_WRITE_U32)
22ZLANG_STATIC_INLINE bool zlang_std_io_write_u32(uint32_t value) { return zlang_print_u32(value); }
23#endif
24
25#if defined(ZLANG_STD_IO_WRITE_I64)
26ZLANG_STATIC_INLINE bool zlang_std_io_write_i64(int64_t value) { return zlang_print_i64(value); }
27#endif
28
29#if defined(ZLANG_STD_IO_WRITE_U64)
30ZLANG_STATIC_INLINE bool zlang_std_io_write_u64(uint64_t value) { return zlang_print_u64(value); }
31#endif
32
33#if defined(ZLANG_STD_IO_WRITE_F32)
34ZLANG_STATIC_INLINE bool zlang_std_io_write_f32(float value) { return zlang_print_f32(value); }
35#endif
36
37#if defined(ZLANG_STD_IO_WRITE_F64)
38ZLANG_STATIC_INLINE bool zlang_std_io_write_f64(double value) { return zlang_print_f64(value); }
39#endif
40
41#if defined(ZLANG_STD_IO_READ) || defined(ZLANG_STD_IO_READ_LINE)
42ZLANG_STATIC_INLINE int32_t zlang_std_io_read_impl(zlang_string_t *view, char *storage,
43 uint32_t capacity, bool keep_terminator) {
44 if (view == NULL || storage == NULL || capacity == 0U) {
46 }
47 uint32_t _length_k = 0U;
48 int _character_k = getchar();
49 while (_character_k != EOF && _character_k != '\n') {
50 if (_length_k >= capacity) {
52 }
53 storage[_length_k++] = (char)_character_k;
54 _character_k = getchar();
55 }
56 if (_character_k == EOF && _length_k == 0U) {
57 view->data_ = storage;
58 view->length_ = 0U;
59 return -1;
60 }
61 if (keep_terminator && _character_k == '\n') {
62 if (_length_k >= capacity) {
64 }
65 storage[_length_k++] = '\n';
66 }
67 storage[_length_k] = '\0';
68 view->data_ = storage;
69 view->length_ = _length_k;
70 return (int32_t)_length_k;
71}
72#endif
73
74#if defined(ZLANG_STD_IO_READ)
75ZLANG_STATIC_INLINE int32_t zlang_std_io_read(zlang_string_t *view, char *storage,
76 uint32_t capacity) {
77 return zlang_std_io_read_impl(view, storage, capacity, true);
78}
79#endif
80
81#if defined(ZLANG_STD_IO_READ_LINE)
82ZLANG_STATIC_INLINE int32_t zlang_std_io_read_line(zlang_string_t *view, char *storage,
83 uint32_t capacity) {
84 return zlang_std_io_read_impl(view, storage, capacity, false);
85}
86#endif
87
88#endif /* ZLANG_STANDARD_IO_IO_H */
#define ZLANG_STATIC_INLINE
Definition inline.h:9
Runtime primitives called by C code emitted from Zlang.
ZLANG_STATIC_INLINE void zlang_trap(uint8_t code)
Terminate execution with a fixed trap code.
Definition runtime.h:73
ZLANG_STATIC_INLINE bool zlang_print_u64(uint64_t value)
Print an unsigned 64-bit integer using fixed stack storage.
Definition runtime.h:298
ZLANG_STATIC_INLINE bool zlang_print_f64(double value)
Print a 64-bit double with a deterministic decimal form.
Definition runtime.h:326
ZLANG_STATIC_INLINE bool zlang_print_bool(bool value)
Print a boolean as true or false.
Definition runtime.h:151
ZLANG_STATIC_INLINE bool zlang_print_i32(int32_t value)
Print a signed integer using fixed stack storage.
Definition runtime.h:111
ZLANG_STATIC_INLINE bool zlang_print_u32(uint32_t value)
Print an unsigned integer using fixed stack storage.
Definition runtime.h:133
#define ZLANG_TRAP_CAPACITY
Definition runtime.h:24
ZLANG_STATIC_INLINE bool zlang_print(zlang_string_t value)
Write a string view to standard output.
Definition runtime.h:61
#define ZLANG_TRAP_INVALID_ARGUMENT
Definition runtime.h:25
ZLANG_STATIC_INLINE bool zlang_print_i64(int64_t value)
Print a signed 64-bit integer using fixed stack storage.
Definition runtime.h:278
ZLANG_STATIC_INLINE bool zlang_print_f32(float value)
Print a 32-bit float with a deterministic decimal form.
Definition runtime.h:314
Definition runtime.h:35
const char * data_
Definition runtime.h:36
uint32_t length_
Definition runtime.h:37