diff --git a/src/shared/cpp/generic_posix.cpp b/src/shared/cpp/generic_posix.cpp index b8cc26f..a44a19a 100644 --- a/src/shared/cpp/generic_posix.cpp +++ b/src/shared/cpp/generic_posix.cpp @@ -21,10 +21,7 @@ #include "native.h" #include "generic.h" -#include #include -#include -#include void mark_failed_with_errno(JNIEnv *env, const char* message, jobject result) { const char * errno_message = NULL; @@ -37,47 +34,4 @@ void mark_failed_with_errno(JNIEnv *env, const char* message, jobject result) { mark_failed_with_code(env, message, errno, errno_message, result); } -char* java_to_char(JNIEnv *env, jstring string, jobject result) { - size_t stringLen = env->GetStringLength(string); - wchar_t* wideString = (wchar_t*)malloc(sizeof(wchar_t) * (stringLen+1)); - const jchar* javaString = env->GetStringChars(string, NULL); - for (size_t i = 0; i < stringLen; i++) { - wideString[i] = javaString[i]; - } - wideString[stringLen] = L'\0'; - env->ReleaseStringChars(string, javaString); - - size_t bytes = wcstombs(NULL, wideString, 0); - if (bytes == (size_t)-1) { - mark_failed_with_message(env, "could not convert string to current locale", result); - free(wideString); - return NULL; - } - - char* chars = (char*)malloc(bytes + 1); - wcstombs(chars, wideString, bytes+1); - free(wideString); - - return chars; -} - -jstring char_to_java(JNIEnv* env, const char* chars, jobject result) { - size_t bytes = strlen(chars); - wchar_t* wideString = (wchar_t*)malloc(sizeof(wchar_t) * (bytes+1)); - if (mbstowcs(wideString, chars, bytes+1) == (size_t)-1) { - mark_failed_with_message(env, "could not convert string from current locale", result); - free(wideString); - return NULL; - } - size_t stringLen = wcslen(wideString); - jchar* javaString = (jchar*)malloc(sizeof(jchar) * stringLen); - for (int i =0; i < stringLen; i++) { - javaString[i] = (jchar)wideString[i]; - } - jstring string = env->NewString(javaString, stringLen); - free(wideString); - free(javaString); - return string; -} - #endif diff --git a/src/shared/cpp/osx.cpp b/src/shared/cpp/osx.cpp new file mode 100644 index 0000000..1064043 --- /dev/null +++ b/src/shared/cpp/osx.cpp @@ -0,0 +1,41 @@ +/* + * Copyright 2012 Adam Murdoch + * + * 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. + */ + +/* + * POSIX platform functions. + */ +#ifdef __APPLE__ + +#include "native.h" +#include "generic.h" +#include +#include +#include + +char* java_to_char(JNIEnv *env, jstring string, jobject result) { + size_t len = env->GetStringLength(string); + size_t bytes = env->GetStringUTFLength(string); + char* chars = (char*)malloc(bytes + 1); + env->GetStringUTFRegion(string, 0, len, chars); + chars[bytes] = 0; + return chars; +} + +jstring char_to_java(JNIEnv* env, const char* chars, jobject result) { + return env->NewStringUTF(chars); +} + +#endif diff --git a/src/shared/cpp/unix_strings.cpp b/src/shared/cpp/unix_strings.cpp new file mode 100644 index 0000000..c6aa3fa --- /dev/null +++ b/src/shared/cpp/unix_strings.cpp @@ -0,0 +1,71 @@ +/* + * Copyright 2012 Adam Murdoch + * + * 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. + */ + +/* + * UNIX string conversion functions. + */ +#if defined(__linux__) || defined(__FreeBSD__) + +#include "native.h" +#include "generic.h" +#include +#include +#include + +char* java_to_char(JNIEnv *env, jstring string, jobject result) { + size_t stringLen = env->GetStringLength(string); + wchar_t* wideString = (wchar_t*)malloc(sizeof(wchar_t) * (stringLen+1)); + const jchar* javaString = env->GetStringChars(string, NULL); + for (size_t i = 0; i < stringLen; i++) { + wideString[i] = javaString[i]; + } + wideString[stringLen] = L'\0'; + env->ReleaseStringChars(string, javaString); + + size_t bytes = wcstombs(NULL, wideString, 0); + if (bytes == (size_t)-1) { + mark_failed_with_message(env, "could not convert string to current locale", result); + free(wideString); + return NULL; + } + + char* chars = (char*)malloc(bytes + 1); + wcstombs(chars, wideString, bytes+1); + free(wideString); + + return chars; +} + +jstring char_to_java(JNIEnv* env, const char* chars, jobject result) { + size_t bytes = strlen(chars); + wchar_t* wideString = (wchar_t*)malloc(sizeof(wchar_t) * (bytes+1)); + if (mbstowcs(wideString, chars, bytes+1) == (size_t)-1) { + mark_failed_with_message(env, "could not convert string from current locale", result); + free(wideString); + return NULL; + } + size_t stringLen = wcslen(wideString); + jchar* javaString = (jchar*)malloc(sizeof(jchar) * stringLen); + for (int i =0; i < stringLen; i++) { + javaString[i] = (jchar)wideString[i]; + } + jstring string = env->NewString(javaString, stringLen); + free(wideString); + free(javaString); + return string; +} + +#endif