Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
file.h
Go to the documentation of this file.
1/* Copyright (C) 2022 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/sysdep/filesystem.h" // O_*, S_*
32
33namespace ERR
34{
35 const Status FILE_ACCESS = -110300;
36 const Status FILE_NOT_FOUND = -110301;
37}
38
39// @param oflag: either O_RDONLY or O_WRONLY (in which case O_CREAT and
40// O_TRUNC are added), plus O_DIRECT if aio is desired
41// @return file descriptor or a negative Status
42Status FileOpen(const OsPath& pathname, int oflag);
43void FileClose(int& fd);
44
45class File
46{
47public:
50 {
51 }
52
53 File(const OsPath& pathname, int oflag)
54 {
55 THROW_STATUS_IF_ERR(Open(pathname, oflag));
56 }
57
59 {
60 Close();
61 }
62
63 Status Open(const OsPath& pathName, int openFlag)
64 {
65 Status ret = FileOpen(pathName, openFlag);
67 m_PathName = pathName;
68 m_FileDescriptor = static_cast<int>(ret);
69 m_OpenFlag = openFlag;
70 return INFO::OK;
71 }
72
73 void Close()
74 {
76 }
77
78 const OsPath& Pathname() const
79 {
80 return m_PathName;
81 }
82
83 int Descriptor() const
84 {
85 return m_FileDescriptor;
86 }
87
88 int Flags() const
89 {
90 return m_OpenFlag;
91 }
92
93private:
97};
98
99typedef std::shared_ptr<File> PFile;
100
101#endif // #ifndef INCLUDED_FILE
Definition: file.h:46
int m_OpenFlag
Definition: file.h:96
Status Open(const OsPath &pathName, int openFlag)
Definition: file.h:63
void Close()
Definition: file.h:73
const OsPath & Pathname() const
Definition: file.h:78
int Flags() const
Definition: file.h:88
File(const OsPath &pathname, int oflag)
Definition: file.h:53
OsPath m_PathName
Definition: file.h:94
~File()
Definition: file.h:58
int Descriptor() const
Definition: file.h:83
File()
Definition: file.h:48
int m_FileDescriptor
Definition: file.h:95
Definition: path.h:80
void FileClose(int &fd)
Definition: file.cpp:56
Status FileOpen(const OsPath &pathname, int oflag)
Definition: file.cpp:39
std::shared_ptr< File > PFile
Definition: file.h:99
Definition: debug.h:395
const Status FILE_ACCESS
Definition: file.h:35
const Status FILE_NOT_FOUND
Definition: file.h:36
const Status OK
Definition: status.h:388
#define RETURN_STATUS_IF_ERR(expression)
Definition: status.h:278
#define THROW_STATUS_IF_ERR(expression)
Definition: status.h:313
i64 Status
Error handling system.
Definition: status.h:173