mirror of
https://github.com/Unidata/netcdf-c.git
synced 2025-02-17 16:50:18 +08:00
magic numbers working with user-defined formats
This commit is contained in:
parent
5521e062a4
commit
7aa073eb5d
@ -145,7 +145,7 @@ nc_def_user_format(int mode_flag, NC_Dispatch *dispatch_table, char *magic_numbe
|
||||
case NC_UDF1:
|
||||
UDF1_dispatch_table = dispatch_table;
|
||||
if (magic_number)
|
||||
strncpy(UDF0_magic_number, magic_number, NC_MAX_MAGIC_NUMBER_LEN);
|
||||
strncpy(UDF1_magic_number, magic_number, NC_MAX_MAGIC_NUMBER_LEN);
|
||||
break;
|
||||
}
|
||||
|
||||
@ -217,15 +217,15 @@ NC_interpret_magic_number(char* magic, int* model, int* version)
|
||||
*model = 0;
|
||||
*version = 0;
|
||||
#ifdef USE_NETCDF4
|
||||
if (strlen(UDF0_magic_number) &&
|
||||
!strncmp(magic, UDF0_magic_number, NC_MAX_MAGIC_NUMBER_LEN))
|
||||
if (strlen(UDF0_magic_number) && !strncmp(UDF0_magic_number, magic,
|
||||
strlen(UDF0_magic_number)))
|
||||
{
|
||||
*model = NC_FORMATX_UDF0;
|
||||
*version = 6; /* redundant */
|
||||
goto done;
|
||||
}
|
||||
if (strlen(UDF1_magic_number) &&
|
||||
!strncmp(magic, UDF1_magic_number, NC_MAX_MAGIC_NUMBER_LEN))
|
||||
if (strlen(UDF1_magic_number) && !strncmp(UDF1_magic_number, magic,
|
||||
strlen(UDF1_magic_number)))
|
||||
{
|
||||
*model = NC_FORMATX_UDF1;
|
||||
*version = 7; /* redundant */
|
||||
|
@ -156,6 +156,8 @@ NC_NOTNC4_get_var_chunk_cache
|
||||
|
||||
#define NUM_UDFS 2
|
||||
|
||||
int mode[NUM_UDFS] = {NC_UDF0, NC_UDF1};
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
@ -163,7 +165,6 @@ main(int argc, char **argv)
|
||||
printf("*** testing simple user-defined format...");
|
||||
{
|
||||
int ncid;
|
||||
int mode[NUM_UDFS] = {NC_UDF0, NC_UDF1};
|
||||
NC_Dispatch *disp_in;
|
||||
int i;
|
||||
|
||||
@ -196,24 +197,44 @@ main(int argc, char **argv)
|
||||
printf("*** testing user-defined format with magic number...");
|
||||
{
|
||||
int ncid;
|
||||
NC_Dispatch *disp_in;
|
||||
char magic_number[5] = "1111";
|
||||
char dummy_data[11] = "0123456789";
|
||||
char magic_number_in[NC_MAX_MAGIC_NUMBER_LEN];
|
||||
FILE *FP;
|
||||
int i;
|
||||
|
||||
/* Create an empty file to play with. */
|
||||
if (nc_create(FILE_NAME, 0, &ncid)) ERR;
|
||||
if (nc_close(ncid)) ERR;
|
||||
/* Create a file with magic number at start. */
|
||||
if (!(FP = fopen(FILE_NAME, "w"))) ERR;
|
||||
if (fwrite(magic_number, sizeof(char), strlen(magic_number), FP)
|
||||
!= strlen(magic_number)) ERR;
|
||||
if (fwrite(dummy_data, sizeof(char), strlen(dummy_data), FP)
|
||||
!= strlen(dummy_data)) ERR;
|
||||
if (fclose(FP)) ERR;
|
||||
|
||||
/* Add our test user defined format. */
|
||||
if (nc_def_user_format(NC_UDF0, &tst_dispatcher, magic_number)) ERR;
|
||||
/* Test all available user-defined format slots. */
|
||||
for (i = 0; i < NUM_UDFS; i++)
|
||||
{
|
||||
/* Add our test user defined format. */
|
||||
if (nc_def_user_format(mode[i], &tst_dispatcher, magic_number)) ERR;
|
||||
|
||||
/* Check that our user-defined format has been added. */
|
||||
if (nc_inq_user_format(mode[i], &disp_in, magic_number_in)) ERR;
|
||||
if (disp_in != &tst_dispatcher) ERR;
|
||||
if (strncmp(magic_number, magic_number_in, strlen(magic_number))) ERR;
|
||||
|
||||
/* Open file with our defined functions. */
|
||||
if (nc_open(FILE_NAME, NC_UDF0, &ncid)) ERR;
|
||||
if (nc_close(ncid)) ERR;
|
||||
|
||||
/* Open file again and abort, which is the same as closing it. */
|
||||
if (nc_open(FILE_NAME, NC_UDF0, &ncid)) ERR;
|
||||
if (nc_inq_format(ncid, NULL) != TEST_VAL_42) ERR;
|
||||
if (nc_inq_format_extended(ncid, NULL, NULL) != TEST_VAL_42) ERR;
|
||||
if (nc_abort(ncid) != TEST_VAL_42) ERR;
|
||||
/* Open file with our defined functions. */
|
||||
if (nc_open(FILE_NAME, mode[i], &ncid)) ERR;
|
||||
if (nc_close(ncid)) ERR;
|
||||
|
||||
/* Open file again and abort, which is the same as closing
|
||||
* it. This time we don't specify a mode, because the magic
|
||||
* number is used to identify the file. */
|
||||
if (nc_open(FILE_NAME, 0, &ncid)) ERR;
|
||||
if (nc_inq_format(ncid, NULL) != TEST_VAL_42) ERR;
|
||||
if (nc_inq_format_extended(ncid, NULL, NULL) != TEST_VAL_42) ERR;
|
||||
if (nc_abort(ncid) != TEST_VAL_42) ERR;
|
||||
}
|
||||
}
|
||||
SUMMARIZE_ERR;
|
||||
FINAL_RESULTS;
|
||||
|
Loading…
Reference in New Issue
Block a user