IOSS  2.0
Ioss_FileInfo.h
Go to the documentation of this file.
1 // Copyright(C) 1999-2020 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6 
7 #ifndef IOSS_Ioss_FileInfo_h
8 #define IOSS_Ioss_FileInfo_h
9 
10 #include <Ioss_CodeTypes.h>
11 #include <ctime> // for time_t
12 #include <string> // for string, operator!=, etc
13 #include <sys/types.h> // for off_t
14 
15 namespace Ioss {
16 
17  /*! \class FileInfo
18  * \author Greg Sjaardema
19  * \brief Return information about the specified file.
20  *
21  * A very minimal class (at least it used to be) for providing
22  * information about a file. FileInfo provides information about a
23  * file's name, path, and type (directory, symbolic link, file). Other
24  * information could be added as needed. It currently does not cache
25  * any information, so if it is heavily used, a caching capability
26  * should be added. See the Qt Toolkit QFileInfo class for a richer
27  * file class.
28  */
29 
30  class FileInfo
31  {
32  public:
33  //! Empty class referring to no file.
35 
36  //! Create object referring to file with name \a filename
37  //! \param my_filename name of file
38  explicit FileInfo(std::string my_filename);
39 
40  //! Create object referring to file with name \a filename
41  //! \param my_filename name of file
42  explicit FileInfo(const char *my_filename);
43 
44  //! Copy constructor
45  FileInfo(const FileInfo & /*copy_from*/);
46 
47  //! Constructor
48  //! \param dirpath Directory Path
49  //! \param my_filename base filename
50  FileInfo(const std::string &dirpath, const std::string &my_filename);
51 
53 
54  //! returns the number of processors that this file exists.
55  //! 0: Exists nowhere
56  //! \#proc: Exists everywhere
57  //! else: exists on some proc, but not all.
58  //! In the last case, a list of processors where it is missing is returned in 'where' on
59  //! processor 0.
60  int parallel_exists(MPI_Comm communicator, std::string &where) const;
61 
62  bool exists() const; //!< returns True if file exists, false if nonexistent
63  bool is_readable() const; //!< Exists and is readable
64  bool is_writable() const; //!< Exists and is writable
65  bool is_executable() const; //!< Exists and is executable
66 
67  bool is_file() const; //!< Is a plain file
68  bool is_dir() const; //!< Is a directory
69  bool is_symlink() const; //!< Is a symbolic link to a file or directory
70 
71  time_t modified() const; //!< Time of last data modification. See 'man stat(2)'
72  time_t accessed() const; //!< Time of last access
73  time_t created() const; //!< Time of last status change. (creation, chmod, ...)
74 
75  off_t size() const; //!< File size in bytes. Only if is_file() == true
76 
77  std::string filename() const; //!< Complete filename including path
78  std::string basename() const; //!< strip path and extension
79  std::string tailname() const; //!< basename() + extension()
80  std::string extension() const; //!< file extension.
81  std::string pathname() const; //!< directory path, no filename
82  std::string realpath() const; //!< canonicalized absolute path
83 
84  void set_filename(const std::string &name);
85  void set_filename(const char *name);
86 
87  bool operator==(const FileInfo &other) const { return filename_ == other.filename_; }
88 
89  bool operator!=(const FileInfo &other) const { return filename_ != other.filename_; }
90 
91  bool remove_file();
92 
93  //! This function is used to create the path to an output directory (or history, restart, etc.)
94  //! if it does not exist. Called by all processors. Will throw exception if path does not
95  //! specify a valid directory or if the path cannot be created.
96  static void create_path(const std::string &filename, MPI_Comm communicator);
97  static void create_path(const std::string &filename);
98 
99  private:
100  std::string filename_{};
101  bool exists_{false}; ///< this is used frequently, check on creation
102  bool readable_{false}; ///< this is used frequently, check on creation
103  };
104 } // namespace Ioss
105 #endif // IOSS_Ioss_FileInfo_h
Ioss::FileInfo::operator!=
bool operator!=(const FileInfo &other) const
Definition: Ioss_FileInfo.h:89
Ioss::FileInfo::filename
std::string filename() const
Complete filename including path.
Definition: Ioss_FileInfo.C:223
Ioss::FileInfo::created
time_t created() const
Time of last status change. (creation, chmod, ...)
Definition: Ioss_FileInfo.C:197
Ioss::FileInfo::remove_file
bool remove_file()
Definition: Ioss_FileInfo.C:307
Ioss::FileInfo::operator==
bool operator==(const FileInfo &other) const
Definition: Ioss_FileInfo.h:87
Ioss::FileInfo::tailname
std::string tailname() const
basename() + extension()
Definition: Ioss_FileInfo.C:267
Ioss::FileInfo::basename
std::string basename() const
strip path and extension
Definition: Ioss_FileInfo.C:277
Ioss::FileInfo::parallel_exists
int parallel_exists(MPI_Comm communicator, std::string &where) const
Definition: Ioss_FileInfo.C:83
Ioss::FileInfo::create_path
static void create_path(const std::string &filename, MPI_Comm communicator)
Definition: Ioss_FileInfo.C:354
Ioss::FileInfo::is_file
bool is_file() const
Is a plain file.
Definition: Ioss_FileInfo.C:130
Ioss::FileInfo::size
off_t size() const
File size in bytes. Only if is_file() == true.
Definition: Ioss_FileInfo.C:210
Ioss::FileInfo::is_executable
bool is_executable() const
Exists and is executable.
Definition: Ioss_FileInfo.C:126
Ioss::FileInfo::is_writable
bool is_writable() const
Exists and is writable.
Definition: Ioss_FileInfo.C:123
Ioss
The main namespace for the Ioss library.
Definition: Ioad_DatabaseIO.C:40
Ioss::FileInfo::is_dir
bool is_dir() const
Is a directory.
Definition: Ioss_FileInfo.C:144
Ioss::FileInfo::pathname
std::string pathname() const
directory path, no filename
Definition: Ioss_FileInfo.C:257
Ioss::FileInfo::exists_
bool exists_
this is used frequently, check on creation
Definition: Ioss_FileInfo.h:101
Ioss::FileInfo::realpath
std::string realpath() const
canonicalized absolute path
Definition: Ioss_FileInfo.C:290
Ioss::FileInfo::FileInfo
FileInfo()
Empty class referring to no file.
Ioss::FileInfo::set_filename
void set_filename(const std::string &name)
Definition: Ioss_FileInfo.C:226
Ioss::FileInfo::is_readable
bool is_readable() const
Exists and is readable.
Definition: Ioss_FileInfo.C:120
Ioss::FileInfo::is_symlink
bool is_symlink() const
Is a symbolic link to a file or directory.
Definition: Ioss_FileInfo.C:157
Ioss::FileInfo::modified
time_t modified() const
Time of last data modification. See 'man stat(2)'.
Definition: Ioss_FileInfo.C:171
Ioss::FileInfo::filename_
std::string filename_
Definition: Ioss_FileInfo.h:100
Ioss::FileInfo
Return information about the specified file.
Definition: Ioss_FileInfo.h:31
Ioss::FileInfo::~FileInfo
~FileInfo()
Ioss::FileInfo::FileInfo
FileInfo(const FileInfo &)
Copy constructor.
Ioss::FileInfo::accessed
time_t accessed() const
Time of last access.
Definition: Ioss_FileInfo.C:184
Ioss::FileInfo::extension
std::string extension() const
file extension.
Definition: Ioss_FileInfo.C:244
Ioss::FileInfo::exists
bool exists() const
returns True if file exists, false if nonexistent
Definition: Ioss_FileInfo.C:81
Ioss::FileInfo::readable_
bool readable_
this is used frequently, check on creation
Definition: Ioss_FileInfo.h:102
anonymous_namespace{io_info.C}::name
std::string name(const Ioss::GroupingEntity *entity)
Definition: io_info.C:65
MPI_Comm
int MPI_Comm
Definition: Ioss_CodeTypes.h:66
Ioss_CodeTypes.h