/* * Insert a word and it's interpretation into the diction tree。 * root初始指向树根,word是要插入的单词,inter指向单词对应的意思。 */ void InsertDict(TrieNode *root, char *word, char *inter) { int wordLen = strlen(word); int index; /* * 单词长度为0的则无须插入,直接返回。 */ if (wordLen == 0) return; /* * 正在处理的字母对应的ascii码值即为它在child分量中的索引。 */ index = (int)(*word); if (wordLen > 1) { if (root->child[index] != NULL) InsertDict(root->child[index], ++word, inter); else { root->child[index] = (TrieNode*)calloc(1, sizeof(TrieNode)); InsertDict(root->child[index], ++word, inter); } } else if (wordLen == 1){//单词长度变为1时要特殊处理,因为要保存对应的含义了。 if (root->child[index] != NULL) { if (root->child[index]->inter == NULL) { root->child[index]->inter = (char*)calloc(strlen(inter) + 1, sizeof(char)); if (root->child[index]->inter == NULL) { fprintf(stderr, "FATAL ERROR: malloc new TrieNode->inter(calloc)\n"); exit(1); } strcpy(root->child[index]->inter, inter); return; } else { return; } } else { root->child[index] = (TrieNode*)calloc(1, sizeof(TrieNode)); if (root->child[index] == NULL) { fprintf(stderr, "FATAL ERROR: malloc new TrieNode(calloc)\n"); exit(1); } root->child[index]->inter = (char*)calloc(strlen(inter) + 1, sizeof(char)); if (root->child[index]->inter == NULL) { fprintf(stderr, "FATAL ERROR: malloc new TrieNode->inter(calloc)\n"); exit(1); } strcpy(root->child[index]->inter, inter); } } }
bool Dictionaries::addFromFile(std::string* filename) { IDictionaryParser* parser = new DictionaryParser; IFileReader* reader = new FileReader;//FIX ME IFiledataValidator* validator = new FiledataValidator; std::vector<std::string*>* filedata = reader->Read(filename); ReturnedData data(filedata); bool is_Success = validator->is_valid(&data); if (!is_Success) { delete parser; delete reader;//FIX ME delete validator; return false; } Dictionaries* dict = parser->pars((filedata)); InsertDict(dict); delete parser; delete reader;//FIX ME delete validator; return true; }
bool CDatabase::InsertDict(std::string sKey,int nValue) { char buf[100]={0}; // itoa(nValue,buf,10); // _itoa(nValue,buf,10); _itoa_s(nValue,buf,100,10); std::string sValue = std::string(buf); return InsertDict(sKey,sValue); }
bool CDatabase::BackupDB(std::string sFileName) { std::string newFileName(sFileName); newFileName += ".bak"; if(::CopyFile(CString(sFileName.c_str()),CString(newFileName.c_str()),FALSE)) { DeleteDict(LastBackupKey); InsertDict(LastBackupKey,(int)time(NULL)); return true; } return false; }
/* * 读取原始文件创建字典,最后返回字典树的根节点。 * 内部调用上面的InsertDict函数。 */ TrieNode * CreateDict() { FILE *fp = NULL; char word[300], inter[300]; size_t wordNumber = 0; /* * 创建字典树根节点。 */ TrieNode *root = (TrieNode*)calloc(1, sizeof(TrieNode)); if (!root) { fprintf(stderr, "ERROR: create root error.(calloc)\n"); exit(1); } /* * 打开同一个目录下的原始文件。 */ fp = fopen("raw-dict", "r"); if (!fp) { fprintf(stderr, "FATAL ERROR: raw-dict not exist\n"); exit(1); } /* * 读取原始文件,单词放到word数组中,对应的中文意思放到inter数组中。 */ while (fgets(word, sizeof(word), fp) && fgets(inter, sizeof(word), fp)) { /* * 插入到字典中。 */ word[strlen(word) - 1] = '\0'; inter[strlen(inter) - 1] = '\0'; //printf("%s %s\n", word, inter); wordNumber++; InsertDict(root, word, inter); } fclose(fp); printf("*****Total number of words is %u.*****\n", wordNumber); return root; }
bool CDatabase::InsertDict(std::string sKey,bool nValue) { return InsertDict(sKey,(int)nValue); }
bool CDatabase::InsertDict(std::string sKey,DWORD dwValue) { return InsertDict(sKey,(int)dwValue); }
//打开数据库文件 bool CDatabase::OpenDB(std::string sFileName) { //先关闭连接 CloseDB(); _pDB = new CppSQLite3DB(); try { _pDB->open(sFileName.c_str()); } catch(CppSQLite3Exception e) { return false; } catch(...) { return false; } //检测是否成功打开 if(!IsOpen()) return false; try { //词典是否存在 if(!IsExistTable(DicTableName)) {//不存在词典,则新建词典 static const char sSql[] = "CREATE TABLE %Q (key text primary key,value text,comment text)"; if(SQLERROR == this->ExecSql(sSql,DicTableName)) goto _OnError; } if(!OnCreateTables()) goto _OnError; //取得数据库文件版本 const int OldDBVersion = GetDBVersion(); if(OldDBVersion > DBVersion) { goto _OnError; } if(OldDBVersion < DBVersion) { if(!BackupDB(sFileName)) goto _OnError; if(!OnUpdateTables(OldDBVersion,DBVersion)) goto _OnError; DeleteDict(DBVersionKey); if(!InsertDict(DBVersionKey,DBVersion)) goto _OnError; } else { int nLastBackupTime = 0; if(GetDictValue(LastBackupKey,nLastBackupTime)) { int nOffTime = (int)time(NULL) - nLastBackupTime; if(nOffTime > BackupDBRate) BackupDB(sFileName); } } }catch(CppSQLite3Exception e) { assert(false); goto _OnError; } catch(...) { assert(false); goto _OnError; } return true; _OnError: { this->CloseDB(); return false; } }