implemented basic char_to_java()

This commit is contained in:
Adam Murdoch
2012-09-09 14:17:58 +10:00
parent 7e2a573ff2
commit 208d1be584
3 changed files with 24 additions and 20 deletions

View File

@@ -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

View File

@@ -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) {

View File

@@ -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.