1 /* 2 * ***************************************************************************** 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 * 6 * Copyright (c) 2018-2021 Gavin D. Howard and contributors. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * * Redistributions of source code must retain the above copyright notice, this 12 * list of conditions and the following disclaimer. 13 * 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. 17 * 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. 29 * 30 * ***************************************************************************** 31 * 32 * Definitions for implementing buffered I/O on my own terms. 33 * 34 */ 35 36 #ifndef BC_FILE_H 37 #define BC_FILE_H 38 39 #include <stdarg.h> 40 41 #include <vector.h> 42 43 #define BC_FILE_ULL_LENGTH (21) 44 45 /// The file struct. 46 typedef struct BcFile { 47 48 // The actual file descriptor. 49 int fd; 50 51 // The buffer for the file. 52 char *buf; 53 54 // The length (number of actual chars) in the buffer. 55 size_t len; 56 57 // The capacity (max number of chars) of the buffer. 58 size_t cap; 59 60 } BcFile; 61 62 #if BC_ENABLE_HISTORY 63 64 /// Types of flushing. These are important because of history and printing 65 /// strings without newlines, something that users could use as their own 66 /// prompts. 67 typedef enum BcFlushType { 68 69 /// Do not clear the stored partial line, but don't add to it. 70 BC_FLUSH_NO_EXTRAS_NO_CLEAR, 71 72 /// Do not clear the stored partial line and add to it. 73 BC_FLUSH_SAVE_EXTRAS_NO_CLEAR, 74 75 /// Clear the stored partial line and do not save the new stuff either. 76 BC_FLUSH_NO_EXTRAS_CLEAR, 77 78 /// Clear the stored partial line, but save the new stuff. 79 BC_FLUSH_SAVE_EXTRAS_CLEAR, 80 81 } BcFlushType; 82 83 #else // BC_ENABLE_HISTORY 84 85 // These make sure that the BcFlushType parameter disappears if history is not 86 // used. 87 88 #define bc_file_putchar(f, t, c) bc_file_putchar(f, c) 89 #define bc_file_flushErr(f, t) bc_file_flushErr(f) 90 #define bc_file_flush(f, t) bc_file_flush(f) 91 #define bc_file_write(f, t, b, n) bc_file_write(f, b, n) 92 #define bc_file_puts(f, t, s) bc_file_puts(f, s) 93 94 #endif // BC_ENABLE_HISTORY 95 96 /** 97 * Initialize a file. 98 * @param f The file to initialize. 99 * @param fd The file descriptor. 100 * @param buf The buffer for the file. 101 * @param cap The capacity of the buffer. 102 */ 103 void bc_file_init(BcFile *f, int fd, char *buf, size_t cap); 104 105 /** 106 * Frees a file, including flushing it. 107 * @param f The file to free. 108 */ 109 void bc_file_free(BcFile *f); 110 111 /** 112 * Print a char into the file. 113 * @param f The file to print to. 114 * @param type The flush type. 115 * @param c The character to write. 116 */ 117 void bc_file_putchar(BcFile *restrict f, BcFlushType type, uchar c); 118 119 /** 120 * Flush and return an error if it failed. This is meant to be used when needing 121 * to flush in error situations when an error is already in flight. It would be 122 * a very bad deal to throw another error. 123 * @param f The file to flush. 124 * @param type The flush type. 125 * @return A status indicating if an error occurred. 126 */ 127 BcStatus bc_file_flushErr(BcFile *restrict f, BcFlushType type); 128 129 /** 130 * Flush and throw an error on failure. 131 * @param f The file to flush. 132 * @param type The flush type. 133 */ 134 void bc_file_flush(BcFile *restrict f, BcFlushType type); 135 136 /** 137 * Write the contents of buf to the file. 138 * @param f The file to flush. 139 * @param type The flush type. 140 * @param buf The buffer whose contents will be written to the file. 141 * @param n The length of buf. 142 */ 143 void bc_file_write(BcFile *restrict f, BcFlushType type, 144 const char *buf, size_t n); 145 146 /** 147 * Write to the file like fprintf would. This is very rudimentary. 148 * @param f The file to flush. 149 * @param fmt The format string. 150 */ 151 void bc_file_printf(BcFile *restrict f, const char *fmt, ...); 152 153 /** 154 * Write to the file like vfprintf would. This is very rudimentary. 155 * @param f The file to flush. 156 * @param fmt The format string. 157 */ 158 void bc_file_vprintf(BcFile *restrict f, const char *fmt, va_list args); 159 160 /** 161 * Write str to the file. 162 * @param f The file to flush. 163 * @param type The flush type. 164 * @param str The string to write to the file. 165 */ 166 void bc_file_puts(BcFile *restrict f, BcFlushType type, const char *str); 167 168 #if BC_ENABLE_HISTORY 169 170 // Some constant flush types for ease of use. 171 extern const BcFlushType bc_flush_none; 172 extern const BcFlushType bc_flush_err; 173 extern const BcFlushType bc_flush_save; 174 175 #endif // BC_ENABLE_HISTORY 176 177 #endif // BC_FILE_H 178