PageRenderTime 60ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/java/jni/jni/org_pocketworkstation_pckeyboard_BinaryDictionary.cpp

https://code.google.com/p/hackerskeyboard/
C++ | 191 lines | 134 code | 32 blank | 25 comment | 18 complexity | 914e684c47e5415c891ba2d2606fd8b6 MD5 | raw file
  1. /*
  2. **
  3. ** Copyright 2009, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #include <stdio.h>
  18. #include <assert.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <jni.h>
  22. #include "dictionary.h"
  23. // ----------------------------------------------------------------------------
  24. using namespace latinime;
  25. //
  26. // helper function to throw an exception
  27. //
  28. static void throwException(JNIEnv *env, const char* ex, const char* fmt, int data)
  29. {
  30. if (jclass cls = env->FindClass(ex)) {
  31. char msg[1000];
  32. sprintf(msg, fmt, data);
  33. env->ThrowNew(cls, msg);
  34. env->DeleteLocalRef(cls);
  35. }
  36. }
  37. static jint latinime_BinaryDictionary_open
  38. (JNIEnv *env, jobject object, jobject dictDirectBuffer,
  39. jint typedLetterMultiplier, jint fullWordMultiplier, jint size)
  40. {
  41. void *dict = env->GetDirectBufferAddress(dictDirectBuffer);
  42. if (dict == NULL) {
  43. fprintf(stderr, "DICT: Dictionary buffer is null\n");
  44. return 0;
  45. }
  46. Dictionary *dictionary = new Dictionary(dict, typedLetterMultiplier, fullWordMultiplier, size);
  47. return (jint) dictionary;
  48. }
  49. static int latinime_BinaryDictionary_getSuggestions(
  50. JNIEnv *env, jobject object, jint dict, jintArray inputArray, jint arraySize,
  51. jcharArray outputArray, jintArray frequencyArray, jint maxWordLength, jint maxWords,
  52. jint maxAlternatives, jint skipPos, jintArray nextLettersArray, jint nextLettersSize)
  53. {
  54. Dictionary *dictionary = (Dictionary*) dict;
  55. if (dictionary == NULL) return 0;
  56. int *frequencies = env->GetIntArrayElements(frequencyArray, NULL);
  57. int *inputCodes = env->GetIntArrayElements(inputArray, NULL);
  58. jchar *outputChars = env->GetCharArrayElements(outputArray, NULL);
  59. int *nextLetters = nextLettersArray != NULL ? env->GetIntArrayElements(nextLettersArray, NULL)
  60. : NULL;
  61. int count = dictionary->getSuggestions(inputCodes, arraySize, (unsigned short*) outputChars,
  62. frequencies, maxWordLength, maxWords, maxAlternatives, skipPos, nextLetters,
  63. nextLettersSize);
  64. env->ReleaseIntArrayElements(frequencyArray, frequencies, 0);
  65. env->ReleaseIntArrayElements(inputArray, inputCodes, JNI_ABORT);
  66. env->ReleaseCharArrayElements(outputArray, outputChars, 0);
  67. if (nextLetters) {
  68. env->ReleaseIntArrayElements(nextLettersArray, nextLetters, 0);
  69. }
  70. return count;
  71. }
  72. static int latinime_BinaryDictionary_getBigrams
  73. (JNIEnv *env, jobject object, jint dict, jcharArray prevWordArray, jint prevWordLength,
  74. jintArray inputArray, jint inputArraySize, jcharArray outputArray,
  75. jintArray frequencyArray, jint maxWordLength, jint maxBigrams, jint maxAlternatives)
  76. {
  77. Dictionary *dictionary = (Dictionary*) dict;
  78. if (dictionary == NULL) return 0;
  79. jchar *prevWord = env->GetCharArrayElements(prevWordArray, NULL);
  80. int *inputCodes = env->GetIntArrayElements(inputArray, NULL);
  81. jchar *outputChars = env->GetCharArrayElements(outputArray, NULL);
  82. int *frequencies = env->GetIntArrayElements(frequencyArray, NULL);
  83. int count = dictionary->getBigrams((unsigned short*) prevWord, prevWordLength, inputCodes,
  84. inputArraySize, (unsigned short*) outputChars, frequencies, maxWordLength, maxBigrams,
  85. maxAlternatives);
  86. env->ReleaseCharArrayElements(prevWordArray, prevWord, JNI_ABORT);
  87. env->ReleaseIntArrayElements(inputArray, inputCodes, JNI_ABORT);
  88. env->ReleaseCharArrayElements(outputArray, outputChars, 0);
  89. env->ReleaseIntArrayElements(frequencyArray, frequencies, 0);
  90. return count;
  91. }
  92. static jboolean latinime_BinaryDictionary_isValidWord
  93. (JNIEnv *env, jobject object, jint dict, jcharArray wordArray, jint wordLength)
  94. {
  95. Dictionary *dictionary = (Dictionary*) dict;
  96. if (dictionary == NULL) return (jboolean) false;
  97. jchar *word = env->GetCharArrayElements(wordArray, NULL);
  98. jboolean result = dictionary->isValidWord((unsigned short*) word, wordLength);
  99. env->ReleaseCharArrayElements(wordArray, word, JNI_ABORT);
  100. return result;
  101. }
  102. static void latinime_BinaryDictionary_close
  103. (JNIEnv *env, jobject object, jint dict)
  104. {
  105. Dictionary *dictionary = (Dictionary*) dict;
  106. delete (Dictionary*) dict;
  107. }
  108. // ----------------------------------------------------------------------------
  109. static JNINativeMethod gMethods[] = {
  110. {"openNative", "(Ljava/nio/ByteBuffer;III)I",
  111. (void*)latinime_BinaryDictionary_open},
  112. {"closeNative", "(I)V", (void*)latinime_BinaryDictionary_close},
  113. {"getSuggestionsNative", "(I[II[C[IIIII[II)I", (void*)latinime_BinaryDictionary_getSuggestions},
  114. {"isValidWordNative", "(I[CI)Z", (void*)latinime_BinaryDictionary_isValidWord},
  115. {"getBigramsNative", "(I[CI[II[C[IIII)I", (void*)latinime_BinaryDictionary_getBigrams}
  116. };
  117. static int registerNativeMethods(JNIEnv* env, const char* className,
  118. JNINativeMethod* gMethods, int numMethods)
  119. {
  120. jclass clazz;
  121. clazz = env->FindClass(className);
  122. if (clazz == NULL) {
  123. fprintf(stderr,
  124. "Native registration unable to find class '%s'\n", className);
  125. return JNI_FALSE;
  126. }
  127. if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
  128. fprintf(stderr, "RegisterNatives failed for '%s'\n", className);
  129. return JNI_FALSE;
  130. }
  131. return JNI_TRUE;
  132. }
  133. static int registerNatives(JNIEnv *env)
  134. {
  135. const char* const kClassPathName = "org/pocketworkstation/pckeyboard/BinaryDictionary";
  136. return registerNativeMethods(env,
  137. kClassPathName, gMethods, sizeof(gMethods) / sizeof(gMethods[0]));
  138. }
  139. /*
  140. * Returns the JNI version on success, -1 on failure.
  141. */
  142. jint JNI_OnLoad(JavaVM* vm, void* reserved)
  143. {
  144. JNIEnv* env = NULL;
  145. jint result = -1;
  146. if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
  147. fprintf(stderr, "ERROR: GetEnv failed\n");
  148. goto bail;
  149. }
  150. assert(env != NULL);
  151. if (!registerNatives(env)) {
  152. fprintf(stderr, "ERROR: BinaryDictionary native registration failed\n");
  153. goto bail;
  154. }
  155. /* success -- return valid version number */
  156. result = JNI_VERSION_1_4;
  157. bail:
  158. return result;
  159. }