Skip to content

Commit aa40c39

Browse files
[libc] Add vsscanf function
1 parent 3402a1a commit aa40c39

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

libc/spec/stdc.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,13 @@ def StdC : StandardSpec<"stdc"> {
968968
RetValSpec<IntType>,
969969
[ArgSpec<IntType>, ArgSpec<FILEPtr>]
970970
>,
971+
FunctionSpec<
972+
"vsscanf",
973+
RetValSpec<IntType>,
974+
[ArgSpec<ConstCharPtr>,
975+
ArgSpec<ConstCharPtr>,
976+
ArgSpec<VaListType>]
977+
>,
971978
],
972979
[
973980
ObjectSpec<

libc/src/stdio/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,16 @@ add_entrypoint_object(
203203
libc.src.stdio.printf_core.vfprintf_internal
204204
)
205205

206+
add_entrypoint_object(
207+
vsscanf
208+
SRCS
209+
vsscanf.cpp
210+
HDRS
211+
vsscanf.h
212+
DEPENDS
213+
libc.src.__support.arg_list
214+
)
215+
206216
add_stdio_entrypoint_object(
207217
fileno
208218
SRCS

libc/src/stdio/vsscanf.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===-- Implementation of vsscanf -------------------------------*- 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/vsscanf.h"
10+
11+
#include "src/__support/CPP/limits.h"
12+
#include "src/__support/arg_list.h"
13+
#include "src/stdio/scanf_core/reader.h"
14+
#include "src/stdio/scanf_core/scanf_main.h"
15+
16+
#include <stdarg.h>
17+
#include <stdio.h>
18+
19+
namespace LIBC_NAMESPACE {
20+
21+
LLVM_LIBC_FUNCTION(int, vsscanf,
22+
(const char *buffer, const char *format, va_list vlist)) {
23+
internal::ArgList args(vlist); // This holder class allows for easier copying
24+
// and pointer semantics, as well as handling
25+
// destruction automatically.
26+
27+
scanf_core::ReadBuffer rb{const_cast<char *>(buffer),
28+
cpp::numeric_limits<size_t>::max()};
29+
scanf_core::Reader reader(&rb);
30+
int ret_val = scanf_core::scanf_main(&reader, format, args);
31+
// This is done to avoid including stdio.h in the internals. On most systems
32+
// EOF is -1, so this will be transformed into just "return ret_val".
33+
return (ret_val == -1) ? EOF : ret_val;
34+
}
35+
36+
} // namespace LIBC_NAMESPACE

libc/src/stdio/vsscanf.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header of vsscanf ------------------------*- 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+
#ifndef LLVM_LIBC_SRC_STDIO_VSSCANF_H
10+
#define LLVM_LIBC_SRC_STDIO_VSSCANF_H
11+
12+
#include <stdarg.h>
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
int vsscanf(const char *s, const char *format, va_list vlist);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_STDIO_VSSCANF_H

0 commit comments

Comments
 (0)