/*1* *****************************************************************************2*3* SPDX-License-Identifier: BSD-2-Clause4*5* Copyright (c) 2018-2025 Gavin D. Howard and contributors.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions are met:9*10* * Redistributions of source code must retain the above copyright notice, this11* list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright notice,14* this list of conditions and the following disclaimer in the documentation15* and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"18* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE21* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS24* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN25* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)26* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE27* POSSIBILITY OF SUCH DAMAGE.28*29* *****************************************************************************30*31* Definitions for implementing buffered I/O on my own terms.32*33*/3435#ifndef BC_FILE_H36#define BC_FILE_H3738#include <stdarg.h>3940#include <history.h>41#include <vector.h>4243#define BC_FILE_ULL_LENGTH (21)4445#if BC_ENABLE_LINE_LIB4647#include <stdio.h>4849/// The file struct.50typedef struct BcFile51{52// The file. This is here simply to make the line lib code as compatible53// with the existing code as possible.54FILE* f;5556// True if errors should be fatal, false otherwise.57bool errors_fatal;5859} BcFile;6061#else // BC_ENABLE_LINE_LIB6263/// The file struct.64typedef struct BcFile65{66// The actual file descriptor.67int fd;6869// True if errors should be fatal, false otherwise.70bool errors_fatal;7172// The buffer for the file.73char* buf;7475// The length (number of actual chars) in the buffer.76size_t len;7778// The capacity (max number of chars) of the buffer.79size_t cap;8081} BcFile;8283#endif // BC_ENABLE_LINE_LIB8485#if BC_ENABLE_HISTORY && !BC_ENABLE_LINE_LIB8687/// Types of flushing. These are important because of history and printing88/// strings without newlines, something that users could use as their own89/// prompts.90typedef enum BcFlushType91{92/// Do not clear the stored partial line, but don't add to it.93BC_FLUSH_NO_EXTRAS_NO_CLEAR,9495/// Do not clear the stored partial line and add to it.96BC_FLUSH_SAVE_EXTRAS_NO_CLEAR,9798/// Clear the stored partial line and do not save the new stuff either.99BC_FLUSH_NO_EXTRAS_CLEAR,100101/// Clear the stored partial line, but save the new stuff.102BC_FLUSH_SAVE_EXTRAS_CLEAR,103104} BcFlushType;105106// These are here to satisfy a clang warning about recursive macros.107108#define bc_file_putchar(f, t, c) bc_file_putchar_impl(f, t, c)109#define bc_file_flushErr(f, t) bc_file_flushErr_impl(f, t)110#define bc_file_flush(f, t) bc_file_flush_impl(f, t)111#define bc_file_write(f, t, b, n) bc_file_write_impl(f, t, b, n)112#define bc_file_puts(f, t, s) bc_file_puts_impl(f, t, s)113114#else // BC_ENABLE_HISTORY && !BC_ENABLE_LINE_LIB115116// These make sure that the BcFlushType parameter disappears if history is not117// used, editline is used, or readline is used.118119#define bc_file_putchar(f, t, c) bc_file_putchar_impl(f, c)120#define bc_file_flushErr(f, t) bc_file_flushErr_impl(f)121#define bc_file_flush(f, t) bc_file_flush_impl(f)122#define bc_file_write(f, t, b, n) bc_file_write_impl(f, b, n)123#define bc_file_puts(f, t, s) bc_file_puts_impl(f, s)124125#endif // BC_ENABLE_HISTORY && !BC_ENABLE_LINE_LIB126127#if BC_ENABLE_LINE_LIB128129/**130* Initialize a file.131* @param f The file to initialize.132* @param file The stdio file.133* @param errors_fatal True if errors should be fatal, false otherwise.134*/135void136bc_file_init(BcFile* f, FILE* file, bool errors_fatal);137138#else // BC_ENABLE_LINE_LIB139140/**141* Initialize a file.142* @param f The file to initialize.143* @param fd The file descriptor.144* @param buf The buffer for the file.145* @param cap The capacity of the buffer.146* @param errors_fatal True if errors should be fatal, false otherwise.147*/148void149bc_file_init(BcFile* f, int fd, char* buf, size_t cap, bool errors_fatal);150151#endif // BC_ENABLE_LINE_LIB152153/**154* Frees a file, including flushing it.155* @param f The file to free.156*/157void158bc_file_free(BcFile* f);159160/**161* Print a char into the file.162* @param f The file to print to.163* @param type The flush type.164* @param c The character to write.165*/166void167bc_file_putchar(BcFile* restrict f, BcFlushType type, uchar c);168169/**170* Flush and return an error if it failed. This is meant to be used when needing171* to flush in error situations when an error is already in flight. It would be172* a very bad deal to throw another error.173* @param f The file to flush.174* @param type The flush type.175* @return A status indicating if an error occurred.176*/177BcStatus178bc_file_flushErr(BcFile* restrict f, BcFlushType type);179180/**181* Flush and throw an error on failure.182* @param f The file to flush.183* @param type The flush type.184*/185void186bc_file_flush(BcFile* restrict f, BcFlushType type);187188/**189* Write the contents of buf to the file.190* @param f The file to flush.191* @param type The flush type.192* @param buf The buffer whose contents will be written to the file.193* @param n The length of buf.194*/195void196bc_file_write(BcFile* restrict f, BcFlushType type, const char* buf, size_t n);197198/**199* Write to the file like fprintf would. This is very rudimentary.200* @param f The file to flush.201* @param fmt The format string.202*/203void204bc_file_printf(BcFile* restrict f, const char* fmt, ...);205206/**207* Write to the file like vfprintf would. This is very rudimentary.208* @param f The file to flush.209* @param fmt The format string.210*/211void212bc_file_vprintf(BcFile* restrict f, const char* fmt, va_list args);213214/**215* Write str to the file.216* @param f The file to flush.217* @param type The flush type.218* @param str The string to write to the file.219*/220void221bc_file_puts(BcFile* restrict f, BcFlushType type, const char* str);222223#if BC_ENABLE_HISTORY && !BC_ENABLE_LINE_LIB224225// Some constant flush types for ease of use.226extern const BcFlushType bc_flush_none;227extern const BcFlushType bc_flush_err;228extern const BcFlushType bc_flush_save;229230#endif // BC_ENABLE_HISTORY && !BC_ENABLE_LINE_LIB231232#endif // BC_FILE_H233234235