1 //===-- Implementation of sscanf --------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/stdio/sscanf.h"
11 #include "src/__support/arg_list.h"
12 #include "src/stdio/scanf_core/reader.h"
13 #include "src/stdio/scanf_core/scanf_main.h"
14 #include "src/stdio/scanf_core/string_reader.h"
19 namespace __llvm_libc
{
21 LLVM_LIBC_FUNCTION(int, sscanf
,
22 (const char *__restrict buffer
,
23 const char *__restrict format
, ...)) {
25 va_start(vlist
, format
);
26 internal::ArgList
args(vlist
); // This holder class allows for easier copying
27 // and pointer semantics, as well as handling
28 // destruction automatically.
30 scanf_core::StringReader
string_reader(buffer
);
31 scanf_core::Reader
reader(&string_reader
);
32 int ret_val
= scanf_core::scanf_main(&reader
, format
, args
);
33 // This is done to avoid including stdio.h in the internals. On most systems
34 // EOF is -1, so this will be transformed into just "return ret_val".
35 return (ret_val
== -1) ? EOF
: ret_val
;
38 } // namespace __llvm_libc