Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 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 | |
Steven Moreland | 3544a93 | 2017-07-19 10:26:05 -0700 | [diff] [blame] | 17 | #include <nativehelper/JniConstants.h> |
| 18 | #include <nativehelper/toStringArray.h> |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 19 | |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 20 | jobjectArray newStringArray(JNIEnv* env, size_t count) { |
| 21 | return env->NewObjectArray(count, JniConstants::stringClass, NULL); |
| 22 | } |
| 23 | |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 24 | struct ArrayCounter { |
| 25 | const char* const* strings; |
Chih-Hung Hsieh | 0133214 | 2016-05-02 11:47:43 -0700 | [diff] [blame] | 26 | explicit ArrayCounter(const char* const* strings) : strings(strings) {} |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 27 | 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; |
Chih-Hung Hsieh | 0133214 | 2016-05-02 11:47:43 -0700 | [diff] [blame] | 38 | explicit ArrayGetter(const char* const* strings) : strings(strings) {} |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 39 | const char* operator()(size_t i) { |
| 40 | return strings[i]; |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | jobjectArray toStringArray(JNIEnv* env, const char* const* strings) { |
| 45 | ArrayCounter counter(strings); |
| 46 | ArrayGetter getter(strings); |
| 47 | return toStringArray(env, &counter, &getter); |
| 48 | } |