File: sync.cpp

package info (click to toggle)
libgooglepinyin 0.1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,640 kB
  • ctags: 841
  • sloc: cpp: 8,256; ansic: 200; makefile: 13
file content (112 lines) | stat: -rw-r--r-- 2,369 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "../include/sync.h"
#include <assert.h>
#include <string.h>

#ifdef ___SYNC_ENABLED___

namespace ime_pinyin {

Sync::Sync()
  : userdict_(NULL),
    dictfile_(NULL),
    last_count_(0) {
};

Sync::~Sync() {
}


bool Sync::begin(const char * filename) {
  if (userdict_) {
    finish();
  }

  if (!filename) {
    return false;
  }

  dictfile_ = strdup(filename);
  if (!dictfile_) {
    return false;
  }

  userdict_ = new UserDict();
  if (!userdict_) {
    free(dictfile_);
    dictfile_ = NULL;
    return false;
  }

  if (userdict_->load_dict((const char*)dictfile_, kUserDictIdStart,
                           kUserDictIdEnd) == false) {
    delete userdict_;
    userdict_ = NULL;
    free(dictfile_);
    dictfile_ = NULL;
    return false;
  }

  userdict_->set_limit(kUserDictMaxLemmaCount, kUserDictMaxLemmaSize, kUserDictRatio);

  return true;
}

int Sync::put_lemmas(char16 * lemmas, int len) {
  return userdict_->put_lemmas_no_sync_from_utf16le_string(lemmas, len);
}

int Sync::get_lemmas(char16 * str, int size) {
  return userdict_->get_sync_lemmas_in_utf16le_string_from_beginning(str, size, &last_count_);
}

int Sync::get_last_got_count() {
  return last_count_;
}

int Sync::get_total_count() {
  return userdict_->get_sync_count();
}

void Sync::clear_last_got() {
  if (last_count_ < 0) {
    return;
  }
  userdict_->clear_sync_lemmas(0, last_count_);
  last_count_ = 0;
}

void Sync::finish() {
  if (userdict_) {
    userdict_->close_dict();
    delete userdict_;
    userdict_ = NULL;
    free(dictfile_);
    dictfile_ = NULL;
    last_count_ = 0;
  }
}

int Sync::get_capacity() {
  UserDict::UserDictStat stat;
  userdict_->state(&stat);
  return stat.limit_lemma_count - stat.lemma_count;
}

}
#endif