adding support for magic numbers

This commit is contained in:
Ed Hartnett 2018-06-05 12:22:38 -06:00
parent 3a8987967e
commit 17da700a5c
3 changed files with 17 additions and 4 deletions

View File

@ -158,8 +158,10 @@ Use this in mode flags for both nc_create() and nc_open(). */
#define NC_PNETCDF (NC_MPIIO) /**< Use parallel-netcdf library; alias for NC_MPIIO. */ #define NC_PNETCDF (NC_MPIIO) /**< Use parallel-netcdf library; alias for NC_MPIIO. */
#define NC_UDF0 0x0080 /**< User-defined format. */ #define NC_UDF0 0x0080 /**< User-defined format 0. */
#define NC_UDF1 0x0002 /**< User-defined format. */ #define NC_UDF1 0x0002 /**< User-defined format 1. */
#define NC_MAX_MAGIC_NUMBER_LEN 8 /**< Max len of ser-defined format magic number. */
/** Format specifier for nc_set_default_format() and returned /** Format specifier for nc_set_default_format() and returned
* by nc_inq_format. This returns the format as provided by * by nc_inq_format. This returns the format as provided by

View File

@ -67,7 +67,9 @@ static char HDF5_SIGNATURE[MAGIC_NUMBER_LEN] = "\211HDF\r\n\032\n";
#ifdef USE_NETCDF4 #ifdef USE_NETCDF4
/* User-defined formats. */ /* User-defined formats. */
NC_Dispatch *UDF0_dispatch_table = NULL; NC_Dispatch *UDF0_dispatch_table = NULL;
char UDF0_magic_number[NC_MAX_MAGIC_NUMBER_LEN + 1];
NC_Dispatch *UDF1_dispatch_table = NULL; NC_Dispatch *UDF1_dispatch_table = NULL;
char UDF1_magic_number[NC_MAX_MAGIC_NUMBER_LEN + 1];
#endif /* USE_NETCDF4 */ #endif /* USE_NETCDF4 */
/** \defgroup datasets NetCDF File and Data I/O /** \defgroup datasets NetCDF File and Data I/O
@ -128,15 +130,22 @@ nc_def_user_format(int mode_flag, NC_Dispatch *dispatch_table, char *magic_numbe
return NC_EINVAL; return NC_EINVAL;
if (!dispatch_table) if (!dispatch_table)
return NC_EINVAL; return NC_EINVAL;
if (magic_number && strlen(magic_number) > NC_MAX_MAGIC_NUMBER_LEN)
return NC_EINVAL;
/* Retain a pointer to the dispatch_table. */ /* Retain a pointer to the dispatch_table and a copy of the magic
* number, if one was provided. */
switch(mode_flag) switch(mode_flag)
{ {
case NC_UDF0: case NC_UDF0:
UDF0_dispatch_table = dispatch_table; UDF0_dispatch_table = dispatch_table;
if (magic_number)
strncpy(UDF0_magic_number, magic_number, NC_MAX_MAGIC_NUMBER_LEN);
break; break;
case NC_UDF1: case NC_UDF1:
UDF1_dispatch_table = dispatch_table; UDF1_dispatch_table = dispatch_table;
if (magic_number)
strncpy(UDF0_magic_number, magic_number, NC_MAX_MAGIC_NUMBER_LEN);
break; break;
} }

View File

@ -191,12 +191,14 @@ main(int argc, char **argv)
printf("*** testing user-defined format with magic number..."); printf("*** testing user-defined format with magic number...");
{ {
int ncid; int ncid;
char magic_number[5] = "1111";
/* Create an empty file to play with. */ /* Create an empty file to play with. */
if (nc_create(FILE_NAME, 0, &ncid)) ERR; if (nc_create(FILE_NAME, 0, &ncid)) ERR;
if (nc_close(ncid)) ERR; if (nc_close(ncid)) ERR;
/* Add our test user defined format. */ /* Add our test user defined format. */
if (nc_def_user_format(NC_UDF0, &tst_dispatcher, NULL)) ERR; if (nc_def_user_format(NC_UDF0, &tst_dispatcher, magic_number)) ERR;
/* Open file with our defined functions. */ /* Open file with our defined functions. */
if (nc_open(FILE_NAME, NC_UDF0, &ncid)) ERR; if (nc_open(FILE_NAME, NC_UDF0, &ncid)) ERR;