Cthulhu  0.2.10
Cthulhu compiler collection
io.cpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-3.0-only
2 #include "editor/utils/io.hpp"
3 
4 #include "memory/memory.h"
5 #include "os/os.h"
6 
7 using namespace ctu;
8 
9 void OsError::get_message()
10 {
11  size_t size = os_error_get_string(error, nullptr, 0);
12  msg.resize(size);
13 
14  size_t written = os_error_get_string(error, msg.data(), size);
15  msg.resize(written);
16 }
17 
18 OsError::OsError(os_error_t err)
19  : error(err)
20 {
21  get_message();
22 }
23 
24 bool OsError::success() const
25 {
26  return error == eOsSuccess;
27 }
28 
29 bool OsError::failed() const
30 {
31  return error != eOsSuccess;
32 }
33 
34 const char *OsError::what() const
35 {
36  return msg.c_str();
37 }
38 
39 Io Io::file(const char *path)
40 {
41  return io_file(path, eOsAccessRead, get_global_arena());
42 }
43 
45 {
46  return io_error(io.get());
47 }
48 
49 size_t Io::size() const
50 {
51  return io_size(io.get());
52 }
53 
54 const void *Io::map() const
55 {
56  return io_map(io.get(), eOsProtectRead);
57 }
58 
59 std::string_view Io::text() const
60 {
61  const char *data = static_cast<const char*>(map());
62  return std::string_view{data, size()};
63 }
64 
65 const char *Io::name() const
66 {
67  return io_name(io.get());
68 }
Definition: io.hpp:30
static Io file(const char *path)
Definition: io.cpp:39
const char * name() const
Definition: io.cpp:65
OsError error() const
Definition: io.cpp:44
std::string_view text() const
Definition: io.cpp:59
const void * map() const
Definition: io.cpp:54
size_t size() const
Definition: io.cpp:49
bool success() const
Definition: io.cpp:24
const char * what() const
Definition: io.cpp:34
bool failed() const
Definition: io.cpp:29
OsError(os_error_t error)
Definition: io.cpp:18
CT_NODISCARD size_t size
Definition: scan.h:128
CT_NODISCARD CT_OS_API size_t os_error_get_string(os_error_t error, STA_WRITES(size) char *buffer, size_t size)
convert an os error code to a string writes to a buffer rather than allocating. if size is 0,...
CT_MEMORY_API arena_t * get_global_arena(void)
get the global memory arena
Definition: memory.c:16
CT_NODISCARD CT_IO_API io_t * io_file(const char *path, os_access_t mode, arena_t *arena)
create an IO object from a file
Definition: file.c:125
CT_NODISCARD CT_IO_API os_error_t io_error(const io_t *io)
get the last error from the io object
Definition: io.c:144
CT_NODISCARD CT_IO_API size_t io_size(io_t *io)
get the total size of an io objects contents
Definition: io.c:94
CT_NODISCARD CT_IO_API const char * io_name(const io_t *io)
get the name of an io object
Definition: io.c:112
CT_NODISCARD CT_IO_API void * io_map(io_t *io, os_protect_t protect)
map an io object into memory maps an io objects entire contents into memory.
Definition: io.c:120
Definition: io.hpp:7
@ eOsSuccess
Definition: posix.h:24