1 /*
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
17 #include "JniConstants.h"
18 #include "toStringArray.h"
19
newStringArray(JNIEnv * env,size_t count)20 jobjectArray newStringArray(JNIEnv* env, size_t count) {
21 return env->NewObjectArray(count, JniConstants::stringClass, NULL);
22 }
23
24 struct ArrayCounter {
25 const char* const* strings;
ArrayCounterArrayCounter26 explicit ArrayCounter(const char* const* strings) : strings(strings) {}
operator ()ArrayCounter27 size_t operator()() {
28 size_t count = 0;
29 while (strings[count] != NULL) {
30 ++count;
31 }
32 return count;
33 }
34 };
35
36 struct ArrayGetter {
37 const char* const* strings;
ArrayGetterArrayGetter38 explicit ArrayGetter(const char* const* strings) : strings(strings) {}
operator ()ArrayGetter39 const char* operator()(size_t i) {
40 return strings[i];
41 }
42 };
43
toStringArray(JNIEnv * env,const char * const * strings)44 jobjectArray toStringArray(JNIEnv* env, const char* const* strings) {
45 ArrayCounter counter(strings);
46 ArrayGetter getter(strings);
47 return toStringArray(env, &counter, &getter);
48 }
49