kodi
NptFile.h
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 #include "NptTime.h"
41 
42 /*----------------------------------------------------------------------
43 | constants
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;
55 
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;
73 
74 const unsigned int NPT_FILE_ATTRIBUTE_READ_ONLY = 0x01;
75 const unsigned int NPT_FILE_ATTRIBUTE_LINK = 0x02;
76 
77 #define NPT_FILE_STANDARD_INPUT "@STDIN"
78 #define NPT_FILE_STANDARD_OUTPUT "@STDOUT"
79 #define NPT_FILE_STANDARD_ERROR "@STDERR"
80 
81 /*----------------------------------------------------------------------
82 | class references
83 +---------------------------------------------------------------------*/
84 class NPT_DataBuffer;
85 
86 /*----------------------------------------------------------------------
87 | NPT_FileInfo
88 +---------------------------------------------------------------------*/
90 {
91  // types
92  typedef enum {
93  FILE_TYPE_NONE,
94  FILE_TYPE_REGULAR,
95  FILE_TYPE_DIRECTORY,
96  FILE_TYPE_SPECIAL,
97  FILE_TYPE_OTHER
98  } FileType;
99 
100  // constructor
101  NPT_FileInfo() : m_Type(FILE_TYPE_NONE), m_Size(0), m_AttributesMask(0), m_Attributes(0) {}
102 
103  // members
104  FileType m_Type;
105  NPT_UInt64 m_Size;
106  NPT_Flags m_AttributesMask;
107  NPT_Flags m_Attributes;
108  NPT_TimeStamp m_CreationTime;
109  NPT_TimeStamp m_ModificationTime;
110 };
111 
112 /*----------------------------------------------------------------------
113 | NPT_FilePath
114 +---------------------------------------------------------------------*/
116 {
117 public:
118  // class members
119  static const char* const Separator;
120 
121  // class methods
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);
126 
127 private:
128  NPT_FilePath() {} // this class can't have instances
129 };
130 
131 /*----------------------------------------------------------------------
132 | NPT_FileInterface
133 +---------------------------------------------------------------------*/
135 {
136 public:
137  // types
138  typedef unsigned int OpenMode;
139 
140  // constructors and destructor
141  virtual ~NPT_FileInterface() {}
142 
143  // methods
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;
148 };
149 
150 /*----------------------------------------------------------------------
151 | NPT_File
152 +---------------------------------------------------------------------*/
154 {
155 public:
156  // class methods
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);
174 
175  // constructors and destructor
176  NPT_File(const char* path);
177  ~NPT_File() override { delete m_Delegate; }
178 
179  // methods
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);
187 
188  // NPT_FileInterface methods
189  NPT_Result Open(OpenMode mode) override {
190  return m_Delegate->Open(mode);
191  }
192  NPT_Result Close() override {
193  return m_Delegate->Close();
194  }
195  NPT_Result GetInputStream(NPT_InputStreamReference& stream) override {
196  return m_Delegate->GetInputStream(stream);
197  }
198  NPT_Result GetOutputStream(NPT_OutputStreamReference& stream) override {
199  return m_Delegate->GetOutputStream(stream);
200  }
201 
202  // operators
203  NPT_File& operator=(const NPT_File& file);
204 
205 protected:
206  // members
207  NPT_FileInterface* m_Delegate;
208  NPT_String m_Path;
209  bool m_IsSpecial;
210 };
211 
212 /*----------------------------------------------------------------------
213 | NPT_FileDateComparator
214 +---------------------------------------------------------------------*/
216 public:
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);
223  }
224 
225 private:
226  NPT_String m_Directory;
227 };
228 
229 #endif // _NPT_FILE_H_
Definition: NptFile.h:89
Definition: NptFile.h:115
Definition: NptTime.h:50
Definition: NptFile.h:215
Definition: NptDataBuffer.h:44
Definition: NptFile.h:153
Definition: NptStrings.h:57
Definition: NptFile.h:134