File: hdf5plugins.c

package info (click to toggle)
netcdf 1%3A4.9.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 115,904 kB
  • sloc: ansic: 278,886; sh: 14,183; cpp: 5,971; yacc: 2,612; makefile: 1,999; lex: 1,218; javascript: 280; xml: 173; awk: 2
file content (245 lines) | stat: -rw-r--r-- 6,324 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* Copyright 2003-2018, University Corporation for Atmospheric
 * Research. See the COPYRIGHT file for copying and redistribution
 * conditions.
 */
/**
 * @file @internal netcdf-4 functions for the plugin list.
 *
 * @author Dennis Heimbigner
 */

#include "config.h"
#include <stddef.h>
#include <stdlib.h>
#include "netcdf.h"
#include "ncbytes.h"
#include "hdf5internal.h"
#include "hdf5debug.h"
#include "ncplugins.h"

#undef TPLUGINS

/**************************************************/
/**
 * @file
 * @internal
 * Internal netcdf hdf5 plugin path functions.
 *
 * @author Dennis Heimbigner
 */
/**************************************************/

/**
 * Return the length of the current sequence of directories
 * in the internal global plugin path list.
 * @param ndirsp length is returned here
 * @return NC_NOERR | NC_EXXX
 *
 * @author Dennis Heimbigner
 */
int
NC4_hdf5_plugin_path_ndirs(size_t* ndirsp)
{
    int stat = NC_NOERR;
    size_t ndirs = 0;
    unsigned undirs = 0;  
    herr_t hstat = 0;

    /* Get the length of the HDF5 plugin path set */
    if((hstat = H5PLsize(&undirs))<0) goto done;
    ndirs = (size_t)undirs;
    if(ndirsp) *ndirsp = ndirs;
done:
    return THROW(stat);
}

/**
 * Return the current sequence of directories in the internal global
 * plugin path list. Since this function does not modify the plugin path,
 * it can be called at any time.
 * @param dirs pointer to an NCPluginList object
 * @return NC_NOERR | NC_EXXX
 * @author Dennis Heimbigner
 *
 * WARNING: if dirs->dirs is NULL, then space for the directory
 * vector will be allocated. If not NULL, then the specified space will
 * be overwritten with the vector.
 *
 * @author: Dennis Heimbigner
*/
int
NC4_hdf5_plugin_path_get(NCPluginList* dirs)
{
    int stat = NC_NOERR;
    unsigned i;
    herr_t hstat = 0;
    unsigned undirs = 0;  
    ssize_t dirlen;
    char* dirbuf = NULL;

    if(dirs == NULL) {stat = NC_EINVAL; goto done;}

    /* Get the length of the HDF5 plugin path set */
    if((hstat = H5PLsize(&undirs))<0) goto done;
    dirs->ndirs = (size_t)undirs;

    /* Copy out the paths from the HDF5 library */
    /* Watch out for nul term handling WRT dir string length */
    if(dirs->dirs == NULL) {
	if((dirs->dirs=(char**)calloc(dirs->ndirs,sizeof(char*)))==NULL)
	    {stat = NC_ENOMEM; goto done;}
    }
    for(i=0;i<undirs;i++) {
	if((dirlen = H5PLget(i, NULL, 0))<0) {stat = NC_EHDFERR; goto done;}
	nullfree(dirbuf); dirbuf = NULL; /* suspenders and belt */
	if((dirbuf = (char*)malloc((size_t)dirlen+1))==NULL) {stat = NC_ENOMEM; goto done;} /* dirlen does not include nul term */
	if((dirlen = H5PLget(i, dirbuf, ((size_t)dirlen)+1))<0) {stat = NC_EHDFERR; goto done;}
	dirs->dirs[i] = dirbuf; dirbuf = NULL;	    
    }
    
done:
    if(hstat < 0 && stat != NC_NOERR) stat = NC_EHDFERR;
    return THROW(stat);
}

