comparison deps/Platinum/ThirdParty/Neptune/Source/Core/NptFile.h @ 0:3425707ddbf6

Initial import (hopefully this mercurial stuff works...)
author fraserofthenight
date Mon, 06 Jul 2009 08:06:28 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3425707ddbf6
1 /*****************************************************************
2 |
3 | Neptune - Files
4 |
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
6 | All rights reserved.
7 |
8 | Redistribution and use in source and binary forms, with or without
9 | modification, are permitted provided that the following conditions are met:
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 | * Neither the name of Axiomatic Systems nor the
16 | names of its contributors may be used to endorse or promote products
17 | derived from this software without specific prior written permission.
18 |
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 |
30 ****************************************************************/
31
32 #ifndef _NPT_FILE_H_
33 #define _NPT_FILE_H_
34
35 /*----------------------------------------------------------------------
36 | includes
37 +---------------------------------------------------------------------*/
38 #include "NptTypes.h"
39 #include "NptStreams.h"
40
41 /*----------------------------------------------------------------------
42 | constants
43 +---------------------------------------------------------------------*/
44 const int NPT_ERROR_NO_SUCH_FILE = NPT_ERROR_BASE_FILE - 0;
45 const int NPT_ERROR_FILE_NOT_OPEN = NPT_ERROR_BASE_FILE - 1;
46 const int NPT_ERROR_FILE_BUSY = NPT_ERROR_BASE_FILE - 2;
47 const int NPT_ERROR_FILE_ALREADY_OPEN = NPT_ERROR_BASE_FILE - 3;
48 const int NPT_ERROR_FILE_NOT_READABLE = NPT_ERROR_BASE_FILE - 4;
49 const int NPT_ERROR_FILE_NOT_WRITABLE = NPT_ERROR_BASE_FILE - 5;
50 const int NPT_ERROR_FILE_NOT_DIRECTORY = NPT_ERROR_BASE_FILE - 6;
51 const int NPT_ERROR_FILE_ALREADY_EXISTS = NPT_ERROR_BASE_FILE - 7;
52 const int NPT_ERROR_FILE_NOT_ENOUGH_SPACE = NPT_ERROR_BASE_FILE - 8;
53 const int NPT_ERROR_DIRECTORY_NOT_EMPTY = NPT_ERROR_BASE_FILE - 9;
54
55 const unsigned int NPT_FILE_OPEN_MODE_READ = 0x01;
56 const unsigned int NPT_FILE_OPEN_MODE_WRITE = 0x02;
57 const unsigned int NPT_FILE_OPEN_MODE_CREATE = 0x04;
58 const unsigned int NPT_FILE_OPEN_MODE_TRUNCATE = 0x08;
59 const unsigned int NPT_FILE_OPEN_MODE_UNBUFFERED = 0x10;
60 const unsigned int NPT_FILE_OPEN_MODE_APPEND = 0x20;
61
62 const unsigned int NPT_FILE_ATTRIBUTE_READ_ONLY = 0x01;
63 const unsigned int NPT_FILE_ATTRIBUTE_LINK = 0x02;
64
65 #define NPT_FILE_STANDARD_INPUT "@STDIN"
66 #define NPT_FILE_STANDARD_OUTPUT "@STDOUT"
67 #define NPT_FILE_STANDARD_ERROR "@STDERR"
68
69 /*----------------------------------------------------------------------
70 | class references
71 +---------------------------------------------------------------------*/
72 class NPT_DataBuffer;
73
74 /*----------------------------------------------------------------------
75 | NPT_FileInfo
76 +---------------------------------------------------------------------*/
77 struct NPT_FileInfo
78 {
79 // types
80 typedef enum {
81 FILE_TYPE_NONE,
82 FILE_TYPE_REGULAR,
83 FILE_TYPE_DIRECTORY,
84 FILE_TYPE_SPECIAL,
85 FILE_TYPE_OTHER
86 } FileType;
87
88 // constructor
89 NPT_FileInfo() : m_Type(FILE_TYPE_NONE), m_Size(0), m_AttributesMask(0), m_Attributes(0) {}
90
91 // members
92 FileType m_Type;
93 NPT_UInt64 m_Size;
94 NPT_Flags m_AttributesMask;
95 NPT_Flags m_Attributes;
96 };
97
98 /*----------------------------------------------------------------------
99 | NPT_FilePath
100 +---------------------------------------------------------------------*/
101 class NPT_FilePath
102 {
103 public:
104 // class members
105 static const NPT_String Separator;
106
107 // class methods
108 static NPT_String BaseName(const char* path, bool with_extension = true);
109 static NPT_String DirectoryName(const char* path);
110 static NPT_String FileExtension(const char* path);
111 static NPT_String Create(const char* directory, const char* base);
112
113 private:
114 NPT_FilePath() {} // this class can't have instances
115 };
116
117 /*----------------------------------------------------------------------
118 | NPT_FileInterface
119 +---------------------------------------------------------------------*/
120 class NPT_FileInterface
121 {
122 public:
123 // types
124 typedef unsigned int OpenMode;
125
126 // constructors and destructor
127 virtual ~NPT_FileInterface() {}
128
129 // methods
130 virtual NPT_Result Open(OpenMode mode) = 0;
131 virtual NPT_Result Close() = 0;
132 virtual NPT_Result GetSize(NPT_LargeSize& size) = 0;
133 virtual NPT_Result GetInputStream(NPT_InputStreamReference& stream) = 0;
134 virtual NPT_Result GetOutputStream(NPT_OutputStreamReference& stream) = 0;
135 };
136
137 /*----------------------------------------------------------------------
138 | NPT_File
139 +---------------------------------------------------------------------*/
140 class NPT_File : public NPT_FileInterface
141 {
142 public:
143 // class methods
144 static NPT_Result GetRoots(NPT_List<NPT_String>& roots);
145 static NPT_Result GetInfo(const char* path, NPT_FileInfo* info = NULL);
146 static NPT_Result GetCount(const char* path, NPT_Cardinal& count);
147 static bool Exists(const char* path) { return NPT_SUCCEEDED(GetInfo(path)); }
148 static NPT_Result Delete(const char* path);
149 static NPT_Result DeleteFile(const char* path);
150 static NPT_Result DeleteDirectory(const char* path);
151 static NPT_Result Rename(const char* from_path, const char* to_path);
152 static NPT_Result ListDirectory(const char* path, NPT_List<NPT_String>& entries, NPT_Ordinal start = 0, NPT_Cardinal count = 0);
153 static NPT_Result CreateDirectory(const char* path);
154 static NPT_Result GetWorkingDirectory(NPT_String& path);
155 static NPT_Result Load(const char* path, NPT_DataBuffer& buffer, NPT_FileInterface::OpenMode mode = NPT_FILE_OPEN_MODE_READ);
156 static NPT_Result Load(const char* path, NPT_String& data, NPT_FileInterface::OpenMode mode = NPT_FILE_OPEN_MODE_READ);
157 static NPT_Result Save(const char* path, NPT_String& data);
158 static NPT_Result Save(const char* path, const NPT_DataBuffer& buffer);
159
160 // constructors and destructor
161 NPT_File(const char* path);
162 ~NPT_File() { delete m_Delegate; }
163
164 // methods
165 NPT_Result Load(NPT_DataBuffer& buffer);
166 NPT_Result Save(const NPT_DataBuffer& buffer);
167 const NPT_String& GetPath() { return m_Path; }
168 //NPT_Result GetSize(NPT_LargeSize &size);
169 NPT_Result GetInfo(NPT_FileInfo& info);
170 NPT_Result ListDirectory(NPT_List<NPT_String>& entries);
171 NPT_Result Rename(const char* path);
172 NPT_Result GetCount(NPT_Cardinal& count);
173
174 // NPT_FileInterface methods
175 NPT_Result Open(OpenMode mode) {
176 return m_Delegate->Open(mode);
177 }
178 NPT_Result Close() {
179 return m_Delegate->Close();
180 }
181 NPT_Result GetSize(NPT_LargeSize& size) {
182 return m_Delegate->GetSize(size);
183 }
184 NPT_Result GetInputStream(NPT_InputStreamReference& stream) {
185 return m_Delegate->GetInputStream(stream);
186 }
187 NPT_Result GetOutputStream(NPT_OutputStreamReference& stream) {
188 return m_Delegate->GetOutputStream(stream);
189 }
190
191 // operators
192 NPT_File& operator=(const NPT_File& file);
193
194 protected:
195 // members
196 NPT_FileInterface* m_Delegate;
197 NPT_String m_Path;
198 NPT_FileInfo m_Info;
199 };
200
201 #endif // _NPT_FILE_H_