Filename

class Filename

The name of a file, such as a texture file or an Egg file. 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.

Inheritance diagram

Inheritance diagram of Filename

enum Type
enumerator T_general = 0

These type values must fit within the bits allocated for F_type, below.

enumerator T_dso = 1
enumerator T_executable = 2
Filename(void)
Filename(Filename const &dirname, Filename const &basename)
Filename(PyObject *path)

Creates an empty Filename.

This constructor composes the filename out of a directory part and a basename part. It will insert an intervening ‘/’ if necessary.

Filename binary_filename(Filename const &filename)
Filename binary_filename(std::string const &filename)
char const *c_str(void) const
bool chdir(void) const

Changes directory to the specified location. Returns true if successful, false if failure.

int compare_timestamps(Filename const &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.

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.

int compare_to(Filename const &other) const
bool copy_to(Filename const &other) const

Copies the file to the indicated new filename, by reading the contents and writing it to the new file. Returns true if successful, false on failure. The copy is always binary, regardless of the filename settings.

Filename dso_filename(std::string const &filename)
bool empty(void) const
Filename executable_filename(std::string const &filename)
bool exists(void) const

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.

static Filename expand_from(std::string const &user_string, Type type = T_general)

Returns the same thing as from_os_specific(), but embedded environment variable references (e.g. “$DMODELS/foo.txt”) are expanded out. It also automatically elevates the file to its true case if needed.

void extract_components(vector_string &components) const

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.

int find_on_searchpath(DSearchPath const &searchpath)

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.

static Filename from_os_specific(std::string const &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).

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.

static Filename from_os_specific_w(std::wstring const &os_specific, Type type = T_general)

The wide-string variant of from_os_specific(). Returns a new Filename, converted from an os-specific wide-character string.

time_t get_access_timestamp(void) const

Returns a time_t value that represents the time the file was last accessed, if this information is available. See also get_timestamp(), which returns the last modification time.

std::string get_basename(void) const

Returns the basename part of the filename. This is everything in the filename after the rightmost slash, including any extensions.

std::string get_basename_wo_extension(void) const

Returns the basename part of the filename, without the file extension.

static TypeHandle get_class_type(void)
static Filename const &get_common_appdata_directory(void)

Returns a path to a system-defined directory appropriate for creating a subdirectory for storing application-specific data, common to all users.

std::string get_dirname(void) const

Returns the directory part of the filename. This is everything in the filename up to, but not including the rightmost slash.

std::string get_extension(void) const

Returns the file extension. This is everything after the rightmost dot, if there is one, or the empty string if there is not.

std::streamsize get_file_size(void) const

Returns the size of the file in bytes, or 0 if there is an error.

Filename get_filename_index(int index) const

If the pattern flag is set for this Filename and the filename string actually includes a sequence of hash marks, then this returns a new Filename with the sequence of hash marks replaced by the indicated index number.

If the pattern flag is not set for this Filename or it does not contain a sequence of hash marks, this quietly returns the original filename.

TextEncoder::Encoding get_filesystem_encoding(void)

Specifies the default encoding to be used for all subsequent Filenames objects. See set_filesystem_encoding().

std::string get_fullpath(void) const

Or, you can use any of these.

Returns the entire filename: directory, basename, extension. This is the same thing returned by the string typecast operator.

std::wstring get_fullpath_w(void) const

Returns the entire filename as a wide-character string.

std::string get_fullpath_wo_extension(void) const

Returns the full filename–directory and basename parts–except for the extension.

int get_hash(void) const

Returns a hash code that attempts to be mostly unique for different Filenames.

std::string get_hash_to_end(void) const

Returns the part of the filename beginning at the hash sequence (if any), and continuing to the end of the filename.

static Filename const &get_home_directory(void)

Returns a path to the user’s home directory, if such a thing makes sense in the current OS, or to the nearest equivalent. This may or may not be directly writable by the application.

bool get_pattern(void) const

Returns the flag indicating whether this is a filename pattern. See set_pattern().

static Filename const &get_temp_directory(void)

Returns a path to a system-defined temporary directory.

time_t get_timestamp(void) const

Returns a time_t value that represents the time the file was last modified, to within whatever precision the operating system records this information (on a Windows95 system, for instance, this may only be accurate to within 2 seconds).

If the timestamp cannot be determined, either because it is not supported by the operating system or because there is some error (such as file not found), returns 0.

Type get_type(void) const

Returns the type of the file represented by the filename, as previously set by set_type().

static Filename const &get_user_appdata_directory(void)

Returns a path to a system-defined directory appropriate for creating a subdirectory for storing application-specific data, specific to the current user.

bool has_hash(void) const

Returns true if the filename is indicated to be a filename pattern (that is, set_pattern(true) was called), and the filename pattern did include a sequence of hash marks, or false if it was not a filename pattern or did not include hash marks. If this is true, then get_filename_index() will return a different filename each time.

bool is_binary(void) const

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.

bool is_binary_or_text(void) const

Returns true either is_binary() or is_text() is true; that is, that the filename has been specified as either binary or text. If this is false, the filename has not been specified.

bool is_directory(void) const

Returns true if the filename exists and is a directory name, false otherwise.

bool is_executable(void) const

Returns true if the filename exists and is executable

bool is_fully_qualified(void) const

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).

bool is_local(void) const

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.

bool is_regular_file(void) const

Returns true if the filename exists and is the name of a regular file (i.e. not a directory or device), false otherwise.

bool is_text(void) const

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.

bool is_writable(void) const

