filename.h 7.5 KB

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