// FileDirectory.h #ifndef dataset_util__FileDirectory_H #define dataset_util__FileDirectory_H // David Adams // February 2003 // // Class describing a file system directory. // Data includes the subdirectories and other files contained // inside this directory. // This data reflects the state of the file system at the time // of construction or the most recent call to update(). #include #include #include "dataset_util/FileStatus.h" #include "dataset_util/FileName.h" class FileDirectory { public: // typedefs typedef std::string Name; typedef std::vector NameList; typedef std::vector FileList; typedef std::vector DirectoryList; private: // data // Specified name for the directory. Name m_name; // Validity. // Set at construction or last update. FileStatus m_status; // List of files. NameList m_file_names; FileList m_files; // List of subdirectories. mutable bool m_subdirs_filled; mutable NameList m_subdir_names; mutable DirectoryList m_subdirs; public: // methods // Constructor from file name. explicit FileDirectory(Name name =""); // Update the contents with the current state of the file system. // Returns if the directory is valid. bool update(); // Return the directory name. Name name() const { return m_name; } // Return if the direectory is valid. // Name must be valid and directory must exist. // It need not be readable. bool is_valid() const; // Return the status of the directory. const FileStatus& status() const { return m_status; } // Return the list of file names relative to the directory. // (including subdirectories but not . and ..). // Created upon construction and update. const NameList& file_names() const { return m_file_names; }; // Return the list of contained files (including subdirectories // but not . and ..). // Created upon construction and update. const FileList& files() const { return m_files; }; // Return the subdirectory names relative to the directory. // Created on request and cleared during update. const NameList& subdir_names() const; // Return the subdirectories dirname/subdirname. // Created and cleared along with the subdirectory names. const DirectoryList& subdirs() const; // Return the directory+name for a file inside this directory. // E.g. myfile.dat --> mydir/myfile.dat // Blank is returned if the input name is blank Name prepend(Name fname) const; // Find a file in a directory. // Returns directory+name for file name if the file exists. // Otherwise returns blank. Name find(Name fname) const; }; std::ostream& operator<<(std::ostream& lhs, const FileDirectory& rhs); #endif