filename.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #ifndef FILENAME_H
  15. #define FILENAME_H
  16. #include "ppremake.h"
  17. #include "vector_string.h"
  18. #include <assert.h>
  19. class DSearchPath;
  20. ////////////////////////////////////////////////////////////////////
  21. // Class : Filename
  22. // Description : The name of a file, such as a texture file or an Egg
  23. // file. Stores the full pathname, and includes
  24. // functions for extracting out the directory prefix
  25. // part and the file extension and stuff.
  26. //
  27. // A Filename is also aware of the mapping between the
  28. // Unix-like filename convention we use internally, and
  29. // the local OS's specific filename convention, and it
  30. // knows how to perform basic OS-specific I/O, like
  31. // testing for file existence and searching a
  32. // searchpath, as well as the best way to open an
  33. // fstream for reading or writing.
  34. ////////////////////////////////////////////////////////////////////
  35. class EXPCL_DTOOL Filename {
  36. PUBLISHED:
  37. enum Type {
  38. // These type values must fit within the bits allocated for
  39. // F_type, below.
  40. T_general = 0x00,
  41. T_dso = 0x01,
  42. T_executable = 0x02,
  43. // Perhaps other types will be added later.
  44. };
  45. public:
  46. enum Flags {
  47. F_type = 0x0f,
  48. F_binary = 0x10,
  49. F_text = 0x20,
  50. F_pattern = 0x40,
  51. };
  52. PUBLISHED:
  53. INLINE Filename(const string &filename = "");
  54. INLINE Filename(const char *filename);
  55. INLINE Filename(const Filename &copy);
  56. Filename(const Filename &dirname, const Filename &basename);
  57. INLINE ~Filename();
  58. // Static constructors to explicitly create a filename that refers
  59. // to a text or binary file. This is in lieu of calling set_text()
  60. // or set_binary() or set_type().
  61. INLINE static Filename text_filename(const string &filename);
  62. INLINE static Filename binary_filename(const string &filename);
  63. INLINE static Filename dso_filename(const string &filename);
  64. INLINE static Filename executable_filename(const string &filename);
  65. INLINE static Filename pattern_filename(const string &filename);
  66. static Filename from_os_specific(const string &os_specific,
  67. Type type = T_general);
  68. static Filename expand_from(const string &user_string,
  69. Type type = T_general);
  70. static Filename temporary(const string &dirname, const string &prefix,
  71. const string &suffix = string(),
  72. Type type = T_general);
  73. // Assignment is via the = operator.
  74. INLINE Filename &operator = (const string &filename);
  75. INLINE Filename &operator = (const char *filename);
  76. INLINE Filename &operator = (const Filename &copy);
  77. // And retrieval is by any of the classic string operations.
  78. INLINE operator const string & () const;
  79. INLINE const char *c_str() const;
  80. INLINE bool empty() const;
  81. INLINE size_t length() const;
  82. INLINE char operator [] (int n) const;
  83. INLINE string substr(size_t begin, size_t end = string::npos) const;
  84. // Or, you can use any of these.
  85. INLINE string get_fullpath() const;
  86. INLINE string get_dirname() const;
  87. INLINE string get_basename() const;
  88. INLINE string get_fullpath_wo_extension() const;
  89. INLINE string get_basename_wo_extension() const;
  90. INLINE string get_extension() const;
  91. // You can also use any of these to reassign pieces of the filename.
  92. void set_fullpath(const string &s);
  93. void set_dirname(const string &s);
  94. void set_basename(const string &s);
  95. void set_fullpath_wo_extension(const string &s);
  96. void set_basename_wo_extension(const string &s);
  97. void set_extension(const string &s);
  98. // Setting these flags appropriately is helpful when opening or
  99. // searching for a file; it helps the Filename resolve OS-specific
  100. // conventions (for instance, that dynamic library names should
  101. // perhaps be changed from .so to .dll).
  102. INLINE void set_binary();
  103. INLINE void set_text();
  104. INLINE bool is_binary() const;
  105. INLINE bool is_text() const;
  106. INLINE void set_type(Type type);
  107. INLINE Type get_type() const;
  108. INLINE void set_pattern(bool pattern);
  109. INLINE bool get_pattern() const;
  110. INLINE bool has_hash() const;
  111. Filename get_filename_index(int index) const;
  112. INLINE string get_hash_to_end() const;
  113. void set_hash_to_end(const string &s);
  114. void extract_components(vector_string &components) const;
  115. void standardize();
  116. // The following functions deal with the outside world.
  117. INLINE bool is_local() const;
  118. INLINE bool is_fully_qualified() const;
  119. void make_absolute();
  120. void make_absolute(const Filename &start_directory);
  121. bool make_canonical();
  122. bool make_true_case();
  123. string to_os_specific() const;
  124. string to_os_generic() const;
  125. string to_os_short_name() const;
  126. string to_os_long_name() const;
  127. bool exists() const;
  128. bool is_regular_file() const;
  129. bool is_directory() const;
  130. bool is_executable() const;
  131. int compare_timestamps(const Filename &other,
  132. bool this_missing_is_old = true,
  133. bool other_missing_is_old = true) const;
  134. time_t get_timestamp() const;
  135. time_t get_access_timestamp() const;
  136. off_t get_file_size() const;
  137. bool resolve_filename(const DSearchPath &searchpath,
  138. const string &default_extension = string());
  139. bool make_relative_to(Filename directory, bool allow_backups = true);
  140. int find_on_searchpath(const DSearchPath &searchpath);
  141. bool scan_directory(vector_string &contents) const;
  142. bool open_read(ifstream &stream) const;
  143. bool open_write(ofstream &stream, bool truncate = true) const;
  144. bool open_append(ofstream &stream) const;
  145. bool open_read_write(fstream &stream) const;
  146. bool chdir() const;
  147. bool touch() const;
  148. bool unlink() const;
  149. bool rename_to(const Filename &other) const;
  150. bool make_dir() const;
  151. // Comparison operators are handy.
  152. INLINE bool operator == (const string &other) const;
  153. INLINE bool operator != (const string &other) const;
  154. INLINE bool operator < (const string &other) const;
  155. INLINE int compare_to(const Filename &other) const;
  156. INLINE void output(ostream &out) const;
  157. public:
  158. bool atomic_compare_and_exchange_contents(string &orig_contents, const string &old_contents, const string &new_contents) const;
  159. bool atomic_read_contents(string &contents) const;
  160. protected:
  161. void locate_basename();
  162. void locate_extension();
  163. void locate_hash();
  164. size_t get_common_prefix(const string &other) const;
  165. static int count_slashes(const string &str);
  166. bool r_make_canonical(const Filename &cwd);
  167. string _filename;
  168. // We'll make these size_t instead of string::size_type to help out
  169. // cppParser.
  170. size_t _dirname_end;
  171. size_t _basename_start;
  172. size_t _basename_end;
  173. size_t _extension_start;
  174. size_t _hash_start;
  175. size_t _hash_end;
  176. int _flags;
  177. };
  178. INLINE ostream &operator << (ostream &out, const Filename &n) {
  179. n.output(out);
  180. return out;
  181. }
  182. #include "filename.I"
  183. #endif