1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdlib.h>
18#include "../include/pinyinime.h"
19#include "../include/dicttrie.h"
20#include "../include/matrixsearch.h"
21#include "../include/spellingtrie.h"
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27  using namespace ime_pinyin;
28
29  // The maximum number of the prediction items.
30  static const size_t kMaxPredictNum = 500;
31
32  // Used to search Pinyin string and give the best candidate.
33  MatrixSearch* matrix_search = NULL;
34
35  char16 predict_buf[kMaxPredictNum][kMaxPredictSize + 1];
36
37  bool im_open_decoder(const char *fn_sys_dict, const char *fn_usr_dict) {
38    if (NULL != matrix_search)
39      delete matrix_search;
40
41    matrix_search = new MatrixSearch();
42    if (NULL == matrix_search) {
43      return false;
44    }
45
46    return matrix_search->init(fn_sys_dict, fn_usr_dict);
47  }
48
49  bool im_open_decoder_fd(int sys_fd, long start_offset, long length,
50                          const char *fn_usr_dict) {
51    if (NULL != matrix_search)
52      delete matrix_search;
53
54    matrix_search = new MatrixSearch();
55    if (NULL == matrix_search)
56      return false;
57
58    return matrix_search->init_fd(sys_fd, start_offset, length, fn_usr_dict);
59  }
60
61  void im_close_decoder() {
62    if (NULL != matrix_search) {
63      matrix_search->close();
64      delete matrix_search;
65    }
66    matrix_search = NULL;
67  }
68
69  void im_set_max_lens(size_t max_sps_len, size_t max_hzs_len) {
70    if (NULL != matrix_search) {
71      matrix_search->set_max_lens(max_sps_len, max_hzs_len);
72    }
73  }
74
75  void im_flush_cache() {
76    if (NULL != matrix_search)
77      matrix_search->flush_cache();
78  }
79
80  // To be updated.
81  size_t im_search(const char* pybuf, size_t pylen) {
82    if (NULL == matrix_search)
83      return 0;
84
85    matrix_search->search(pybuf, pylen);
86    return matrix_search->get_candidate_num();
87  }
88
89  size_t im_delsearch(size_t pos, bool is_pos_in_splid,
90                      bool clear_fixed_this_step) {
91    if (NULL == matrix_search)
92      return 0;
93    matrix_search->delsearch(pos, is_pos_in_splid, clear_fixed_this_step);
94    return matrix_search->get_candidate_num();
95  }
96
97  void im_reset_search() {
98    if (NULL == matrix_search)
99      return;
100
101    matrix_search->reset_search();
102  }
103
104  // To be removed
105  size_t im_add_letter(char ch) {
106    return 0;
107  }
108
109  const char* im_get_sps_str(size_t *decoded_len) {
110    if (NULL == matrix_search)
111      return NULL;
112
113    return matrix_search->get_pystr(decoded_len);
114  }
115
116  char16* im_get_candidate(size_t cand_id, char16* cand_str,
117                        size_t max_len) {
118    if (NULL == matrix_search)
119      return NULL;
120
121    return matrix_search->get_candidate(cand_id, cand_str, max_len);
122  }
123
124  size_t im_get_spl_start_pos(const uint16 *&spl_start) {
125    if (NULL == matrix_search)
126      return 0;
127
128    return matrix_search->get_spl_start(spl_start);
129  }
130
131  size_t im_choose(size_t choice_id) {
132    if (NULL == matrix_search)
133      return 0;
134
135    return matrix_search->choose(choice_id);
136  }
137
138  size_t im_cancel_last_choice() {
139    if (NULL == matrix_search)
140      return 0;
141
142    return matrix_search->cancel_last_choice();
143  }
144
145  size_t im_get_fixed_len() {
146    if (NULL == matrix_search)
147      return 0;
148
149    return matrix_search->get_fixedlen();
150  }
151
152  // To be removed
153  bool im_cancel_input() {
154    return true;
155  }
156
157
158  size_t im_get_predicts(const char16 *his_buf,
159                         char16 (*&pre_buf)[kMaxPredictSize + 1]) {
160    if (NULL == his_buf)
161      return 0;
162
163    size_t fixed_len = utf16_strlen(his_buf);
164    const char16 *fixed_ptr = his_buf;
165    if (fixed_len > kMaxPredictSize) {
166      fixed_ptr += fixed_len - kMaxPredictSize;
167      fixed_len = kMaxPredictSize;
168    }
169
170    pre_buf = predict_buf;
171    return matrix_search->get_predicts(his_buf, pre_buf, kMaxPredictNum);
172  }
173
174  void im_enable_shm_as_szm(bool enable) {
175    SpellingTrie &spl_trie = SpellingTrie::get_instance();
176    spl_trie.szm_enable_shm(enable);
177  }
178
179  void im_enable_ym_as_szm(bool enable) {
180    SpellingTrie &spl_trie = SpellingTrie::get_instance();
181    spl_trie.szm_enable_ym(enable);
182  }
183
184#ifdef __cplusplus
185}
186#endif
187