BALL 1.5.0
file.h
Go to the documentation of this file.
1// -*- Mode: C++; tab-width: 2; -*-
2// vi: set ts=2:
3//
4
5#ifndef BALL_SYSTEM_FILE_H
6#define BALL_SYSTEM_FILE_H
7
8#ifndef BALL_DATATYPE_STRING_H
10#endif
11
12#ifndef BALL_SYSTEM_FILESYSTEM_H
14#endif
15
17
18#include <cstdlib> // 'getenv'
19#include <sys/stat.h> // 'stat', 'lstat'
20#include <cstdio> // 'rename'
21#include <algorithm> // 'reverse'
22
23#ifdef BALL_COMPILER_MSVC
24#ifndef S_ISREG
25# define S_ISREG _S_ISREG
26#endif
27#ifndef S_ISDIR
28# define S_ISDIR _S_ISDIR
29#endif
30# define S_ISCHR _S_ISCHR
31# define S_ISBLK _S_ISBLK
32# define S_ISFIFO _S_ISFIFO
33#endif
34
35#include <iostream>
36#include <fstream>
37#include <sys/types.h>
38#include <map>
39#include <algorithm>
40
41#ifdef BALL_HAS_UNISTD_H
42# include <unistd.h> // 'access', 'rename', 'truncate'
43#endif
44
45#ifdef BALL_COMPILER_MSVC
46# include <fcntl.h>
47# include <io.h>
48# include <share.h> // shflags for _sopen_s
49
50 // Define the missing symbols from <unistd.h>,
51 // which M$, in its infinite wisdom, was unable to provide.
52# define F_OK 0
53# define W_OK 2
54# define R_OK 4
55#endif
56
57
58namespace BALL
59{
78 {
79 public:
80
84
87
90
92
95
97 void registerTransformation(const String& pattern, const String& command);
98
100 void unregisterTransformation(const String& pattern);
101
103 String findTransformation(const String& name) const;
104
122 String transform(const String& name);
124
125 protected:
126
128 std::map<String, String> transformation_methods_;
129 };
130
135 : public std::fstream
136 {
137 public:
138
144 {
145 public:
146 CannotWrite(const char* file, int line, const String& filename);
147
149 throw();
150
151 String getFilename() const;
152
153 protected:
154 std::string filename_;
155 };
156
160
165 typedef std::ios::openmode OpenMode;
166
168
173 static const OpenMode MODE_IN;
174
176 static const OpenMode MODE_OUT;
177
179 static const OpenMode MODE_APP;
180
182 static const OpenMode MODE_BINARY;
183
185 static const OpenMode MODE_ATE;
186
188 static const OpenMode MODE_TRUNC;
190
194
199 {
201 TRANSFORMATION__EXEC = 1,
203 TRANSFORMATION__FILTER = 2,
205 TRANSFORMATION__URL = 3
206 };
207
210 enum Type
211 {
213 TYPE__UNKNOWN = 0,
215 TYPE__DIRECTORY = 1,
217 TYPE__CHAR_SPECIAL_FILE = 2,
219 TYPE__BLOCK_SPECIAL_FILE = 3,
221 TYPE__REGULAR_FILE = 4,
223 TYPE__SYMBOLIC_LINK = 5,
225 TYPE__SOCKET = 6,
227 TYPE__FIFO_SPECIAL_FILE = 7
228 };
229
232
235
238
241
243
246
250
257 File(const String& name, OpenMode open_mode = std::ios::in);
258
262 virtual ~File();
263
266 virtual void clear();
268
272
277// const File& operator = (const File& file);
278
280
284
292 bool open(const String& name, File::OpenMode open_mode = std::ios::in);
293
299 bool reopen();
300
307 bool reopen(File::OpenMode open_mode);
308
311 void close();
312
316 const String& getName() const;
317
321 void setName(const String& name);
322
325 const String& getOriginalName() const;
326
333
338 static Size getSize(String name);
339
345
352 static Type getType(String name, bool trace_link);
353
359 Type getType(bool trace_link) const;
360
369 static bool copy(String source_name, String destination_name, Size buffer_size = 4096);
370
378 bool copyTo(const String& destination_name, Size buffer_size = 4096);
379
387 static bool move(const String& source_name, const String& destination_name);
388
395 bool moveTo(const String& destination_name);
396
401 static bool remove(String name);
402
406 bool remove();
407
414 static bool rename(String old_path, String new_path);
415
422 bool renameTo(const String& new_path);
423
430 static bool truncate(String path, Size size = 0);
431
437 bool truncate(Size size = 0);
438
447 static bool createTemporaryFilename(String& temporary, const String& suffix = ".TMP");
448
453 std::fstream& getFileStream();
454
456
460
467
474
477 static void enableTransformation(Transformation transformation);
478
481 static void disableTransformation(Transformation transformation);
482
485 static bool isTransformationEnabled(Transformation transformation);
486
489 static void registerTransformation(const String& pattern, const String& exec);
490
493 static void unregisterTransformation(const String& pattern);
494
496
499
503 bool operator == (const File& file) const;
504
508 bool operator != (const File& file) const;
509
514 bool isOpen() const;
515
520 bool isClosed() const;
521
526 static bool isAccessible(String name);
527
532 bool isAccessible() const;
533
541 bool isCanonized() const;
542
548 static bool isReadable(String name);
549
554 bool isReadable() const;
555
561 static bool isWritable(String name);
562
567 bool isWritable() const;
568
574 static bool isExecutable(String name);
575
580 bool isExecutable() const;
581
583
586
592 bool isValid() const;
593
595
596 private:
597 const File& operator = (const File& file);
598
599 protected:
600
607
610 };
611
612# ifndef BALL_NO_INLINE_FUNCTIONS
613# include <BALL/SYSTEM/file.iC>
614# endif
615
616} // namespace BALL
617
618#endif // BALL_SYSTEM_FILE_H
STL namespace.
Definition: constants.h:13
BALL_EXPORT bool operator==(const String &s1, const String &s2)
BALL_EXPORT bool operator!=(const String &s1, const String &s2)
~TransformationManager()
Destructor.
String transform(const String &name)
String findTransformation(const String &name) const
Find a transformation matching a given file name.
void registerTransformation(const String &pattern, const String &command)
Add a new transformation.
std::map< String, String > transformation_methods_
The map containing all transformation methods.
Definition: file.h:128
void unregisterTransformation(const String &pattern)
Delete a transformation.
TransformationManager()
Default constructor.
String original_name_
Definition: file.h:602
static bool createTemporaryFilename(String &temporary, const String &suffix=".TMP")
bool isClosed() const
static bool copy(String source_name, String destination_name, Size buffer_size=4096)
static void registerTransformation(const String &pattern, const String &exec)
static void enableTransformation(Transformation transformation)
File::OpenMode getOpenMode() const
static bool move(const String &source_name, const String &destination_name)
static bool remove(String name)
bool reopen(File::OpenMode open_mode)
static bool truncate(String path, Size size=0)
static bool isTransformationEnabled(Transformation transformation)
bool isAccessible() const
static Size transformation_methods_
Definition: file.h:609
bool isExecutable() const
std::ios::openmode OpenMode
Definition: file.h:165
static HashSet< String > created_temp_filenames_
Definition: file.h:606
static void disableTransformation(Transformation transformation)
static const String TRANSFORMATION_HTTP_PREFIX
Prefix for HTTP-transfer "http://".
Definition: file.h:240
static void unregisterTransformation(const String &pattern)
const String & getName() const
static bool isWritable(String name)
static Type getType(String name, bool trace_link)
static const String TRANSFORMATION_EXEC_PREFIX
Prefix for filenames that are created through the execution of commands "exec:".
Definition: file.h:231
const TransformationManager & getTransformationManager() const
bool isOpen() const
bool renameTo(const String &new_path)
static bool rename(String old_path, String new_path)
bool isCanonized() const
bool open(const String &name, File::OpenMode open_mode=std::ios::in)
bool truncate(Size size=0)
virtual ~File()
bool is_open_
Definition: file.h:604
bool isValid() const
static Size getSize(String name)
static TransformationManager transformation_manager_
Definition: file.h:608
static bool isAccessible(String name)
bool copyTo(const String &destination_name, Size buffer_size=4096)
TransformationManager & getTransformationManager()
bool isWritable() const
bool isReadable() const
const String & getOriginalName() const
void close()
Type getType(bool trace_link) const
static bool isExecutable(String name)
virtual void clear()
String name_
Definition: file.h:601
std::fstream & getFileStream()
Size getSize()
File(const String &name, OpenMode open_mode=std::ios::in)
static const String TRANSFORMATION_FILE_PREFIX
Prefix for files (to mimick URL-like behavior) "file:".
Definition: file.h:234
void setName(const String &name)
bool moveTo(const String &destination_name)
bool is_temporary_
Definition: file.h:605
bool remove()
OpenMode open_mode_
Definition: file.h:603
Transformation
Definition: file.h:199
static bool isReadable(String name)
static const String TRANSFORMATION_FTP_PREFIX
Prefix for FTP-transfers "ftp://".
Definition: file.h:237
bool reopen()
CannotWrite(const char *file, int line, const String &filename)
#define BALL_EXPORT
Definition: COMMON/global.h:50