xref: /llvm-project/libc/src/stdio/scanf_core/ptr_converter.cpp (revision a0c4f854cad2b97e44a1b58dc1fd982e1c4d60f3)
1 //===-- Int type specifier converters for scanf -----------------*- 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/scanf_core/ptr_converter.h"
10 
11 #include "src/__support/ctype_utils.h"
12 #include "src/__support/macros/config.h"
13 #include "src/stdio/scanf_core/converter_utils.h"
14 #include "src/stdio/scanf_core/core_structs.h"
15 #include "src/stdio/scanf_core/int_converter.h"
16 #include "src/stdio/scanf_core/reader.h"
17 
18 #include <stddef.h>
19 
20 namespace LIBC_NAMESPACE_DECL {
21 namespace scanf_core {
22 int convert_pointer(Reader *reader, const FormatSection &to_conv) {
23   static const char nullptr_string[] = "(nullptr)";
24 
25   // Check if it's exactly the nullptr string, if so then it's a nullptr.
26   char cur_char = reader->getc();
27   size_t i = 0;
28   for (; i < (sizeof(nullptr_string) - 1) &&
29          internal::tolower(cur_char) == nullptr_string[i];
30        ++i) {
31     cur_char = reader->getc();
32   }
33   if (i == (sizeof(nullptr_string) - 1)) {
34     *reinterpret_cast<void **>(to_conv.output_ptr) = nullptr;
35     return READ_OK;
36   } else if (i > 0) {
37     return MATCHING_FAILURE;
38   }
39 
40   reader->ungetc(cur_char);
41 
42   // Else treat it as a hex int
43   return convert_int(reader, to_conv);
44 }
45 } // namespace scanf_core
46 } // namespace LIBC_NAMESPACE_DECL
47