123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- #include "nc4internal.h"
- #include "nc4dispatch.h"
- int
- NC4_def_grp(int parent_ncid, const char *name, int *new_ncid)
- {
- NC_GRP_INFO_T *grp, *g;
- NC_HDF5_FILE_INFO_T *h5;
- char norm_name[NC_MAX_NAME + 1];
- int retval;
- LOG((2, "nc_def_grp: parent_ncid 0x%x name %s", parent_ncid, name));
-
- if ((retval = nc4_find_grp_h5(parent_ncid, &grp, &h5)))
- return retval;
- if (!h5)
- return NC_ENOTNC4;
-
- if ((retval = nc4_check_name(name, norm_name)))
- return retval;
-
- if ((retval = nc4_check_dup_name(grp, norm_name)))
- return retval;
-
- if (h5->cmode & NC_CLASSIC_MODEL)
- return NC_ESTRICTNC3;
-
- if (!(h5->flags & NC_INDEF))
- if ((retval = NC4_redef(parent_ncid)))
- return retval;
-
- if ((retval = nc4_grp_list_add(&(grp->children), h5->next_nc_grpid,
- grp, grp->file, norm_name, &g)))
- return retval;
- if (new_ncid)
- *new_ncid = grp->file->ext_ncid | h5->next_nc_grpid;
- h5->next_nc_grpid++;
-
- return NC_NOERR;
- }
- int
- NC4_inq_ncid(int ncid, const char *name, int *grp_ncid)
- {
- NC_GRP_INFO_T *grp, *g;
- NC_HDF5_FILE_INFO_T *h5;
- char norm_name[NC_MAX_NAME + 1];
- int retval;
- LOG((2, "nc_inq_ncid: ncid 0x%x name %s", ncid, name));
-
-
- if ((retval = nc4_find_grp_h5(ncid, &grp, &h5)))
- return retval;
-
- if (!h5)
- return NC_ENOTNC4;
-
- if ((retval = nc4_normalize_name(name, norm_name)))
- return retval;
-
- for (g = grp->children; g; g = g->next)
- if (!strcmp(norm_name, g->name))
- {
- if (grp_ncid)
- *grp_ncid = grp->file->ext_ncid | g->nc_grpid;
- return NC_NOERR;
- }
-
-
- return NC_ENOGRP;
- }
- int
- NC4_inq_grps(int ncid, int *numgrps, int *ncids)
- {
- NC_GRP_INFO_T *grp, *g;
- NC_HDF5_FILE_INFO_T *h5;
- int num = 0;
- int retval;
- LOG((2, "nc_inq_grps: ncid 0x%x", ncid));
-
- if ((retval = nc4_find_grp_h5(ncid, &grp, &h5)))
- return retval;
-
- if (!h5)
- {
- if (numgrps)
- *numgrps = 0;
- return NC_NOERR;
- }
-
- for (g = grp->children; g; g = g->next)
- {
- if (ncids)
- {
-
- *ncids = g->nc_grpid | g->file->ext_ncid;
- ncids++;
- }
- num++;
- }
-
- if (numgrps)
- *numgrps = num;
- return NC_NOERR;
- }
- int
- NC4_inq_grpname(int ncid, char *name)
- {
- NC_GRP_INFO_T *grp;
- NC_HDF5_FILE_INFO_T *h5;
- int retval;
- LOG((2, "nc_inq_grpname: ncid 0x%x", ncid));
-
- if ((retval = nc4_find_grp_h5(ncid, &grp, &h5)))
- return retval;
- if (name)
- {
- if (!h5)
- strcpy(name, "/");
- else
- strcpy(name, grp->name);
- }
- return NC_NOERR;
- }
- int
- NC4_inq_grpname_full(int ncid, size_t *lenp, char *full_name)
- {
- char *name, grp_name[NC_MAX_NAME + 1];
- int g, id = ncid, parent_id, *gid;
- int i, ret = NC_NOERR;
-
- for (g = 0; !nc_inq_grp_parent(id, &parent_id); g++, id = parent_id)
- ;
-
- if (!(name = malloc((g + 1) * (NC_MAX_NAME + 1) + 1)))
- return NC_ENOMEM;
- if (!(gid = malloc((g + 1) * sizeof(int))))
- {
- free(name);
- return NC_ENOMEM;
- }
- assert(name && gid);
-
- strcpy(name, "/");
-
- gid[0] = ncid;
- for (i = 1; i < g && !ret; i++)
- ret = nc_inq_grp_parent(gid[i - 1], &gid[i]);
-
- for (i = g - 1; !ret && i >= 0 && !ret; i--)
- {
- if ((ret = nc_inq_grpname(gid[i], grp_name)))
- break;
- strcat(name, grp_name);
- if (i)
- strcat(name, "/");
- }
-
- if (!ret && lenp)
- *lenp = strlen(name);
-
- if (!ret && full_name)
- strcpy(full_name, name);
- free(gid);
- free(name);
- return ret;
- }
- int
- NC4_inq_grp_parent(int ncid, int *parent_ncid)
- {
- NC_GRP_INFO_T *grp;
- NC_HDF5_FILE_INFO_T *h5;
- int retval;
- LOG((2, "nc_inq_grp_parent: ncid 0x%x", ncid));
-
- if ((retval = nc4_find_grp_h5(ncid, &grp, &h5)))
- return retval;
-
- if (!h5)
- return NC_ENOGRP;
-
- if (grp->parent)
- {
- if (parent_ncid)
- *parent_ncid = grp->file->ext_ncid | grp->parent->nc_grpid;
- }
- else
- return NC_ENOGRP;
-
- return NC_NOERR;
- }
- int
- NC4_inq_grp_full_ncid(int ncid, const char *full_name, int *grp_ncid)
- {
- NC_GRP_INFO_T *grp;
- NC_HDF5_FILE_INFO_T *h5;
- int id1 = ncid, id2;
- char *cp, *full_name_cpy;
- int ret;
- if (!full_name)
- return NC_EINVAL;
-
- if ((ret = nc4_find_grp_h5(ncid, &grp, &h5)))
- return ret;
-
- if (!(full_name_cpy = malloc(strlen(full_name) + 1)))
- return NC_ENOMEM;
- strcpy(full_name_cpy, full_name);
-
- if (!(cp = strtok(full_name_cpy, "/")))
- {
-
- if (!grp->parent)
- id2 = ncid;
- else
- {
- free(full_name_cpy);
- return NC_ENOGRP;
- }
- }
- else
- {
-
- for (; cp; id1 = id2)
- {
- if ((ret = nc_inq_grp_ncid(id1, cp, &id2)))
- {
- free(full_name_cpy);
- return ret;
- }
- cp = strtok(NULL, "/");
- }
- }
-
- if (grp_ncid)
- *grp_ncid = id2;
- free(full_name_cpy);
- return NC_NOERR;
- }
- int
- NC4_inq_varids(int ncid, int *nvars, int *varids)
- {
- NC_GRP_INFO_T *grp;
- NC_HDF5_FILE_INFO_T *h5;
- NC_VAR_INFO_T *var;
- int v, num_vars = 0;
- int retval;
- LOG((2, "nc_inq_varids: ncid 0x%x", ncid));
-
- if ((retval = nc4_find_grp_h5(ncid, &grp, &h5)))
- return retval;
- if (!h5)
- {
-
- if ((retval = nc_inq(ncid, NULL, &num_vars, NULL, NULL)))
- return retval;
- if (varids)
- for (v = 0; v < num_vars; v++)
- varids[v] = v;
- }
- else
- {
-
- if (grp->var)
- {
- for (var = grp->var; var; var = var->next)
- {
- if (varids)
- varids[num_vars] = var->varid;
- num_vars++;
- }
- }
- }
-
- if (nvars)
- *nvars = num_vars;
- return NC_NOERR;
- }
- int int_cmp(const void *a, const void *b)
- {
- const int *ia = (const int *)a;
- const int *ib = (const int *)b;
- return *ia - *ib;
- }
- int
- NC4_inq_dimids(int ncid, int *ndims, int *dimids, int include_parents)
- {
- NC_GRP_INFO_T *grp, *g;
- NC_HDF5_FILE_INFO_T *h5;
- NC_DIM_INFO_T *dim;
- int d, num = 0;
- int retval;
- LOG((2, "nc_inq_dimids: ncid 0x%x include_parents: %d", ncid,
- include_parents));
-
- if ((retval = nc4_find_grp_h5(ncid, &grp, &h5)))
- return retval;
- if (!h5)
- {
-
- if ((retval = nc_inq(ncid, &num, NULL, NULL, NULL)))
- return retval;
- if (dimids)
- for (d = 0; d < num; d++)
- dimids[d] = d;
- }
- else
- {
-
- for (dim = grp->dim; dim; dim = dim->next)
- num++;
- if (include_parents)
- for (g = grp->parent; g; g = g->parent)
- for (dim = g->dim; dim; dim = dim->next)
- num++;
-
-
- if (dimids)
- {
- int n = 0;
-
- for (dim = grp->dim; dim; dim = dim->next)
- dimids[n++] = dim->dimid;
-
- if (include_parents)
- for (g = grp->parent; g; g = g->parent)
- for (dim = g->dim; dim; dim = dim->next)
- dimids[n++] = dim->dimid;
-
- qsort(dimids, num, sizeof(int), int_cmp);
- }
- }
-
- if (ndims)
- *ndims = num;
- return NC_NOERR;
- }
|