Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/bc/include/file.h
39534 views
1
/*
2
* *****************************************************************************
3
*
4
* SPDX-License-Identifier: BSD-2-Clause
5
*
6
* Copyright (c) 2018-2025 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 <history.h>
42
#include <vector.h>
43
44
#define BC_FILE_ULL_LENGTH (21)
45
46
#if BC_ENABLE_LINE_LIB
47
48
#include <stdio.h>
49
50
/// The file struct.
51
typedef struct BcFile
52
{
53
// The file. This is here simply to make the line lib code as compatible
54
// with the existing code as possible.
55
FILE* f;
56
57
// True if errors should be fatal, false otherwise.
58
bool errors_fatal;
59
60
} BcFile;
61
62
#else // BC_ENABLE_LINE_LIB
63
64
/// The file struct.
65
typedef struct BcFile
66
{
67
// The actual file descriptor.
68
int fd;
69
70
// True if errors should be fatal, false otherwise.
71
bool errors_fatal;
72
73
// The buffer for the file.
74
char* buf;
75
76
// The length (number of actual chars) in the buffer.
77
size_t len;
78
79
// The capacity (max number of chars) of the buffer.
80
size_t cap;
81
82
} BcFile;
83
84
#endif // BC_ENABLE_LINE_LIB
85
86
#if BC_ENABLE_HISTORY && !BC_ENABLE_LINE_LIB
87
88
/// Types of flushing. These are important because of history and printing
89
/// strings without newlines, something that users could use as their own
90
/// prompts.
91
typedef enum BcFlushType
92
{
93
/// Do not clear the stored partial line, but don't add to it.
94
BC_FLUSH_NO_EXTRAS_NO_CLEAR,
95
96
/// Do not clear the stored partial line and add to it.
97
BC_FLUSH_SAVE_EXTRAS_NO_CLEAR,
98
99
/// Clear the stored partial line and do not save the new stuff either.
100
BC_FLUSH_NO_EXTRAS_CLEAR,
101
102
/// Clear the stored partial line, but save the new stuff.
103
BC_FLUSH_SAVE_EXTRAS_CLEAR,
104
105
} BcFlushType;
106
107
// These are here to satisfy a clang warning about recursive macros.
108
109
#define bc_file_putchar(f, t, c) bc_file_putchar_impl(f, t, c)
110
#define bc_file_flushErr(f, t) bc_file_flushErr_impl(f, t)
111
#define bc_file_flush(f, t) bc_file_flush_impl(f, t)
112
#define bc_file_write(f, t, b, n) bc_file_write_impl(f, t, b, n)
113
#define bc_file_puts(f, t, s) bc_file_puts_impl(f, t, s)
114
115
#else // BC_ENABLE_HISTORY && !BC_ENABLE_LINE_LIB
116
117
// These make sure that the BcFlushType parameter disappears if history is not
118
// used, editline is used, or readline is used.
119
120
#define bc_file_putchar(f, t, c) bc_file_putchar_impl(f, c)
121
#define bc_file_flushErr(f, t) bc_file_flushErr_impl(f)
122
#define bc_file_flush(f, t) bc_file_flush_impl(f)
123
#define bc_file_write(f, t, b, n) bc_file_write_impl(f, b, n)
124
#define bc_file_puts(f, t, s) bc_file_puts_impl(f, s)
125
126
#endif // BC_ENABLE_HISTORY && !BC_ENABLE_LINE_LIB
127
128
#if BC_ENABLE_LINE_LIB
129
130
/**
131
* Initialize a file.
132
* @param f The file to initialize.
133
* @param file The stdio file.
134
* @param errors_fatal True if errors should be fatal, false otherwise.
135
*/
136
void
137
bc_file_init(BcFile* f, FILE* file, bool errors_fatal);
138
139
#else // BC_ENABLE_LINE_LIB
140
141
/**
142
* Initialize a file.
143
* @param f The file to initialize.
144
* @param fd The file descriptor.
145
* @param buf The buffer for the file.
146
* @param cap The capacity of the buffer.
147
* @param errors_fatal True if errors should be fatal, false otherwise.
148
*/
149
void
150
bc_file_init(BcFile* f, int fd, char* buf, size_t cap, bool errors_fatal);
151
152
#endif // BC_ENABLE_LINE_LIB
153
154
/**
155
* Frees a file, including flushing it.
156
* @param f The file to free.
157
*/
158
void
159
bc_file_free(BcFile* f);
160
161
/**
162
* Print a char into the file.
163
* @param f The file to print to.
164
* @param type The flush type.
165
* @param c The character to write.
166
*/
167
void
168
bc_file_putchar(BcFile* restrict f, BcFlushType type, uchar c);
169
170
/**
171
* Flush and return an error if it failed. This is meant to be used when needing
172
* to flush in error situations when an error is already in flight. It would be
173
* a very bad deal to throw another error.
174
* @param f The file to flush.
175
* @param type The flush type.
176
* @return A status indicating if an error occurred.
177
*/
178
BcStatus
179
bc_file_flushErr(BcFile* restrict f, BcFlushType type);
180
181
/**
182
* Flush and throw an error on failure.
183
* @param f The file to flush.
184
* @param type The flush type.
185
*/
186
void
187
bc_file_flush(BcFile* restrict f, BcFlushType type);
188
189
/**
190
* Write the contents of buf to the file.
191
* @param f The file to flush.
192
* @param type The flush type.
193
* @param buf The buffer whose contents will be written to the file.
194
* @param n The length of buf.
195
*/
196
void
197
bc_file_write(BcFile* restrict f, BcFlushType type, const char* buf, size_t n);
198
199
/**
200
* Write to the file like fprintf would. This is very rudimentary.
201
* @param f The file to flush.
202
* @param fmt The format string.
203
*/
204
void
205
bc_file_printf(BcFile* restrict f, const char* fmt, ...);
206
207
/**
208
* Write to the file like vfprintf would. This is very rudimentary.
209
* @param f The file to flush.
210
* @param fmt The format string.
211
*/
212
void
213
bc_file_vprintf(BcFile* restrict f, const char* fmt, va_list args);
214
215
/**
216
* Write str to the file.
217
* @param f The file to flush.
218
* @param type The flush type.
219
* @param str The string to write to the file.
220
*/
221
void
222
bc_file_puts(BcFile* restrict f, BcFlushType type, const char* str);
223
224
#if BC_ENABLE_HISTORY && !BC_ENABLE_LINE_LIB
225
226
// Some constant flush types for ease of use.
227
extern const BcFlushType bc_flush_none;
228
extern const BcFlushType bc_flush_err;
229
extern const BcFlushType bc_flush_save;
230
231
#endif // BC_ENABLE_HISTORY && !BC_ENABLE_LINE_LIB
232
233
#endif // BC_FILE_H
234
235