Returns true if the filename exists and is either a directory or a regular file that can be written to, or false otherwise.

std::size_t length(void) const
void make_absolute(void)
void make_absolute(Filename const &start_directory)

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.

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.

bool make_canonical(void)

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).

bool make_dir(void) const

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.

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).

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.

bool make_true_case(void)

On a case-insensitive operating system (e.g. Windows), this method looks up the file in the file system and resets the Filename to represent the actual case of the file as it exists on the disk. The return value is true if the file exists and the conversion can be made, or false if there is some error.

On a case-sensitive operating system, this method does nothing and always returns true.

An empty filename is considered to exist in this case.

bool mkdir(void) const

Creates the directory named by this filename. Unlike make_dir(), this assumes that the Filename contains the directory name itself. Also, parent directories are not automatically created; this function fails if any parent directory is missing.

bool open_append(std::ofstream &stream) const
bool open_append(pofstream &stream) const

Opens the indicated ofstream 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().

Opens the indicated pifstream 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().

bool open_read(std::ifstream &stream) const
bool open_read(pifstream &stream) const

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().

Opens the indicated pifstream 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().

bool open_read_append(std::fstream &stream) const
bool open_read_append(pfstream &stream) const

Opens the indicated ifstream for reading and writing the file, if possible; writes are appended to the end of the file. 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().

Opens the indicated pfstream for reading and writing the file, if possible; writes are appended to the end of the file. 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().

bool open_read_write(std::fstream &stream, bool truncate = false) const
bool open_read_write(pfstream &stream, bool truncate = false) const

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_write() without first calling one of set_text() or set_binary().

bool open_write(std::ofstream &stream, bool truncate = true) const
bool open_write(pofstream &stream, bool truncate = true) const

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.

Opens the indicated pifstream 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.

void output(std::ostream &out) const
Filename pattern_filename(std::string const &filename)

Constructs a filename that represents a sequence of numbered files. See set_pattern().

bool rename_to(Filename const &other) const

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 on failure.

bool resolve_filename(DSearchPath const &searchpath, std::string const &default_extension = string())

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.

bool rmdir(void) const

The inverse of mkdir(): this removes the directory named by this Filename, if it is in fact a directory.

bool scan_directory(vector_string &contents) const
PyObject *scan_directory(void) const

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.

void set_basename(std::string const &s)

Replaces the basename part of the filename. This is everything in the filename after the rightmost slash, including any extensions.

void set_basename_wo_extension(std::string const &s)

Replaces the basename part of the filename, without the file extension.

void set_binary(void)

Setting these flags appropriately is helpful when opening or searching for a file; it helps the Filename resolve OS-specific conventions (for instance, that dynamic library names should perhaps be changed from .so to .dll).

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.

void set_dirname(std::string const &s)

Replaces the directory part of the filename. This is everything in the filename up to, but not including the rightmost slash.

void set_extension(std::string const &s)

Replaces the file extension. This is everything after the rightmost dot, if there is one, or the empty string if there is not.

void set_filesystem_encoding(TextEncoder::Encoding encoding)

Specifies the default encoding to be used for all subsequent Filenames. This is used to represent wide-character (Unicode) filenames internally. On non-Windows-based systems, the encoded filename is also passed to the underlying operating system.

void set_fullpath(std::string const &s)

You can also use any of these to reassign pieces of the filename.

Replaces the entire filename: directory, basename, extension. This can also be achieved with the assignment operator.

void set_fullpath_wo_extension(std::string const &s)

Replaces the full filename–directory and basename parts–except for the extension.

void set_hash_to_end(std::string const &s)

Replaces the part of the filename from the beginning of the hash sequence to the end of the filename.

void set_pattern(bool pattern)

Sets the flag indicating whether this is a filename pattern. When this is true, the filename is understood to be a placeholder for a numbered sequence of filename, such as an image sequence. In this case, a sequence of one or more hash characters (“#”) should appear in the filename string; these characters will be filled in with the corresponding number (or more) of digits representing the sequence number. Sequence numbers always begin counting at 0.

When this is true, methods like has_hash() and get_hash_to_end() and get_filename_index() may be called. Methods like is_exists() will implicitly test for existance of filename sequence 0.

void set_text(void)

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.

void set_type(Filename::Type type)

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).

void standardize(void)

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.

std::string substr(std::size_t begin) const
std::string substr(std::size_t begin, std::size_t end) const
static Filename temporary(std::string const &dirname, std::string const &prefix, std::string const &suffix = string(), Type type = T_general)

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.

Filename text_filename(Filename const &filename)
Filename text_filename(std::string const &filename)

Static constructors to explicitly create a filename that refers to a text or binary file. This is in lieu of calling set_text() or set_binary() or set_type().

std::string to_os_generic(void) 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. 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.

std::string to_os_long_name(void) const

This is the opposite of to_os_short_name(): it returns the “long name” of the filename, if the filename exists. On non-Windows platforms, this returns the same thing as to_os_specific().

std::string to_os_short_name(void) const

This works like to_os_generic(), but it returns the “short name” version of the filename, if it exists, or the original filename otherwise.

On Windows platforms, this returns the 8.3 filename version of the given filename, if the file exists, and the same thing as to_os_specific() otherwise. On non-Windows platforms, this always returns the same thing as to_os_specific().

std::string to_os_specific(void) 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). Returns the string representing the converted filename, but does not change the Filename itself.

See also from_os_specific().

std::wstring to_os_specific_w(void) const

The wide-string variant on to_os_specific().

bool touch(void) const

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.

bool unlink(void) const

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).