• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "../include/sync.h"
18 #include <assert.h>
19 #include <string.h>
20 
21 #ifdef ___SYNC_ENABLED___
22 
23 namespace ime_pinyin {
24 
Sync()25 Sync::Sync()
26   : userdict_(NULL),
27     dictfile_(NULL),
28     last_count_(0) {
29 };
30 
~Sync()31 Sync::~Sync() {
32 }
33 
34 
begin(const char * filename)35 bool Sync::begin(const char * filename) {
36   if (userdict_) {
37     finish();
38   }
39 
40   if (!filename) {
41     return false;
42   }
43 
44   dictfile_ = strdup(filename);
45   if (!dictfile_) {
46     return false;
47   }
48 
49   userdict_ = new UserDict();
50   if (!userdict_) {
51     free(dictfile_);
52     dictfile_ = NULL;
53     return false;
54   }
55 
56   if (userdict_->load_dict((const char*)dictfile_, kUserDictIdStart,
57                            kUserDictIdEnd) == false) {
58     delete userdict_;
59     userdict_ = NULL;
60     free(dictfile_);
61     dictfile_ = NULL;
62     return false;
63   }
64 
65   userdict_->set_limit(kUserDictMaxLemmaCount, kUserDictMaxLemmaSize, kUserDictRatio);
66 
67   return true;
68 }
69 
put_lemmas(char16 * lemmas,int len)70 int Sync::put_lemmas(char16 * lemmas, int len) {
71   return userdict_->put_lemmas_no_sync_from_utf16le_string(lemmas, len);
72 }
73 
get_lemmas(char16 * str,int size)74 int Sync::get_lemmas(char16 * str, int size) {
75   return userdict_->get_sync_lemmas_in_utf16le_string_from_beginning(str, size, &last_count_);
76 }
77 
get_last_got_count()78 int Sync::get_last_got_count() {
79   return last_count_;
80 }
81 
get_total_count()82 int Sync::get_total_count() {
83   return userdict_->get_sync_count();
84 }
85 
clear_last_got()86 void Sync::clear_last_got() {
87   if (last_count_ < 0) {
88     return;
89   }
90   userdict_->clear_sync_lemmas(0, last_count_);
91   last_count_ = 0;
92 }
93 
finish()94 void Sync::finish() {
95   if (userdict_) {
96     userdict_->close_dict();
97     delete userdict_;
98     userdict_ = NULL;
99     free(dictfile_);
100     dictfile_ = NULL;
101     last_count_ = 0;
102   }
103 }
104 
get_capacity()105 int Sync::get_capacity() {
106   UserDict::UserDictStat stat;
107   userdict_->state(&stat);
108   return stat.limit_lemma_count - stat.lemma_count;
109 }
110 
111 }
112 #endif
113