diff --git a/cxx4/ncFile.cpp b/cxx4/ncFile.cpp index 9a7fe55..cd851d5 100644 --- a/cxx4/ncFile.cpp +++ b/cxx4/ncFile.cpp @@ -13,28 +13,43 @@ using namespace netCDF::exceptions; NcFile::~NcFile() { // destructor may be called due to an exception being thrown - // hence throwing an exception from within a destructor + // hence throwing an exception from within a destructor // causes undefined behaviour! so just printing a warning message try { - if (!nullObject) - ncCheck(nc_close(myId),__FILE__,__LINE__); + close(); } - catch (NcException &e) + catch (NcException &e) { cerr << e.what() << endl; } } +void NcFile::close() +{ + if (!nullObject) + ncCheck(nc_close(myId),__FILE__,__LINE__); + nullObject = true; +} + // Constructor generates a null object. -NcFile::NcFile() : +NcFile::NcFile() : NcGroup() // invoke base class constructor {} // constructor NcFile::NcFile(const string& filePath, const FileMode fMode) { - switch (fMode) + open(filePath, fMode); +} + +// open a file from path and mode +void NcFile::open(const string& filePath, const FileMode fMode) +{ + if (!nullObject) + close(); + + switch (fMode) { case NcFile::write: ncCheck(nc_open(filePath.c_str(), NC_WRITE, &myId),__FILE__,__LINE__); @@ -55,6 +70,14 @@ NcFile::NcFile(const string& filePath, const FileMode fMode) // constructor with file type specified NcFile::NcFile(const string& filePath, const FileMode fMode, const FileFormat fFormat ) { + open(filePath, fMode, fFormat); +} + +void NcFile::open(const string& filePath, const FileMode fMode, const FileFormat fFormat ) +{ + if (!nullObject) + close(); + int format; switch (fFormat) { @@ -71,7 +94,7 @@ NcFile::NcFile(const string& filePath, const FileMode fMode, const FileFormat fF format = NC_NETCDF4 | NC_CLASSIC_MODEL; break; } - switch (fMode) + switch (fMode) { case NcFile::write: ncCheck(nc_open(filePath.c_str(), format | NC_WRITE, &myId),__FILE__,__LINE__); diff --git a/cxx4/ncFile.h b/cxx4/ncFile.h index 08a1a76..b44ea85 100644 --- a/cxx4/ncFile.h +++ b/cxx4/ncFile.h @@ -10,16 +10,16 @@ namespace netCDF { - /*! + /*! Class represents a netCDF root group. - The Ncfile class is the same as the NcGroup class with the additional functionality for opening + The Ncfile class is the same as the NcGroup class with the additional functionality for opening and closing files. */ class NcFile : public NcGroup { public: - - enum FileMode + + enum FileMode { read, //!< File exists, open read-only. write, //!< File exists, open for writing. @@ -34,12 +34,12 @@ namespace netCDF nc4, //!< (default) netCDF-4/HDF5 format, enhanced data model nc4classic //!< netCDF-4/HDF5 format, classic data model }; - - + + /*! Constructor generates a \ref isNull "null object". */ NcFile(); - /*! + /*! Opens a netCDF file. \param filePath Name of netCDF optional path. \param fMode The file mode: @@ -49,8 +49,18 @@ namespace netCDF - 'newFile' Create new file, fail it exists already. */ NcFile(const std::string& filePath, FileMode fMode); + /*! + Opens a netCDF file. + \param filePath Name of netCDF optional path. + \param fMode The file mode: + - 'read' File exists, open for read-only. + - 'write' File exists, open for writing. + - 'replace' Create new file, even it already exists. + - 'newFile' Create new file, fail it exists already. + */ + void open(const std::string& filePath, FileMode fMode); - /*! + /*! Creates a netCDF file of a specified format. \param filePath Name of netCDF optional path. \param fMode The file mode: @@ -58,10 +68,21 @@ namespace netCDF - 'newFile' Create new file, fail it exists already. */ NcFile(const std::string& filePath, FileMode fMode, FileFormat fFormat); - + /*! + Creates a netCDF file of a specified format. + \param filePath Name of netCDF optional path. + \param fMode The file mode: + - 'replace' Create new file, even it already exists. + - 'newFile' Create new file, fail it exists already. + */ + void open(const std::string& filePath, FileMode fMode, FileFormat fFormat); + + //! Close a file before destructor call + void close(); + /*! destructor */ virtual ~NcFile(); //closes file and releases all resources - + //! Synchronize an open netcdf dataset to disk void sync(); @@ -77,9 +98,9 @@ namespace netCDF NcFile(const NcGroup& rhs); NcFile(const NcFile& rhs); }; - + } - + #endif