summaryrefslogtreecommitdiffstats
path: root/src/storage
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2013-04-07 16:25:06 +0800
committerPeng Wu <alexepico@gmail.com>2013-04-07 16:25:06 +0800
commitc7e3226b3090e7a54c16bcf0fe0b8f23f6cc2122 (patch)
treecbf39d047bd665dfc9e5698f95e95275d699f0e3 /src/storage
parent92df059d779e886d02b8a77d00fea8af297578cb (diff)
downloadlibpinyin-c7e3226b3090e7a54c16bcf0fe0b8f23f6cc2122.tar.gz
libpinyin-c7e3226b3090e7a54c16bcf0fe0b8f23f6cc2122.tar.xz
libpinyin-c7e3226b3090e7a54c16bcf0fe0b8f23f6cc2122.zip
write table info in progress
Diffstat (limited to 'src/storage')
-rw-r--r--src/storage/phrase_index.h2
-rw-r--r--src/storage/table_info.cpp141
-rw-r--r--src/storage/table_info.h7
3 files changed, 149 insertions, 1 deletions
diff --git a/src/storage/phrase_index.h b/src/storage/phrase_index.h
index 6a14ff7..0ecfbec 100644
--- a/src/storage/phrase_index.h
+++ b/src/storage/phrase_index.h
@@ -839,7 +839,7 @@ typedef enum {
} PHRASE_FILE_TYPE;
typedef struct {
- const PHRASE_INDEX_LIBRARIES m_dict_index; /* for assert purpose. */
+ const guint8 m_dict_index; /* for assert purpose. */
const char * m_table_filename;
const char * m_system_filename;
const char * m_user_filename;
diff --git a/src/storage/table_info.cpp b/src/storage/table_info.cpp
index e531c55..536b4a2 100644
--- a/src/storage/table_info.cpp
+++ b/src/storage/table_info.cpp
@@ -20,6 +20,8 @@
*/
#include "table_info.h"
+#include <stdio.h>
+#include <assert.h>
using namespace pinyin;
@@ -33,3 +35,142 @@ static const pinyin_table_info_t reserved_tables[] = {
{USER_DICTIONARY, NULL, NULL, "user.bin", USER_FILE}
};
+
+
+SystemTableInfo::SystemTableInfo() {
+ m_binary_format_version = 0;
+ m_model_data_version = 0;
+ m_lambda = 0.;
+
+ size_t i;
+ for (i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
+ pinyin_table_info_t * tableinfo = &m_table_info[i];
+
+ tableinfo->m_dict_index = i;
+ tableinfo->m_table_filename = NULL;
+ tableinfo->m_system_filename = NULL;
+ tableinfo->m_user_filename = NULL;
+ tableinfo->m_file_type = NOT_USED;
+ }
+}
+
+SystemTableInfo::~SystemTableInfo() {
+ reset();
+}
+
+void SystemTableInfo::reset() {
+ m_binary_format_version = 0;
+ m_model_data_version = 0;
+ m_lambda = 0.;
+
+ size_t i;
+ for (i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
+ pinyin_table_info_t * tableinfo = &m_table_info[i];
+
+ g_free(tableinfo->m_table_filename);
+ tableinfo->m_table_filename = NULL;
+ g_free(tableinfo->m_system_filename);
+ tableinfo->m_system_filename = NULL;
+ g_free(tableinfo->m_user_filename);
+ tableinfo->m_user_filename = NULL;
+ tableinfo->m_file_type = NOT_USED;
+ }
+}
+
+void SystemTableInfo::postfix_tables() {
+ size_t i;
+ for (i = 0; i < G_N_ELEMENTS(reserved_tables); ++i) {
+ pinyin_table_info_t * postfix = &reserved_tables[i];
+
+ guint8 index = postfix->m_dict_index;
+ pinyin_table_info_t * tableinfo = &m_table_info[index];
+ assert(tableinfo->m_dict_index == index);
+
+ tableinfo->m_table_filename = g_strdup(postfix->m_table_filename);
+ tableinfo->m_system_filename = g_strdup(postfix->m_system_filename);
+ tableinfo->m_user_filename = g_strdup(postfix->m_user_filename);
+ tableinfo->m_file_type = postfix->m_file_type;
+ }
+}
+
+static gchar * to_string(const char * str) {
+ if (0 == strcmp(str, "NULL"))
+ return NULL;
+
+ return g_strdup(str);
+}
+
+static PHRASE_FILE_TYPE to_file_type(const char * str) {
+#define HANDLE(x) { \
+ if (0 == strcmp(str, #x)) \
+ return x; \
+ }
+
+ HANDLE(NOT_USED);
+ HANDLE(SYSTEM_FILE);
+ HANDLE(DICTIONARY);
+ HANDLE(USER_FILE);
+
+ assert(false);
+
+#undef HANDLE
+}
+
+bool SystemTableInfo::load(const char * filename) {
+ reset();
+
+ FILE * input = fopen(filename, "r");
+ if (NULL == input) {
+ fprintf(stderr, "open %s failed.\n", filename);
+ return false;
+ }
+
+ int binver = 0, modelver = 0;
+ gfloat lambda = 0.;
+
+ int num = fscanf(input, "binary format version:%d", &binver);
+ if (1 != num)
+ return false;
+
+ num = fscanf(input, "model data version:%d", &modelver);
+ if (1 != num)
+ return false;
+
+ num = fscanf(input, "lambda parameter:%f", &lambda);
+ if (1 != num)
+ return false;
+
+#if 0
+ printf("binver:%d modelver:%d lambda:%f", binver, modelver, lambda);
+#endif
+
+ m_binary_format_version = binver;
+ m_model_data_version = modelver;
+ m_lambda = lambda;
+
+ int index = 0;
+ char tablefile[256], sysfile[256], userfile[256], filetype[256];
+ while (!feof(infile)){
+ num = fscanf("%d %s %s %s %s",
+ &index, tablefile, sysfile, userfile, filetype);
+
+ if (5 != num)
+ continue;
+
+ if (!(0 <= index && index < PHRASE_INDEX_LIBRARY_COUNT))
+ continue;
+
+ /* save into m_table_info. */
+ pinyin_table_info_t * tableinfo = &m_table_info[index];
+ assert(index == tableinfo->m_dict_index);
+
+ table_info->m_table_filename = to_string(tablefile);
+ table_info->m_system_filename = to_string(sysfile);
+ table_info->m_user_filename = to_string(userfile);
+
+ table_info->m_file_type = to_file_type(filetype);
+ }
+
+ postfix_tables();
+ return true;
+}
diff --git a/src/storage/table_info.h b/src/storage/table_info.h
index 6adc1af..dc1c474 100644
--- a/src/storage/table_info.h
+++ b/src/storage/table_info.h
@@ -35,9 +35,16 @@ private:
pinyin_table_info_t m_table_info[PHRASE_INDEX_LIBRARY_COUNT];
+private:
+ void reset();
+
+ void postfix_tables();
+
public:
SystemTableInfo();
+ ~SystemTableInfo();
+
bool load(const char * filename);
pinyin_table_info_t * get_table_info();