LCOV - code coverage report
Current view: top level - tools - FileBase.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 92 92 100.0 %
Date: 2025-11-25 13:55:50 Functions: 14 15 93.3 %

          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             : #include "FileBase.h"
      23             : #include "Exception.h"
      24             : #include "core/Action.h"
      25             : #include "core/PlumedMain.h"
      26             : #include "core/Value.h"
      27             : #include "Communicator.h"
      28             : #include "Tools.h"
      29             : #include <cstdarg>
      30             : #include <cstring>
      31             : #include <cstdlib>
      32             : 
      33             : #include <iostream>
      34             : #include <string>
      35             : 
      36             : #ifdef __PLUMED_HAS_ZLIB
      37             : #include <zlib.h>
      38             : #endif
      39             : 
      40             : namespace PLMD {
      41             : 
      42       21209 : FileBase& FileBase::link(FILE*fp) {
      43       21209 :   plumed_massert(!this->fp,"cannot link an already open file");
      44       21209 :   this->fp=fp;
      45       21209 :   cloned=true;
      46       21209 :   return *this;
      47             : }
      48             : 
      49        5846 : FileBase& FileBase::flush() {
      50        5846 :   if(fp) {
      51        5778 :     fflush(fp);
      52             :   }
      53        5846 :   return *this;
      54             : }
      55             : 
      56      812562 : FileBase& FileBase::link(Communicator&comm) {
      57      812562 :   plumed_massert(!fp,"cannot link an already open file");
      58      812562 :   this->comm=&comm;
      59      812562 :   return *this;
      60             : }
      61             : 
      62        5497 : FileBase& FileBase::link(PlumedMain&plumed) {
      63        5497 :   plumed_massert(!fp,"cannot link an already open file");
      64        5497 :   this->plumed=&plumed;
      65        5497 :   link(plumed.comm);
      66        5497 :   return *this;
      67             : }
      68             : 
      69        4441 : FileBase& FileBase::link(Action&action) {
      70        4441 :   plumed_massert(!fp,"cannot link an already open file");
      71        4441 :   this->action=&action;
      72        4441 :   link(action.plumed);
      73        4441 :   return *this;
      74             : }
      75             : 
      76        2094 : bool FileBase::FileExist(const std::string& path) {
      77             :   bool do_exist=false;
      78        4188 :   this->path=appendSuffix(path,getSuffix());
      79        2094 :   mode="r";
      80             :   // first try with suffix
      81        2094 :   FILE *ff=std::fopen(const_cast<char*>(this->path.c_str()),"r");
      82             : // call fclose when ff goes out of scope
      83             :   auto deleter=[](auto f) {
      84             :     if(f) {
      85        1587 :       std::fclose(f);
      86             :     }
      87        1587 :   };
      88             :   std::unique_ptr<FILE,decltype(deleter)> fp_deleter(ff,deleter);
      89             : 
      90        2094 :   if(!ff) {
      91             :     this->path=path;
      92             :     // then try without suffic
      93         507 :     ff=std::fopen(const_cast<char*>(this->path.c_str()),"r");
      94             :     mode="r";
      95             :   }
      96        2094 :   if(ff) {
      97             :     do_exist=true;
      98             :   }
      99        2094 :   if(comm) {
     100        1771 :     comm->Barrier();
     101             :   }
     102        2094 :   return do_exist;
     103             : }
     104             : 
     105      814533 : bool FileBase::isOpen() {
     106             :   bool isopen=false;
     107      814533 :   if(fp) {
     108             :     isopen=true;
     109             :   }
     110      814533 :   return isopen;
     111             : }
     112             : 
     113        1056 : void        FileBase::close() {
     114        1056 :   plumed_assert(!cloned);
     115        1056 :   eof=false;
     116        1056 :   err=false;
     117        1056 :   if(fp) {
     118        1056 :     std::fclose(fp);
     119             :   }
     120             : #ifdef __PLUMED_HAS_ZLIB
     121        1056 :   if(gzfp) {
     122           3 :     gzclose(gzFile(gzfp));
     123             :   }
     124             : #endif
     125        1056 :   fp=NULL;
     126        1056 :   gzfp=NULL;
     127        1056 : }
     128             : 
     129      834067 : FileBase::FileBase():
     130      834067 :   fp(NULL),
     131      834067 :   gzfp(NULL),
     132      834067 :   comm(NULL),
     133      834067 :   plumed(NULL),
     134      834067 :   action(NULL),
     135      834067 :   cloned(false),
     136      834067 :   eof(false),
     137      834067 :   err(false),
     138      834067 :   heavyFlush(false),
     139      834067 :   enforcedSuffix_(false) {
     140      834067 : }
     141             : 
     142      834067 : FileBase::~FileBase() {
     143      834067 :   if(plumed) {
     144        5497 :     plumed->eraseFile(*this);
     145             :   }
     146      834067 :   if(!cloned && fp) {
     147        4751 :     std::fclose(fp);
     148             :   }
     149             : #ifdef __PLUMED_HAS_ZLIB
     150      834067 :   if(!cloned && gzfp) {
     151          28 :     gzclose(gzFile(gzfp));
     152             :   }
     153             : #endif
     154      834067 : }
     155             : 
     156    31263203 : FileBase::operator bool()const {
     157    31263203 :   return !eof;
     158             : }
     159             : 
     160        7525 : std::string FileBase::appendSuffix(const std::string&path,const std::string&suffix) {
     161        7525 :   if(path=="/dev/null") {
     162         175 :     return path;  // do not append a suffix to /dev/null
     163             :   }
     164        7350 :   std::string ret=path;
     165        7350 :   std::string ext=Tools::extension(path);
     166             : 
     167             : // These are the recognized extensions so far:
     168             : // gz xtc trr
     169             : // If a file name ends with one of these extensions, the suffix is added *before*
     170             : // the extension. This is useful when extensions are conventionally used
     171             : // to detect file type, so as to allow easier file manipulation.
     172             : // Removing this line, any extension recognized by Tools::extension() would be considered
     173             : //  if(ext!="gz" && ext!="xtc" && ext!="trr") ext="";
     174             : 
     175        7350 :   if(ext.length()>0) {
     176        5127 :     int l=path.length()-(ext.length()+1);
     177        5127 :     plumed_assert(l>=0);
     178        5127 :     ret.resize(l);
     179             :   }
     180             :   ret+=suffix;
     181        7350 :   if(ext.length()>0) {
     182       10254 :     ret+="."+ext;
     183             :   }
     184             :   return ret;
     185             : }
     186             : 
     187         258 : FileBase& FileBase::enforceSuffix(const std::string&suffix) {
     188         258 :   enforcedSuffix_=true;
     189         258 :   enforcedSuffix=suffix;
     190         258 :   return *this;
     191             : }
     192             : 
     193        6326 : std::string FileBase::getSuffix()const {
     194        6326 :   if(enforcedSuffix_) {
     195         264 :     return enforcedSuffix;
     196             :   }
     197        6062 :   if(plumed) {
     198        5354 :     return plumed->getSuffix();
     199             :   }
     200         708 :   return "";
     201             : }
     202             : 
     203             : }

Generated by: LCOV version 1.16