filename.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. };
  55. PUBLISHED:
  56. INLINE Filename(const string &filename = "");
  57. INLINE Filename(const char *filename);
  58. INLINE Filename(const Filename &copy);
  59. Filename(const Filename &dirname, const Filename &basename);
  60. INLINE ~Filename();
  61. // Static constructors to explicitly create a filename that refers
  62. // to a text or binary file. This is in lieu of calling set_text()
  63. // or set_binary() or set_type().
  64. INLINE static Filename text_filename(const string &filename);
  65. INLINE static Filename binary_filename(const string &filename);
  66. INLINE static Filename dso_filename(const string &filename);
  67. INLINE static Filename executable_filename(const string &filename);
  68. static Filename from_os_specific(const string &os_specific,
  69. Type type = T_general);
  70. static Filename expand_from(const string &user_string,
  71. Type type = T_general);
  72. static Filename temporary(const string &dirname, const string &prefix,
  73. Type type = T_general);
  74. // Assignment is via the = operator.
  75. INLINE Filename &operator = (const string &filename);
  76. INLINE Filename &operator = (const char *filename);
  77. INLINE Filename &operator = (const Filename &copy);
  78. // And retrieval is by any of the classic string operations.
  79. INLINE operator const string & () const;
  80. INLINE const char *c_str() const;
  81. INLINE bool empty() const;
  82. INLINE size_t length() const;
  83. INLINE char operator [] (int n) 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. void extract_components(vector_string &components) const;
  109. void standardize();
  110. // The following functions deal with the outside world.
  111. INLINE bool is_local() const;
  112. INLINE bool is_fully_qualified() const;
  113. void make_absolute();
  114. void make_absolute(const Filename &start_directory);
  115. bool make_canonical();
  116. string to_os_specific() const;
  117. string to_os_generic() const;
  118. bool exists() const;
  119. bool is_regular_file() const;
  120. bool is_directory() const;
  121. bool is_executable() const;
  122. int compare_timestamps(const Filename &other,
  123. bool this_missing_is_old = true,
  124. bool other_missing_is_old = true) const;
  125. bool resolve_filename(const DSearchPath &searchpath,
  126. const string &default_extension = string());
  127. bool make_relative_to(Filename directory, bool allow_backups = true);
  128. int find_on_searchpath(const DSearchPath &searchpath);
  129. bool scan_directory(vector_string &contents) const;
  130. bool open_read(ifstream &stream) const;
  131. bool open_write(ofstream &stream, bool truncate = true) const;
  132. bool open_append(ofstream &stream) const;
  133. bool open_read_write(fstream &stream) const;
  134. bool touch() const;
  135. bool unlink() const;
  136. bool rename_to(const Filename &other) const;
  137. bool make_dir() const;
  138. // Comparison operators are handy.
  139. INLINE bool operator == (const string &other) const;
  140. INLINE bool operator != (const string &other) const;
  141. INLINE bool operator < (const string &other) const;
  142. INLINE void output(ostream &out) const;
  143. private:
  144. void locate_basename();
  145. void locate_extension();
  146. size_t get_common_prefix(const string &other) const;
  147. static int count_slashes(const string &str);
  148. bool r_make_canonical(const Filename &cwd);
  149. string _filename;
  150. // We'll make these size_t instead of string::size_type to help out
  151. // cppParser.
  152. size_t _dirname_end;
  153. size_t _basename_start;
  154. size_t _basename_end;
  155. size_t _extension_start;
  156. int _flags;
  157. };
  158. INLINE ostream &operator << (ostream &out, const Filename &n) {
  159. n.output(out);
  160. return out;
  161. }
  162. #include "filename.I"
  163. #endif