1 /*****************************************************************
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
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.
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.
30 ****************************************************************/
35 /*----------------------------------------------------------------------
37 +---------------------------------------------------------------------*/
39 #include "NptStreams.h"
42 /*----------------------------------------------------------------------
44 +---------------------------------------------------------------------*/
45 const int NPT_ERROR_NO_SUCH_FILE
= NPT_ERROR_BASE_FILE
- 0;
46 const int NPT_ERROR_FILE_NOT_OPEN
= NPT_ERROR_BASE_FILE
- 1;
47 const int NPT_ERROR_FILE_BUSY
= NPT_ERROR_BASE_FILE
- 2;
48 const int NPT_ERROR_FILE_ALREADY_OPEN
= NPT_ERROR_BASE_FILE
- 3;
49 const int NPT_ERROR_FILE_NOT_READABLE
= NPT_ERROR_BASE_FILE
- 4;
50 const int NPT_ERROR_FILE_NOT_WRITABLE
= NPT_ERROR_BASE_FILE
- 5;
51 const int NPT_ERROR_FILE_NOT_DIRECTORY
= NPT_ERROR_BASE_FILE
- 6;
52 const int NPT_ERROR_FILE_ALREADY_EXISTS
= NPT_ERROR_BASE_FILE
- 7;
53 const int NPT_ERROR_FILE_NOT_ENOUGH_SPACE
= NPT_ERROR_BASE_FILE
- 8;
54 const int NPT_ERROR_DIRECTORY_NOT_EMPTY
= NPT_ERROR_BASE_FILE
- 9;
58 * Use a combination of these flags to indicate how a file should be opened
59 * Note all combinations of flags are valid or meaningful:
60 * If NPT_FILE_OPEN_MODE_WRITE is not set, then NPT_FILE_OPEN_MODE_CREATE,
61 * NPT_FILE_OPEN_MODE_TRUNCATE and NPT_FILE_OPEN_MODE_APPEND are ignored.
62 * If NPT_FILE_OPEN_MODE_APPEND is set, then NPT_FILE_OPEN_MODE_CREATE is
63 * automatically implied whether it is set or not.
64 * NPT_FILE_OPEN_MODE_CREATE and NPT_FILE_OPEN_MODE_TRUNCATE imply each
65 * other (if one is set, the other one is automatically implied)
67 const unsigned int NPT_FILE_OPEN_MODE_READ
= 0x01;
68 const unsigned int NPT_FILE_OPEN_MODE_WRITE
= 0x02;
69 const unsigned int NPT_FILE_OPEN_MODE_CREATE
= 0x04;
70 const unsigned int NPT_FILE_OPEN_MODE_TRUNCATE
= 0x08;
71 const unsigned int NPT_FILE_OPEN_MODE_UNBUFFERED
= 0x10;
72 const unsigned int NPT_FILE_OPEN_MODE_APPEND
= 0x20;
74 const unsigned int NPT_FILE_ATTRIBUTE_READ_ONLY
= 0x01;
75 const unsigned int NPT_FILE_ATTRIBUTE_LINK
= 0x02;
77 #define NPT_FILE_STANDARD_INPUT "@STDIN"
78 #define NPT_FILE_STANDARD_OUTPUT "@STDOUT"
79 #define NPT_FILE_STANDARD_ERROR "@STDERR"
81 /*----------------------------------------------------------------------
83 +---------------------------------------------------------------------*/
86 /*----------------------------------------------------------------------
88 +---------------------------------------------------------------------*/
101 NPT_FileInfo() : m_Type(FILE_TYPE_NONE
), m_Size(0), m_AttributesMask(0), m_Attributes(0) {}
106 NPT_Flags m_AttributesMask
;
107 NPT_Flags m_Attributes
;
108 NPT_TimeStamp m_CreationTime
;
109 NPT_TimeStamp m_ModificationTime
;
112 /*----------------------------------------------------------------------
114 +---------------------------------------------------------------------*/
119 static const char* const Separator
;
122 static NPT_String
BaseName(const char* path
, bool with_extension
= true);
123 static NPT_String
DirName(const char* path
);
124 static NPT_String
FileExtension(const char* path
);
125 static NPT_String
Create(const char* directory
, const char* base
);
128 NPT_FilePath() {} // this class can't have instances
131 /*----------------------------------------------------------------------
133 +---------------------------------------------------------------------*/
134 class NPT_FileInterface
138 typedef unsigned int OpenMode
;
140 // constructors and destructor
141 virtual ~NPT_FileInterface() {}
144 virtual NPT_Result
Open(OpenMode mode
) = 0;
145 virtual NPT_Result
Close() = 0;
146 virtual NPT_Result
GetInputStream(NPT_InputStreamReference
& stream
) = 0;
147 virtual NPT_Result
GetOutputStream(NPT_OutputStreamReference
& stream
) = 0;
150 /*----------------------------------------------------------------------
152 +---------------------------------------------------------------------*/
153 class NPT_File
: public NPT_FileInterface
157 static NPT_Result
GetRoots(NPT_List
<NPT_String
>& roots
);
158 static NPT_Result
GetSize(const char* path
, NPT_LargeSize
&size
);
159 static NPT_Result
GetInfo(const char* path
, NPT_FileInfo
* info
= NULL
);
160 static bool Exists(const char* path
) { return NPT_SUCCEEDED(GetInfo(path
)); }
161 static NPT_Result
Remove(const char* path
, bool recurse
= false);
162 static NPT_Result
RemoveFile(const char* path
);
163 static NPT_Result
RemoveDir(const char* path
);
164 static NPT_Result
RemoveDir(const char* path
, bool force_if_not_empty
);
165 static NPT_Result
Rename(const char* from_path
, const char* to_path
);
166 static NPT_Result
ListDir(const char* path
, NPT_List
<NPT_String
>& entries
, NPT_Ordinal start
= 0, NPT_Cardinal count
= 0);
167 static NPT_Result
CreateDir(const char* path
);
168 static NPT_Result
CreateDir(const char* path
, bool create_intermediate_dirs
);
169 static NPT_Result
GetWorkingDir(NPT_String
& path
);
170 static NPT_Result
Load(const char* path
, NPT_DataBuffer
& buffer
, NPT_FileInterface::OpenMode mode
= NPT_FILE_OPEN_MODE_READ
);
171 static NPT_Result
Load(const char* path
, NPT_String
& data
, NPT_FileInterface::OpenMode mode
= NPT_FILE_OPEN_MODE_READ
);
172 static NPT_Result
Save(const char* path
, NPT_String
& data
);
173 static NPT_Result
Save(const char* path
, const NPT_DataBuffer
& buffer
);
175 // constructors and destructor
176 NPT_File(const char* path
);
177 ~NPT_File() override
{ delete m_Delegate
; }
180 NPT_Result
Load(NPT_DataBuffer
& buffer
);
181 NPT_Result
Save(const NPT_DataBuffer
& buffer
);
182 const NPT_String
& GetPath() { return m_Path
; }
183 NPT_Result
GetSize(NPT_LargeSize
&size
);
184 NPT_Result
GetInfo(NPT_FileInfo
& info
);
185 NPT_Result
ListDir(NPT_List
<NPT_String
>& entries
);
186 NPT_Result
Rename(const char* path
);
188 // NPT_FileInterface methods
189 NPT_Result
Open(OpenMode mode
) override
{
190 return m_Delegate
->Open(mode
);
192 NPT_Result
Close() override
{
193 return m_Delegate
->Close();
195 NPT_Result
GetInputStream(NPT_InputStreamReference
& stream
) override
{
196 return m_Delegate
->GetInputStream(stream
);
198 NPT_Result
GetOutputStream(NPT_OutputStreamReference
& stream
) override
{
199 return m_Delegate
->GetOutputStream(stream
);
203 NPT_File
& operator=(const NPT_File
& file
);
207 NPT_FileInterface
* m_Delegate
;
212 /*----------------------------------------------------------------------
213 | NPT_FileDateComparator
214 +---------------------------------------------------------------------*/
215 class NPT_FileDateComparator
{
217 NPT_FileDateComparator(const char* directory
) : m_Directory(directory
) {}
218 NPT_Int32
operator()(const NPT_String
& file1
, const NPT_String
& file2
) const {
219 NPT_FileInfo info1
, info2
;
220 if (NPT_FAILED(NPT_File::GetInfo(NPT_FilePath::Create(m_Directory
, file1
), &info1
))) return -1;
221 if (NPT_FAILED(NPT_File::GetInfo(NPT_FilePath::Create(m_Directory
, file2
), &info2
))) return -1;
222 return (info1
.m_ModificationTime
== info2
.m_ModificationTime
) ? 0 : (info1
.m_ModificationTime
< info2
.m_ModificationTime
? -1 : 1);
226 NPT_String m_Directory
;
229 #endif // _NPT_FILE_H_