VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gfile.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gfile.h
19 ///
20 
21 #ifndef G_FILE_H
22 #define G_FILE_H
23 
24 #include "gdef.h"
25 #include "gpath.h"
26 #include "gexception.h"
27 #include "gdatetime.h"
28 #include <cstdio> // std::remove()
29 
30 namespace G
31 {
32  class File ;
33  class DirectoryIteratorImp ;
34 }
35 
36 /// \class G::File
37 /// A simple static class for dealing with files.
38 /// \see G::Path, G::FileSystem, G::Directory
39 ///
40 class G::File
41 {
42 public:
43  G_EXCEPTION( StatError , "cannot access file" ) ;
44  G_EXCEPTION( CannotRemove , "cannot delete file" ) ;
45  G_EXCEPTION( CannotRename , "cannot rename file" ) ;
46  G_EXCEPTION( CannotCopy , "cannot copy file" ) ;
47  G_EXCEPTION( CannotMkdir , "cannot mkdir" ) ;
48  G_EXCEPTION( CannotChmod , "cannot chmod file" ) ;
49  G_EXCEPTION( CannotLink , "cannot create symlink" ) ;
50  G_EXCEPTION( CannotCreate , "cannot create empty file" ) ;
51  G_EXCEPTION( SizeOverflow , "file size overflow" ) ;
52  G_EXCEPTION( TimeError , "cannot get file modification time" ) ;
53  class NoThrow /// An overload discriminator class for File methods.
54  {} ;
55 
56  static bool remove( const Path & path , const NoThrow & ) ;
57  ///< Deletes the file or directory. Returns false on error.
58 
59  static void remove( const Path & path ) ;
60  ///< Deletes the file or directory. Throws an exception on error.
61 
62  static bool rename( const Path & from , const Path & to , const NoThrow & ) ;
63  ///< Renames the file. Returns false on error.
64 
65  static void rename( const Path & from , const Path & to ) ;
66  ///< Renames the file.
67 
68  static bool copy( const Path & from , const Path & to , const NoThrow & ) ;
69  ///< Copies a file. Returns false on error.
70 
71  static void copy( const Path & from , const Path & to ) ;
72  ///< Copies a file.
73 
74  static void copy( std::istream & from , std::ostream & to ,
75  std::streamsize limit = 0U , std::string::size_type block = 0U ) ;
76  ///< Copies a stream with an optional size limit.
77 
78  static bool mkdirs( const Path & dir , const NoThrow & , int = 100 ) ;
79  ///< Creates a directory and all necessary parents. Returns false on error.
80  ///< Does chmodx() on all created directories.
81 
82  static void mkdirs( const Path & dir , int = 100 ) ;
83  ///< Creates a directory and all necessary parents.
84  ///< Does chmodx() on all created directories.
85 
86  static bool mkdir( const Path & dir , const NoThrow & ) ;
87  ///< Creates a directory. Returns false on error.
88 
89  static void mkdir( const Path & dir ) ;
90  ///< Creates a directory.
91 
92  static std::string sizeString( const Path & file ) ;
93  ///< Returns the file's size in string format.
94  ///< Returns the empty string on error.
95 
96  static bool exists( const Path & file ) ;
97  ///< Returns true if the file (directory, device etc.)
98  ///< exists. Symlinks are followed. Throws an exception
99  ///< if permission denied or too many symlinks etc.
100 
101  static bool exists( const Path & file , const NoThrow & ) ;
102  ///< Returns true if the file (directory, device etc.)
103  ///< exists. Symlinks are followed. Returns false on error.
104 
105  static bool isDirectory( const Path & path ) ;
106  ///< Returns true if the path exists() and is a directory.
107  ///< Symlinks are followed.
108 
109  static EpochTime time( const Path & file ) ;
110  ///< Returns the file's timestamp.
111 
112  static EpochTime time( const Path & file , const NoThrow & ) ;
113  ///< Returns the file's timestamp. Returns EpochTime(0)
114  ///< on error.
115 
116  static void chmodx( const Path & file ) ;
117  ///< Makes the file executable.
118 
119  static bool chmodx( const Path & file , const NoThrow & ) ;
120  ///< Makes the file executable.
121 
122  static void link( const Path & target , const Path & new_link ) ;
123  ///< Creates a symlink.
124 
125  static bool link( const Path & target , const Path & new_link , const NoThrow & ) ;
126  ///< Creates a symlink.
127 
128  static bool executable( const Path & ) ;
129  ///< Returns true if the path is probably executable.
130  ///< Because of portability and implementation difficulties
131  ///< this does not return a definitive result so it should
132  ///< only used for generating warnings on a false return.
133 
134  static void create( const Path & ) ;
135  ///< Creates an empty file. Throws on error.
136 
137 private:
138  friend class G::DirectoryIteratorImp ;
139  static std::string copy( const Path & , const Path & , int ) ;
140  static std::string sizeString( g_uint32_t hi , g_uint32_t lo ) ; // win32
141  static bool exists( const Path & , bool , bool ) ;
142  static bool exists( const char * , bool & , bool & ) ; // o/s-specific
143  static bool chmodx( const Path & file , bool ) ;
144 } ;
145 
146 #endif
A subsecond-resolution timestamp based on a time_t.
Definition: gdatetime.h:39
static bool copy(const Path &from, const Path &to, const NoThrow &)
Copies a file. Returns false on error.
Definition: gfile.cpp:70
static bool isDirectory(const Path &path)
Returns true if the path exists() and is a directory.
Definition: gfile_unix.cpp:67
static void create(const Path &)
Creates an empty file. Throws on error.
Definition: gfile.cpp:217
A simple static class for dealing with files.
Definition: gfile.h:40
static bool rename(const Path &from, const Path &to, const NoThrow &)
Renames the file. Returns false on error.
Definition: gfile.cpp:46
static bool mkdirs(const Path &dir, const NoThrow &, int=100)
Creates a directory and all necessary parents.
Definition: gfile.cpp:172
static bool executable(const Path &)
Returns true if the path is probably executable.
Definition: gfile_unix.cpp:73
static bool exists(const Path &file)
Returns true if the file (directory, device etc.) exists.
Definition: gfile.cpp:132
static bool mkdir(const Path &dir, const NoThrow &)
Creates a directory. Returns false on error.
Definition: gfile_unix.cpp:45
An overload discriminator class for File methods.
Definition: gfile.h:53
static void chmodx(const Path &file)
Makes the file executable.
Definition: gfile.cpp:167
static void link(const Path &target, const Path &new_link)
Creates a symlink.
Definition: gfile_unix.cpp:143
static std::string sizeString(const Path &file)
Returns the file's size in string format.
Definition: gfile_unix.cpp:90
A pimple-pattern implementation class for DirectoryIterator using opendir()/readdir().
static EpochTime time(const Path &file)
Returns the file's timestamp.
Definition: gfile_unix.cpp:101
A Path object represents a file system path.
Definition: gpath.h:72