11 #include "nc4internal.h"
12 #include "hdf5internal.h"
17 #define NCPROPS_MAX_NAME 1024
18 #define NCPROPS_MAX_VALUE 1024
19 #define HDF5_MAX_NAME 1024
21 #define ESCAPECHARS "\\=|,"
24 #define NCHECK(expr) {if((expr)!=NC_NOERR) {goto done;}}
27 #define HCHECK(expr) {if((expr)<0) {ncstat = NC_EHDFERR; goto done;}}
29 static int globalpropinitialized = 0;
33 static int properties_parse(
const char* text0, NClist* pairs);
45 NC4_provenance_init(
void)
52 unsigned major,minor,release;
54 if(globalpropinitialized)
67 if((name = strdup(NCPNCLIB2)) == NULL)
72 if((value = strdup(PACKAGE_VERSION)) == NULL)
78 if((name = strdup(NCPHDF5LIB2)) == NULL)
83 stat = NC4_hdf5get_libversion(&major,&minor,&release);
87 snprintf(sversion,
sizeof(sversion),
"%1u.%1u.%1u",major,minor,release);
88 if((value = strdup(sversion)) == NULL)
97 if(other == NULL) {stat =
NC_ENOMEM;
goto done;}
99 stat = properties_parse(NCPROPERTIES_EXTRA,other);
103 for(i=0;i<nclistlength(other);i++)
105 nclistfreeall(other);
109 if(name != NULL) free(name);
110 if(value != NULL) free(value);
112 nclistfreeall(other);
118 globalpropinitialized = 1;
126 locate(
char* p,
char tag)
131 for(next = p;(c = *next);next++) {
134 else if(c ==
'\\' && next[1] !=
'\0')
147 NC4_provenance_finalize(
void)
163 properties_parse(
const char* text0, NClist* pairs)
170 if(text0 == NULL || strlen(text0) == 0)
173 text = strdup(text0);
178 if(*p == NCPROPSSEP1)
190 q = locate(p,NCPROPSSEP2);
201 nclistpush(pairs,strdup(name));
202 nclistpush(pairs,strdup(value));
212 escapify(NCbytes* buffer,
const char* s)
216 if(strchr(ESCAPECHARS,*p) != NULL)
217 ncbytesappend(buffer,
'\\');
218 ncbytesappend(buffer,*p);
224 propinfo_default(
struct NCPROPINFO* dst,
const struct NCPROPINFO* dfalt)
227 if(dst->properties == NULL) {
228 dst->properties = nclistnew();
229 if(dst->properties == NULL)
return NC_ENOMEM;
231 dst->version = dfalt->version;
232 for(i=0;i<nclistlength(dfalt->properties);i++) {
233 char* s = nclistget(dfalt->properties,i);
236 nclistpush(dst->properties,s);
254 NC4_buildpropinfo(
struct NCPROPINFO* info,
char** propdatap)
258 NCbytes* buffer = NULL;
261 LOG((3,
"%s", __func__));
263 if(info == NULL || info->version == 0 || propdatap == NULL)
268 buffer = ncbytesnew();
269 if(!buffer) {stat =
NC_ENOMEM;
goto done;}
272 ncbytescat(buffer,NCPVERSION);
273 ncbytesappend(buffer,
'=');
274 snprintf(sversion,
sizeof(sversion),
"%d",info->version);
275 ncbytescat(buffer,sversion);
277 for(i=0;i<nclistlength(info->properties);i+=2) {
279 name = nclistget(info->properties,i);
280 if(name == NULL)
continue;
281 value = nclistget(info->properties,i+1);
282 ncbytesappend(buffer,NCPROPSSEP2);
283 escapify(buffer,name);
284 ncbytesappend(buffer,
'=');
285 escapify(buffer,value);
289 *propdatap = ncbytesextract(buffer);
292 if(buffer != NULL) ncbytesfree(buffer);
312 NC4_set_provenance(NC_FILE_INFO_T* file,
const struct NCPROPINFO* dfalt)
315 struct NCPROVENANCE* provenance = NULL;
318 LOG((3,
"%s: ncid 0x%x", __func__, file->root_grp->hdr.id));
320 assert(file->provenance == NULL);
322 provenance = calloc(1,
sizeof(
struct NCPROVENANCE));
323 if(provenance == NULL) {ncstat =
NC_ENOMEM;
goto done;}
328 if((ncstat = NC4_hdf5get_superblock(file,&superblock)))
330 provenance->superblockversion = superblock;
333 provenance->propattr.properties = nclistnew();
334 if(provenance->propattr.properties == NULL)
339 for(i=0;i<nclistlength(dfalt->properties);i++) {
340 char* prop = nclistget(dfalt->properties,i);
343 if(prop == NULL) {ncstat =
NC_ENOMEM;
goto done;}
344 nclistpush(provenance->propattr.properties,prop);
351 LOG((0,
"Could not create _NCProperties attribute"));
352 (void)NC4_free_provenance(provenance);
354 file->provenance = provenance;
375 NC4_get_provenance(NC_FILE_INFO_T* file,
const char* propstring,
const struct NCPROPINFO* dfalt)
378 struct NCPROVENANCE* provenance;
384 LOG((3,
"%s: ncid 0x%x propstring %s", __func__, file->root_grp->hdr.id, propstring));
386 assert(file->provenance == NULL);
387 if((file->provenance = calloc(1,
sizeof(
struct NCPROVENANCE))) == NULL)
389 provenance = file->provenance;
390 if((provenance->propattr.properties = nclistnew()) == NULL)
394 if((ncstat = NC4_hdf5get_superblock(file,&superblock)))
396 provenance->superblockversion = superblock;
398 if(propstring == NULL) {
400 if((ncstat=propinfo_default(&provenance->propattr,dfalt)))
403 NClist* list = provenance->propattr.properties;
404 if((ncstat=properties_parse(propstring,list)))
407 if(nclistlength(list) < 2)
410 name = nclistremove(list,0);
411 value = nclistremove(list,0);
412 if(strcmp(name,NCPVERSION) == 0) {
413 if(sscanf(value,
"%d",&v) != 1)
415 if(v <= 0 || v > NCPROPS_VERSION)
417 provenance->propattr.version = v;
421 if(provenance->propattr.version == 1) {
423 for(i=0;i<nclistlength(list);i+=2) {
424 char* newname = NULL;
425 name = nclistget(list,i);
426 if(name == NULL)
continue;
427 if(strcmp(name,NCPNCLIB1) == 0)
429 else if(strcmp(name,NCPHDF5LIB1) == 0)
430 newname = NCPHDF5LIB2;
433 nclistset(list,i,strdup(newname));
434 if(name) {free(name); name = NULL;}
439 if(name != NULL) free(name);
440 if(value != NULL) free(value);
454 NC4_free_provenance(
struct NCPROVENANCE* prov)
456 LOG((3,
"%s", __func__));
459 if(prov->propattr.properties != NULL)
460 nclistfreeall(prov->propattr.properties);
461 prov->propattr.properties = NULL;
468 NC4_read_ncproperties(NC_FILE_INFO_T* h5)
471 hid_t hdf5grpid = -1;
480 LOG((3,
"%s", __func__));
482 hdf5grpid = ((NC_HDF5_GRP_INFO_T *)(h5->root_grp->format_grp_info))->hdf_grpid;
484 if(H5Aexists(hdf5grpid,NCPROPS) <= 0) {
491 attid = H5Aopen_name(hdf5grpid, NCPROPS);
493 aspace = H5Aget_space(attid);
494 atype = H5Aget_type(attid);
496 t_class = H5Tget_class(atype);
497 if(t_class != H5T_STRING)
499 size = H5Tget_size(atype);
502 text = (
char*)malloc(1+(
size_t)size);
505 if((ntype = H5Tget_native_type(atype, H5T_DIR_DEFAULT)) < 0)
507 if((H5Aread(attid, ntype, text)) < 0)
510 text[(size_t)size] =
'\0';
516 if(text != NULL) free(text);
518 if(attid > 0 && H5Aclose(attid) < 0) retval =
NC_EHDFERR;
519 if(aspace > 0 && H5Sclose(aspace) < 0) retval =
NC_EHDFERR;
520 if(atype > 0 && H5Tclose(atype) < 0) retval =
NC_EHDFERR;
521 if(ntype > 0 && H5Tclose(ntype) < 0) retval =
NC_EHDFERR;
526 LOG((0,
"Invalid _NCProperties attribute: ignored"));
534 NC4_write_ncproperties(NC_FILE_INFO_T* h5)
537 hid_t hdf5grpid = -1;
544 LOG((3,
"%s", __func__));
550 hdf5grpid = ((NC_HDF5_GRP_INFO_T *)(h5->root_grp->format_grp_info))->hdf_grpid;
552 if(H5Aexists(hdf5grpid,NCPROPS) > 0)
556 if((retval = NC4_buildpropinfo(&h5->provenance->propattr,&text)))
560 if ((atype = H5Tcopy(H5T_C_S1)) < 0)
562 if (H5Tset_strpad(atype, H5T_STR_NULLTERM) < 0)
564 if(H5Tset_cset(atype, H5T_CSET_ASCII) < 0)
567 if(H5Tset_size(atype, len) < 0)
571 if((aspace = H5Screate(H5S_SCALAR)) < 0)
573 if ((attid = H5Acreate(hdf5grpid, NCPROPS, atype, aspace, H5P_DEFAULT)) < 0)
575 if (H5Awrite(attid, atype, text) < 0)
582 hsize_t dsize, tsize;
583 typev = H5Aget_type(attid);
584 spacev = H5Aget_space(attid);
585 dsize = H5Aget_storage_size(attid);
586 tsize = H5Tget_size(typev);
587 fprintf(stderr,
"dsize=%lu tsize=%lu\n",(
unsigned long)dsize,(
unsigned long)tsize);
592 if(text != NULL) free(text);
594 if(attid > 0 && H5Aclose(attid) < 0) retval =
NC_EHDFERR;
595 if(aspace > 0 && H5Sclose(aspace) < 0) retval =
NC_EHDFERR;
596 if(atype > 0 && H5Tclose(atype) < 0) retval =
NC_EHDFERR;
607 LOG((0,
"Invalid _NCProperties attribute"));
617 ncprintpropinfo(
struct NCPROPINFO* info)
620 fprintf(stderr,
"[%p] version=%d\n",info,info->version);
621 for(i=0;i<nclistlength(info->properties);i+=2) {
622 char* name = nclistget(info->properties,i);
623 char* value = nclistget(info->properties,i+1);
624 fprintf(stderr,
"\t[%d] name=|%s| value=|%s|\n",i,name,value);
629 ncprintprovenance(
struct NCPROVENANCE* prov)
631 fprintf(stderr,
"[%p] superblockversion=%d\n",prov,prov->superblockversion);
632 ncprintpropinfo(&prov->propattr);
struct NCPROPINFO globalpropinfo
Global property info.
#define NC_ENOMEM
Memory allocation (malloc) failure.
#define NC_EHDFERR
Error at HDF5 layer.
#define NC_EFILEMETA
Problem with file metadata.
#define NC_EINVAL
Invalid Argument.
#define NC_EPERM
Write to read only.
#define NC_NOERR
No Error.