BALL  1.4.79
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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_REGULAREXPRESSION_H
10 #endif
11 
12 #ifndef BALL_DATATYPE_STRING_H
13 # include <BALL/DATATYPE/string.h>
14 #endif
15 
16 #ifndef BALL_SYSTEM_FILESYSTEM_H
17 # include <BALL/SYSTEM/fileSystem.h>
18 #endif
19 
20 #include <BALL/DATATYPE/hashSet.h>
21 
22 #include <cstdlib> // 'getenv'
23 #include <sys/stat.h> // 'stat', 'lstat'
24 #include <cstdio> // 'rename'
25 #include <algorithm> // 'reverse'
26 
27 #ifdef BALL_COMPILER_MSVC
28 #ifndef S_ISREG
29 # define S_ISREG _S_ISREG
30 #endif
31 #ifndef S_ISDIR
32 # define S_ISDIR _S_ISDIR
33 #endif
34 # define S_ISCHR _S_ISCHR
35 # define S_ISBLK _S_ISBLK
36 # define S_ISFIFO _S_ISFIFO
37 # define access _access
38 #endif
39 
40 #include <iostream>
41 #include <fstream>
42 #include <sys/types.h>
43 #include <map>
44 #include <algorithm>
45 
46 #ifdef BALL_HAS_UNISTD_H
47 # include <unistd.h> // 'access', 'rename', 'truncate'
48 #endif
49 
50 #ifdef BALL_COMPILER_MSVC
51 # include <fcntl.h>
52 # include <io.h>
53  // Define the missing symbols from <unistd.h>,
54  // which M$, in its infinite wisdom, was unable to provide.
55 # define F_OK 0
56 # define W_OK 2
57 # define R_OK 4
58 #endif
59 
60 
61 namespace BALL
62 {
81  {
82  public:
83 
87 
90 
93 
95 
98 
100  void registerTransformation(const String& pattern, const String& command);
101 
103  void unregisterTransformation(const String& pattern);
104 
106  String findTransformation(const String& name) const;
107 
125  String transform(const String& name);
127 
128  protected:
129 
131  std::map<String, String> transformation_methods_;
132  };
133 
138  : public std::fstream
139  {
140  public:
141 
147  {
148  public:
149  CannotWrite(const char* file, int line, const String& filename);
150 
151  ~CannotWrite()
152  throw();
153 
154  String getFilename() const;
155 
156  protected:
157  std::string filename_;
158  };
159 
163 
168  typedef std::ios::openmode OpenMode;
169 
171 
175  static const OpenMode MODE_IN;
177 
179  static const OpenMode MODE_OUT;
180 
182  static const OpenMode MODE_APP;
183 
185  static const OpenMode MODE_BINARY;
186 
188  static const OpenMode MODE_ATE;
189 
191  static const OpenMode MODE_TRUNC;
193 
197 
202  {
204  TRANSFORMATION__EXEC = 1,
206  TRANSFORMATION__FILTER = 2,
208  TRANSFORMATION__URL = 3
209  };
210 
213  enum Type
214  {
216  TYPE__UNKNOWN = 0,
218  TYPE__DIRECTORY = 1,
220  TYPE__CHAR_SPECIAL_FILE = 2,
222  TYPE__BLOCK_SPECIAL_FILE = 3,
224  TYPE__REGULAR_FILE = 4,
226  TYPE__SYMBOLIC_LINK = 5,
228  TYPE__SOCKET = 6,
230  TYPE__FIFO_SPECIAL_FILE = 7
231  };
232 
235 
238 
241 
244 
246 
249 
252  File();
253 
260  File(const String& name, OpenMode open_mode = std::ios::in);
261 
265  virtual ~File();
266 
269  virtual void clear();
271 
275 
280 // const File& operator = (const File& file);
281 
283 
287 
295  bool open(const String& name, File::OpenMode open_mode = std::ios::in);
296 
302  bool reopen();
303 
310  bool reopen(File::OpenMode open_mode);
311 
314  void close();
315 
319  const String& getName() const;
320 
324  void setName(const String& name);
325 
328  const String& getOriginalName() const;
329 
335  Size getSize();
336 
341  static Size getSize(String name);
342 
347  File::OpenMode getOpenMode() const;
348 
355  static Type getType(String name, bool trace_link);
356 
362  Type getType(bool trace_link) const;
363 
372  static bool copy(String source_name, String destination_name, Size buffer_size = 4096);
373 
381  bool copyTo(const String& destination_name, Size buffer_size = 4096);
382 
390  static bool move(const String& source_name, const String& destination_name);
391 
398  bool moveTo(const String& destination_name);
399 
404  static bool remove(String name);
405 
409  bool remove();
410 
417  static bool rename(String old_path, String new_path);
418 
425  bool renameTo(const String& new_path);
426 
433  static bool truncate(String path, Size size = 0);
434 
440  bool truncate(Size size = 0);
441 
450  static bool createTemporaryFilename(String& temporary, const String& suffix = ".TMP");
451 
456  std::fstream& getFileStream();
457 
459 
463 
469  TransformationManager& getTransformationManager();
470 
476  const TransformationManager& getTransformationManager() const;
477 
480  static void enableTransformation(Transformation transformation);
481 
484  static void disableTransformation(Transformation transformation);
485 
488  static bool isTransformationEnabled(Transformation transformation);
489 
492  static void registerTransformation(const String& pattern, const String& exec);
493 
496  static void unregisterTransformation(const String& pattern);
497 
499 
502 
506  bool operator == (const File& file) const;
507 
511  bool operator != (const File& file) const;
512 
517  bool isOpen() const;
518 
523  bool isClosed() const;
524 
529  static bool isAccessible(String name);
530 
535  bool isAccessible() const;
536 
544  bool isCanonized() const;
545 
551  static bool isReadable(String name);
552 
557  bool isReadable() const;
558 
564  static bool isWritable(String name);
565 
570  bool isWritable() const;
571 
577  static bool isExecutable(String name);
578 
583  bool isExecutable() const;
584 
586 
589 
595  bool isValid() const;
596 
598 
599  private:
600  const File& operator = (const File& file);
601 
602  protected:
603 
607  bool is_open_;
610 
613  };
614 
615 # ifndef BALL_NO_INLINE_FUNCTIONS
616 # include <BALL/SYSTEM/file.iC>
617 # endif
618 
619 } // namespace BALL
620 
621 #endif // BALL_SYSTEM_FILE_H
String name_
Definition: file.h:604
BALL_VIEW_EXPORT String createTemporaryFilename()
Create a temporary filename in the users home dir.
static HashSet< String > created_temp_filenames_
Definition: file.h:609
static const String TRANSFORMATION_EXEC_PREFIX
Prefix for filenames that are created through the execution of commands "exec:".
Definition: file.h:234
static const OpenMode MODE_OUT
Open for output.
Definition: file.h:179
static const String TRANSFORMATION_FILE_PREFIX
Prefix for files (to mimick URL-like behavior) "file:".
Definition: file.h:237
std::map< String, String > transformation_methods_
The map containing all transformation methods.
Definition: file.h:131
std::ios::openmode OpenMode
Definition: file.h:168
static const OpenMode MODE_APP
Append. Seek to end before each write operation.
Definition: file.h:182
static const String TRANSFORMATION_FTP_PREFIX
Prefix for FTP-transfers "ftp://".
Definition: file.h:240
static const OpenMode MODE_TRUNC
Truncate an existing file.
Definition: file.h:191
OpenMode open_mode_
Definition: file.h:606
BALL_EXPORT bool operator!=(const String &s1, const String &s2)
const char * getName()
Definition: rtti.h:97
BALL_EXPORT bool operator==(const String &s1, const String &s2)
bool is_open_
Definition: file.h:607
Transformation
Definition: file.h:201
static const OpenMode MODE_ATE
Seek to end directly after opening.
Definition: file.h:188
String original_name_
Definition: file.h:605
static Size transformation_methods_
Definition: file.h:612
static const OpenMode MODE_BINARY
Binary mode.
Definition: file.h:185
static TransformationManager transformation_manager_
Definition: file.h:611
bool is_temporary_
Definition: file.h:608
#define BALL_EXPORT
Definition: COMMON/global.h:50
std::string filename_
Definition: file.h:157
static const String TRANSFORMATION_HTTP_PREFIX
Prefix for HTTP-transfer "http://".
Definition: file.h:243