Michael Jones | 36991d8 | 2022-10-19 13:36:59 -0700 | [diff] [blame] | 1 | //===-- Implementation of sscanf --------------------------------*- 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/sscanf.h" |
| 10 | |
michaelrj-google | a5a008f | 2023-09-22 12:50:02 -0700 | [diff] [blame] | 11 | #include "src/__support/CPP/limits.h" |
Michael Jones | 36991d8 | 2022-10-19 13:36:59 -0700 | [diff] [blame] | 12 | #include "src/__support/arg_list.h" |
Petr Hosek | 5ff3ff3 | 2024-07-12 09:28:41 -0700 | [diff] [blame] | 13 | #include "src/__support/macros/config.h" |
Michael Jones | 36991d8 | 2022-10-19 13:36:59 -0700 | [diff] [blame] | 14 | #include "src/stdio/scanf_core/reader.h" |
| 15 | #include "src/stdio/scanf_core/scanf_main.h" |
Michael Jones | 36991d8 | 2022-10-19 13:36:59 -0700 | [diff] [blame] | 16 | |
Michael Jones | 5aed6d6 | 2024-07-11 11:09:51 -0700 | [diff] [blame] | 17 | #include "hdr/stdio_macros.h" |
| 18 | #include "hdr/types/FILE.h" |
Michael Jones | 36991d8 | 2022-10-19 13:36:59 -0700 | [diff] [blame] | 19 | #include <stdarg.h> |
Michael Jones | 36991d8 | 2022-10-19 13:36:59 -0700 | [diff] [blame] | 20 | |
Petr Hosek | 5ff3ff3 | 2024-07-12 09:28:41 -0700 | [diff] [blame] | 21 | namespace LIBC_NAMESPACE_DECL { |
Michael Jones | 36991d8 | 2022-10-19 13:36:59 -0700 | [diff] [blame] | 22 | |
| 23 | LLVM_LIBC_FUNCTION(int, sscanf, |
| 24 | (const char *__restrict buffer, |
| 25 | const char *__restrict format, ...)) { |
| 26 | va_list vlist; |
| 27 | va_start(vlist, format); |
| 28 | internal::ArgList args(vlist); // This holder class allows for easier copying |
| 29 | // and pointer semantics, as well as handling |
| 30 | // destruction automatically. |
| 31 | va_end(vlist); |
Michael Jones | 1ae0dae | 2024-11-11 14:28:36 -0800 | [diff] [blame] | 32 | scanf_core::ReadBuffer rb{buffer, cpp::numeric_limits<size_t>::max()}; |
michaelrj-google | a5a008f | 2023-09-22 12:50:02 -0700 | [diff] [blame] | 33 | scanf_core::Reader reader(&rb); |
Michael Jones | 36991d8 | 2022-10-19 13:36:59 -0700 | [diff] [blame] | 34 | int ret_val = scanf_core::scanf_main(&reader, format, args); |
| 35 | // This is done to avoid including stdio.h in the internals. On most systems |
| 36 | // EOF is -1, so this will be transformed into just "return ret_val". |
| 37 | return (ret_val == -1) ? EOF : ret_val; |
| 38 | } |
| 39 | |
Petr Hosek | 5ff3ff3 | 2024-07-12 09:28:41 -0700 | [diff] [blame] | 40 | } // namespace LIBC_NAMESPACE_DECL |