file.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2023 Jonathan Müller and lexy contributors
2 // SPDX-License-Identifier: BSL-1.0
3 
4 #ifndef LEXY_INPUT_FILE_HPP_INCLUDED
5 #define LEXY_INPUT_FILE_HPP_INCLUDED
6 
8 #include <lexy/_detail/std.hpp>
9 #include <lexy/input/base.hpp>
10 #include <lexy/input/buffer.hpp>
11 
12 #ifdef LEXY_DISABLE_FILE
13 # error "lexy::read_file() and lexy::read_stdin() have been disabled"
14 #endif
15 
16 namespace lexy
17 {
19 enum class file_error
20 {
21  _success,
23  os_error,
28 };
29 } // namespace lexy
30 
31 namespace lexy::_detail
32 {
33 using file_callback = void (*)(void* user_data, const char* memory, std::size_t size);
34 
35 // Reads the entire contents of the specified file into memory.
36 // On success, invokes the callback before freeing the memory.
37 // On error, returns the error without invoking the callback.
38 //
39 // Do not change ABI, especially with different build configurations!
40 file_error read_file(const char* path, file_callback cb, void* user_data);
41 
42 // Same as above, but reads from stdin.
43 file_error read_stdin(file_callback cb, void* user_data);
44 } // namespace lexy::_detail
45 
46 namespace lexy
47 {
48 template <typename Encoding = default_encoding, typename MemoryResource = void>
50 {
51 public:
52  using encoding = Encoding;
53  using char_type = typename encoding::char_type;
54 
55  explicit operator bool() const noexcept
56  {
57  return _ec == file_error::_success;
58  }
59 
61  {
62  LEXY_PRECONDITION(*this);
63  return _buffer;
64  }
66  {
67  LEXY_PRECONDITION(*this);
68  return LEXY_MOV(_buffer);
69  }
70 
71  file_error error() const noexcept
72  {
73  LEXY_PRECONDITION(!*this);
74  return _ec;
75  }
76 
77 public:
78  // Pretend these two don't exist.
81  : _buffer(LEXY_MOV(buffer)), _ec(ec)
82  {}
83  explicit read_file_result(file_error ec, MemoryResource* resource) noexcept
84  : _buffer(resource), _ec(ec)
85  {
86  LEXY_PRECONDITION(!*this);
87  }
88 
89 private:
92 };
93 
94 template <typename Encoding, encoding_endianness Endian, typename MemoryResource>
96 {
98  MemoryResource* resource;
99 
101 
102  static auto callback()
103  {
104  return [](void* _user_data, const char* memory, std::size_t size) {
105  auto user_data = static_cast<_read_file_user_data*>(_user_data);
106 
107  user_data->buffer
108  = lexy::make_buffer_from_raw<Encoding, Endian>(memory, size, user_data->resource);
109  };
110  }
111 };
112 
114 template <typename Encoding = default_encoding,
115  encoding_endianness Endian = encoding_endianness::bom, typename MemoryResource = void>
116 auto read_file(const char* path,
117  MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
119 {
121  auto error = _detail::read_file(path, user_data.callback(), &user_data);
122  return read_file_result(error, LEXY_MOV(user_data.buffer));
123 }
124 
126 template <typename Encoding = default_encoding,
127  encoding_endianness Endian = encoding_endianness::bom, typename MemoryResource = void>
128 auto read_stdin(MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
130 {
132  auto error = _detail::read_stdin(user_data.callback(), &user_data);
133  return read_file_result(error, LEXY_MOV(user_data.buffer));
134 }
135 } // namespace lexy
136 
137 #endif // LEXY_INPUT_FILE_HPP_INCLUDED
138 
lexy::_detail::read_stdin
file_error read_stdin(file_callback cb, void *user_data)
Definition: file.cpp:190
cx::size
constexpr auto size(const C &c) -> decltype(c.size())
Definition: wildcards.hpp:636
LEXY_MOV
#define LEXY_MOV(...)
Definition: config.hpp:21
lexy::encoding_endianness
encoding_endianness
The endianness used by an encoding.
Definition: encoding.hpp:15
lexy::_read_file_user_data::resource
MemoryResource * resource
Definition: file.hpp:98
magic_enum::char_type
string_view::value_type char_type
Definition: magic_enum.hpp:145
lexy::file_error::permission_denied
@ permission_denied
The file cannot be opened.
lexy::read_file
auto read_file(const char *path, MemoryResource *resource=_detail::get_memory_resource< MemoryResource >()) -> read_file_result< Encoding, MemoryResource >
Reads the file at the specified path into a buffer.
Definition: file.hpp:116
lexy::read_file_result::buffer
lexy::buffer< Encoding, MemoryResource > && buffer() &&noexcept
Definition: file.hpp:65
lexy::read_stdin
auto read_stdin(MemoryResource *resource=_detail::get_memory_resource< MemoryResource >()) -> read_file_result< Encoding, MemoryResource >
Reads stdin into a buffer.
Definition: file.hpp:128
lexy::_read_file_user_data::buffer
lexy::buffer< Encoding, MemoryResource > buffer
Definition: file.hpp:97
lexy::_read_file_user_data
Definition: file.hpp:95
std.hpp
lexy
Definition: any_ref.hpp:12
LEXY_PRECONDITION
#define LEXY_PRECONDITION(Expr)
Definition: assert.hpp:36
lexy::_read_file_user_data::callback
static auto callback()
Definition: file.hpp:102
detail::void
j template void())
Definition: json.hpp:4893
lexy::error
Generic failure.
Definition: error.hpp:14
lexy::read_file_result
Definition: file.hpp:49
lexy::read_file_result::read_file_result
read_file_result(file_error ec, lexy::buffer< Encoding, MemoryResource > &&buffer) noexcept
Definition: file.hpp:79
lexy::_read_file_user_data::_read_file_user_data
_read_file_user_data(MemoryResource *resource)
Definition: file.hpp:100
lexy::read_file_result::char_type
typename encoding::char_type char_type
Definition: file.hpp:53
lexy::file_error::_success
@ _success
lexy::read_file_result::error
file_error error() const noexcept
Definition: file.hpp:71
lexy::file_error
file_error
Errors that might occur while reading the file.
Definition: file.hpp:19
lexy::read_file_result::_ec
file_error _ec
Definition: file.hpp:91
lexy::encoding_endianness::bom
@ bom
lexy::file_error::os_error
@ os_error
An internal OS error, such as failure to read from the file.
lexy::_detail
Definition: any_ref.hpp:12
lexy::buffer
Definition: buffer.hpp:81
lexy::read_file_result::encoding
Encoding encoding
Definition: file.hpp:52
buffer.hpp
lexy::read_file_result::read_file_result
read_file_result(file_error ec, MemoryResource *resource) noexcept
Definition: file.hpp:83
lexy::_detail::file_callback
void(*)(void *user_data, const char *memory, std::size_t size) file_callback
Definition: file.hpp:33
base.hpp
lexy::read_file_result::_buffer
lexy::buffer< Encoding, MemoryResource > _buffer
Definition: file.hpp:90
lexy::read_file_result::buffer
const lexy::buffer< Encoding, MemoryResource > & buffer() const &noexcept
Definition: file.hpp:60
lexy::file_error::file_not_found
@ file_not_found
The file was not found.
lazy_init.hpp
lexy::_detail::read_file
file_error read_file(const char *path, file_callback cb, void *user_data)
Definition: file.cpp:156


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07