aboutsummaryrefslogtreecommitdiffstats
path: root/native/jni/src/utils/jni_data_utils.h
blob: 2ce02dc0565155a5dd866b5ffb5c6a1399a01565 (about) (plain) (blame)
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
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * 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.
 */

#ifndef LATINIME_JNI_DATA_UTILS_H
#define LATINIME_JNI_DATA_UTILS_H

#include <vector>

#include "defines.h"
#include "jni.h"
#include "suggest/core/policy/dictionary_header_structure_policy.h"
#include "suggest/policyimpl/dictionary/header/header_read_write_utils.h"

namespace latinime {

class JniDataUtils {
 public:
    static void jintarrayToVector(JNIEnv *env, jintArray array, std::vector<int> *const outVector) {
        if (!array) {
            outVector->clear();
            return;
        }
        const jsize arrayLength = env->GetArrayLength(array);
        outVector->resize(arrayLength);
        env->GetIntArrayRegion(array, 0 /* start */, arrayLength, outVector->data());
    }

    static DictionaryHeaderStructurePolicy::AttributeMap constructAttributeMap(JNIEnv *env,
            jobjectArray attributeKeyStringArray, jobjectArray attributeValueStringArray) {
        DictionaryHeaderStructurePolicy::AttributeMap attributeMap;
        const int keyCount = env->GetArrayLength(attributeKeyStringArray);
        for (int i = 0; i < keyCount; i++) {
            jstring keyString = static_cast<jstring>(
                    env->GetObjectArrayElement(attributeKeyStringArray, i));
            const jsize keyUtf8Length = env->GetStringUTFLength(keyString);
            char keyChars[keyUtf8Length + 1];
            env->GetStringUTFRegion(keyString, 0, env->GetStringLength(keyString), keyChars);
            keyChars[keyUtf8Length] = '\0';
            DictionaryHeaderStructurePolicy::AttributeMap::key_type key;
            HeaderReadWriteUtils::insertCharactersIntoVector(keyChars, &key);

            jstring valueString = static_cast<jstring>(
                    env->GetObjectArrayElement(attributeValueStringArray, i));
            const jsize valueUtf8Length = env->GetStringUTFLength(valueString);
            char valueChars[valueUtf8Length + 1];
            env->GetStringUTFRegion(valueString, 0, env->GetStringLength(valueString), valueChars);
            valueChars[valueUtf8Length] = '\0';
            DictionaryHeaderStructurePolicy::AttributeMap::mapped_type value;
            HeaderReadWriteUtils::insertCharactersIntoVector(valueChars, &value);
            attributeMap[key] = value;
        }
        return attributeMap;
    }

 private:
    DISALLOW_IMPLICIT_CONSTRUCTORS(JniDataUtils);
};
} // namespace latinime
#endif // LATINIME_JNI_DATA_UTILS_H