IOSS 2.0

Ioss_FileInfo.h
Go to the documentation of this file.
1 // Copyright(C) 1999-2017 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 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
16 //
17 // * Neither the name of NTESS nor the names of its
18 // contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 #ifndef IOSS_Ioss_FileInfo_h
34 #define IOSS_Ioss_FileInfo_h
35
36 #include <Ioss_CodeTypes.h>
37 #include <ctime> // for time_t
38 #include <string> // for string, operator!=, etc
39 #include <sys/types.h> // for off_t
40
41 namespace Ioss {
42
43 /*! \class FileInfo
44 * \author Greg Sjaardema
45 * \brief Return information about the specified file.
46 *
47 * A very minimal class (at least it used to be) for providing
48 * information about a file. FileInfo provides information about a
49 * file's name, path, and type (directory, symbolic link, file). Other
50 * information could be added as needed. It currently does not cache
51 * any information, so if it is heavily used, a caching capability
52 * should be added. See the Qt Toolkit QFileInfo class for a richer
53 * file class.
54 */
55
56 class FileInfo
57 {
58 public:
59 //! Empty class referring to no file.
60 FileInfo();
61
62 //! Create object referring to file with name \a filename
63 //! \param my_filename name of file
64 explicit FileInfo(std::string my_filename);
65
66 //! Create object referring to file with name \a filename
67 //! \param my_filename name of file
68 explicit FileInfo(const char *my_filename);
69
70 //! Copy constructor
71 FileInfo(const FileInfo & /*copy_from*/);
72
73 //! Constructor
74 //! \param dirpath Directory Path
75 //! \param my_filename base filename
76 FileInfo(const std::string &dirpath, const std::string &my_filename);
77
78 ~FileInfo();
79
80 //! returns the number of processors that this file exists.
81 //! 0: Exists nowhere
82 //! \#proc: Exists everywhere
83 //! else: exists on some proc, but not all.
84 //! In the last case, a list of processors where it is missing is returned in 'where' on
85 //! processor 0.
86 int parallel_exists(MPI_Comm communicator, std::string &where) const;
87
88 bool exists() const; //!< returns True if file exists, false if nonexistent
89 bool is_readable() const; //!< Exists and is readable
90 bool is_writable() const; //!< Exists and is writable
91 bool is_executable() const; //!< Exists and is executable
92
93 bool is_file() const; //!< Is a plain file
94 bool is_dir() const; //!< Is a directory
95 bool is_symlink() const; //!< Is a symbolic link to a file or directory
96
97 time_t modified() const; //!< Time of last data modification. See 'man stat(2)'
98 time_t accessed() const; //!< Time of last access
99 time_t created() const; //!< Time of last status change. (creation, chmod, ...)
100
101 off_t size() const; //!< File size in bytes. Only if is_file() == true
102
103 const std::string filename() const; //!< Complete filename including path
104 const std::string basename() const; //!< strip path and extension
105 const std::string tailname() const; //!< basename() + extension()
106 const std::string extension() const; //!< file extension.
107 const std::string pathname() const; //!< directory path, no filename
108 const std::string realpath() const; //!< canonicalized absolute path
109
110 void set_filename(const std::string &name);
111 void set_filename(const char *name);
112
113 bool operator==(const FileInfo &other) const { return filename_ == other.filename_; }
114
115 bool operator!=(const FileInfo &other) const { return filename_ != other.filename_; }
116
117 bool remove_file();
118
119 private:
120 std::string filename_{};
121 bool exists_{false}; ///< this is used frequently, check on creation
122 bool readable_{false}; ///< this is used frequently, check on creation
123 };
124 } // namespace Ioss
125 #endif // IOSS_Ioss_FileInfo_h
Ioss::FileInfo::operator!=
bool operator!=(const FileInfo &other) const
Definition: Ioss_FileInfo.h:115
Ioss::FileInfo::created
time_t created() const
Time of last status change. (creation, chmod, ...)
Definition: Ioss_FileInfo.C:219
Ioss::FileInfo::remove_file
bool remove_file()
Definition: Ioss_FileInfo.C:329
Ioss::FileInfo::extension
const std::string extension() const
file extension.
Definition: Ioss_FileInfo.C:266
Ioss::FileInfo::operator==
bool operator==(const FileInfo &other) const
Definition: Ioss_FileInfo.h:113
Ioss::FileInfo::pathname
const std::string pathname() const
directory path, no filename
Definition: Ioss_FileInfo.C:279
Ioss::FileInfo::parallel_exists
int parallel_exists(MPI_Comm communicator, std::string &where) const
Definition: Ioss_FileInfo.C:105
Ioss::FileInfo::is_file
bool is_file() const
Is a plain file.
Definition: Ioss_FileInfo.C:152
Ioss::FileInfo::size
off_t size() const
File size in bytes. Only if is_file() == true.
Definition: Ioss_FileInfo.C:232
Ioss::FileInfo::is_executable
bool is_executable() const
Exists and is executable.
Definition: Ioss_FileInfo.C:148
Ioss::FileInfo::is_writable
bool is_writable() const
Exists and is writable.
Definition: Ioss_FileInfo.C:145
Ioss
The main namespace for the Ioss library.
Definition: Ioad_DatabaseIO.C:66
Ioss::FileInfo::is_dir
bool is_dir() const
Is a directory.
Definition: Ioss_FileInfo.C:166
Ioss::FileInfo::exists_
bool exists_
this is used frequently, check on creation
Definition: Ioss_FileInfo.h:121
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:248
Ioss::FileInfo::is_readable
bool is_readable() const
Exists and is readable.
Definition: Ioss_FileInfo.C:142
Ioss::FileInfo::realpath
const std::string realpath() const
canonicalized absolute path
Definition: Ioss_FileInfo.C:312
Ioss::FileInfo::is_symlink
bool is_symlink() const
Is a symbolic link to a file or directory.
Definition: Ioss_FileInfo.C:179
Ioss::FileInfo::tailname
const std::string tailname() const
basename() + extension()
Definition: Ioss_FileInfo.C:289
Ioss::FileInfo::modified
time_t modified() const
Time of last data modification. See 'man stat(2)'.
Definition: Ioss_FileInfo.C:193
Ioss::FileInfo::filename_
std::string filename_
Definition: Ioss_FileInfo.h:120
Ioss::FileInfo
Return information about the specified file.
Definition: Ioss_FileInfo.h:56
Ioss::FileInfo::~FileInfo
~FileInfo()
Ioss::FileInfo::basename
const std::string basename() const
strip path and extension
Definition: Ioss_FileInfo.C:299
Ioss::FileInfo::accessed
time_t accessed() const
Time of last access.
Definition: Ioss_FileInfo.C:206
Ioss::FileInfo::filename
const std::string filename() const
Complete filename including path.
Definition: Ioss_FileInfo.C:245
Ioss::FileInfo::exists
bool exists() const
returns True if file exists, false if nonexistent
Definition: Ioss_FileInfo.C:103
Ioss::FileInfo::readable_
bool readable_
this is used frequently, check on creation
Definition: Ioss_FileInfo.h:122
anonymous_namespace{io_info.C}::name
std::string name(const Ioss::GroupingEntity *entity)
Definition: io_info.C:89
MPI_Comm
int MPI_Comm
Definition: Ioss_CodeTypes.h:96
Ioss_CodeTypes.h