xref: /llvm-project/libc/src/stdio/sscanf.cpp (revision 1ae0dae368e4bbf2177603d5c310e794c4fd0bd8)
136991d83SMichael Jones //===-- Implementation of sscanf --------------------------------*- C++ -*-===//
236991d83SMichael Jones //
336991d83SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
436991d83SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
536991d83SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
636991d83SMichael Jones //
736991d83SMichael Jones //===----------------------------------------------------------------------===//
836991d83SMichael Jones 
936991d83SMichael Jones #include "src/stdio/sscanf.h"
1036991d83SMichael Jones 
11a5a008ffSmichaelrj-google #include "src/__support/CPP/limits.h"
1236991d83SMichael Jones #include "src/__support/arg_list.h"
135ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
1436991d83SMichael Jones #include "src/stdio/scanf_core/reader.h"
1536991d83SMichael Jones #include "src/stdio/scanf_core/scanf_main.h"
1636991d83SMichael Jones 
175aed6d67SMichael Jones #include "hdr/stdio_macros.h"
185aed6d67SMichael Jones #include "hdr/types/FILE.h"
1936991d83SMichael Jones #include <stdarg.h>
2036991d83SMichael Jones 
215ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
2236991d83SMichael Jones 
2336991d83SMichael Jones LLVM_LIBC_FUNCTION(int, sscanf,
2436991d83SMichael Jones                    (const char *__restrict buffer,
2536991d83SMichael Jones                     const char *__restrict format, ...)) {
2636991d83SMichael Jones   va_list vlist;
2736991d83SMichael Jones   va_start(vlist, format);
2836991d83SMichael Jones   internal::ArgList args(vlist); // This holder class allows for easier copying
2936991d83SMichael Jones                                  // and pointer semantics, as well as handling
3036991d83SMichael Jones                                  // destruction automatically.
3136991d83SMichael Jones   va_end(vlist);
32*1ae0dae3SMichael Jones   scanf_core::ReadBuffer rb{buffer, cpp::numeric_limits<size_t>::max()};
33a5a008ffSmichaelrj-google   scanf_core::Reader reader(&rb);
3436991d83SMichael Jones   int ret_val = scanf_core::scanf_main(&reader, format, args);
3536991d83SMichael Jones   // This is done to avoid including stdio.h in the internals. On most systems
3636991d83SMichael Jones   // EOF is -1, so this will be transformed into just "return ret_val".
3736991d83SMichael Jones   return (ret_val == -1) ? EOF : ret_val;
3836991d83SMichael Jones }
3936991d83SMichael Jones 
405ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
41