Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

dtool/src/dtoolutil/filename.h

Go to the documentation of this file.
00001 // Filename: filename.h
00002 // Created by:  drose (18Jan99)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #ifndef FILENAME_H
00020 #define FILENAME_H
00021 
00022 #include "dtoolbase.h"
00023 
00024 #include "vector_string.h"
00025 
00026 #include <assert.h>
00027 
00028 class DSearchPath;
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //       Class : Filename
00032 // Description : The name of a file, such as a texture file or an Egg
00033 //               file.  Stores the full pathname, and includes
00034 //               functions for extracting out the directory prefix
00035 //               part and the file extension and stuff.
00036 //
00037 //               A Filename is also aware of the mapping between the
00038 //               Unix-like filename convention we use internally, and
00039 //               the local OS's specific filename convention, and it
00040 //               knows how to perform basic OS-specific I/O, like
00041 //               testing for file existence and searching a
00042 //               searchpath, as well as the best way to open an
00043 //               fstream for reading or writing.
00044 ////////////////////////////////////////////////////////////////////
00045 class EXPCL_DTOOL Filename {
00046 PUBLISHED:
00047   enum Type {
00048     // These type values must fit within the bits allocated for
00049     // F_type, below.
00050     T_general    = 0x00,
00051     T_dso        = 0x01,
00052     T_executable = 0x02,
00053     // Perhaps other types will be added later.
00054   };
00055 
00056 public:
00057   enum Flags {
00058     F_type            = 0x0f,
00059     F_binary          = 0x10,
00060     F_text            = 0x20,
00061   };
00062 
00063 PUBLISHED:
00064   INLINE Filename(const string &filename = "");
00065   INLINE Filename(const char *filename);
00066   INLINE Filename(const Filename &copy);
00067   Filename(const Filename &dirname, const Filename &basename);
00068   INLINE ~Filename();
00069 
00070   // Static constructors to explicitly create a filename that refers
00071   // to a text or binary file.  This is in lieu of calling set_text()
00072   // or set_binary() or set_type().
00073   INLINE static Filename text_filename(const string &filename);
00074   INLINE static Filename binary_filename(const string &filename);
00075   INLINE static Filename dso_filename(const string &filename);
00076   INLINE static Filename executable_filename(const string &filename);
00077 
00078   static Filename from_os_specific(const string &os_specific,
00079                                    Type type = T_general);
00080   static Filename expand_from(const string &user_string, 
00081                               Type type = T_general);
00082   static Filename temporary(const string &dirname, const string &prefix,
00083                             Type type = T_general);
00084 
00085   // Assignment is via the = operator.
00086   INLINE Filename &operator = (const string &filename);
00087   INLINE Filename &operator = (const char *filename);
00088   INLINE Filename &operator = (const Filename &copy);
00089 
00090   // And retrieval is by any of the classic string operations.
00091   INLINE operator const string & () const;
00092   INLINE const char *c_str() const;
00093   INLINE bool empty() const;
00094   INLINE size_t length() const;
00095   INLINE char operator [] (int n) const;
00096 
00097   // Or, you can use any of these.
00098   INLINE string get_fullpath() const;
00099   INLINE string get_dirname() const;
00100   INLINE string get_basename() const;
00101   INLINE string get_fullpath_wo_extension() const;
00102   INLINE string get_basename_wo_extension() const;
00103   INLINE string get_extension() const;
00104 
00105   // You can also use any of these to reassign pieces of the filename.
00106   void set_fullpath(const string &s);
00107   void set_dirname(const string &s);
00108   void set_basename(const string &s);
00109   void set_fullpath_wo_extension(const string &s);
00110   void set_basename_wo_extension(const string &s);
00111   void set_extension(const string &s);
00112 
00113   // Setting these flags appropriately is helpful when opening or
00114   // searching for a file; it helps the Filename resolve OS-specific
00115   // conventions (for instance, that dynamic library names should
00116   // perhaps be changed from .so to .dll).
00117   INLINE void set_binary();
00118   INLINE void set_text();
00119   INLINE bool is_binary() const;
00120   INLINE bool is_text() const;
00121 
00122   INLINE void set_type(Type type);
00123   INLINE Type get_type() const;
00124 
00125   void extract_components(vector_string &components) const;
00126   void standardize();
00127 
00128   // The following functions deal with the outside world.
00129 
00130   INLINE bool is_local() const;
00131   INLINE bool is_fully_qualified() const;
00132   void make_absolute();
00133   void make_absolute(const Filename &start_directory);
00134 
00135   bool make_canonical();
00136 
00137   string to_os_specific() const;
00138   string to_os_generic() const;
00139 
00140   bool exists() const;
00141   bool is_regular_file() const;
00142   bool is_directory() const;
00143   bool is_executable() const;
00144   int compare_timestamps(const Filename &other,
00145                          bool this_missing_is_old = true,
00146                          bool other_missing_is_old = true) const;
00147   bool resolve_filename(const DSearchPath &searchpath,
00148                         const string &default_extension = string());
00149   bool make_relative_to(Filename directory, bool allow_backups = true);
00150   int find_on_searchpath(const DSearchPath &searchpath);
00151 
00152   bool scan_directory(vector_string &contents) const;
00153 
00154   bool open_read(ifstream &stream) const;
00155   bool open_write(ofstream &stream, bool truncate = true) const;
00156   bool open_append(ofstream &stream) const;
00157   bool open_read_write(fstream &stream) const;
00158 
00159   bool touch() const;
00160 
00161   bool unlink() const;
00162   bool rename_to(const Filename &other) const;
00163 
00164   bool make_dir() const;
00165 
00166   // Comparison operators are handy.
00167   INLINE bool operator == (const string &other) const;
00168   INLINE bool operator != (const string &other) const;
00169   INLINE bool operator < (const string &other) const;
00170 
00171   INLINE void output(ostream &out) const;
00172 
00173 private:
00174   void locate_basename();
00175   void locate_extension();
00176   size_t get_common_prefix(const string &other) const;
00177   static int count_slashes(const string &str);
00178 
00179   string _filename;
00180   // We'll make these size_t instead of string::size_type to help out
00181   // cppParser.
00182   size_t _dirname_end;
00183   size_t _basename_start;
00184   size_t _basename_end;
00185   size_t _extension_start;
00186 
00187   int _flags;
00188 };
00189 
00190 INLINE ostream &operator << (ostream &out, const Filename &n) {
00191   n.output(out);
00192   return out;
00193 }
00194 
00195 #include "filename.I"
00196 
00197 #endif
00198 
00199 
00200 

Generated on Thu May 1 22:12:59 2003 for DTool by doxygen1.3