Skip to content

Commit

Permalink
Remove global g_ncid
Browse files Browse the repository at this point in the history
Closes Unidata#30
Fixes Unidata#32

Global file ID is no longer needed now that `nc_inq_type` can take a
group ID (valid since netCDF-C 4.4.1)
  • Loading branch information
ZedThree committed Jan 4, 2024
1 parent 1b1e0e6 commit 2ac2837
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
7 changes: 0 additions & 7 deletions cxx4/ncFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ using namespace std;
using namespace netCDF;
using namespace netCDF::exceptions;

int g_ncid = -1;

// destructor
NcFile::~NcFile()
{
Expand All @@ -31,7 +29,6 @@ void NcFile::close()
{
if (!nullObject) {
ncCheck(nc_close(myId),__FILE__,__LINE__);
g_ncid = -1;
}

nullObject = true;
Expand Down Expand Up @@ -65,7 +62,6 @@ void NcFile::open(const string& filePath, int ncFileFlags) {
close();

ncCheck(nc_open(filePath.c_str(), ncFileFlags, &myId),__FILE__,__LINE__);
g_ncid = myId;

nullObject=false;
}
Expand Down Expand Up @@ -95,7 +91,6 @@ void NcFile::open(const string& filePath, const FileMode fMode)
break;
}

g_ncid = myId;

nullObject=false;
}
Expand All @@ -117,7 +112,6 @@ void NcFile::create(const string& filePath, const int ncFileFlags) {

ncCheck(nc_create(filePath.c_str(),ncFileFlags,&myId),__FILE__,__LINE__);

g_ncid = myId;

nullObject=false;
}
Expand Down Expand Up @@ -159,7 +153,6 @@ void NcFile::open(const string& filePath, const FileMode fMode, const FileFormat
break;
}

g_ncid = myId;
nullObject=false;
}

Expand Down
9 changes: 2 additions & 7 deletions cxx4/ncType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "ncCheck.h"
using namespace std;

extern int g_ncid;

namespace netCDF {
// Global comparator operator ==============
Expand Down Expand Up @@ -95,11 +94,7 @@ string NcType::getName() const{
char charName[NC_MAX_NAME+1];
size_t *sizep=NULL;

/* We cannot call nc_inq_type without a valid
netcdf file ID (ncid), which is not *groupid*.
Working around this for now. */

ncCheck(nc_inq_type(g_ncid,myId,charName,sizep),__FILE__,__LINE__);
ncCheck(nc_inq_type(groupId,myId,charName,sizep),__FILE__,__LINE__);
return string(charName);

};
Expand All @@ -108,7 +103,7 @@ string NcType::getName() const{
size_t NcType::getSize() const{
char* charName=NULL;
size_t sizep;
ncCheck(nc_inq_type(g_ncid,myId,charName,&sizep),__FILE__,__LINE__);
ncCheck(nc_inq_type(groupId,myId,charName,&sizep),__FILE__,__LINE__);
return sizep;
};

Expand Down
17 changes: 17 additions & 0 deletions cxx4/test_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <stdio.h>
#include <stddef.h>
#include <type_traits>
#include "test_utilities.h"
using namespace std;
using namespace netCDF;
Expand Down Expand Up @@ -553,6 +554,22 @@ try
cout <<" ----------- passed\n";


{
// Test for issue 30/32
cout<<left<<std::setw(57)<<"Testing NcType::getName() with two open files";
NcFile nc4_file("nc4_format.cdf", NcFile::replace, NcFile::nc4);
NcFile classic_file("classic_format.cdf", NcFile::replace, NcFile::classic);

nc4_file.addVar("int64", ncInt64);

struct struct11 {
int mem1;
};
const auto compound_type11 = nc4_file.addCompoundType("struct11", sizeof(struct11));
const auto name = compound_type11.getName();
if (name != "struct11") throw NcException("Wrong name for compound type", __FILE__, __LINE__);
}
cout <<" ----------- passed\n";

}
catch (NcException& e)
Expand Down

0 comments on commit 2ac2837

Please sign in to comment.