IOSS 2.0
Loading...
Searching...
No Matches
Ioss_FileInfo.h
Go to the documentation of this file.
1// Copyright(C) 1999-2020, 2022, 2024 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#pragma once
8
9#include "ioss_export.h"
10
11#include "Ioss_CodeTypes.h"
12#include <ctime> // for time_t
13#include <string> // for string, operator!=, etc
14#include <sys/types.h> // for off_t
15
16namespace Ioss {
17
18 /*! \class FileInfo
19 * \author Greg Sjaardema
20 * \brief Return information about the specified file.
21 *
22 * A very minimal class (at least it used to be) for providing
23 * information about a file. FileInfo provides information about a
24 * file's name, path, and type (directory, symbolic link, file). Other
25 * information could be added as needed. It currently does not cache
26 * any information, so if it is heavily used, a caching capability
27 * should be added. See the Qt Toolkit QFileInfo class for a richer
28 * file class.
29 */
30
31 class IOSS_EXPORT FileInfo
32 {
33 public:
34 //! Empty class referring to no file.
36
37 //! Create object referring to file with name \a filename
38 //! \param my_filename name of file
39 explicit FileInfo(std::string my_filename);
40
41 //! Create object referring to file with name \a filename
42 //! \param my_filename name of file
43 explicit FileInfo(const char *my_filename);
44
45 //! Copy constructor
46 FileInfo(const FileInfo & /*copy_from*/);
47
48 //! Constructor
49 //! \param dirpath Directory Path
50 //! \param my_filename base filename
51 FileInfo(const std::string &dirpath, const std::string &my_filename);
52
53 //! returns the number of processors that this file exists.
54 //! 0: Exists nowhere
55 //! \#proc: Exists everywhere
56 //! else: exists on some proc, but not all.
57 //! In the last case, a list of processors where it is missing is returned in 'where' on
58 //! processor 0.
59 IOSS_NODISCARD int parallel_exists(Ioss_MPI_Comm communicator, std::string &where) const;
60
61 IOSS_NODISCARD bool exists() const; //!< returns True if file exists, false if nonexistent
62 IOSS_NODISCARD bool is_readable() const; //!< Exists and is readable
63 IOSS_NODISCARD bool is_writable() const; //!< Exists and is writable
64 IOSS_NODISCARD bool is_executable() const; //!< Exists and is executable
65
66 IOSS_NODISCARD bool is_file() const; //!< Is a plain file
67 IOSS_NODISCARD bool is_dir() const; //!< Is a directory
68 IOSS_NODISCARD bool is_symlink() const; //!< Is a symbolic link to a file or directory
69 IOSS_NODISCARD bool is_nfs() const; //!< Is on an NFS filesystem
70
71 IOSS_NODISCARD time_t modified() const; //!< Time of last data modification. See 'man stat(2)'
72 IOSS_NODISCARD time_t accessed() const; //!< Time of last access
73 IOSS_NODISCARD time_t created() const; //!< Time of last status change. (creation, chmod, ...)
74
75 IOSS_NODISCARD off_t size() const; //!< File size in bytes. Only if is_file() == true
76
77 IOSS_NODISCARD std::string filename() const; //!< Complete filename including path
78 IOSS_NODISCARD std::string basename() const; //!< strip path and extension
79 IOSS_NODISCARD std::string tailname() const; //!< basename() + extension()
80 IOSS_NODISCARD std::string extension() const; //!< file extension.
81 IOSS_NODISCARD std::string pathname() const; //!< directory path, no filename
82 IOSS_NODISCARD 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 IOSS_NODISCARD bool operator==(const FileInfo &other) const
88 {
89 return filename_ == other.filename_;
90 }
91
92 IOSS_NODISCARD bool operator!=(const FileInfo &other) const
93 {
94 return filename_ != other.filename_;
95 }
96
97 bool remove_file();
98
99 //! This function is used to create the path to an output directory (or history, restart, etc.)
100 //! if it does not exist. Called by all processors. Will throw exception if path does not
101 //! specify a valid directory or if the path cannot be created.
102 static void create_path(const std::string &filename,
103 IOSS_MAYBE_UNUSED Ioss_MPI_Comm communicator);
104 static void create_path(const std::string &filename);
105
106 private:
107 std::string filename_{};
108 bool exists_{false}; ///< this is used frequently, check on creation
109 bool readable_{false}; ///< this is used frequently, check on creation
110 };
111} // namespace Ioss
#define IOSS_MAYBE_UNUSED
Definition Ioss_CodeTypes.h:53
#define IOSS_NODISCARD
Definition Ioss_CodeTypes.h:54
int Ioss_MPI_Comm
Definition Ioss_CodeTypes.h:63
Return information about the specified file.
Definition Ioss_FileInfo.h:32
IOSS_NODISCARD bool operator==(const FileInfo &other) const
Definition Ioss_FileInfo.h:87
IOSS_NODISCARD bool operator!=(const FileInfo &other) const
Definition Ioss_FileInfo.h:92
FileInfo()
Empty class referring to no file.
std::string filename_
Definition Ioss_FileInfo.h:107
FileInfo(const FileInfo &)
Copy constructor.
The main namespace for the Ioss library.
Definition Ioad_DatabaseIO.C:40