util/file.h
Go to the documentation of this file.
1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ZORBA_FILE_H
18 #define ZORBA_FILE_H
19 
20 #ifndef WIN32
21 #include <stdint.h>
22 #endif
23 
24 #include <cstdio>
25 #include <string>
26 #include <time.h>
27 #include <vector>
28 
29 #include <zorba/config.h>
30 #include <zorba/file.h>
31 #include <zorba/util/path.h>
32 
33 namespace zorba {
34 
35 class ZORBA_DLL_PUBLIC file : public filesystem_path
36 {
37 public:
38 
39  enum filetype {
46  type_other
47  };
48 
50 
51 protected:
52  filetype do_stat( bool follow_symlinks = true, file_size_t *size = 0 ) const;
53 
54 public:
55  file(const filesystem_path &path, int flags = 0);
56 
57 public: // common methods
58  void set_path(std::string const& _path ) { *((filesystem_path *) this) = _path; }
59  void set_filetype(filetype) { /* do nothing */ } // deprecated
60 
61  filetype get_filetype( bool follow_symlinks = true ) const {
62  return do_stat( follow_symlinks );
63  }
64 
65  bool is_directory( bool follow_symlinks = true ) const {
66  return do_stat( follow_symlinks ) == type_directory;
67  }
68 
69  bool is_file( bool follow_symlinks = true ) const {
70  return do_stat( follow_symlinks ) == type_file;
71  }
72 
73  bool is_link() const {
74  return do_stat( false ) == type_link;
75  }
76 
77  bool is_volume( bool follow_symlinks = true ) const {
78  return do_stat( follow_symlinks ) == type_volume;
79  }
80 
81  bool is_invalid() const { // deprecated
82  return false;
83  }
84 
85  bool exists( bool follow_symlinks = true ) const {
86  return do_stat( follow_symlinks ) != type_non_existent;
87  }
88 
89  time_t lastModified() const;
90 
91 public: // file methods
92  void create();
93  void remove(bool ignore = true);
94  void rename(std::string const& newpath);
95 
97  file_size_t size;
98  do_stat( true, &size );
99  return size;
100  }
101 
102 public: // directory methods
103  void mkdir();
104  void deep_mkdir();
105  void rmdir(bool ignore = true);
106  void lsdir(std::vector<std::string> &list);
107 #ifndef _WIN32_WCE
108  void chdir();
109 #endif
110 
111  bool is_empty() const { return get_size() == 0; }
112 };
113 
114 
115 } // namespace zorba
116 #endif /* ZORBA_FILE_H */
117 /*
118  * Local variables:
119  * mode: c++
120  * End:
121  */
122 /* vim:set et sw=2 ts=2: */