1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 Copyright (c) 2012-2021 The plumed team
3 (see the PEOPLE file at the root of the distribution for a list of names)
4
5 See http://www.plumed.org for more information.
6
7 This file is part of plumed, version 2.
8
9 plumed is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 plumed is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 #ifndef __PLUMED_tools_FileBase_h
23 #define __PLUMED_tools_FileBase_h
24
25 #include <string>
26
27 namespace PLMD {
28
29 class Communicator;
30 class PlumedMain;
31 class Action;
32
33 /**
34 Base class for dealing with files.
35
36 This class just provides things which are common among OFile and IFile
37 */
38
39 class FileBase {
40 /// Copy constructor is disabled
41 FileBase(const FileBase&) = delete;
42 /// Assignment operator is disabled
43 FileBase& operator=(const FileBase&) = delete;
44 protected:
45 /// Internal tool.
46 /// Base for IFile::Field and OFile::Field
47 class FieldBase {
48 // everything is public to simplify usage
49 public:
50 std::string name;
51 std::string value;
52 bool constant;
FieldBase()53 FieldBase(): constant(false) {}
54 };
55
56 /// file pointer
57 FILE* fp;
58 /// zip file pointer.
59 void* gzfp;
60 /// communicator. NULL if not set
61 Communicator* comm;
62 /// pointer to main plumed object. NULL if not linked
63 PlumedMain* plumed;
64 /// pointer to corresponding action. NULL if not linked
65 Action* action;
66 /// Control closing on destructor.
67 /// If true, file will not be closed in destructor
68 bool cloned;
69 /// Private constructor.
70 /// In this manner one cannot instantiate a FileBase object
71 FileBase();
72 /// Set to true when end of file is encountered
73 bool eof;
74 /// Set to true when error is encountered
75 bool err;
76 /// path of the opened file
77 std::string path;
78 /// mode of the opened file
79 std::string mode;
80 /// Set to true if you want flush to be heavy (close/reopen)
81 bool heavyFlush;
82 public:
83 /// Append suffix.
84 /// It appends the desired suffix to the string. Notice that
85 /// it conserves some suffix (e.g. gz/xtc/trr).
86 static std::string appendSuffix(const std::string&path,const std::string&suffix);
87 private:
88 /// Enforced suffix:
89 std::string enforcedSuffix;
90 /// If true, use enforcedSuffix, else get it from PlumedMain
91 bool enforcedSuffix_;
92 public:
93 /// Link to an already open filed
94 FileBase& link(FILE*);
95 /// Link to a PlumedMain object
96 /// Automatically links also the corresponding Communicator.
97 FileBase& link(PlumedMain&);
98 /// Link to a Communicator object
99 FileBase& link(Communicator&);
100 /// Link to an Action object.
101 /// Automatically links also the corresponding PlumedMain and Communicator.
102 FileBase& link(Action&);
103 /// Enforce suffix.
104 /// Overrides the one set in PlumedMain&
105 FileBase& enforceSuffix(const std::string&suffix);
106 /// Flushes the file to disk
107 virtual FileBase& flush();
108 /// Closes the file
109 /// Should be used only for explicitely opened files.
110 void close();
111 /// Virtual destructor (allows inheritance)
112 virtual ~FileBase();
113 /// Check for error/eof.
114 operator bool () const;
115 /// Set heavyFlush flag
setHeavyFlush()116 void setHeavyFlush() { heavyFlush=true;}
117 /// Opens the file
118 virtual FileBase& open(const std::string&name)=0;
119 /// Check if the file exists
120 bool FileExist(const std::string& path);
121 /// Check if a file is open
122 bool isOpen();
123 /// Retrieve the path
124 std::string getPath()const;
125 /// Retrieve the mode
126 std::string getMode()const;
127 /// Get the file suffix
128 std::string getSuffix()const;
129 /// Get the underlying file pointer.
130 /// It might be null even if the file is open, e.g. when the file
131 /// was open as a gzipped file.
132 FILE* getFILE()const;
133 };
134
135 inline
getPath()136 std::string FileBase::getPath()const {
137 return path;
138 }
139
140 inline
getMode()141 std::string FileBase::getMode()const {
142 return mode;
143 }
144
145 inline
getFILE()146 FILE* FileBase::getFILE()const {
147 return fp;
148 }
149
150 }
151
152 #endif
153