/**
 * Empty the current internal path sequence
 * and replace with the sequence of directories argument.
 * Using a dirs->ndirs argument of 0 will clear the set of plugin dirs.
 *
 * @param dirs to overwrite the current internal dir list
 * @return NC_NOERR | NC_EXXX
 *
 * @author Dennis Heimbigner
*/
int
NC4_hdf5_plugin_path_set(NCPluginList* dirs)
{
    int stat = NC_NOERR;
    size_t i;
    herr_t hstat = 0;
    unsigned undirs = 0;  

    /* validate */
    if(dirs == NULL || (dirs->ndirs > 0 && dirs->dirs == NULL))
	{stat = NC_EINVAL; goto done;}

    /* Clear the current path list */
    if((hstat = H5PLsize(&undirs))<0) goto done;
    if(undirs > 0) {
	for(i=0;i<undirs;i++) {
	    /* Always remove the first element to avoid index confusion */
	    if((hstat = H5PLremove(0))<0) {stat = NC_EINVAL; goto done;}
	}
    }

    /* Insert the new path list */
    for(i=0;i<dirs->ndirs;i++) {
	/* Always append */
	if((hstat = H5PLappend(dirs->dirs[i]))<0)
	    {stat = NC_EINVAL; goto done;}
    }

done:
    if(hstat < 0 && stat != NC_NOERR) stat = NC_EHDFERR;
    return stat;
}

int
NC4_hdf5_plugin_path_initialize(void)
{
    return NC_NOERR;
}

int
NC4_hdf5_plugin_path_finalize(void)
{
    return NC_NOERR;
}

/**************************************************/
/* Debug printer for HDF5 plugin paths */

static NCbytes* ncppbuf;
const char*
NC4_hdf5_plugin_path_tostring(void)
{
    int stat = NC_NOERR;
    herr_t hstat = 0;
    unsigned npaths = 0;
    char* dir = NULL;

    if(ncppbuf == NULL) ncppbuf = ncbytesnew();
    ncbytesclear(ncppbuf);

    if((hstat = H5PLsize(&npaths))<0) {stat = NC_EINVAL; goto done;}
    if(npaths > 0) {
	ssize_t dirlen = 0;
	unsigned i;
	for(i=0;i<npaths;i++) {
	    dirlen = H5PLget(i,NULL,0);
	    if(dirlen < 0) {stat = NC_EINVAL; goto done;}
	    if((dir = (char*)malloc(1+(size_t)dirlen))==NULL)
		{stat = NC_ENOMEM; goto done;}
	    /* returned dirlen does not include the nul terminator, but the length argument must include it */
	    dirlen = H5PLget(i,dir,(size_t)(dirlen+1));
	    dir[dirlen] = '\0';
	    if(i > 0) ncbytescat(ncppbuf,";");
	    ncbytescat(ncppbuf,dir);
	    nullfree(dir); dir = NULL;
	}
    }

done:
    nullfree(dir);
    if(stat != NC_NOERR) ncbytesclear(ncppbuf);
    ncbytesnull(ncppbuf);
    return ncbytescontents(ncppbuf);
}

/**************************************************/
#ifdef TPLUGINS

static void
printplugin1(struct NC_HDF5_Plugin* nfs)
{
    int i;
    if(nfs == NULL) {
	fprintf(stderr,"{null}");
	return;
    }
    fprintf(stderr,"{%u,(%u)",nfs->pluginid,(int)nfs->nparams);
    for(i=0;i<nfs->nparams;i++) {
      fprintf(stderr," %s",nfs->params[i]);
    }
    fprintf(stderr,"}");
}

static void
printplugin(struct NC_HDF5_Plugin* nfs, const char* tag, int line)
{
    fprintf(stderr,"%s: line=%d: ",tag,line);
    printplugin1(nfs);
    fprintf(stderr,"\n");
}

static void
printpluginlist(NC_VAR_INFO_T* var, const char* tag, int line)
{
    int i;
    const char* name;
    if(var == NULL) name = "null";
    else if(var->hdr.name == NULL) name = "?";
    else name = var->hdr.name;
    fprintf(stderr,"%s: line=%d: var=%s plugins=",tag,line,name);
    if(var != NULL) {
	NClist* plugins = (NClist*)var->plugins;
        for(i=0;i<nclistlength(plugins);i++) {
	    struct NC_HDF5_Plugin* nfs = (struct NC_HDF5_Plugin*)nclistget(plugins,i);
	    fprintf(stderr,"[%d]",i);
	    printplugin1(nfs);
	}
    }
    fprintf(stderr,"\n");
}
#endif /*TPLUGINS*/