Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
file.h
Go to the documentation of this file.
1/* Copyright (C) 2025 Wildfire Games.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23/*
24 * simple POSIX file wrapper.
25 */
26
27#ifndef INCLUDED_FILE
28#define INCLUDED_FILE
29
30#include "lib/os_path.h"
31#include "lib/status.h"
32
33#include <memory>
34
35namespace ERR
36{
37 const Status FILE_ACCESS = -110300;
38 const Status FILE_NOT_FOUND = -110301;
39}
40
41// @param oflag: either O_RDONLY or O_WRONLY (in which case O_CREAT and
42// O_TRUNC are added), plus O_DIRECT if aio is desired
43// @return file descriptor or a negative Status
44Status FileOpen(const OsPath& pathname, int oflag);
45void FileClose(int& fd);
46
47class File
48{
49public:
52 {
53 }
54
55 File(const OsPath& pathname, int oflag)
56 {
57 THROW_STATUS_IF_ERR(Open(pathname, oflag));
58 }
59
61 {
62 Close();
63 }
64
65 Status Open(const OsPath& pathName, int openFlag)
66 {
67 Status ret = FileOpen(pathName, openFlag);
69 m_PathName = pathName;
70 m_FileDescriptor = static_cast<int>(ret);
71 m_OpenFlag = openFlag;
72 return INFO::OK;
73 }
74
75 void Close()
76 {
78 }
79
80 const OsPath& Pathname() const
81 {
82 return m_PathName;
83 }
84
85 int Descriptor() const
86 {
87 return m_FileDescriptor;
88 }
89
90 int Flags() const
91 {
92 return m_OpenFlag;
93 }
94
95private:
99};
100
101typedef std::shared_ptr<File> PFile;
102
103#endif // #ifndef INCLUDED_FILE
Definition: file.h:48
int m_OpenFlag
Definition: file.h:98
Status Open(const OsPath &pathName, int openFlag)
Definition: file.h:65
void Close()
Definition: file.h:75
const OsPath & Pathname() const
Definition: file.h:80
int Flags() const
Definition: file.h:90
File(const OsPath &pathname, int oflag)
Definition: file.h:55
OsPath m_PathName
Definition: file.h:96
~File()
Definition: file.h:60
int Descriptor() const
Definition: file.h:85
File()
Definition: file.h:50
int m_FileDescriptor
Definition: file.h:97
Definition: path.h:84
void FileClose(int &fd)
Definition: file.cpp:64
Status FileOpen(const OsPath &pathname, int oflag)
Definition: file.cpp:47
std::shared_ptr< File > PFile
Definition: file.h:101
Definition: debug.h:340
const Status FILE_ACCESS
Definition: file.h:37
const Status FILE_NOT_FOUND
Definition: file.h:38
const Status OK
Definition: status.h:394
#define RETURN_STATUS_IF_ERR(expression)
Definition: status.h:284
std::int64_t Status
Error handling system.
Definition: status.h:174
#define THROW_STATUS_IF_ERR(expression)
Definition: status.h:319