#include <filename.h>
Public Types | |
enum | Type { T_general = 0x00, T_dso = 0x01, T_executable = 0x02 } |
enum | Flags { F_type = 0x0f, F_binary = 0x10, F_text = 0x20 } |
Public Member Functions | |
Filename (const string &filename="") | |
Filename (const char *filename) | |
Filename (const Filename ©) | |
Filename (const Filename &dirname, const Filename &basename) | |
This constructor composes the filename out of a directory part and a basename part. | |
~Filename () | |
Filename & | operator= (const string &filename) |
Filename & | operator= (const char *filename) |
Filename & | operator= (const Filename ©) |
operator const string & () const | |
const char * | c_str () const |
bool | empty () const |
size_t | length () const |
char | operator[] (int n) const |
string | get_fullpath () const |
Returns the entire filename: directory, basename, extension. | |
string | get_dirname () const |
Returns the directory part of the filename. | |
string | get_basename () const |
Returns the basename part of the filename. | |
string | get_fullpath_wo_extension () const |
Returns the full filename--directory and basename parts--except for the extension. | |
string | get_basename_wo_extension () const |
Returns the basename part of the filename, without the file extension. | |
string | get_extension () const |
Returns the file extension. | |
void | set_fullpath (const string &s) |
Replaces the entire filename: directory, basename, extension. | |
void | set_dirname (const string &s) |
Replaces the directory part of the filename. | |
void | set_basename (const string &s) |
Replaces the basename part of the filename. | |
void | set_fullpath_wo_extension (const string &s) |
Replaces the full filename--directory and basename parts--except for the extension. | |
void | set_basename_wo_extension (const string &s) |
Replaces the basename part of the filename, without the file extension. | |
void | set_extension (const string &s) |
Replaces the file extension. | |
void | set_binary () |
Indicates that the filename represents a binary file. | |
void | set_text () |
Indicates that the filename represents a text file. | |
bool | is_binary () const |
Returns true if the Filename has been indicated to represent a binary file via a previous call to set_binary(). | |
bool | is_text () const |
Returns true if the Filename has been indicated to represent a text file via a previous call to set_text(). | |
void | set_type (Type type) |
Sets the type of the file represented by the filename. | |
Type | get_type () const |
Returns the type of the file represented by the filename, as previously set by set_type(). | |
void | extract_components (vector_string &components) const |
Extracts out the individual directory components of the path into a series of strings. | |
void | standardize () |
Converts the filename to standard form by replacing consecutive slashes with a single slash, removing a trailing slash if present, and backing up over ../ sequences within the filename where possible. | |
bool | is_local () const |
Returns true if the filename is local, e.g. | |
bool | is_fully_qualified () const |
Returns true if the filename is fully qualified, e.g. | |
void | make_absolute () |
Converts the filename to a fully-qualified pathname from the root (if it is a relative pathname), and then standardizes it (see standardize()). | |
void | make_absolute (const Filename &start_directory) |
Converts the filename to a fully-qualified filename from the root (if it is a relative filename), and then standardizes it (see standardize()). | |
bool | make_canonical () |
Converts this filename to a canonical name by replacing the directory part with the fully-qualified directory part. | |
string | to_os_specific () const |
Converts the filename from our generic Unix-like convention (forward slashes starting with the root at '/') to the corresponding filename in the local operating system (slashes in the appropriate direction, starting with the root at C:\, for instance). | |
string | to_os_generic () const |
This is similar to to_os_specific(), but it is designed to generate a filename that can be understood on as many platforms as possible. | |
bool | exists () const |
Returns true if the filename exists on the disk, false otherwise. | |
bool | is_regular_file () const |
Returns true if the filename exists and is the name of a regular file (i.e. | |
bool | is_directory () const |
Returns true if the filename exists and is a directory name, false otherwise. | |
bool | is_executable () const |
Returns true if the filename exists and is executable. | |
int | compare_timestamps (const Filename &other, bool this_missing_is_old=true, bool other_missing_is_old=true) const |
Returns a number less than zero if the file named by this object is older than the given file, zero if they have the same timestamp, or greater than zero if this one is newer. | |
bool | resolve_filename (const DSearchPath &searchpath, const string &default_extension=string()) |
Searches the given search path for the filename. | |
bool | make_relative_to (Filename directory, bool allow_backups=true) |
Adjusts this filename, which must be a fully-specified pathname beginning with a slash, to make it a relative filename, relative to the fully-specified directory indicated (which must also begin with, and may or may not end with, a slash--a terminating slash is ignored). | |
int | find_on_searchpath (const DSearchPath &searchpath) |
Performs the reverse of the resolve_filename() operation: assuming that the current filename is fully-specified pathname (i.e. | |
bool | scan_directory (vector_string &contents) const |
Attempts to open the named filename as if it were a directory and looks for the non-hidden files within the directory. | |
bool | open_read (ifstream &stream) const |
Opens the indicated ifstream for reading the file, if possible. | |
bool | open_write (ofstream &stream, bool truncate=true) const |
Opens the indicated ifstream for writing the file, if possible. | |
bool | open_append (ofstream &stream) const |
Opens the indicated ifstream for writing the file, if possible. | |
bool | open_read_write (fstream &stream) const |
Opens the indicated fstream for read/write access to the file, if possible. | |
bool | touch () const |
Updates the modification time of the file to the current time. | |
bool | unlink () const |
Permanently deletes the file associated with the filename, if possible. | |
bool | rename_to (const Filename &other) const |
Renames the file to the indicated new filename. | |
bool | make_dir () const |
Creates all the directories in the path to the file specified in the filename, except for the basename itself. | |
bool | operator== (const string &other) const |
bool | operator!= (const string &other) const |
bool | operator< (const string &other) const |
void | output (ostream &out) const |
Static Public Member Functions | |
Filename | text_filename (const string &filename) |
Filename | binary_filename (const string &filename) |
Filename | dso_filename (const string &filename) |
Filename | executable_filename (const string &filename) |
Filename | from_os_specific (const string &os_specific, Type type=T_general) |
This named constructor returns a Panda-style filename (that is, using forward slashes, and no drive letter) based on the supplied filename string that describes a filename in the local system conventions (for instance, on Windows, it may use backslashes or begin with a drive letter and a colon). | |
Filename | expand_from (const string &user_string, Type type=T_general) |
Returns the same thing as from_os_specific(), but embedded environment variable references (e.g. | |
Filename | temporary (const string &dirname, const string &prefix, Type type=T_general) |
Generates a temporary filename within the indicated directory, using the indicated prefix. | |
Private Member Functions | |
void | locate_basename () |
After the string has been reassigned, search for the slash marking the beginning of the basename, and set _dirname_end and _basename_start correctly. | |
void | locate_extension () |
Once the end of the directory prefix has been found, and _dirname_end and _basename_start are set correctly, search for the dot marking the beginning of the extension, and set _basename_end and _extension_start correctly. | |
size_t | get_common_prefix (const string &other) const |
Returns the length of the longest common initial substring of this string and the other one that ends in a slash. | |
Static Private Member Functions | |
int | count_slashes (const string &str) |
Returns the number of non-consecutive slashes in the indicated string, not counting a terminal slash. | |
Private Attributes | |
string | _filename |
size_t | _dirname_end |
size_t | _basename_start |
size_t | _basename_end |
size_t | _extension_start |
int | _flags |
Stores the full pathname, and includes functions for extracting out the directory prefix part and the file extension and stuff.
A Filename is also aware of the mapping between the Unix-like filename convention we use internally, and the local OS's specific filename convention, and it knows how to perform basic OS-specific I/O, like testing for file existence and searching a searchpath, as well as the best way to open an fstream for reading or writing.
Definition at line 62 of file filename.h.
|
Definition at line 74 of file filename.h. |
|
Definition at line 64 of file filename.h. Referenced by set_binary(). |
|
Definition at line 30 of file filename.I. |
|
Definition at line 43 of file filename.I. References INLINE. |
|
Definition at line 57 of file filename.I. References INLINE, and set_text(). |
|
This constructor composes the filename out of a directory part and a basename part. It will insert an intervening '/' if necessary. Definition at line 263 of file filename.cxx. |
|
Definition at line 131 of file filename.I. |
|
Definition at line 89 of file filename.I. |
|
Definition at line 203 of file filename.I. References _filename, and INLINE. Referenced by CPPFile::replace_nearer(). |
|
Returns a number less than zero if the file named by this object is older than the given file, zero if they have the same timestamp, or greater than zero if this one is newer. If this_missing_is_old is true, it indicates that a missing file will be treated as if it were older than any other file; otherwise, a missing file will be treated as if it were newer than any other file. Similarly for other_missing_is_old. Definition at line 1137 of file filename.cxx. References _filename, DSearchPath::get_directory(), DSearchPath::get_num_directories(), and make_relative_to(). |
|
Returns the number of non-consecutive slashes in the indicated string, not counting a terminal slash.
Definition at line 2037 of file filename.cxx. Referenced by is_executable(). |
|
Definition at line 103 of file filename.I. |
|
Definition at line 215 of file filename.I. References _filename, and INLINE. Referenced by is_executable(), make_absolute(), and InterrogateDatabase::remap_indices(). |
|
Definition at line 117 of file filename.I. References _filename, INLINE, locate_basename(), and locate_extension(). |
|
Returns true if the filename exists on the disk, false otherwise. If the type is indicated to be executable, this also tests that the file has execute permission. Definition at line 988 of file filename.cxx. Referenced by DSearchPath::get_num_directories(), is_regular_file(), and make_canonical(). |
|
Returns the same thing as from_os_specific(), but embedded environment variable references (e.g. "$DMODELS/foo.txt") are expanded out. Definition at line 379 of file filename.cxx. |
|
Extracts out the individual directory components of the path into a series of strings. get_basename() will be the last component stored in the vector. Note that no distinction is made by this method between a leading slash and no leading slash, but you can call is_local() to differentiate the two cases. Definition at line 634 of file filename.cxx. |
|
Performs the reverse of the resolve_filename() operation: assuming that the current filename is fully-specified pathname (i.e. beginning with '/'), look on the indicated search path for a directory under which the file can be found. When found, adjust the Filename to be relative to the indicated directory name. Returns the index of the directory on the searchpath at which the file was found, or -1 if it was not found. Definition at line 1377 of file filename.cxx. References ios_openmode, is_binary(), is_text(), and to_os_specific(). |
|
This named constructor returns a Panda-style filename (that is, using forward slashes, and no drive letter) based on the supplied filename string that describes a filename in the local system conventions (for instance, on Windows, it may use backslashes or begin with a drive letter and a colon). Use this function to create a Filename from an externally-given filename string. Use to_os_specific() again later to reconvert it back to the local operating system's conventions. This function will do the right thing even if the filename is partially local conventions and partially Panda conventions; e.g. some backslashes and some forward slashes. Definition at line 316 of file filename.cxx. Referenced by Config::ConfigTable::ReadConfigFile(). |
|
Returns the basename part of the filename. This is everything in the filename after the rightmost slash, including any extensions. Definition at line 294 of file filename.I. |
|
Returns the basename part of the filename, without the file extension.
Definition at line 326 of file filename.I. |
|
Returns the length of the longest common initial substring of this string and the other one that ends in a slash. This is the lowest directory common to both filenames. Definition at line 2009 of file filename.cxx. Referenced by is_executable(). |
|
Returns the directory part of the filename. This is everything in the filename up to, but not including the rightmost slash. Definition at line 277 of file filename.I. References _extension_start, _filename, and INLINE. |
|
Returns the file extension. This is everything after the rightmost dot, if there is one, or the empty string if there is not. Definition at line 348 of file filename.I. References _flags, F_type, INLINE, set_binary(), T_dso, T_executable, and T_general. Referenced by is_regular_file(). |
|
Returns the entire filename: directory, basename, extension. This is the same thing returned by the string typecast operator, so this function is a little redundant. Definition at line 260 of file filename.I. References _basename_end, _basename_start, _filename, and INLINE. Referenced by is_executable(), and is_regular_file(). |
|
Returns the full filename--directory and basename parts--except for the extension.
Definition at line 310 of file filename.I. |
|
Returns the type of the file represented by the filename, as previously set by set_type().
Definition at line 480 of file filename.I. Referenced by make_absolute(). |
|
Returns true if the Filename has been indicated to represent a binary file via a previous call to set_binary(). It is possible that neither is_binary() nor is_text() will be true, if neither set_binary() nor set_text() was ever called. Definition at line 411 of file filename.I. Referenced by find_on_searchpath(). |
|
Returns true if the filename exists and is a directory name, false otherwise.
Definition at line 1058 of file filename.cxx. |
|
Returns true if the filename exists and is executable.
Definition at line 1091 of file filename.cxx. References _filename, count_slashes(), empty(), get_common_prefix(), get_fullpath(), size_t, and standardize(). |
|
Returns true if the filename is fully qualified, e.g. begins with a slash. This is almost, but not quite, the same thing as !is_local(). It's not exactly the same because a special case is made for filenames that begin with a single dot followed by a slash--these are considered to be fully qualified (they are explicitly relative to the current directory, and do not refer to a filename on a search path somewhere). Definition at line 526 of file filename.I. Referenced by DSearchPath::get_num_directories(), and DSearchPath::prepend_path(). |
|
Returns true if the filename is local, e.g. does not begin with a slash, or false if the filename is fully specified from the root. Definition at line 497 of file filename.I. Referenced by is_regular_file(). |
|
Returns true if the filename exists and is the name of a regular file (i.e. not a directory or device), false otherwise. Definition at line 1024 of file filename.cxx. References exists(), DSearchPath::find_file(), get_extension(), get_fullpath(), is_local(), and set_extension(). |
|
Returns true if the Filename has been indicated to represent a text file via a previous call to set_text(). It is possible that neither is_binary() nor is_text() will be true, if neither set_binary() nor set_text() was ever called. Definition at line 432 of file filename.I. References INLINE. Referenced by find_on_searchpath(). |
|
Definition at line 227 of file filename.I. References _dirname_end, _filename, and INLINE. |
|
After the string has been reassigned, search for the slash marking the beginning of the basename, and set _dirname_end and _basename_start correctly.
Definition at line 1902 of file filename.cxx. Referenced by executable_filename(). |
|
Once the end of the directory prefix has been found, and _dirname_end and _basename_start are set correctly, search for the dot marking the beginning of the extension, and set _basename_end and _extension_start correctly.
Definition at line 1963 of file filename.cxx. Referenced by executable_filename(), and set_dirname(). |
|
Converts the filename to a fully-qualified filename from the root (if it is a relative filename), and then standardizes it (see standardize()). This flavor accepts a specific starting directory that the filename is known to be relative to. Definition at line 773 of file filename.cxx. References T_executable. |
|
Converts the filename to a fully-qualified pathname from the root (if it is a relative pathname), and then standardizes it (see standardize()). This is sometimes a little problematic, since it may convert the file to its 'true' absolute pathname, which could be an ugly NFS-named file, irrespective of symbolic links (e.g. /.automount/dimbo/root/usr2/fit/people/drose instead of /fit/people/drose); besides being ugly, filenames like this may not be consistent across multiple different platforms. Definition at line 752 of file filename.cxx. References empty(), get_type(), standardize(), and T_dso. |
|
Converts this filename to a canonical name by replacing the directory part with the fully-qualified directory part. This is done by changing to that directory and calling getcwd(). This has the effect of (a) converting relative paths to absolute paths (but see make_absolute() if this is the only effect you want), and (b) always resolving a given directory name to the same string, even if different symbolic links are traversed, and (c) changing nice symbolic-link paths like /fit/people/drose to ugly NFS automounter names like /hosts/dimbo/usr2/fit/people/drose. This can be troubling, but sometimes this is exactly what you want, particularly if you're about to call make_relative_to() between two filenames. The return value is true if successful, or false on failure (usually because the directory name does not exist or cannot be chdir'ed into). Definition at line 828 of file filename.cxx. References DWORD, exists(), and to_os_specific(). |
|
Creates all the directories in the path to the file specified in the filename, except for the basename itself. This assumes that the Filename contains the name of a file, not a directory name; it ensures that the directory containing the file exists. However, if the filename ends in a slash, it assumes the Filename represents the name of a directory, and creates all the paths. Definition at line 1845 of file filename.cxx. |
|
Adjusts this filename, which must be a fully-specified pathname beginning with a slash, to make it a relative filename, relative to the fully-specified directory indicated (which must also begin with, and may or may not end with, a slash--a terminating slash is ignored). This only performs a string comparsion, so it may be wise to call make_canonical() on both filenames before calling make_relative_to(). If allow_backups is false, the filename will only be adjusted to be made relative if it is already somewhere within or below the indicated directory. If allow_backups is true, it will be adjusted in all cases, even if this requires putting a series of ../ characters before the filename--unless it would have to back all the way up to the root. Returns true if the file was adjusted, false if it was not. Definition at line 1310 of file filename.cxx. Referenced by compare_timestamps(). |
|
Opens the indicated ifstream for writing the file, if possible. Returns true if successful, false otherwise. This requires the setting of the set_text()/set_binary() flags to open the file appropriately as indicated; it is an error to call open_read() without first calling one of set_text() or set_binary(). Definition at line 1620 of file filename.cxx. |
|
Opens the indicated ifstream for reading the file, if possible. Returns true if successful, false otherwise. This requires the setting of the set_text()/set_binary() flags to open the file appropriately as indicated; it is an error to call open_read() without first calling one of set_text() or set_binary(). Definition at line 1513 of file filename.cxx. References to_os_specific(). Referenced by InterrogateDatabase::remap_indices(), and CPPPreprocessor::InputFile::~InputFile(). |
|
Opens the indicated fstream for read/write access to the file, if possible. Returns true if successful, false otherwise. This requires the setting of the set_text()/set_binary() flags to open the file appropriately as indicated; it is an error to call open_read() without first calling one of set_text() or set_binary(). Definition at line 1665 of file filename.cxx. |
|
Opens the indicated ifstream for writing the file, if possible. Returns true if successful, false otherwise. This requires the setting of the set_text()/set_binary() flags to open the file appropriately as indicated; it is an error to call open_read() without first calling one of set_text() or set_binary(). If truncate is true, the file is truncated to zero length upon opening it, if it already exists. Otherwise, the file is kept at its original length. Definition at line 1561 of file filename.cxx. |
|
Definition at line 191 of file filename.I. |
|
Definition at line 552 of file filename.I. |
|
Definition at line 564 of file filename.I. |
|
Definition at line 172 of file filename.I. |
|
Definition at line 159 of file filename.I. |
|
Definition at line 143 of file filename.I. References _basename_end, _basename_start, _dirname_end, _extension_start, _filename, _flags, and INLINE. |
|
Definition at line 540 of file filename.I. |
|
Definition at line 239 of file filename.I. References _basename_start, _filename, and INLINE. |
|
Definition at line 577 of file filename.I. |
|
Renames the file to the indicated new filename. If the new filename is in a different directory, this will perform a move. Returns true if successful, false if failure. Definition at line 1813 of file filename.cxx. |
|
Searches the given search path for the filename. If it is found, updates the filename to the full pathname found and returns true; otherwise, returns false. Definition at line 1218 of file filename.cxx. |
|
Attempts to open the named filename as if it were a directory and looks for the non-hidden files within the directory. Fills the given vector up with the sorted list of filenames that are local to this directory. It is the user's responsibility to ensure that the contents vector is empty before making this call; otherwise, the new files will be appended to it. Returns true on success, false if the directory could not be read for some reason. Definition at line 1423 of file filename.cxx. References NULL. |
|
Replaces the basename part of the filename. This is everything in the filename after the rightmost slash, including any extensions. Definition at line 526 of file filename.cxx. References _basename_end, _extension_start, and _filename. |
|
Replaces the basename part of the filename, without the file extension.
Definition at line 566 of file filename.cxx. References _filename. |
|
Indicates that the filename represents a binary file. This is primarily relevant to the read_file() and write_file() methods, so they can set the appropriate flags to the OS. Definition at line 371 of file filename.I. References _flags, F_type, INLINE, and Type. Referenced by get_extension(), and text_filename(). |
|
Replaces the directory part of the filename. This is everything in the filename up to, but not including the rightmost slash. Definition at line 469 of file filename.cxx. References _basename_start, _filename, and locate_extension(). |
|
Replaces the file extension. This is everything after the rightmost dot, if there is one, or the empty string if there is not. Definition at line 594 of file filename.cxx. References _filename, and size_t. Referenced by is_regular_file(). |
|
Replaces the entire filename: directory, basename, extension. This can also be achieved with the assignment operator. Definition at line 452 of file filename.cxx. References _basename_end, _basename_start, and _extension_start. |
|
Replaces the full filename--directory and basename parts--except for the extension.
Definition at line 543 of file filename.cxx. |
|
Indicates that the filename represents a text file. This is primarily relevant to the read_file() and write_file() methods, so they can set the appropriate flags to the OS. Definition at line 390 of file filename.I. References _filename. Referenced by Filename(), and InterrogateDatabase::remap_indices(). |
|
Sets the type of the file represented by the filename. This is useful for to_os_specific(), resolve_filename(), test_existence(), and all such real-world access functions. It helps the Filename know how to map the internal filename to the OS-specific filename (for instance, maybe executables should have an .exe extension). Definition at line 457 of file filename.I. |
|
Converts the filename to standard form by replacing consecutive slashes with a single slash, removing a trailing slash if present, and backing up over ../ sequences within the filename where possible.
Definition at line 671 of file filename.cxx. Referenced by is_executable(), and make_absolute(). |
|
Generates a temporary filename within the indicated directory, using the indicated prefix. If the directory is empty, a system-defined directory is chosen instead. The generated filename did not exist when the Filename checked, but since it does not specifically create the file, it is possible that another process could simultaneously create a file by the same name. Definition at line 409 of file filename.cxx. References _basename_end, _basename_start, _dirname_end, _extension_start, and _filename. |
|
Definition at line 75 of file filename.I. References INLINE, and set_binary(). |
|
This is similar to to_os_specific(), but it is designed to generate a filename that can be understood on as many platforms as possible. Since Windows can usually understand a forward-slash-delimited filename, this means it does the same thing as to_os_specific(), but it uses forward slashes instead of backslashes. This method has a pretty limited use; it should generally be used for writing file references to a file that might be read on any operating system. Definition at line 952 of file filename.cxx. References to_os_specific(). |
|
Converts the filename from our generic Unix-like convention (forward slashes starting with the root at '/') to the corresponding filename in the local operating system (slashes in the appropriate direction, starting with the root at C:\, for instance). Returns the string representing the converted filename, but does not change the Filename itself. See also from_os_specific(). Definition at line 902 of file filename.cxx. Referenced by find_on_searchpath(), make_canonical(), open_read(), and to_os_generic(). |
|
Updates the modification time of the file to the current time. If the file does not already exist, it will be created. Returns true if successful, false if there is an error. Definition at line 1704 of file filename.cxx. |
|
Permanently deletes the file associated with the filename, if possible. Returns true if successful, false if failure (for instance, because the file did not exist, or because permissions were inadequate). Definition at line 1792 of file filename.cxx. |
|
Definition at line 201 of file filename.h. Referenced by get_fullpath(), operator=(), set_basename(), set_fullpath(), and temporary(). |
|
Definition at line 200 of file filename.h. Referenced by get_fullpath(), operator=(), operator[](), set_dirname(), set_fullpath(), and temporary(). |
|
Definition at line 199 of file filename.h. Referenced by length(), operator=(), and temporary(). |
|
Definition at line 202 of file filename.h. Referenced by get_dirname(), operator=(), set_basename(), set_fullpath(), and temporary(). |
|
Definition at line 196 of file filename.h. Referenced by c_str(), compare_timestamps(), empty(), executable_filename(), get_dirname(), get_fullpath(), is_executable(), length(), operator const string &(), operator=(), operator[](), set_basename(), set_basename_wo_extension(), set_dirname(), set_extension(), set_text(), and temporary(). |
|
Definition at line 204 of file filename.h. Referenced by Filename(), get_basename(), get_basename_wo_extension(), get_extension(), get_fullpath_wo_extension(), operator=(), and set_binary(). |