HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
file_posix_impl.hpp
1// Copyright Take Vos 2019, 2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../telemetry/telemetry.hpp"
8#include "../utility/utility.hpp"
9#include "../macros.hpp"
10#include <fcntl.h>
11#include <sys/types.h>
12#include <sys/stat.h>
13#include <unistd.h>
14
15hi_export_module(hikogui.file.file_impl);
16
17hi_export namespace hi::inline v1 {
18
19File::File(std::filesystem::path const &path, AccessMode accessMode) : accessMode(accessMode), location(location)
20{
21 int openFlags = 0;
22
23 if (accessMode >= (AccessMode::Read | AccessMode::Write)) {
24 openFlags = O_RDWR;
25 } else if (accessMode >= AccessMode::Read) {
26 openFlags = O_RDONLY;
27 ;
28 } else if (accessMode >= AccessMode::Write) {
29 openFlags = O_WRONLY;
30 ;
31 } else {
32 throw io_error("{}: Invalid AccessMode; expecting Readable and/or Writeable.", location());
33 }
34
35 if (accessMode >= AccessMode::WriteLock) {
36 openFlags |= O_EXLOCK;
37 } else if (accessMode >= AccessMode::ReadLock) {
38 openFlags |= O_SHLOCK;
39 }
40
41 if (accessMode >= (AccessMode::Create | AccessMode::Open)) {
42 openFlags |= O_CREAT;
43 if (accessMode >= AccessMode::Truncate) {
44 openFlags |= O_TRUNC;
45 }
46
47 } else if (accessMode >= AccessMode::Create) {
48 openFlags |= (O_CREAT | O_EXCL);
49
50 } else if (accessMode >= AccessMode::Open) {
51 if (accessMode >= AccessMode::Truncate) {
52 openFlags |= O_TRUNC;
53 }
54
55 } else {
56 throw io_error("{}: Invalid AccessMode; expecting CreateFile and/or OpenFile.", location());
57 }
58
59 // int advise = 0;
60 // if (accessMode >= AccessMode::Random) {
61 // advise = POSIX_FADV_RANDOM;
62 //} if (accessMode >= AccessMode::Sequential) {
63 // advise = POSIX_FADV_SEQUENTIAL;
64 //} else {
65 // advise = 0;
66 //}
67 //
68 // if (accessMode >= AccessMode::NoReuse) {
69 // advise |= POSIX_FADV_NOREUSE;
70 //}
71
72 int permissions = 0666;
73
74 auto const fileName = location.nativePath();
75 if ((fileHandle = ::open(fileName.data(), openFlags, permissions)) == -1) {
76 throw io_error("{}: Could not open file. '{}'", location(), get_last_error_message());
77 }
78}
79
80File::~File() noexcept
81{
82 close();
83}
84
85void File::close()
86{
87 if (fileHandle != -1) {
88 if (::close(fileHandle) != 0) {
89 throw io_error("{}: Could not close file. '{}'", location(), get_last_error_message());
90 }
91 fileHandle = -1;
92 }
93}
94
95std::size_t File::fileSize(std::filesystem::path const &path)
96{
97 auto const name = url.nativePath();
98
99 struct ::stat statbuf;
100
101 if (::stat(name.data(), &statbuf) == -1) {
102 throw io_error("{}: Could not retrieve file attributes. '{}'", location(), get_last_error_message());
103 }
104
105 return narrow_cast<std::size_t>(statbuf.st_size);
106}
107
108} // namespace hi::inline v1
hi_export std::string get_last_error_message(uint32_t error_code)
Get the error message from an error code.
Definition exception_win32_impl.hpp:21
DOXYGEN BUG.
Definition algorithm_misc.hpp:20