1 | /*! |
---|
2 | * @file file.h |
---|
3 | * @brief Definition of File Handler class |
---|
4 | */ |
---|
5 | |
---|
6 | #ifndef __FILE_H_ |
---|
7 | #define __FILE_H_ |
---|
8 | |
---|
9 | |
---|
10 | #include <string> |
---|
11 | struct stat; |
---|
12 | |
---|
13 | //! A Class to Handle Files. |
---|
14 | class File |
---|
15 | { |
---|
16 | public: |
---|
17 | //! How the File should be opened. |
---|
18 | typedef enum { |
---|
19 | ReadOnly = 0, //!< ReadOnly mode |
---|
20 | WriteOnly = 1, //!< WriteOnly mode |
---|
21 | ReadWrite = 2, //!< Read and Write mode together |
---|
22 | Append = 3 //!< Append at the end. |
---|
23 | } OpenMode; |
---|
24 | public: |
---|
25 | File(); |
---|
26 | File(const std::string& fileName); |
---|
27 | File(const File& file); |
---|
28 | virtual ~File(); |
---|
29 | |
---|
30 | /// Set-Up |
---|
31 | void setFileName(const std::string& fileName); |
---|
32 | File& operator=(const std::string& fileName); |
---|
33 | File& operator=(const File& file); |
---|
34 | |
---|
35 | /// Comparison |
---|
36 | bool operator==(const std::string& fileName) const; |
---|
37 | bool operator==(const File& file) const; |
---|
38 | |
---|
39 | virtual bool open(OpenMode mode); |
---|
40 | virtual bool close(); |
---|
41 | FILE* handle() const |
---|
42 | { return this->_handle; }; |
---|
43 | inline OpenMode mode() const |
---|
44 | { return _mode; } |
---|
45 | /** @returns the FileName of this File */ |
---|
46 | const std::string& name() const { return this->_name; }; |
---|
47 | |
---|
48 | /// Testing |
---|
49 | bool exists() const; |
---|
50 | bool isLink() const; |
---|
51 | bool isFile() const; |
---|
52 | bool isDirectory() const; |
---|
53 | bool isReadable() const; |
---|
54 | bool isWriteable() const; |
---|
55 | bool isExecutable() const; |
---|
56 | |
---|
57 | |
---|
58 | /// Operate on the FileSystem |
---|
59 | bool copy(const File& destination); |
---|
60 | bool rename(const File& destination); |
---|
61 | bool touch(); |
---|
62 | bool remove(); |
---|
63 | |
---|
64 | /// Transformations |
---|
65 | static void relToAbs(std::string& relFileName); |
---|
66 | static void absToRel(std::string& absFileName); |
---|
67 | static const std::string& cwd(); |
---|
68 | |
---|
69 | private: |
---|
70 | void init(); |
---|
71 | void statFile(); |
---|
72 | void homeDirCheck(std::string& fileName); |
---|
73 | |
---|
74 | private: |
---|
75 | FILE* _handle; //!< The FileHandle (if set). |
---|
76 | std::string _name; //!< The Name of the File. |
---|
77 | stat* _status; //!< The Stat of the File. |
---|
78 | OpenMode _mode; |
---|
79 | static std::string _cwd; //!< The currend Working directory. |
---|
80 | |
---|
81 | }; |
---|
82 | |
---|
83 | #endif /* __FILE_H_ */ |
---|