Panda3D
 All Classes Functions Variables Enumerations
filename.h
1 // Filename: filename.h
2 // Created by: drose (18Jan99)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef FILENAME_H
16 #define FILENAME_H
17 
18 #include "dtoolbase.h"
19 #include "pandaFileStream.h"
20 #include "typeHandle.h"
21 #include "register_type.h"
22 #include "vector_string.h"
23 #include "textEncoder.h"
24 
25 #include <assert.h>
26 
27 class DSearchPath;
28 
29 ////////////////////////////////////////////////////////////////////
30 // Class : Filename
31 // Description : The name of a file, such as a texture file or an Egg
32 // file. Stores the full pathname, and includes
33 // functions for extracting out the directory prefix
34 // part and the file extension and stuff.
35 //
36 // A Filename is also aware of the mapping between the
37 // Unix-like filename convention we use internally, and
38 // the local OS's specific filename convention, and it
39 // knows how to perform basic OS-specific I/O, like
40 // testing for file existence and searching a
41 // searchpath, as well as the best way to open an
42 // fstream for reading or writing.
43 ////////////////////////////////////////////////////////////////////
44 class EXPCL_DTOOL Filename {
45 PUBLISHED:
46  enum Type {
47  // These type values must fit within the bits allocated for
48  // F_type, below.
49  T_general = 0x00,
50  T_dso = 0x01,
51  T_executable = 0x02,
52  // Perhaps other types will be added later.
53  };
54 
55 public:
56  enum Flags {
57  F_type = 0x0f,
58  F_binary = 0x10,
59  F_text = 0x20,
60  F_pattern = 0x40,
61  };
62 
63  INLINE Filename(const char *filename);
64 
65 PUBLISHED:
66  INLINE Filename(const string &filename = "");
67  INLINE Filename(const wstring &filename);
68  INLINE Filename(const Filename &copy);
69  Filename(const Filename &dirname, const Filename &basename);
70  INLINE ~Filename();
71 
72 #ifdef USE_MOVE_SEMANTICS
73  INLINE Filename(string &&filename) NOEXCEPT;
74  INLINE Filename(Filename &&from) NOEXCEPT;
75 #endif
76 
77 #ifdef HAVE_PYTHON
78  EXTENSION(PyObject *__reduce__(PyObject *self) const);
79 #endif
80 
81  // Static constructors to explicitly create a filename that refers
82  // to a text or binary file. This is in lieu of calling set_text()
83  // or set_binary() or set_type().
84  INLINE static Filename text_filename(const Filename &filename);
85  INLINE static Filename text_filename(const string &filename);
86  INLINE static Filename binary_filename(const Filename &filename);
87  INLINE static Filename binary_filename(const string &filename);
88  INLINE static Filename dso_filename(const string &filename);
89  INLINE static Filename executable_filename(const string &filename);
90 
91  INLINE static Filename pattern_filename(const string &filename);
92 
93  static Filename from_os_specific(const string &os_specific,
94  Type type = T_general);
95  static Filename from_os_specific_w(const wstring &os_specific,
96  Type type = T_general);
97  static Filename expand_from(const string &user_string,
98  Type type = T_general);
99  static Filename temporary(const string &dirname, const string &prefix,
100  const string &suffix = string(),
101  Type type = T_general);
102 
103  static const Filename &get_home_directory();
104  static const Filename &get_temp_directory();
105  static const Filename &get_user_appdata_directory();
106  static const Filename &get_common_appdata_directory();
107 
108  // Assignment is via the = operator.
109  INLINE Filename &operator = (const string &filename);
110  INLINE Filename &operator = (const wstring &filename);
111  INLINE Filename &operator = (const char *filename);
112  INLINE Filename &operator = (const Filename &copy);
113 
114 #ifdef USE_MOVE_SEMANTICS
115  INLINE Filename &operator = (string &&filename) NOEXCEPT;
116  INLINE Filename &operator = (Filename &&from) NOEXCEPT;
117 #endif
118 
119  // And retrieval is by any of the classic string operations.
120  INLINE operator const string & () const;
121  INLINE const char *c_str() const;
122  INLINE bool empty() const;
123  INLINE size_t length() const;
124  INLINE char operator [] (int n) const;
125 
126  EXTENSION(PyObject *__repr__() const);
127 
128  INLINE string substr(size_t begin, size_t end = string::npos) const;
129  INLINE void operator += (const string &other);
130  INLINE Filename operator + (const string &other) const;
131 
132  INLINE Filename operator / (const Filename &other) const;
133 
134  // Or, you can use any of these.
135  INLINE string get_fullpath() const;
136  INLINE wstring get_fullpath_w() const;
137  INLINE string get_dirname() const;
138  INLINE string get_basename() const;
139  INLINE string get_fullpath_wo_extension() const;
140  INLINE string get_basename_wo_extension() const;
141  INLINE string get_extension() const;
142 
143  // You can also use any of these to reassign pieces of the filename.
144  void set_fullpath(const string &s);
145  void set_dirname(const string &s);
146  void set_basename(const string &s);
147  void set_fullpath_wo_extension(const string &s);
148  void set_basename_wo_extension(const string &s);
149  void set_extension(const string &s);
150 
151  // Setting these flags appropriately is helpful when opening or
152  // searching for a file; it helps the Filename resolve OS-specific
153  // conventions (for instance, that dynamic library names should
154  // perhaps be changed from .so to .dll).
155  INLINE void set_binary();
156  INLINE void set_text();
157  INLINE bool is_binary() const;
158  INLINE bool is_text() const;
159  INLINE bool is_binary_or_text() const;
160 
161  INLINE void set_type(Type type);
162  INLINE Type get_type() const;
163 
164  INLINE void set_pattern(bool pattern);
165  INLINE bool get_pattern() const;
166 
167  INLINE bool has_hash() const;
168  Filename get_filename_index(int index) const;
169 
170  INLINE string get_hash_to_end() const;
171  void set_hash_to_end(const string &s);
172 
173  void extract_components(vector_string &components) const;
174  void standardize();
175 
176  // The following functions deal with the outside world.
177 
178  INLINE bool is_local() const;
179  INLINE bool is_fully_qualified() const;
180  void make_absolute();
181  void make_absolute(const Filename &start_directory);
182 
183  bool make_canonical();
184  bool make_true_case();
185 
186  string to_os_specific() const;
187  wstring to_os_specific_w() const;
188  string to_os_generic() const;
189  string to_os_short_name() const;
190  string to_os_long_name() const;
191 
192  bool exists() const;
193  bool is_regular_file() const;
194  bool is_writable() const;
195  bool is_directory() const;
196  bool is_executable() const;
197  int compare_timestamps(const Filename &other,
198  bool this_missing_is_old = true,
199  bool other_missing_is_old = true) const;
200  time_t get_timestamp() const;
201  time_t get_access_timestamp() const;
202  streamsize get_file_size() const;
203 
204  bool resolve_filename(const DSearchPath &searchpath,
205  const string &default_extension = string());
206  bool make_relative_to(Filename directory, bool allow_backups = true);
207  int find_on_searchpath(const DSearchPath &searchpath);
208 
209  bool scan_directory(vector_string &contents) const;
210 #ifdef HAVE_PYTHON
211  EXTENSION(PyObject *scan_directory() const);
212 #endif
213 
214  bool open_read(ifstream &stream) const;
215  bool open_write(ofstream &stream, bool truncate = true) const;
216  bool open_append(ofstream &stream) const;
217  bool open_read_write(fstream &stream, bool truncate = false) const;
218  bool open_read_append(fstream &stream) const;
219 
220 #ifdef USE_PANDAFILESTREAM
221  bool open_read(pifstream &stream) const;
222  bool open_write(pofstream &stream, bool truncate = true) const;
223  bool open_append(pofstream &stream) const;
224  bool open_read_write(pfstream &stream, bool truncate = false) const;
225  bool open_read_append(pfstream &stream) const;
226 #endif // USE_PANDAFILESTREAM
227 
228  bool chdir() const;
229  bool touch() const;
230  bool unlink() const;
231  BLOCKING bool rename_to(const Filename &other) const;
232  BLOCKING bool copy_to(const Filename &other) const;
233 
234  bool make_dir() const;
235  bool mkdir() const;
236  bool rmdir() const;
237 
238  // Comparison operators are handy.
239  INLINE bool operator == (const string &other) const;
240  INLINE bool operator != (const string &other) const;
241  INLINE bool operator < (const string &other) const;
242  INLINE int compare_to(const Filename &other) const;
243  INLINE bool __nonzero__() const;
244  int get_hash() const;
245 
246  INLINE void output(ostream &out) const;
247 
248  INLINE static void set_filesystem_encoding(TextEncoder::Encoding encoding);
249  INLINE static TextEncoder::Encoding get_filesystem_encoding();
250 
251 public:
252  bool atomic_compare_and_exchange_contents(string &orig_contents, const string &old_contents, const string &new_contents) const;
253  bool atomic_read_contents(string &contents) const;
254 
255 protected:
256  void locate_basename();
257  void locate_extension();
258  void locate_hash();
259  size_t get_common_prefix(const string &other) const;
260  static int count_slashes(const string &str);
261  bool r_make_canonical(const Filename &cwd);
262 
263  string _filename;
264  // We'll make these size_t instead of string::size_type to help out
265  // cppParser.
266  size_t _dirname_end;
267  size_t _basename_start;
268  size_t _basename_end;
269  size_t _extension_start;
270  size_t _hash_start;
271  size_t _hash_end;
272 
273  int _flags;
274 
275  static TextEncoder::Encoding _filesystem_encoding;
276  static TVOLATILE AtomicAdjust::Pointer _home_directory;
277  static TVOLATILE AtomicAdjust::Pointer _temp_directory;
278  static TVOLATILE AtomicAdjust::Pointer _user_appdata_directory;
279  static TVOLATILE AtomicAdjust::Pointer _common_appdata_directory;
280 
281 #ifdef ANDROID
282 public:
283  static string _internal_data_dir;
284 #endif
285 
286 public:
287  static TypeHandle get_class_type() {
288  return _type_handle;
289  }
290  static void init_type() {
291  register_type(_type_handle, "Filename");
292  }
293 
294 private:
295  static TypeHandle _type_handle;
296 };
297 
298 INLINE ostream &operator << (ostream &out, const Filename &n) {
299  n.output(out);
300  return out;
301 }
302 
303 #include "filename.I"
304 
305 #endif
306 
307 
308 
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
An STL function object class, this is intended to be used on any ordered collection of class objects ...
Definition: stl_compares.h:79
This class stores a list of directories that can be searched, in order, to locate a particular file...
Definition: dSearchPath.h:32
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85