2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2023 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * *****************************************************************************
32 * Definitions for implementing buffered I/O on my own terms.
44 #define BC_FILE_ULL_LENGTH (21)
46 #if BC_ENABLE_LINE_LIB
53 // The file. This is here simply to make the line lib code as compatible
54 // with the existing code as possible.
59 #else // BC_ENABLE_LINE_LIB
64 // The actual file descriptor.
67 // The buffer for the file.
70 // The length (number of actual chars) in the buffer.
73 // The capacity (max number of chars) of the buffer.
78 #endif // BC_ENABLE_LINE_LIB
80 #if BC_ENABLE_HISTORY && !BC_ENABLE_LINE_LIB
82 /// Types of flushing. These are important because of history and printing
83 /// strings without newlines, something that users could use as their own
85 typedef enum BcFlushType
87 /// Do not clear the stored partial line, but don't add to it.
88 BC_FLUSH_NO_EXTRAS_NO_CLEAR,
90 /// Do not clear the stored partial line and add to it.
91 BC_FLUSH_SAVE_EXTRAS_NO_CLEAR,
93 /// Clear the stored partial line and do not save the new stuff either.
94 BC_FLUSH_NO_EXTRAS_CLEAR,
96 /// Clear the stored partial line, but save the new stuff.
97 BC_FLUSH_SAVE_EXTRAS_CLEAR,
101 // These are here to satisfy a clang warning about recursive macros.
103 #define bc_file_putchar(f, t, c) bc_file_putchar_impl(f, t, c)
104 #define bc_file_flushErr(f, t) bc_file_flushErr_impl(f, t)
105 #define bc_file_flush(f, t) bc_file_flush_impl(f, t)
106 #define bc_file_write(f, t, b, n) bc_file_write_impl(f, t, b, n)
107 #define bc_file_puts(f, t, s) bc_file_puts_impl(f, t, s)
109 #else // BC_ENABLE_HISTORY && !BC_ENABLE_LINE_LIB
111 // These make sure that the BcFlushType parameter disappears if history is not
112 // used, editline is used, or readline is used.
114 #define bc_file_putchar(f, t, c) bc_file_putchar_impl(f, c)
115 #define bc_file_flushErr(f, t) bc_file_flushErr_impl(f)
116 #define bc_file_flush(f, t) bc_file_flush_impl(f)
117 #define bc_file_write(f, t, b, n) bc_file_write_impl(f, b, n)
118 #define bc_file_puts(f, t, s) bc_file_puts_impl(f, s)
120 #endif // BC_ENABLE_HISTORY && !BC_ENABLE_LINE_LIB
122 #if BC_ENABLE_LINE_LIB
126 * @param f The file to initialize.
127 * @param file The stdio file.
130 bc_file_init(BcFile* f, FILE* file);
132 #else // BC_ENABLE_LINE_LIB
136 * @param f The file to initialize.
137 * @param fd The file descriptor.
138 * @param buf The buffer for the file.
139 * @param cap The capacity of the buffer.
142 bc_file_init(BcFile* f, int fd, char* buf, size_t cap);
144 #endif // BC_ENABLE_LINE_LIB
147 * Frees a file, including flushing it.
148 * @param f The file to free.
151 bc_file_free(BcFile* f);
154 * Print a char into the file.
155 * @param f The file to print to.
156 * @param type The flush type.
157 * @param c The character to write.
160 bc_file_putchar(BcFile* restrict f, BcFlushType type, uchar c);
163 * Flush and return an error if it failed. This is meant to be used when needing
164 * to flush in error situations when an error is already in flight. It would be
165 * a very bad deal to throw another error.
166 * @param f The file to flush.
167 * @param type The flush type.
168 * @return A status indicating if an error occurred.
171 bc_file_flushErr(BcFile* restrict f, BcFlushType type);
174 * Flush and throw an error on failure.
175 * @param f The file to flush.
176 * @param type The flush type.
179 bc_file_flush(BcFile* restrict f, BcFlushType type);
182 * Write the contents of buf to the file.
183 * @param f The file to flush.
184 * @param type The flush type.
185 * @param buf The buffer whose contents will be written to the file.
186 * @param n The length of buf.
189 bc_file_write(BcFile* restrict f, BcFlushType type, const char* buf, size_t n);
192 * Write to the file like fprintf would. This is very rudimentary.
193 * @param f The file to flush.
194 * @param fmt The format string.
197 bc_file_printf(BcFile* restrict f, const char* fmt, ...);
200 * Write to the file like vfprintf would. This is very rudimentary.
201 * @param f The file to flush.
202 * @param fmt The format string.
205 bc_file_vprintf(BcFile* restrict f, const char* fmt, va_list args);
208 * Write str to the file.
209 * @param f The file to flush.
210 * @param type The flush type.
211 * @param str The string to write to the file.
214 bc_file_puts(BcFile* restrict f, BcFlushType type, const char* str);
216 #if BC_ENABLE_HISTORY && !BC_ENABLE_LINE_LIB
218 // Some constant flush types for ease of use.
219 extern const BcFlushType bc_flush_none;
220 extern const BcFlushType bc_flush_err;
221 extern const BcFlushType bc_flush_save;
223 #endif // BC_ENABLE_HISTORY && !BC_ENABLE_LINE_LIB