6 #include "ConfigHolder.h"
21 std::string dictName
= configHolder
.localDictFile().c_str();
22 std::ifstream
ifs(dictName
.c_str());
23 while(!ifs
.is_open()){
26 ifs
.open(dictName
.c_str());
30 while(std::getline(ifs
,lineStr
)) {
31 if( dictItem
.refer(lineStr
) )
32 if(dict
[dictItem
.getW()].size() <= lineStr
.size())
33 dict
[dictItem
.getW()] = lineStr
;
36 ifsgdic
= new std::ifstream(configHolder
.globalDictFile().c_str());
37 if(!ifsgdic
->is_open())
43 bool Dict::lookUp(const std::string
&word
) {
44 if(validFlag
== false)
46 //Find in local dictionary.
47 std::map
<std::string
,std::string
>::iterator itr
= dict
.find(word
);
48 if( itr
!= dict
.end() ) {
49 dictItem
.refer(itr
->second
);
50 setPlainTranslation();
54 //Find in global dictionary.
55 if(findInGlobl(word
) == true) {
56 setPlainTranslation();
63 bool Dict::findInGlobl(const std::string
&swatch
) {
65 ifsgdic
->seekg(0,std::ios::end
);
66 int length
= ifsgdic
->tellg();
71 while(after
-before
>1) {
72 ifsgdic
->seekg((after
+before
)/2);
73 ifsgdic
->ignore(std::numeric_limits
<int>::max(),'\n');
74 current
= ifsgdic
->tellg();
75 getline(*ifsgdic
,line
);
76 if(!dictItem
.refer(line
))
78 if(swatch
> dictItem
.getW())
79 before
= (after
+before
)/2;
80 else if(swatch
< dictItem
.getW())
81 after
= (after
+before
)/2;
82 else if( swatch
== dictItem
.getW() )
87 getline(*ifsgdic
,line
);
88 if(swatch
== dictItem
.getW())
94 bool Dict::merge(const char *newDictName
) {
95 std::ifstream
localDic(configHolder
.localDictFile().c_str());
96 std::ifstream
globalDic(configHolder
.globalDictFile().c_str());
97 std::ofstream
tempDic(newDictName
);
98 if(!localDic
.is_open() || !globalDic
.is_open() || !tempDic
.is_open())
100 std::string localLine
, globalLine
;
101 std::string localWord
, globalWord
;
103 * 1: getline from local.
104 * 2: getline from global.
105 * 3: getline from both local and global.
107 short int getFromFlag
= 3;
109 if((getFromFlag
%2) != 0) { //getFromFlag == 1,3
110 if(std::getline(localDic
,localLine
)) {
111 localWord
= dictItem
.refer(localLine
)
112 ? dictItem
.getW() : std::string("");
113 }else { //getFromFlag == 2
114 while(std::getline(globalDic
,globalLine
))
115 tempDic
<< globalLine
<< std::endl
;
119 if((getFromFlag
- 1) > 0) { //getFromFlag == 2,3
120 if(std::getline(globalDic
,globalLine
)) {
121 globalWord
= dictItem
.refer(globalLine
)
122 ? dictItem
.getW() : std::string("");
124 while(std::getline(globalDic
,localLine
))
125 tempDic
<< localLine
<< std::endl
;
129 if(localWord
< globalWord
) {
131 tempDic
<< localLine
<< std::endl
;
132 }else if(localWord
> globalWord
){
134 tempDic
<< globalLine
<< std::endl
;
135 }else { //Both have the same word.
137 tempDic
<< localLine
<< std::endl
;
144 bool Dict::modify(const std::string
&item
) {
145 static DictItem itemAdd
;
147 if(!itemAdd
.refer(item
)){
151 dict
[itemAdd
.getW()] = item
;
159 std::ofstream
ofs(configHolder
.localDictFile().c_str());
163 std::map
<std::string
,std::string
>::const_iterator itr
= dict
.begin();
164 while(itr
!= dict
.end()) {
168 ofs
<< itr
->second
<< std::endl
;
174 const std::string
&Dict::phonetics() const {
175 static std::string __phonetics
;
177 for(unsigned i
= 0; i
< dictItem
.getT().size(); ++i
) {
178 switch(dictItem
.getT().at(i
)) {
216 __phonetics
+= dictItem
.getT().at(i
);
222 void Dict::setPlainTranslation() {
223 const std::string
&orgTran
= dictItem
.getM();
225 if(plainTran
.capacity() < orgTran
.size())
226 plainTran
.reserve(orgTran
.size());
227 plainTran
= orgTran
.substr(0,orgTran
.find_first_of(std::string("<")));
231 //This is a global variable.
234 } //namespace freeRecite end