filename.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Filename: filename.h
  2. // Created by: drose (18Jan99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, 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://www.panda3d.org/license.txt .
  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 temporary(const string &dirname, const string &prefix,
  71. Type type = T_general);
  72. // Assignment is via the = operator.
  73. INLINE Filename &operator = (const string &filename);
  74. INLINE Filename &operator = (const char *filename);
  75. INLINE Filename &operator = (const Filename &copy);
  76. // And retrieval is by any of the classic string operations.
  77. INLINE operator const string & () const;
  78. INLINE const char *c_str() const;
  79. INLINE bool empty() const;
  80. INLINE size_t length() const;
  81. INLINE char operator [] (int n) const;
  82. // Or, you can use any of these.
  83. INLINE string get_fullpath() const;
  84. INLINE string get_dirname() const;
  85. INLINE string get_basename() const;
  86. INLINE string get_fullpath_wo_extension() const;
  87. INLINE string get_basename_wo_extension() const;
  88. INLINE string get_extension() const;
  89. // You can also use any of these to reassign pieces of the filename.
  90. void set_fullpath(const string &s);
  91. void set_dirname(const string &s);
  92. void set_basename(const string &s);
  93. void set_fullpath_wo_extension(const string &s);
  94. void set_basename_wo_extension(const string &s);
  95. void set_extension(const string &s);
  96. // Setting these flags appropriately is helpful when opening or
  97. // searching for a file; it helps the Filename resolve OS-specific
  98. // conventions (for instance, that dynamic library names should
  99. // perhaps be changed from .so to .dll).
  100. INLINE void set_binary();
  101. INLINE void set_text();
  102. INLINE bool is_binary() const;
  103. INLINE bool is_text() const;
  104. INLINE void set_type(Type type);
  105. INLINE Type get_type() const;
  106. void standardize();
  107. // The following functions deal with the outside world.
  108. INLINE bool is_local() const;
  109. INLINE bool is_fully_qualified() const;
  110. void make_absolute();
  111. void make_absolute(const Filename &start_directory);
  112. bool make_canonical();
  113. string to_os_specific() const;
  114. bool exists() const;
  115. bool is_regular_file() const;
  116. bool is_directory() const;
  117. bool is_executable() const;
  118. int compare_timestamps(const Filename &other,
  119. bool this_missing_is_old = true,
  120. bool other_missing_is_old = true) const;
  121. bool resolve_filename(const DSearchPath &searchpath,
  122. const string &default_extension = string());
  123. bool make_relative_to(Filename directory, bool allow_backups = true);
  124. int find_on_searchpath(const DSearchPath &searchpath);
  125. bool scan_directory(vector_string &contents) const;
  126. bool open_read(ifstream &stream) const;
  127. bool open_write(ofstream &stream) const;
  128. bool open_append(ofstream &stream) const;
  129. bool open_read_write(fstream &stream) const;
  130. bool touch() const;
  131. bool unlink() const;
  132. bool rename_to(const Filename &other) const;
  133. bool make_dir() const;
  134. // Comparison operators are handy.
  135. INLINE bool operator == (const string &other) const;
  136. INLINE bool operator != (const string &other) const;
  137. INLINE bool operator < (const string &other) const;
  138. INLINE void output(ostream &out) const;
  139. private:
  140. void locate_basename();
  141. void locate_extension();
  142. size_t get_common_prefix(const string &other) const;
  143. static int count_slashes(const string &str);
  144. string _filename;
  145. // We'll make these size_t instead of string::size_type to help out
  146. // cppParser.
  147. size_t _dirname_end;
  148. size_t _basename_start;
  149. size_t _basename_end;
  150. size_t _extension_start;
  151. int _flags;
  152. };
  153. INLINE ostream &operator << (ostream &out, const Filename &n) {
  154. n.output(out);
  155. return out;
  156. }
  157. #include "filename.I"
  158. #endif