summaryrefslogtreecommitdiff
path: root/libc/src/stdio/scanf_core/reader.cpp
blob: 4834f4d6a6e79553a9ce9d2dc3258d945abc101f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//===-- Reader definition for scanf -----------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdio/scanf_core/reader.h"
#include <stddef.h>

namespace __llvm_libc {
namespace scanf_core {

char Reader::getc() {
  ++cur_chars_read;
  if (reader_type == ReaderType::String) {
    return string_reader->get_char();
  } else {
    return file_reader->get_char();
  }
}

void Reader::ungetc(char c) {
  --cur_chars_read;
  if (reader_type == ReaderType::String) {
    // The string reader ignores the char c passed to unget since it doesn't
    // need to place anything back into a buffer, and modifying the source
    // string would be dangerous.
    return string_reader->unget_char();
  } else {
    return file_reader->unget_char(c);
  }
}

bool Reader::has_error() {
  if (reader_type == ReaderType::File) {
    return file_reader->has_error();
  }
  return false;
}

} // namespace scanf_core
} // namespace __llvm_libc