mirror of
https://github.com/Unidata/netcdf-c.git
synced 2025-01-18 15:55:12 +08:00
4b936ee26a
re: github issue https://github.com/Unidata/netcdf-fortran/issues/82 This was originally discovered in the Fortran tests, but is a problem in the C library. The problem only occurred when using HDF5-1.10.x. The reason it failed is that starting with 1.10, the hid_t type was changed from 32 bits to 64 bits. The function libsrc4/nc4memcb.c#NC4_image_init was using type int (doh!) to return the hdf fileid instead of hid_t type. This, of course, caused the id to be truncated and in turn later use of the id caused hdf5 to fail. Fix is trivial: replace int with hid_t. This also requires a related change in nc4mem.c. Also added the test case derived from the original Fortran code. You would think I would learn...
91 lines
1.8 KiB
C
91 lines
1.8 KiB
C
#include <config.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#ifdef HAVE_UNISTD_H
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
#include "netcdf.h"
|
|
#include "netcdf_mem.h"
|
|
#include "ncbytes.h"
|
|
#include "nc_tests.h"
|
|
#include "err_macros.h"
|
|
|
|
#ifdef USE_NETCDF4
|
|
#include <hdf5.h>
|
|
extern int H5Eprint1(FILE * stream);
|
|
#endif
|
|
|
|
static int
|
|
readfile(const char* path, NC_memio* memio)
|
|
{
|
|
int status = NC_NOERR;
|
|
FILE* f = NULL;
|
|
size_t filesize = 0;
|
|
size_t count = 0;
|
|
char* memory = NULL;
|
|
char* p = NULL;
|
|
|
|
/* Open the file for reading */
|
|
#ifdef _MSC_VER
|
|
f = fopen(path,"rb");
|
|
#else
|
|
f = fopen(path,"r");
|
|
#endif
|
|
if(f == NULL)
|
|
{status = errno; goto done;}
|
|
/* get current filesize */
|
|
if(fseek(f,0,SEEK_END) < 0)
|
|
{status = errno; goto done;}
|
|
filesize = (size_t)ftell(f);
|
|
/* allocate memory */
|
|
memory = malloc((size_t)filesize);
|
|
if(memory == NULL)
|
|
{status = NC_ENOMEM; goto done;}
|
|
/* move pointer back to beginning of file */
|
|
rewind(f);
|
|
count = filesize;
|
|
p = memory;
|
|
while(count > 0) {
|
|
size_t actual;
|
|
actual = fread(p,1,count,f);
|
|
if(actual == 0 || ferror(f))
|
|
{status = NC_EIO; goto done;}
|
|
count -= actual;
|
|
p += actual;
|
|
}
|
|
if(memio) {
|
|
memio->size = (size_t)filesize;
|
|
memio->memory = memory;
|
|
}
|
|
done:
|
|
if(status != NC_NOERR && memory != NULL)
|
|
free(memory);
|
|
if(f != NULL) fclose(f);
|
|
return status;
|
|
}
|
|
|
|
int
|
|
main(int argc, char** argv)
|
|
{
|
|
int retval = NC_NOERR;
|
|
int ncid;
|
|
NC_memio mem;
|
|
char* path = "f03tst_open_mem.nc";
|
|
|
|
if(argc > 1)
|
|
path = argv[1];
|
|
|
|
if((retval=readfile(path,&mem)))
|
|
goto exit;
|
|
|
|
if((retval = nc_open_mem("mem", NC_INMEMORY|NC_NETCDF4, mem.size, mem.memory, &ncid)))
|
|
goto exit;
|
|
if((retval = nc_close(ncid)))
|
|
goto exit;
|
|
return 0;
|
|
exit:
|
|
fprintf(stderr,"retval=%d\n",retval);
|
|
return 1;
|
|
}
|