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 21182 : FileBase& FileBase::link(FILE*fp) { 43 21182 : plumed_massert(!this->fp,"cannot link an already open file"); 44 21182 : this->fp=fp; 45 21182 : cloned=true; 46 21182 : return *this; 47 : } 48 : 49 5692 : FileBase& FileBase::flush() { 50 5692 : if(fp) fflush(fp); 51 5692 : return *this; 52 : } 53 : 54 812265 : FileBase& FileBase::link(Communicator&comm) { 55 812265 : plumed_massert(!fp,"cannot link an already open file"); 56 812265 : this->comm=&comm; 57 812265 : return *this; 58 : } 59 : 60 5352 : FileBase& FileBase::link(PlumedMain&plumed) { 61 5352 : plumed_massert(!fp,"cannot link an already open file"); 62 5352 : this->plumed=&plumed; 63 5352 : link(plumed.comm); 64 5352 : return *this; 65 : } 66 : 67 4321 : FileBase& FileBase::link(Action&action) { 68 4321 : plumed_massert(!fp,"cannot link an already open file"); 69 4321 : this->action=&action; 70 4321 : link(action.plumed); 71 4321 : return *this; 72 : } 73 : 74 2029 : bool FileBase::FileExist(const std::string& path) { 75 : bool do_exist=false; 76 4058 : this->path=appendSuffix(path,getSuffix()); 77 2029 : mode="r"; 78 : // first try with suffix 79 2029 : FILE *ff=std::fopen(const_cast<char*>(this->path.c_str()),"r"); 80 : // call fclose when ff goes out of scope 81 1523 : auto deleter=[](auto f) { if(f) std::fclose(f); }; 82 : std::unique_ptr<FILE,decltype(deleter)> fp_deleter(ff,deleter); 83 : 84 2029 : if(!ff) { 85 : this->path=path; 86 : // then try without suffic 87 506 : ff=std::fopen(const_cast<char*>(this->path.c_str()),"r"); 88 : mode="r"; 89 : } 90 2029 : if(ff) do_exist=true; 91 2029 : if(comm) comm->Barrier(); 92 2029 : return do_exist; 93 : } 94 : 95 814317 : bool FileBase::isOpen() { 96 : bool isopen=false; 97 814317 : if(fp) isopen=true; 98 814317 : return isopen; 99 : } 100 : 101 1017 : void FileBase::close() { 102 1017 : plumed_assert(!cloned); 103 1017 : eof=false; 104 1017 : err=false; 105 1017 : if(fp) std::fclose(fp); 106 : #ifdef __PLUMED_HAS_ZLIB 107 1017 : if(gzfp) gzclose(gzFile(gzfp)); 108 : #endif 109 1017 : fp=NULL; 110 1017 : gzfp=NULL; 111 1017 : } 112 : 113 833719 : FileBase::FileBase(): 114 833719 : fp(NULL), 115 833719 : gzfp(NULL), 116 833719 : comm(NULL), 117 833719 : plumed(NULL), 118 833719 : action(NULL), 119 833719 : cloned(false), 120 833719 : eof(false), 121 833719 : err(false), 122 833719 : heavyFlush(false), 123 833719 : enforcedSuffix_(false) 124 : { 125 833719 : } 126 : 127 833719 : FileBase::~FileBase() 128 : { 129 833719 : if(plumed) plumed->eraseFile(*this); 130 833719 : if(!cloned && fp) std::fclose(fp); 131 : #ifdef __PLUMED_HAS_ZLIB 132 833719 : if(!cloned && gzfp) gzclose(gzFile(gzfp)); 133 : #endif 134 833719 : } 135 : 136 31254961 : FileBase::operator bool()const { 137 31254961 : return !eof; 138 : } 139 : 140 7362 : std::string FileBase::appendSuffix(const std::string&path,const std::string&suffix) { 141 7362 : if(path=="/dev/null") return path; // do not append a suffix to /dev/null 142 7187 : std::string ret=path; 143 7187 : std::string ext=Tools::extension(path); 144 : 145 : // These are the recognized extensions so far: 146 : // gz xtc trr 147 : // If a file name ends with one of these extensions, the suffix is added *before* 148 : // the extension. This is useful when extensions are conventionally used 149 : // to detect file type, so as to allow easier file manipulation. 150 : // Removing this line, any extension recognized by Tools::extension() would be considered 151 : // if(ext!="gz" && ext!="xtc" && ext!="trr") ext=""; 152 : 153 7187 : if(ext.length()>0) { 154 5067 : int l=path.length()-(ext.length()+1); 155 5067 : plumed_assert(l>=0); 156 5067 : ret.resize(l); 157 : } 158 : ret+=suffix; 159 12254 : if(ext.length()>0)ret+="."+ext; 160 : return ret; 161 : } 162 : 163 258 : FileBase& FileBase::enforceSuffix(const std::string&suffix) { 164 258 : enforcedSuffix_=true; 165 258 : enforcedSuffix=suffix; 166 258 : return *this; 167 : } 168 : 169 6167 : std::string FileBase::getSuffix()const { 170 6167 : if(enforcedSuffix_) return enforcedSuffix; 171 5903 : if(plumed) return plumed->getSuffix(); 172 683 : return ""; 173 : } 174 : 175 : }