blob: 3ccc053b99d38bc8adfac21656da0834d89b94bc [file] [log] [blame]
Brian Carlstromdd8af232012-05-13 23:56:07 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Steven Moreland3544a932017-07-19 10:26:05 -070017#include <nativehelper/JniConstants.h>
18#include <nativehelper/toStringArray.h>
Brian Carlstromdd8af232012-05-13 23:56:07 -070019
Brian Carlstromdd8af232012-05-13 23:56:07 -070020jobjectArray newStringArray(JNIEnv* env, size_t count) {
21 return env->NewObjectArray(count, JniConstants::stringClass, NULL);
22}
23
Brian Carlstromdd8af232012-05-13 23:56:07 -070024struct ArrayCounter {
25 const char* const* strings;
Chih-Hung Hsieh01332142016-05-02 11:47:43 -070026 explicit ArrayCounter(const char* const* strings) : strings(strings) {}
Brian Carlstromdd8af232012-05-13 23:56:07 -070027 size_t operator()() {
28 size_t count = 0;
29 while (strings[count] != NULL) {
30 ++count;
31 }
32 return count;
33 }
34};
35
36struct ArrayGetter {
37 const char* const* strings;
Chih-Hung Hsieh01332142016-05-02 11:47:43 -070038 explicit ArrayGetter(const char* const* strings) : strings(strings) {}
Brian Carlstromdd8af232012-05-13 23:56:07 -070039 const char* operator()(size_t i) {
40 return strings[i];
41 }
42};
43
44jobjectArray toStringArray(JNIEnv* env, const char* const* strings) {
45 ArrayCounter counter(strings);
46 ArrayGetter getter(strings);
47 return toStringArray(env, &counter, &getter);
48}