|
11 | 11 |
|
12 | 12 | #include "src/__support/macros/config.h" |
13 | 13 |
|
| 14 | +#include "hdr/types/FILE.h" |
14 | 15 | #include "src/__support/CPP/string_view.h" |
15 | 16 | #include "src/__support/CPP/type_traits.h" |
16 | 17 | #include "src/__support/FPUtil/FPBits.h" |
| 18 | +#include "src/__support/File/file.h" |
17 | 19 | #include "src/stdio/printf_core/printf_config.h" |
18 | 20 |
|
19 | 21 | #include <inttypes.h> |
@@ -144,6 +146,49 @@ template <typename T> LIBC_INLINE constexpr TypeDesc type_desc_from_type() { |
144 | 146 |
|
145 | 147 | // This is the value to be returned by conversions when no error has occurred. |
146 | 148 | constexpr int WRITE_OK = 0; |
| 149 | +// These are the printf return values for when an error has occurred. They are |
| 150 | +// all negative, and should be distinct. |
| 151 | +constexpr int FILE_WRITE_ERROR = -1; |
| 152 | +constexpr int FILE_STATUS_ERROR = -2; |
| 153 | +constexpr int NULLPTR_WRITE_ERROR = -3; |
| 154 | +constexpr int INT_CONVERSION_ERROR = -4; |
| 155 | +constexpr int FIXED_POINT_CONVERSION_ERROR = -5; |
| 156 | +constexpr int ALLOCATION_ERROR = -6; |
| 157 | + |
| 158 | +// TODO: const file ptr, and mutable for locks? |
| 159 | +LIBC_INLINE static int map_internal_to_errno(int internal_errno, |
| 160 | + FILE *f = nullptr) { |
| 161 | +#if !defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE) |
| 162 | + LIBC_NAMESPACE::File *file = reinterpret_cast<LIBC_NAMESPACE::File *>(f); |
| 163 | +#else |
| 164 | + LIBC_NAMESPACE::File *file = nullptr; |
| 165 | + (void)f; |
| 166 | +#endif |
| 167 | + |
| 168 | + switch (-internal_errno) { |
| 169 | + case -WRITE_OK: |
| 170 | + return 0; |
| 171 | + case FILE_WRITE_ERROR: |
| 172 | + if (file == nullptr) |
| 173 | + return EIO; |
| 174 | + return file->error() ? file->error() : EIO; // TODO: or unlocked? |
| 175 | + case FILE_STATUS_ERROR: |
| 176 | + return EIO; // TODO: or what? |
| 177 | + case NULLPTR_WRITE_ERROR: |
| 178 | + return EINVAL; |
| 179 | + case INT_CONVERSION_ERROR: |
| 180 | + return ERANGE; |
| 181 | + case FIXED_POINT_CONVERSION_ERROR: |
| 182 | + return EINVAL; |
| 183 | + case ALLOCATION_ERROR: |
| 184 | + return ENOMEM; |
| 185 | + default: |
| 186 | + LIBC_ASSERT(false && |
| 187 | + "Invalid printf error code passed to map_internal_to_errno"); |
| 188 | + return EINVAL; |
| 189 | + } |
| 190 | +} |
| 191 | + |
147 | 192 | } // namespace printf_core |
148 | 193 | } // namespace LIBC_NAMESPACE_DECL |
149 | 194 |
|
|
0 commit comments