xref: /llvm-project/libc/src/stdio/scanf_core/reader.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
1 //===-- Reader definition for scanf -----------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/stdio/scanf_core/reader.h"
10 #include "src/__support/macros/config.h"
11 #include <stddef.h>
12 
13 namespace LIBC_NAMESPACE_DECL {
14 namespace scanf_core {
15 
16 void Reader::ungetc(char c) {
17   --cur_chars_read;
18   if (rb != nullptr && rb->buff_cur > 0) {
19     // While technically c should be written back to the buffer, in scanf we
20     // always write the character that was already there. Additionally, the
21     // buffer is most likely to contain a string that isn't part of a file,
22     // which may not be writable.
23     --(rb->buff_cur);
24     return;
25   }
26   stream_ungetc(static_cast<int>(c), input_stream);
27 }
28 } // namespace scanf_core
29 } // namespace LIBC_NAMESPACE_DECL
30