diff --git a/readme.md b/readme.md index 3f05f23..3550a06 100755 --- a/readme.md +++ b/readme.md @@ -125,6 +125,9 @@ You can run `$INSTALL_DIR/bin/native-platform` to run the test application. ### Improvements +* Cache class, method and field lookups (in particular for String conversions). +* Determine C charset once at startup +* Change readLink() implementation so that it does not need to NULL terminate the encoded content * Implement java_to_char_str() * Use iconv() to convert from C char string to UTF-16 when converting from C char string to Java String. * Support for cygwin terminal diff --git a/src/main/cpp/posix.cpp b/src/main/cpp/posix.cpp index bff0379..94798ff 100755 --- a/src/main/cpp/posix.cpp +++ b/src/main/cpp/posix.cpp @@ -23,11 +23,26 @@ void mark_failed_with_errno(JNIEnv *env, const char* message, jobject result) { mark_failed_with_code(env, message, errno, result); } -char_str* java_to_char_str(JNIEnv *env, jstring string, jobject result) { - return NULL; -} +char* java_to_char(JNIEnv *env, jstring string, jobject result) { + // TODO - share this code with nnn_getSystemInfo() below + locale_t locale = newlocale(LC_CTYPE_MASK, "", NULL); + if (locale == NULL) { + mark_failed_with_message(env, "could not create locale", result); + return NULL; + } -void char_str_free(char_str* str) { + jstring encoding = env->NewStringUTF(nl_langinfo_l(CODESET, locale)); + freelocale(locale); + + jclass strClass = env->FindClass("java/lang/String"); + jmethodID method = env->GetMethodID(strClass, "getBytes", "(Ljava/lang/String;)[B"); + jbyteArray byteArray = (jbyteArray)env->CallObjectMethod(string, method, encoding); + size_t len = env->GetArrayLength(byteArray); + char* chars = (char*)malloc(len + 1); + env->GetByteArrayRegion(byteArray, 0, len, (jbyte*)chars); + chars[len] = 0; + + return chars; } jstring char_to_java(JNIEnv* env, const char* chars, jobject result) { diff --git a/src/main/headers/generic.h b/src/main/headers/generic.h index eb6d03d..d68f67f 100755 --- a/src/main/headers/generic.h +++ b/src/main/headers/generic.h @@ -51,26 +51,12 @@ extern void wchar_str_free(wchar_str* str); */ extern jstring wchar_to_java(JNIEnv* env, const wchar_t* chars, size_t len, jobject result); -typedef struct char_struct { - // NULL terminated - char* chars; - // Number of chars in the string, excluding the NULL terminator - size_t len; - jstring source; - JNIEnv *env; -} char_str; - /* - * Converts the given Java string to a char_str. Should call char_str_free() when finished. + * Converts the given Java string to a char_str. Should call free() when finished. * * Returns NULL on failure. */ -extern char_str* java_to_char_str(JNIEnv *env, jstring string, jobject result); - -/* - * Releases resources used by the given string. - */ -extern void char_str_free(char_str* str); +extern char* java_to_char(JNIEnv *env, jstring string, jobject result); /* * Converts the given NULL terminated char string to a Java string.