summaryrefslogtreecommitdiffstats
path: root/java/src/jni/h5rImp.c
blob: 3dcbb8d691d3a18a4a64e5d26d236e7d2e8396be (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 *  For details of the HDF libraries, see the HDF Documentation at:
 *    http://hdfgroup.org/HDF5/doc/
 *
 */
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include "hdf5.h"
#include <jni.h>
#include <stdlib.h>
#include "h5jni.h"
#include "h5rImp.h"

extern JavaVM *jvm;
extern jobject visit_callback;


/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Rcreate
 * Signature: ([BJLjava/lang/String;IJ)I
 */
JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Rcreate
    (JNIEnv *env, jclass clss, jbyteArray ref, jlong loc_id, jstring name, jint ref_type, jlong space_id)
{
    const char *rName;
    herr_t      status = -1;
    jbyte      *refP;
    jboolean    isCopy2;

    PIN_JAVA_STRING(name, rName);
    if (rName != NULL) {
        if (ref == NULL) {
            h5nullArgument( env, "H5Rcreate:  ref is NULL");
        } /* end if */
        else {
            if ((ref_type == H5R_OBJECT) && ENVPTR->GetArrayLength(ENVPAR ref) != H5R_OBJ_REF_BUF_SIZE) {
                h5badArgument( env, "H5Rcreate:  ref input array != H5R_OBJ_REF_BUF_SIZE");
            } /* end if */
            else if ((ref_type == H5R_DATASET_REGION) && ENVPTR->GetArrayLength(ENVPAR ref) != H5R_DSET_REG_REF_BUF_SIZE) {
                h5badArgument( env, "H5Rcreate:  region ref input array != H5R_DSET_REG_REF_BUF_SIZE");
            } /* end else if */
            else if ((ref_type != H5R_OBJECT) && (ref_type != H5R_DATASET_REGION)) {
                h5badArgument( env, "H5Rcreate:  ref_type unknown type ");
            } /* end else if */
            else {
                refP = (jbyte*)ENVPTR->GetByteArrayElements(ENVPAR ref, &isCopy2);
                if (refP == NULL) {
                    h5JNIFatalError(env,  "H5Rcreate:  ref not pinned");
                } /* end if */
                else {
                    status = H5Rcreate(refP, (hid_t)loc_id, rName, (H5R_type_t)ref_type, (hid_t)space_id);

                    if (status < 0) {
                        ENVPTR->ReleaseByteArrayElements(ENVPAR ref, refP, JNI_ABORT);
                        h5libraryError(env);
                    } /* end if */
                    else {
                        ENVPTR->ReleaseByteArrayElements(ENVPAR ref, refP, 0);
                    } /* end else */
                } /* end else */
            } /* end else */
        } /* end else */
        UNPIN_JAVA_STRING(name, rName);
    }

    return (jint)status;
} /* end Java_hdf_hdf5lib_H5_H5Rcreate */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    _H5Rdereference
 * Signature: (JJI[B)J
 */
JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5__1H5Rdereference
    (JNIEnv *env, jclass clss, jlong dataset, jlong access_list, jint ref_type, jbyteArray ref)
{
    jboolean isCopy;
    jbyte   *refP;
    hid_t    retVal = -1;

    if (ref == NULL) {
        h5nullArgument( env, "H5Rdereference:  ref is NULL");
    } /* end if */
    else if ((ref_type == H5R_OBJECT) && ENVPTR->GetArrayLength(ENVPAR ref) != H5R_OBJ_REF_BUF_SIZE) {
        h5badArgument( env, "H5Rdereference:  obj ref input array != H5R_OBJ_REF_BUF_SIZE");
    } /* end else if */
    else if ((ref_type == H5R_DATASET_REGION) && ENVPTR->GetArrayLength(ENVPAR ref) != H5R_DSET_REG_REF_BUF_SIZE) {
        h5badArgument( env, "H5Rdereference:  region ref input array != H5R_DSET_REG_REF_BUF_SIZE");
    } /* end else if */
    else {
        refP = (jbyte*)ENVPTR->GetByteArrayElements(ENVPAR ref, &isCopy);
        if (refP == NULL) {
            h5JNIFatalError(env,  "H5Rderefernce:  ref not pinned");
        } /* end if */
        else {
            retVal = H5Rdereference2((hid_t)dataset, (hid_t)access_list, (H5R_type_t)ref_type, refP);

            ENVPTR->ReleaseByteArrayElements(ENVPAR ref, refP, JNI_ABORT);

            if (retVal < 0)
                h5libraryError(env);
        } /* end else */
    } /* end else */

    return (jlong)retVal;
} /* end Java_hdf_hdf5lib_H5__1H5Rdereference */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Rget_region
 * Signature: (JI[B)J
 */
JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5__1H5Rget_1region
    (JNIEnv *env, jclass clss, jlong dataset, jint ref_type, jbyteArray ref)
{
    hid_t    retVal = -1;
    jboolean isCopy;
    jbyte   *refP;

    if (ref_type != H5R_DATASET_REGION)  {
        h5badArgument( env, "H5Rget_region:  bad ref_type ");
    } /* end if */
    else if (ref == NULL) {
        h5nullArgument( env, "H5Rget_region:  ref is NULL");
    } /* end if */
    else if ( ENVPTR->GetArrayLength(ENVPAR ref) != H5R_DSET_REG_REF_BUF_SIZE) {
        h5badArgument( env, "H5Rget_region:  region ref input array != H5R_DSET_REG_REF_BUF_SIZE");
    } /* end if */
    else {
        refP = (jbyte*)ENVPTR->GetByteArrayElements(ENVPAR ref, &isCopy);
        if (refP == NULL) {
            h5JNIFatalError(env,  "H5Rget_region:  ref not pinned");
        } /* end if */
        else {
            retVal = H5Rget_region((hid_t)dataset, (H5R_type_t)ref_type, refP);

            ENVPTR->ReleaseByteArrayElements(ENVPAR ref, refP, JNI_ABORT);

            if (retVal < 0)
                h5libraryError(env);
        } /* end else */
    } /* end else */

    return (jlong)retVal;
} /* end Java_hdf_hdf5lib_H5__1H5Rget_1region */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5G_obj_t H5Rget_obj_type
 * Signature: (JI[B)I
 */
JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Rget_1obj_1type
    (JNIEnv *env, jclass clss, jlong loc_id, jint ref_type, jbyteArray ref)
{
    int      retVal =-1;
    jboolean isCopy;
    jbyte   *refP;
    H5O_type_t object_info;


    if (ref == NULL) {
        h5nullArgument( env, "H5Rget_object_type:  ref is NULL");
    } /* end if */
    else {
        refP = (jbyte*)ENVPTR->GetByteArrayElements(ENVPAR ref, &isCopy);
        if (refP == NULL) {
            h5JNIFatalError(env,  "H5Rget_object_type:  ref not pinned");
        } /* end if */
        else {
            retVal = H5Rget_obj_type2((hid_t)loc_id, (H5R_type_t)ref_type, refP, &object_info);
            if(retVal >= 0)
                retVal = object_info;

            ENVPTR->ReleaseByteArrayElements(ENVPAR ref, refP, JNI_ABORT);

            if (retVal < 0)
                h5libraryError(env);
        } /* end else */
    } /* end else */

    return (jint)retVal;
} /* end Java_hdf_hdf5lib_H5_H5Rget_1obj_1type */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    int H5Rget_obj_type2
 * Signature: (JI[B[I)I
 */
JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Rget_1obj_1type2
    (JNIEnv *env, jclass clss, jlong loc_id, jint ref_type, jbyteArray ref, jintArray ref_obj)
{

    jint     status;
    jboolean isCopy;
    jbyte   *refP;
    jint    *ref_objP;
    int      retVal = -1;


    if (ref == NULL) {
        h5nullArgument( env, "H5Rget_object_type:  ref is NULL");
    } /* end if */
    else if (ref_obj == NULL) {
        h5nullArgument( env, "H5Rget_object_type:  ref_obj is NULL");
    } /* end else if */
    else {
        refP = (jbyte *)ENVPTR->GetByteArrayElements(ENVPAR ref, &isCopy);
        if (refP == NULL) {
            h5JNIFatalError(env,  "H5Rget_object_type:  ref not pinned");
        } /* end if */
        else {
            ref_objP = (jint *)ENVPTR->GetIntArrayElements(ENVPAR ref_obj, &isCopy);
            if (ref_objP == NULL) {
                ENVPTR->ReleaseByteArrayElements(ENVPAR ref,refP,0);
                h5JNIFatalError(env,  "H5Rget_object_type:  ref_obj not pinned");
            } /* end if */
            else {
                status = H5Rget_obj_type2((hid_t)loc_id, (H5R_type_t)ref_type, refP, (H5O_type_t*)ref_objP);
                retVal = ref_objP[0];

                ENVPTR->ReleaseByteArrayElements(ENVPAR ref, refP, JNI_ABORT);
                if (status < 0) {
                    ENVPTR->ReleaseIntArrayElements(ENVPAR ref_obj,ref_objP, JNI_ABORT);
                    h5libraryError(env);
                } /* end if */
                else {
                    ENVPTR->ReleaseIntArrayElements(ENVPAR ref_obj, ref_objP, 0);
                } /* end else */
            } /* end else */
        } /* end else */
    } /* end else */

    return (jint)retVal;
} /* end Java_hdf_hdf5lib_H5_H5Rget_1obj_1type2 */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Rget_name
 * Signature: (JI[B[Ljava/lang/String;J)J
 */
JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5_H5Rget_1name
    (JNIEnv *env, jclass clss, jlong loc_id, jint ref_type, jbyteArray ref, jobjectArray name, jlong size)
{
    jlong    ret_val = -1;
    jbyte   *refP;
    jboolean isCopy;
    char    *aName = NULL;
    jstring  str;
    size_t   bs;

    bs = (size_t)size;
    if (bs <= 0) {
        h5badArgument(env, "H5Rget_name:  size <= 0");
    } /* end if */
    else if (ref == NULL) {
        h5nullArgument(env, "H5Rget_name:  ref is NULL");
    } /* end else if */
    else {
        if ((ref_type == H5R_OBJECT) && ENVPTR->GetArrayLength(ENVPAR ref) != H5R_OBJ_REF_BUF_SIZE) {
            h5badArgument(env, "H5Rget_name:  obj ref input array != H5R_OBJ_REF_BUF_SIZE");
        } /* end if */
        else if ((ref_type == H5R_DATASET_REGION)
                && ENVPTR->GetArrayLength(ENVPAR ref) != H5R_DSET_REG_REF_BUF_SIZE) {
            h5badArgument(env, "H5Rget_name:  region ref input array != H5R_DSET_REG_REF_BUF_SIZE");
        } /* end else if */
        else {
            refP = (jbyte *)ENVPTR->GetByteArrayElements(ENVPAR ref, &isCopy);
            if (refP == NULL) {
                h5JNIFatalError(env,  "H5Rget_name:  ref not pinned");
            } /* end if */
            else {
                aName = (char*)HDmalloc(sizeof(char)*bs);
                if (aName == NULL) {
                    ENVPTR->ReleaseByteArrayElements(ENVPAR ref, refP, JNI_ABORT);
                    h5outOfMemory(env, "H5Rget_name:  malloc failed");
                } /* end if */
                else {
                    ret_val = (jlong)H5Rget_name((hid_t)loc_id, (H5R_type_t)ref_type, refP, aName, bs) ;

                    ENVPTR->ReleaseByteArrayElements(ENVPAR ref, refP, JNI_ABORT);
                    if (ret_val < 0) {
                        HDfree(aName);
                        h5libraryError(env);
                    } /* end if */
                    else {
                        str = ENVPTR->NewStringUTF(ENVPAR aName);
                        ENVPTR->SetObjectArrayElement(ENVPAR name, 0, str);

                        HDfree(aName);
                    } /* end else */
                } /* end else */
            } /* end else */
        } /* end else */
    } /* end else */

    return ret_val;
} /* end Java_hdf_hdf5lib_H5_H5Rget_1name */


#ifdef __cplusplus
} /* end extern "C" */
#endif /* __cplusplus */