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 
109  void set_filename(const std::string &name);
110  void set_filename(const char *name);
111 
112  bool operator==(const FileInfo &other) const { return filename_ == other.filename_; }
113 
114  bool operator!=(const FileInfo &other) const { return filename_ != other.filename_; }
115 
116  bool remove_file();
117 
118  private:
119  std::string filename_{};
120  bool exists_{false}; ///< this is used frequently, check on creation
121  bool readable_{false}; ///< this is used frequently, check on creation
122  };
123 } // namespace Ioss
124 #endif // IOSS_Ioss_FileInfo_h
FileInfo()
Empty class referring to no file.
The main namespace for the Ioss library.
Definition: Iocgns_DatabaseIO.h:50
const std::string basename() const
strip path and extension
Definition: Ioss_FileInfo.C:286
bool is_readable() const
Exists and is readable.
Definition: Ioss_FileInfo.C:130
time_t accessed() const
Time of last access.
Definition: Ioss_FileInfo.C:193
void set_filename(const std::string &name)
Definition: Ioss_FileInfo.C:235
bool readable_
this is used frequently, check on creation
Definition: Ioss_FileInfo.h:121
time_t modified() const
Time of last data modification. See &#39;man stat(2)&#39;.
Definition: Ioss_FileInfo.C:180
Return information about the specified file.
Definition: Ioss_FileInfo.h:56
bool exists() const
returns True if file exists, false if nonexistent
Definition: Ioss_FileInfo.C:89
const std::string filename() const
Complete filename including path.
Definition: Ioss_FileInfo.C:232
std::string filename_
Definition: Ioss_FileInfo.h:119
bool operator==(const FileInfo &other) const
Definition: Ioss_FileInfo.h:112
const std::string pathname() const
directory path, no filename
Definition: Ioss_FileInfo.C:266
int parallel_exists(MPI_Comm communicator, std::string &where) const
Definition: Ioss_FileInfo.C:91
bool remove_file()
Definition: Ioss_FileInfo.C:299
bool operator!=(const FileInfo &other) const
Definition: Ioss_FileInfo.h:114
bool is_writable() const
Exists and is writable.
Definition: Ioss_FileInfo.C:133
const std::string extension() const
file extension.
Definition: Ioss_FileInfo.C:253
time_t created() const
Time of last status change. (creation, chmod, ...)
Definition: Ioss_FileInfo.C:206
bool is_file() const
Is a plain file.
Definition: Ioss_FileInfo.C:140
off_t size() const
File size in bytes. Only if is_file() == true.
Definition: Ioss_FileInfo.C:219
std::string name(Ioss::GroupingEntity *entity)
Definition: io_info.C:71
bool is_executable() const
Exists and is executable.
Definition: Ioss_FileInfo.C:136
bool exists_
this is used frequently, check on creation
Definition: Ioss_FileInfo.h:120
int MPI_Comm
Definition: Ioss_CodeTypes.h:79
bool is_symlink() const
Is a symbolic link to a file or directory.
Definition: Ioss_FileInfo.C:167
bool is_dir() const
Is a directory.
Definition: Ioss_FileInfo.C:154
const std::string tailname() const
basename() + extension()
Definition: Ioss_FileInfo.C:276