Skip to content

Commit 2bea940

Browse files
committed
[libc] Support _IONBF buffering for read_unlocked
- Add the functions read_unlocked_nbf and read_unlocked_fbf.
1 parent fbc18b8 commit 2bea940

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

libc/src/__support/File/file.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,17 @@ FileIOResult File::read_unlocked(void *data, size_t len) {
190190

191191
prev_op = FileOp::READ;
192192

193+
if (bufmode == _IONBF) { // unbuffered.
194+
return read_unlocked_nbf(static_cast<uint8_t *>(data), len);
195+
} else if (bufmode == _IOFBF) { // fully buffered
196+
return read_unlocked_fbf(static_cast<uint8_t *>(data), len);
197+
} else /*if (bufmode == _IOLBF) */ { // line buffered
198+
// There is no line buffered mode for read. Use fully buffer instead.
199+
return read_unlocked_fbf(static_cast<uint8_t *>(data), len);
200+
}
201+
}
202+
203+
FileIOResult File::read_unlocked_fbf(uint8_t *data, size_t len) {
193204
cpp::span<uint8_t> bufref(static_cast<uint8_t *>(buf), bufsize);
194205
cpp::span<uint8_t> dataref(static_cast<uint8_t *>(data), len);
195206

@@ -245,6 +256,18 @@ FileIOResult File::read_unlocked(void *data, size_t len) {
245256
return {transfer_size + available_data, result.error};
246257
}
247258

259+
FileIOResult File::read_unlocked_nbf(uint8_t *data, size_t len) {
260+
auto result = platform_read(this, data, len);
261+
262+
if (result.has_error() || result < len) {
263+
if (!result.has_error())
264+
eof = true;
265+
else
266+
err = true;
267+
}
268+
return result;
269+
}
270+
248271
int File::ungetc_unlocked(int c) {
249272
// There is no meaning to unget if:
250273
// 1. You are trying to push back EOF.

libc/src/__support/File/file.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ class File {
280280
FileIOResult write_unlocked_fbf(const uint8_t *data, size_t len);
281281
FileIOResult write_unlocked_nbf(const uint8_t *data, size_t len);
282282

283+
FileIOResult read_unlocked_fbf(uint8_t *data, size_t len);
284+
FileIOResult read_unlocked_nbf(uint8_t *data, size_t len);
285+
283286
constexpr void adjust_buf() {
284287
if (read_allowed() && (buf == nullptr || bufsize == 0)) {
285288
// We should allow atleast one ungetc operation.

0 commit comments

Comments
 (0)