浏览代码

Android fix: don't make wheels

LouYihua 7 年之前
父节点
当前提交
da2bc5c40d
共有 1 个文件被更改,包括 13 次插入107 次删除
  1. 13 107
      android/jni/src/android_jni.c

+ 13 - 107
android/jni/src/android_jni.c

@@ -17,114 +17,18 @@ char midiInterFile[1024];
 
 static int jstring_to_utf8(JNIEnv *env, jstring j_str, char *buffer, int capacity)
 {
-    jsize pos = 0, length = (*env)->GetStringLength(env, j_str);
-    const jchar * const base = (*env)->GetStringCritical(env, j_str, NULL);
+    jsize length = (*env)->GetStringUTFLength(env, j_str);
+    const char * const base = (*env)->GetStringUTFChars(env, j_str, NULL);
     if (base == NULL)
     {
         return 0;
     }
-    // Convert at char boundary, no incomplete output can be generated
-    for(const jchar *str = base;pos < length && pos < capacity - 1;str++)
-    {
-        if (*str > 4095 && pos < capacity - 3)
-        {
-            buffer[pos++] = 0xe0 | (*str >> 12);
-            buffer[pos++] = 0x80 | ((*str >> 6) & 0x3f);
-            buffer[pos++] = 0x80 | (*str & 0x3f);
-        }
-        else if (*str > 127 && pos < capacity - 2)
-        {
-            buffer[pos++] = 0xc0 | (*str >> 6);
-            buffer[pos++] = 0x80 | (*str & 0x3f);
-        }
-        else if (*str <= 127)
-        {
-            buffer[pos++] = *str;
-        }
-        else
-        {
-            break;
-        }
-    }
-    (*env)->ReleaseStringCritical(env, j_str, base);
-    buffer[pos] = '\0';
-    return pos;
-}
-
-static jstring jstring_from_utf8(JNIEnv *env, const char *str)
-{
-    jstring retval = NULL;
-    jchar *temp = NULL;
-    int wlen = 0, len = strlen(str);
-    // Count length of the UTF-8 string, stop at any error
-    for(int i = 0, state = 0, count = 0;i < len;i++)
-    {
-        if (state == 0)
-        {
-            if (str[i] < 127)
-            {
-                wlen++;
-            }
-            else if (str[i] >= 0xc0 && str[i] < 0xf0)
-            {
-                state = 1;
-                count = (str[i] >> 5) - 5;
-            }
-            else
-            {
-                break;
-            }
-        }
-        else
-        {
-            if (str[i] >= 0x80 && str[i] < 0xc0)
-            {
-                if (count == 0)
-                {
-                    state = 0;
-                    wlen++;
-                }
-                else
-                {
-                    count--;
-                }
-            }
-            else
-            {
-                break;
-            }
-        }
-    }
-    if (wlen == 0)
-    {
-        return (*env)->NewString(env, L"", 0);
-    }
-
-    temp = (jchar *)malloc(wlen * sizeof(jchar));
-    for(int i = 0, j = 0;j < wlen;j++)
-    {
-        if (str[i] > 127)
-        {
-            // Trick here:
-            // 2-byte form: 110x xxxx -> 0xxx xx000000
-            // 3-byte form: 1110 xxxx -> 10xx xx000000
-            temp[j] = (str[i++] & 0x3f) << 6;
-            temp[j] |= str[i++] & 0x3f;
-            if (temp[j] & 0x800)
-            {
-                // 3-byte form, the top-most bit will be dicarded during shift
-                temp[j] <<= 6;
-                temp[j] |= str[i++] & 0x3f;
-            }
-        }
-        else
-        {
-            temp[j] = str[i++];
-        }
-    }
-    retval = (*env)->NewString(env, temp, wlen);
-    free(temp);
-    return retval;
+    if (capacity <= length)
+        length = capacity - 1;
+    strncpy(buffer, base, length);
+    (*env)->ReleaseStringUTFChars(env, j_str, base);
+    base[length] = '\0';
+    return length;
 }
 
 JavaVM *globalVM;
@@ -156,7 +60,7 @@ JNIEnv *getJNIEnv()
 JNIEXPORT void JNICALL Java_io_github_sdlpal_PalActivity_setExternalStorage(JNIEnv *env, jclass cls, jstring j_str)  
 {
     jstring_to_utf8(env, j_str, externalStoragePath, sizeof(externalStoragePath) - 8);
-    strncat(externalStoragePath, "/sdlpal/", 8);
+    strcat(externalStoragePath, "/sdlpal/");
 }
 
 /* 
@@ -166,7 +70,7 @@ JNIEXPORT void JNICALL Java_io_github_sdlpal_PalActivity_setExternalStorage(JNIE
  */  
 JNIEXPORT void JNICALL Java_io_github_sdlpal_PalActivity_setMIDIInterFile(JNIEnv *env, jclass cls, jstring j_str)  
 {
-    jstring_to_utf8(env, j_str, midiInterFile, sizeof(externalStoragePath));
+    jstring_to_utf8(env, j_str, midiInterFile, sizeof(midiInterFile));
     LOGV("JNI got midi inter filename:%s", midiInterFile);
 }
 
@@ -175,7 +79,9 @@ void* JNI_mediaplayer_load(const char *filename)
     JNIEnv *env = getJNIEnv();
     jclass clazz = (*env)->FindClass(env, "io/github/sdlpal/PalActivity");
     jmethodID mid = (*env)->GetStaticMethodID(env, clazz, "JNI_mediaplayer_load", "(Ljava/lang/String;)Landroid/media/MediaPlayer;");
-    jobject player = (*env)->CallStaticObjectMethod(env, clazz, mid, jstring_from_utf8(env, filename));
+    jstring str = (*env)->NewStringUTF(env, filename);
+    jobject player = (*env)->CallStaticObjectMethod(env, clazz, mid, str);
+    (*env)->DeleteLocalRef(env, str);
     return (*env)->NewGlobalRef(env, player);
 }