FileInfo.h
1 /*
2  * Copyright (C) 2006-2013 Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef Pt_System_FileInfo_h
30 #define Pt_System_FileInfo_h
31 
32 #include <Pt/System/Api.h>
33 #include <Pt/System/Path.h>
34 #include <Pt/DateTime.h>
35 #include <Pt/String.h>
36 #include <Pt/Types.h>
37 #include <string>
38 
39 namespace Pt {
40 
41 namespace System {
42 
103 class PT_SYSTEM_API FileInfo
104 {
105  public:
107  enum Type
108  {
109  Invalid = 0,
110  Directory = 1,
111  File = 2,
112  Link = 4
113  };
114 
115  public:
118  {}
119 
122  explicit FileInfo(const Path& path);
123 
126  explicit FileInfo(const Pt::String& path);
127 
130  explicit FileInfo(const char* path);
131 
134  {}
135 
137  void clear();
138 
144  const Path& path() const
145  { return _path; }
146 
149  { return FileInfo::size(_path); }
150 
152  Type type() const
153  { return FileInfo::type(_path); }
154 
155  bool isLink() const
156  { return FileInfo::isLink(_path); }
157 
158  public:
160  static Type type(const Path& path);
161 
162  static bool isLink(const Path& path);
163 
165  static Pt::uint64_t size(const Path& path);
166 
168  static DateTime lastModified(const Path& path);
169 
171  static bool exists(const Path& path)
172  { return type(path) != Invalid; }
173 
175  static void createFile(const Path& path);
176 
178  static void createDirectory(const Path& path);
179 
181  static void createDirectories(const Path& path);
182 
184  static void resize(const Path& path, Pt::uint64_t n);
185 
187  static void remove(const Path& path);
188 
190  static void removeAll(const Pt::System::Path& path);
191 
193  static void move(const Path& path, const Path& to);
194 
195  public:
197  Path& path()
198  { return _path; }
199 
200  private:
201  Path _path;
202 };
203 
204 
209 inline bool operator<(const FileInfo& a, const FileInfo& b)
210 {
211  return a.path() < b.path();
212 }
213 
218 inline bool operator==(const FileInfo& a, const FileInfo& b)
219 {
220  return a.path() == b.path();
221 }
222 
227 inline bool operator!=(const FileInfo& a, const FileInfo& b)
228 {
229  return !(a == b);
230 }
231 
232 } // namespace System
233 
234 } // namespace Pt
235 
236 #endif // Pt_System_FileInfo_h
Type
File-node type.
Definition: FileInfo.h:108
Core module.
Definition: Allocator.h:33
static void remove(const Path &path)
Removes a file or directory.
bool operator==(const FileInfo &a, const FileInfo &b)
Compare two FileInfo objects.
Definition: FileInfo.h:218
static Pt::uint64_t size(const Path &path)
Returns the size of the file in bytes.
Provides information about a node in the file-system.
Definition: FileInfo.h:104
static void removeAll(const Pt::System::Path &path)
Removes a directory and its contents.
Pt::uint64_t size() const
Returns the file size.
Definition: FileInfo.h:148
FileInfo(const Pt::String &path)
Constructs from the path.
~FileInfo()
Destructor.
Definition: FileInfo.h:133
uint_type uint64_t
Unsigned 64-bit integer type.
Definition: Api-Types.h:62
FileInfo(const Path &path)
Constructs from the path.
static void createDirectory(const Path &path)
Creates a new directory.
Type type() const
Returns the file type.
Definition: FileInfo.h:152
const Path & path() const
Returns the full path of node in the file-system.
Definition: FileInfo.h:144
FileInfo()
Default constructor.
Definition: FileInfo.h:117
static void move(const Path &path, const Path &to)
Moves a file or directory.
FileInfo(const char *path)
Constructs from the path.
static Type type(const Path &path)
Returns the type of file at the path.
static void resize(const Path &path, Pt::uint64_t n)
Resizes a file.
Represents a path in the file-system.
Definition: Path.h:48
static void createFile(const Path &path)
Creates a new file.
static DateTime lastModified(const Path &path)
Returns the time when last modified.
bool operator!=(const FileInfo &a, const FileInfo &b)
Compare two FileInfo objects.
Definition: FileInfo.h:227
Unicode capable basic_string.
Definition: Api-String.h:44
static bool exists(const Path &path)
Returns true if a file or directory exists at the path.
Definition: FileInfo.h:171
static void createDirectories(const Path &path)
Creates a new directory.
void clear()
Clears the state.
Combined Date and Time value.
Definition: DateTime.h:66
bool operator<(const FileInfo &a, const FileInfo &b)
Compare two FileInfo objects.
Definition: FileInfo.h:209