File: h5VLStrHelperImp.c

package info (click to toggle)
libsis-jhdf5-java 19.04.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,668 kB
  • sloc: java: 79,644; ansic: 18,986; sh: 309; makefile: 49; xml: 12
file content (171 lines) | stat: -rw-r--r-- 4,922 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
/*
 * Copyright 2007 - 2018 ETH Zuerich, CISD and SIS.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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


/*
 * Class:     ch_systemsx_cisd_hdf5_hdf5lib_HDFHelper
 * Method:    getPointerSize
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_ch_systemsx_cisd_hdf5_hdf5lib_HDFHelper_getPointerSize
  (JNIEnv *env, jclass clss)
{
    return sizeof(void *);
}

/*
 * Class:     ch_systemsx_cisd_hdf5_hdf5lib_HDFHelper
 * Method:    compoundCpyVLStr
 * Signature: (Ljava/lang/String;[B)I)I
 */
JNIEXPORT jint JNICALL Java_ch_systemsx_cisd_hdf5_hdf5lib_HDFHelper_compoundCpyVLStr
  (JNIEnv *env, 
   jclass clss, 
   jstring str, /* IN: the string to copy */ 
   jbyteArray buf, /* OUT: array of byte */
   jint bufOfs /* The offset to copy the pointer to the string to. */
  )
{
    jbyte *byteP;
    char *strPCpy;
    int numberOfBytes, numberOfCharacters;


    if ( str == NULL ) {
        h5nullArgument( env, "compoundCpyVLStr:  str is NULL");
        return -1;
    }
    if ( buf == NULL ) {
        h5nullArgument( env, "compoundCpyVLStr:  buf is NULL");
        return -1;
    }

	numberOfBytes = (*env)->GetStringUTFLength(env, str);
	strPCpy = calloc(1, numberOfBytes + 1);
	numberOfCharacters = (*env)->GetStringLength(env, str);
    (*env)->GetStringUTFRegion(env, str, 0, numberOfCharacters, strPCpy);

    byteP = (*env)->GetPrimitiveArrayCritical(env, buf, NULL);
    if (byteP == NULL) {
        h5JNIFatalError( env, "compoundCpyVLStr:  buf not pinned");
        return -1;
    }
	*((char**)(byteP + bufOfs)) = strPCpy;
    (*env)->ReleasePrimitiveArrayCritical(env, buf, byteP, 0);

	return 0;
}

/*
 * Class:     ch_systemsx_cisd_hdf5_hdf5lib_HDFHelper
 * Method:    createVLStrFromCompound
 * Signature: ([B)I)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_ch_systemsx_cisd_hdf5_hdf5lib_HDFHelper_createVLStrFromCompound
  (JNIEnv *env, 
   jclass clss, 
   jbyteArray buf, /* IN: array of byte containing the compound or compound array. */
   jint offset /* IN: The offset in the compound or compound array where the pointer to the string is located. */
  )
{
    char *byteP;
    char **strP;
    jstring str;

    if ( buf == NULL ) {
        h5nullArgument( env, "createVLStrFromCompound:  buf is NULL");
        return NULL;
    }

    byteP = (*env)->GetPrimitiveArrayCritical(env, buf, NULL);
    if (byteP == NULL) {
        h5JNIFatalError( env, "createVLStrFromCompound:  buf not pinned");
        return NULL;
    }
    
	strP = (char**) (byteP + offset);
	str = (*env)->NewStringUTF(env, *strP);
	
    (*env)->ReleasePrimitiveArrayCritical(env, buf, byteP, 0);
	
	return str;
}

/*
 * Class:     ch_systemsx_cisd_hdf5_hdf5lib_HDFHelper
 * Method:    freeCompoundVLStr
 * Signature: ([B)I[I))I
 */
JNIEXPORT jint JNICALL Java_ch_systemsx_cisd_hdf5_hdf5lib_HDFHelper_freeCompoundVLStr
  (JNIEnv *env, 
   jclass clss, 
   jbyteArray buf, /* IN: array of byte containing the compound or compound array. */
   jint recordSize, /* IN: The size of one compound record. */
   jintArray vlIndices /* IN: The indices of the variable-length compound members in the record. */
  )
{
    char *byteP, *ptr;
    char **strP;
    jsize bufLen, idxLen;
    int *idxP, i;

    if ( buf == NULL ) {
        h5nullArgument( env, "freeCompoundVLStr:  buf is NULL");
        return -1;
    }
    if ( vlIndices == NULL ) {
        h5nullArgument( env, "freeCompoundVLStr:  vlIndices is NULL");
        return -1;
    }

	idxLen = (*env)->GetArrayLength(env, vlIndices);
	bufLen = (*env)->GetArrayLength(env, buf);

    idxP = (*env)->GetPrimitiveArrayCritical(env, vlIndices, NULL);
    if (idxP == NULL) {
        h5JNIFatalError( env, "freeCompoundVLStr:  vlIndices not pinned");
        return -1;
    }
    byteP = (*env)->GetPrimitiveArrayCritical(env, buf, NULL);
    if (byteP == NULL) {
	    (*env)->ReleasePrimitiveArrayCritical(env, vlIndices, idxP, 0);
        h5JNIFatalError( env, "freeCompoundVLStr:  buf not pinned");
        return -1;
    }
    
	ptr = byteP;
	while (ptr - byteP < bufLen)
	{
	    for (i = 0; i < idxLen; ++i)
	    {
	    	strP = (char**) (ptr + idxP[i]);
	        free(*strP);
	    }
	    ptr += recordSize; 
	}
	
    (*env)->ReleasePrimitiveArrayCritical(env, vlIndices, idxP, 0);
    (*env)->ReleasePrimitiveArrayCritical(env, buf, byteP, 0);
	
	return 0;
}