// BLAsciiFile.hh /* This source file is part of G4beamline, http://g4beamline.muonsinc.com Copyright (C) 2003,2004,2005,2006 by Tom Roberts, all rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. http://www.gnu.org/copyleft/gpl.html */ #ifndef BLASCIIFILE #define BLASCIIFILE #include #include #include #include "globals.hh" /** class BLAsciiFile manages file-descriptors for writing ASCII files. * * Multiple fopen()-s of a given filename will all get the same FILE *. * Multiple fclose()-s of the FILE* are handled properly (fhe first * closes the file, successive ones do nothing). * * The filename-s must match exactly as given, no conversion to absolute * paths (or any other canonicalization) is performed.. * * Only does writing. **/ class BLAsciiFile { static std::map fds; public: /// fopen() will open a file for writing, returning the same FILE * /// for multiple opens. static FILE *fopen(G4String name) { if(fds.count(name) == 0) { FILE *f = ::fopen(name.c_str(),"w"); if(f != 0) fds[name] = f; return f; } return fds[name]; } /// fclose() will close the FILE*, handling multiple calls. static void fclose(FILE *f) { std::map::iterator i; for(i=fds.begin(); i!=fds.end(); ++i) { if(i->second != f) continue; ::fclose(f); fds.erase(i); break; } } }; #endif // BLASCIIFILE