Skip to content

Commit 3cffce6

Browse files
committed
Extract StdinReader into a shared header
1 parent 8f461ff commit 3cffce6

File tree

3 files changed

+15
-36
lines changed

3 files changed

+15
-36
lines changed

libc/src/stdio/baremetal/CMakeLists.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,24 @@ add_entrypoint_object(
5555
libc.src.__support.CPP.string_view
5656
)
5757

58+
add_header_library(
59+
scanf_internal
60+
HDRS
61+
scanf_internal.h
62+
DEPENDS
63+
libc.src.stdio.scanf_core.reader
64+
libc.src.__support.OSUtil.osutil
65+
)
66+
5867
add_entrypoint_object(
5968
scanf
6069
SRCS
6170
scanf.cpp
6271
HDRS
6372
../scanf.h
6473
DEPENDS
74+
.scanf_internal
6575
libc.src.stdio.scanf_core.scanf_main
66-
libc.src.stdio.scanf_core.reader
6776
libc.src.__support.arg_list
6877
libc.src.__support.OSUtil.osutil
6978
)
@@ -88,8 +97,8 @@ add_entrypoint_object(
8897
HDRS
8998
../vscanf.h
9099
DEPENDS
100+
.scanf_internal
91101
libc.src.stdio.scanf_core.scanf_main
92-
libc.src.stdio.scanf_core.reader
93102
libc.src.__support.arg_list
94103
libc.src.__support.OSUtil.osutil
95104
)

libc/src/stdio/baremetal/scanf.cpp

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,13 @@
1212
#include "src/__support/OSUtil/io.h"
1313
#include "src/__support/arg_list.h"
1414
#include "src/__support/macros/config.h"
15-
#include "src/stdio/scanf_core/reader.h"
15+
#include "src/stdio/baremetal/scanf_internal.h"
1616
#include "src/stdio/scanf_core/scanf_main.h"
1717

1818
#include <stdarg.h>
1919

2020
namespace LIBC_NAMESPACE_DECL {
2121

22-
namespace {
23-
24-
struct StreamReader : scanf_core::Reader<StreamReader> {
25-
LIBC_INLINE char getc() {
26-
char buf[1];
27-
auto result = read_from_stdin(buf, sizeof(buf));
28-
if (result <= 0)
29-
return EOF;
30-
return buf[0];
31-
}
32-
LIBC_INLINE void ungetc(int) {}
33-
};
34-
35-
} // namespace
36-
3722
LLVM_LIBC_FUNCTION(int, scanf, (const char *__restrict format, ...)) {
3823
va_list vlist;
3924
va_start(vlist, format);
@@ -42,7 +27,7 @@ LLVM_LIBC_FUNCTION(int, scanf, (const char *__restrict format, ...)) {
4227
// destruction automatically.
4328
va_end(vlist);
4429

45-
StreamReader reader;
30+
scanf_core::StdinReader reader;
4631
int retval = scanf_core::scanf_main(&reader, format, args);
4732
// This is done to avoid including stdio.h in the internals. On most systems
4833
// EOF is -1, so this will be transformed into just "return retval".

libc/src/stdio/baremetal/vscanf.cpp

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,21 @@
1212
#include "src/__support/OSUtil/io.h"
1313
#include "src/__support/arg_list.h"
1414
#include "src/__support/macros/config.h"
15-
#include "src/stdio/scanf_core/reader.h"
15+
#include "src/stdio/baremetal/scanf_internal.h"
1616
#include "src/stdio/scanf_core/scanf_main.h"
1717

1818
#include <stdarg.h>
1919

2020
namespace LIBC_NAMESPACE_DECL {
2121

22-
namespace {
23-
24-
struct StreamReader : scanf_core::Reader<StreamReader> {
25-
LIBC_INLINE char getc() {
26-
char buf[1];
27-
auto result = read_from_stdin(buf, sizeof(buf));
28-
if (result <= 0)
29-
return EOF;
30-
return buf[0];
31-
}
32-
LIBC_INLINE void ungetc(int) {}
33-
};
34-
35-
} // namespace
36-
3722
LLVM_LIBC_FUNCTION(int, vscanf,
3823
(const char *__restrict format, va_list vlist)) {
3924
internal::ArgList args(vlist); // This holder class allows for easier copying
4025
// and pointer semantics, as well as handling
4126
// destruction automatically.
4227
va_end(vlist);
4328

44-
StreamReader reader;
29+
scanf_core::StdinReader reader;
4530
int retval = scanf_core::scanf_main(&reader, format, args);
4631
// This is done to avoid including stdio.h in the internals. On most systems
4732
// EOF is -1, so this will be transformed into just "return retval".

0 commit comments

Comments
 (0)