Pyrogenesis  trunk
file.h
Go to the documentation of this file.
1 /* Copyright (c) 2010 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 
33 namespace 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
42 LIB_API Status FileOpen(const OsPath& pathname, int oflag);
43 LIB_API void FileClose(int& fd);
44 
45 class File
46 {
47 public:
48  File()
49  : pathname(), fd(-1)
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 oflag)
64  {
65  Status ret = FileOpen(pathname, oflag);
67  this->pathname = pathname;
68  this->fd = (int)ret;
69  this->oflag = oflag;
70  return INFO::OK;
71  }
72 
73  void Close()
74  {
75  FileClose(fd);
76  }
77 
78  const OsPath& Pathname() const
79  {
80  return pathname;
81  }
82 
83  int Descriptor() const
84  {
85  return fd;
86  }
87 
88  int Flags() const
89  {
90  return oflag;
91  }
92 
93 private:
95  int fd;
96  int oflag;
97 };
98 
99 typedef shared_ptr<File> PFile;
100 
101 #endif // #ifndef INCLUDED_FILE
const OsPath & Pathname() const
Definition: file.h:78
OsPath pathname
Definition: file.h:94
const Status OK
Definition: status.h:386
int Flags() const
Definition: file.h:88
int fd
Definition: file.h:95
void Close()
Definition: file.h:73
LIB_API Status FileOpen(const OsPath &pathname, int oflag)
Definition: file.cpp:39
shared_ptr< File > PFile
Definition: file.h:99
Definition: path.h:77
int Descriptor() const
Definition: file.h:83
File()
Definition: file.h:48
i64 Status
Error handling system.
Definition: status.h:171
const Status FILE_NOT_FOUND
Definition: file.h:36
Status Open(const OsPath &pathname, int oflag)
Definition: file.h:63
Introduction
Definition: debug.h:404
#define THROW_STATUS_IF_ERR(expression)
Definition: status.h:311
int oflag
Definition: file.h:96
~File()
Definition: file.h:58
File(const OsPath &pathname, int oflag)
Definition: file.h:53
const Status FILE_ACCESS
Definition: file.h:35
LIB_API void FileClose(int &fd)
Definition: file.cpp:56
Definition: file.h:45
#define RETURN_STATUS_IF_ERR(expression)
Definition: status.h:276