Just code cleanup.

Change-Id: I796b47aa168b3b2cc29de65fc7dc5dab5a7f4b17
main
Ken Wakasa 2010-12-03 16:51:14 +09:00
parent 7545ec8df0
commit a46683a47b
1 changed files with 30 additions and 46 deletions

View File

@ -15,13 +15,11 @@
** limitations under the License. ** limitations under the License.
*/ */
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <jni.h>
#include "dictionary.h" #include "dictionary.h"
#include "jni.h"
#include <assert.h>
#include <stdio.h>
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -30,8 +28,7 @@ using namespace latinime;
// //
// helper function to throw an exception // helper function to throw an exception
// //
static void throwException(JNIEnv *env, const char* ex, const char* fmt, int data) static void throwException(JNIEnv *env, const char* ex, const char* fmt, int data) {
{
if (jclass cls = env->FindClass(ex)) { if (jclass cls = env->FindClass(ex)) {
char msg[1000]; char msg[1000];
sprintf(msg, fmt, data); sprintf(msg, fmt, data);
@ -40,11 +37,9 @@ static void throwException(JNIEnv *env, const char* ex, const char* fmt, int dat
} }
} }
static jint latinime_BinaryDictionary_open static jint latinime_BinaryDictionary_open(JNIEnv *env, jobject object, jobject dictDirectBuffer,
(JNIEnv *env, jobject object, jobject dictDirectBuffer,
jint typedLetterMultiplier, jint fullWordMultiplier, jint maxWordLength, jint maxWords, jint typedLetterMultiplier, jint fullWordMultiplier, jint maxWordLength, jint maxWords,
jint maxAlternatives) jint maxAlternatives) {
{
void *dict = env->GetDirectBufferAddress(dictDirectBuffer); void *dict = env->GetDirectBufferAddress(dictDirectBuffer);
if (dict == NULL) { if (dict == NULL) {
fprintf(stderr, "DICT: Dictionary buffer is null\n"); fprintf(stderr, "DICT: Dictionary buffer is null\n");
@ -55,11 +50,9 @@ static jint latinime_BinaryDictionary_open
return (jint) dictionary; return (jint) dictionary;
} }
static int latinime_BinaryDictionary_getSuggestions( static int latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jobject object, jint dict,
JNIEnv *env, jobject object, jint dict, jintArray inputArray, jint arraySize, jintArray inputArray, jint arraySize, jcharArray outputArray, jintArray frequencyArray,
jcharArray outputArray, jintArray frequencyArray, jintArray nextLettersArray, jint nextLettersSize) {
jintArray nextLettersArray, jint nextLettersSize)
{
Dictionary *dictionary = (Dictionary*) dict; Dictionary *dictionary = (Dictionary*) dict;
if (dictionary == NULL) return 0; if (dictionary == NULL) return 0;
@ -82,11 +75,10 @@ static int latinime_BinaryDictionary_getSuggestions(
return count; return count;
} }
static int latinime_BinaryDictionary_getBigrams static int latinime_BinaryDictionary_getBigrams(JNIEnv *env, jobject object, jint dict,
(JNIEnv *env, jobject object, jint dict, jcharArray prevWordArray, jint prevWordLength, jcharArray prevWordArray, jint prevWordLength, jintArray inputArray, jint inputArraySize,
jintArray inputArray, jint inputArraySize, jcharArray outputArray, jcharArray outputArray, jintArray frequencyArray, jint maxWordLength, jint maxBigrams,
jintArray frequencyArray, jint maxWordLength, jint maxBigrams, jint maxAlternatives) jint maxAlternatives) {
{
Dictionary *dictionary = (Dictionary*) dict; Dictionary *dictionary = (Dictionary*) dict;
if (dictionary == NULL) return 0; if (dictionary == NULL) return 0;
@ -108,9 +100,8 @@ static int latinime_BinaryDictionary_getBigrams
} }
static jboolean latinime_BinaryDictionary_isValidWord static jboolean latinime_BinaryDictionary_isValidWord(JNIEnv *env, jobject object, jint dict,
(JNIEnv *env, jobject object, jint dict, jcharArray wordArray, jint wordLength) jcharArray wordArray, jint wordLength) {
{
Dictionary *dictionary = (Dictionary*) dict; Dictionary *dictionary = (Dictionary*) dict;
if (dictionary == NULL) return (jboolean) false; if (dictionary == NULL) return (jboolean) false;
@ -121,32 +112,27 @@ static jboolean latinime_BinaryDictionary_isValidWord
return result; return result;
} }
static void latinime_BinaryDictionary_close static void latinime_BinaryDictionary_close(JNIEnv *env, jobject object, jint dict) {
(JNIEnv *env, jobject object, jint dict)
{
delete (Dictionary*) dict; delete (Dictionary*) dict;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
static JNINativeMethod gMethods[] = { static JNINativeMethod gMethods[] = {
{"openNative", "(Ljava/nio/ByteBuffer;IIIII)I", {"openNative", "(Ljava/nio/ByteBuffer;IIIII)I", (void*)latinime_BinaryDictionary_open},
(void*)latinime_BinaryDictionary_open},
{"closeNative", "(I)V", (void*)latinime_BinaryDictionary_close}, {"closeNative", "(I)V", (void*)latinime_BinaryDictionary_close},
{"getSuggestionsNative", "(I[II[C[I[II)I", (void*)latinime_BinaryDictionary_getSuggestions}, {"getSuggestionsNative", "(I[II[C[I[II)I", (void*)latinime_BinaryDictionary_getSuggestions},
{"isValidWordNative", "(I[CI)Z", (void*)latinime_BinaryDictionary_isValidWord}, {"isValidWordNative", "(I[CI)Z", (void*)latinime_BinaryDictionary_isValidWord},
{"getBigramsNative", "(I[CI[II[C[IIII)I", (void*)latinime_BinaryDictionary_getBigrams} {"getBigramsNative", "(I[CI[II[C[IIII)I", (void*)latinime_BinaryDictionary_getBigrams}
}; };
static int registerNativeMethods(JNIEnv* env, const char* className, static int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* gMethods,
JNINativeMethod* gMethods, int numMethods) int numMethods) {
{
jclass clazz; jclass clazz;
clazz = env->FindClass(className); clazz = env->FindClass(className);
if (clazz == NULL) { if (clazz == NULL) {
fprintf(stderr, fprintf(stderr, "Native registration unable to find class '%s'\n", className);
"Native registration unable to find class '%s'\n", className);
return JNI_FALSE; return JNI_FALSE;
} }
if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) { if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
@ -157,18 +143,16 @@ static int registerNativeMethods(JNIEnv* env, const char* className,
return JNI_TRUE; return JNI_TRUE;
} }
static int registerNatives(JNIEnv *env) static int registerNatives(JNIEnv *env) {
{
const char* const kClassPathName = "com/android/inputmethod/latin/BinaryDictionary"; const char* const kClassPathName = "com/android/inputmethod/latin/BinaryDictionary";
return registerNativeMethods(env, return registerNativeMethods(env, kClassPathName, gMethods,
kClassPathName, gMethods, sizeof(gMethods) / sizeof(gMethods[0])); sizeof(gMethods) / sizeof(gMethods[0]));
} }
/* /*
* Returns the JNI version on success, -1 on failure. * Returns the JNI version on success, -1 on failure.
*/ */
jint JNI_OnLoad(JavaVM* vm, void* reserved) jint JNI_OnLoad(JavaVM* vm, void* reserved) {
{
JNIEnv* env = NULL; JNIEnv* env = NULL;
jint result = -1; jint result = -1;