LCOV - code coverage report
Current view: top level - tools - FileBase.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 5 5 100.0 %
Date: 2026-11-05 10:19:04 Functions: 0 0 -

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2012-2023 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    39970372 :   struct FieldBase {
      48             : // everything is public to simplify usage
      49             :     std::string name;
      50             :     std::string value;
      51             :     bool constant=false;
      52             :   };
      53             : 
      54             : /// file pointer
      55             :   FILE* fp;
      56             : /// zip file pointer.
      57             :   void* gzfp;
      58             : /// communicator. NULL if not set
      59             :   Communicator* comm;
      60             : /// pointer to main plumed object. NULL if not linked
      61             :   PlumedMain* plumed;
      62             : /// pointer to corresponding action. NULL if not linked
      63             :   Action* action;
      64             : /// Control closing on destructor.
      65             : /// If true, file will not be closed in destructor
      66             :   bool cloned;
      67             : /// Private constructor.
      68             : /// In this manner one cannot instantiate a FileBase object
      69             :   FileBase();
      70             : /// Set to true when end of file is encountered
      71             :   bool eof;
      72             : /// Set to true when error is encountered
      73             :   bool err;
      74             : /// path of the opened file
      75             :   std::string path;
      76             : /// mode of the opened file
      77             :   std::string mode;
      78             : /// Set to true if you want flush to be heavy (close/reopen)
      79             :   bool heavyFlush;
      80             : public:
      81             : /// Append suffix.
      82             : /// It appends the desired suffix to the string. Notice that
      83             : /// it conserves some suffix (e.g. gz/xtc/trr).
      84             :   static std::string appendSuffix(const std::string&path,
      85             :                                   const std::string&suffix);
      86             : private:
      87             : /// Enforced suffix:
      88             :   std::string enforcedSuffix;
      89             : /// If true, use enforcedSuffix, else get it from PlumedMain
      90             :   bool enforcedSuffix_;
      91             : public:
      92             : /// Link to an already open filed
      93             :   FileBase& link(FILE*);
      94             : /// Link to a PlumedMain object
      95             : /// Automatically links also the corresponding Communicator.
      96             :   FileBase& link(PlumedMain&);
      97             : /// Link to a Communicator object
      98             :   FileBase& link(Communicator&);
      99             : /// Link to an Action object.
     100             : /// Automatically links also the corresponding PlumedMain and Communicator.
     101             :   FileBase& link(Action&);
     102             : /// Enforce suffix.
     103             : /// Overrides the one set in PlumedMain&
     104             :   FileBase& enforceSuffix(const std::string&suffix);
     105             : /// Flushes the file to disk
     106             :   virtual FileBase& flush();
     107             : /// Closes the file
     108             : /// Should be used only for explicitly opened files.
     109             :   void        close();
     110             : /// Virtual destructor (allows inheritance)
     111             :   virtual ~FileBase();
     112             : /// Check for error/eof.
     113             :   operator bool () const;
     114             : /// Set heavyFlush flag
     115             :   void setHeavyFlush() {
     116        1017 :     heavyFlush=true;
     117             :   }
     118             : /// Opens the file
     119             :   virtual FileBase& open(const std::string&name)=0;
     120             : /// Check if the file exists
     121             :   bool FileExist(const std::string& path);
     122             : /// Check if a file is open
     123             :   bool isOpen();
     124             : /// Retrieve the path
     125             :   std::string getPath()const;
     126             : /// Retrieve the mode
     127             :   std::string getMode()const;
     128             : /// Get the file suffix
     129             :   std::string getSuffix()const;
     130             : /// Get the underlying file pointer.
     131             : /// It might be null even if the file is open, e.g. when the file
     132             : /// was open as a gzipped file.
     133             :   FILE* getFILE()const;
     134             : };
     135             : 
     136             : inline
     137             : std::string FileBase::getPath()const {
     138        7139 :   return path;
     139             : }
     140             : 
     141             : inline
     142             : std::string FileBase::getMode()const {
     143         227 :   return mode;
     144             : }
     145             : 
     146             : inline
     147             : FILE* FileBase::getFILE()const {
     148          18 :   return fp;
     149             : }
     150             : 
     151             : }
     152             : 
     153             : #endif

Generated by: LCOV version 1.16