diff options
author | Peng Wu <alexepico@gmail.com> | 2016-03-23 16:19:14 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2016-03-23 16:35:35 +0800 |
commit | 7bdb2e261bd6d783003f3fa3796aeca8777b9ca8 (patch) | |
tree | bcdfa69716fa347922a8c9e828b77feacb618825 | |
parent | d563bccd2f70d58ccdf8522d740e72772205c6d7 (diff) | |
download | libpinyin-7bdb2e261bd6d783003f3fa3796aeca8777b9ca8.tar.gz libpinyin-7bdb2e261bd6d783003f3fa3796aeca8777b9ca8.tar.xz libpinyin-7bdb2e261bd6d783003f3fa3796aeca8777b9ca8.zip |
update class FacadeChewingTable
-rw-r--r-- | src/storage/facade_chewing_table.h | 64 |
1 files changed, 51 insertions, 13 deletions
diff --git a/src/storage/facade_chewing_table.h b/src/storage/facade_chewing_table.h index 474311c..1905ffb 100644 --- a/src/storage/facade_chewing_table.h +++ b/src/storage/facade_chewing_table.h @@ -92,41 +92,79 @@ public: /** * FacadeChewingTable::load: * @options: the pinyin options. - * @system: the memory chunk of the system chewing table. - * @user: the memory chunk of the user chewing table. + * @system_filename: the file name of the system chewing table. + * @user_filename: the file name of the user chewing table. * @returns: whether the load operation is successful. * - * Load the system or user chewing table from the memory chunks. + * Load the system or user chewing table from the files. * */ - bool load(pinyin_option_t options, MemoryChunk * system, - MemoryChunk * user){ + bool load(pinyin_option_t options, const char * system_filename, + const char * user_filename){ reset(); + MemoryChunk * chunk = NULL; + bool result = false; - if (system) { + + /* load system chewing table. */ + if (system_filename) { + chunk = new MemoryChunk; + +#ifdef LIBPINYIN_USE_MMAP + if (!chunk->mmap(system_filename)) { + fprintf(stderr, "mmap %s failed!\n", system_filename); + return false; + } +#else + if (!chunk->load(system_filename)) { + fprintf(stderr, "open %s failed!\n", system_filename); + return false; + } +#endif + m_system_chewing_table = new ChewingLargeTable(options); - result = m_system_chewing_table->load(system) || result; + result = m_system_chewing_table->load(chunk) || result; + chunk = NULL; } - if (user) { + + /* load user chewing table */ + if (user_filename) { + chunk = new MemoryChunk; + + if (!chunk->load(user_filename)) { + /* hack here: use local Chewing Table to create empty memory chunk. */ + ChewingLargeTable table(options); + table.store(chunk); + } + m_user_chewing_table = new ChewingLargeTable(options); - result = m_user_chewing_table->load(user) || result; + result = m_user_chewing_table->load(chunk) || result; } + return result; } /** * FacadeChewingTable::store: - * @new_user: the memory chunk to store the user chewing table. + * @new_user_filename: the file name to store the user chewing table. * @returns: whether the store operation is successful. * - * Store the user chewing table to the memory chunk. + * Store the user chewing table to the file. * */ - bool store(MemoryChunk * new_user) { + bool store(const char * new_user_filename) { if (NULL == m_user_chewing_table) return false; - return m_user_chewing_table->store(new_user); + + /* save user chewing table */ + MemoryChunk * chunk = new MemoryChunk; + bool retval = m_user_chewing_table->store(chunk); + assert(retval); + + retval = chunk->save(new_user_filename); + delete chunk; + return retval; } /** |