From 65b9e397a706747ba7d087fcc12cd0c2532142e2 Mon Sep 17 00:00:00 2001 From: Kermit Mei Date: Sun, 15 Feb 2009 18:48:38 +0800 Subject: [PATCH] Can merge the dictionary --- src/core/ConfigHolder.h | 10 ++++++-- src/core/Dict.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++------ src/core/Dict.h | 3 +++ src/ui/Cui.cpp | 14 +++++++++++- src/ui/Cui.h | 1 + 5 files changed, 79 insertions(+), 10 deletions(-) diff --git a/src/core/ConfigHolder.h b/src/core/ConfigHolder.h index a72749e..4d6932e 100644 --- a/src/core/ConfigHolder.h +++ b/src/core/ConfigHolder.h @@ -18,7 +18,8 @@ public: const std::string rootDir() const; const std::string tasksDir() const; const std::string mgrFile() const; - const std::string dictFile() const; + const std::string localDictFile() const; + const std::string globalDictFile() const ; const std::string keyFile() const; const std::string doneFile() const; const std::vector *r_list(); @@ -49,11 +50,16 @@ const std::string ConfigHolder::mgrFile() const { } inline -const std::string ConfigHolder::dictFile() const { +const std::string ConfigHolder::localDictFile() const { return rootDirectory + "freeRecite.dict"; } inline +const std::string ConfigHolder::globalDictFile() const { + return "/usr/share/FreeRecite/freeRecite.dict"; +} + +inline const std::string ConfigHolder::keyFile() const { return rootDirectory + "keystone.txt"; } diff --git a/src/core/Dict.cpp b/src/core/Dict.cpp index dbb29c2..8743b98 100644 --- a/src/core/Dict.cpp +++ b/src/core/Dict.cpp @@ -12,19 +12,20 @@ Dict::~Dict() { } bool Dict::load() { - std::string dictName = configHolder.dictFile().c_str(); + std::string dictName = configHolder.localDictFile().c_str(); std::ifstream ifs(dictName.c_str()); - if(!ifs.is_open()) - return false; + while(!ifs.is_open()){ + save(); + ifs.open(dictName.c_str()); + } std::string lineStr; - while(ifs.good()) { - std::getline(ifs,lineStr); + while(std::getline(ifs,lineStr)) { if( dictItem.refer(lineStr) ) dict[dictItem.getW()] = lineStr; } - ifsgdic = new std::ifstream("/usr/share/FreeRecite/freeRecite.dict"); + ifsgdic = new std::ifstream(configHolder.globalDictFile().c_str()); if(!ifsgdic->is_open()) return false; @@ -74,6 +75,52 @@ bool Dict::findInGlobl(const std::string &swatch) { return false; } +bool Dict::merge(const char *newDictName) { + std::ifstream localDic(configHolder.localDictFile().c_str()); + std::ifstream globalDic(configHolder.globalDictFile().c_str()); + std::ofstream tempDic(newDictName); + if(!localDic.is_open() || !globalDic.is_open() || !tempDic.is_open()) + return false; + std::string localLine, globalLine; + std::string localWord, globalWord; + /** + * 1: getline from local. + * 2: getline from global. + * 3: getline from both local and global. + **/ + short int getFromFlag = 3; + do { + if((getFromFlag%2) != 0) { //getFromFlag == 1,3 + if(std::getline(localDic,localLine)) { + localWord = dictItem.refer(localLine) + ? dictItem.getW() : std::string(""); + }else { + while(std::getline(globalDic,globalLine)) + tempDic << globalLine << std::endl; + break; + } + } + if((getFromFlag - 1) > 0) { //getFromFlag == 2,3 + if(std::getline(globalDic,globalLine)) { + globalWord = dictItem.refer(globalLine) + ? dictItem.getW() : std::string(""); + }else { + while(std::getline(globalDic,localLine)) + tempDic << localLine << std::endl; + break; + } + } + if(localWord <= globalWord) { + getFromFlag = 1; + tempDic << localLine << std::endl; + }else { + getFromFlag = 2; + tempDic << globalLine << std::endl; + } + }while(true); + return true; +} + bool Dict::modify(const std::string &item) { static DictItem itemAdd; @@ -89,7 +136,7 @@ bool Dict::modify(const std::string &item) { } bool Dict::save() { - std::ofstream ofs(configHolder.dictFile().c_str()); + std::ofstream ofs(configHolder.localDictFile().c_str()); if(!ofs.is_open()) { return false; } diff --git a/src/core/Dict.h b/src/core/Dict.h index d9e6921..9c0a8a6 100644 --- a/src/core/Dict.h +++ b/src/core/Dict.h @@ -56,6 +56,9 @@ public: //Modify the local dictionary. bool modify(const std::string &item); + //Merge the local and global dictionaries. + bool merge(const char *newDicName); + //Save the local dictionary when you modified bool save(); diff --git a/src/ui/Cui.cpp b/src/ui/Cui.cpp index a4c029d..4365867 100644 --- a/src/ui/Cui.cpp +++ b/src/ui/Cui.cpp @@ -25,7 +25,7 @@ CUI::CUI() exit(EXIT_FAILURE); } if(!dictionary.load()) { - std::cerr << "Can't load " << configHolder.dictFile() << ".\n"; + std::cerr << "Can't load the dictionary.\n"; std::cerr << "You should use command \'frt-init\' to initialize.\n"; exit(EXIT_FAILURE); } @@ -78,6 +78,9 @@ void CUI::run(int argc, char *argv[]) { }else if(!strcmp(argv[1],"new")) { createNew(argv[2]); return; + }else if(!strcmp(argv[1],"merge")) { + merge(argv[2]); + return; }else if(!strcmp(argv[1],"modify")) { modify(std::string(argv[2])); return; @@ -210,6 +213,14 @@ void CUI::exportStress(){ exportFromFile(configHolder.keyFile().c_str()); } +void CUI:: merge(const char *fileName) { + if(!dictionary.merge(fileName)) + std::cerr << "FAIL" << std::endl; + else + std::cerr << "SUCCESS" << std::endl; +} + + void CUI::test(time_t taskID) { Tester tester; if(!manager.hasTask(taskID)) { @@ -534,6 +545,7 @@ void CUI::help() { << std::endl << " new Creat new tasks with the words in the file" << std::endl + << " merge Merge the dictionary into file" << std::endl << " modify [word] Modify the word in the dictionary" << std::endl << " recite Recite the task whose ID is taskID" diff --git a/src/ui/Cui.h b/src/ui/Cui.h index c223383..b07b0d0 100644 --- a/src/ui/Cui.h +++ b/src/ui/Cui.h @@ -26,6 +26,7 @@ private: void exportTask(time_t taskID); void exportStress(); void help(); + void merge(const char *fileName = 0); bool modify(const std::string &word); void recite(const char *fileName); void recite(time_t taskID); -- 2.11.4.GIT