7 #include "ConfigHolder.h"
12 namespace freeRecite
{
15 D_OUTPUT("~Scanner()");
20 const std::string
Scanner::getTaskFileName(time_t initID
) {
21 std::ostringstream oss
;
22 oss
<< static_cast<unsigned>(initID
);
23 return (configHolder
.tasksDir() + oss
.str() + ".tkwd");
26 bool Scanner::loadWords(time_t initID
,bool Random
) {
27 taskFileName
= getTaskFileName(initID
);
29 std::ifstream
ifs(taskFileName
.c_str());
34 std::getline(ifs
,tmpWord
);
35 while(tmpWord
[tmpWord
.size()-1] == ' ')
36 tmpWord
.erase(tmpWord
.size()-1);
38 words
.push_back(tmpWord
);
40 //If Random is false, then do not call makeRandom().
41 return Random
? makeRandom() : true;
44 bool Scanner::loadWords(const char *fileName
, bool Random
) {
45 taskFileName
= fileName
;
46 std::ifstream
ifs(fileName
);
50 std::set
<std::string
> wordFilter
;
52 std::getline(ifs
,tmpWord
);
53 while(tmpWord
[tmpWord
.size()-1] == ' ')
54 tmpWord
.erase(tmpWord
.size()-1);
56 wordFilter
.insert(tmpWord
);
58 std::set
<std::string
>::const_iterator itr
= wordFilter
.begin();
59 while(itr
!= wordFilter
.end()) {
60 words
.push_back(*itr
);
70 //Add a word to the current task.
71 bool Scanner::add(const std::string
&word
) {
72 words
.push_back(word
);
77 //Remove a word from the current task.
78 bool Scanner::remove(const std::string
&word
) {
80 while( index
< words
.size() ) {
81 if(words
[index
] == word
)
86 if(index
== words
.size()) //Can't find this word.
89 words
[index
] = words
[words
.size()-1];
95 bool Scanner::save() {
96 std::set
<std::string
> wordSet
;
97 for(size_t i
= 0; i
< words
.size(); ++i
)
98 if( !wordSet
.insert(words
[i
]).second
)
101 std::ofstream
ofs(taskFileName
.c_str());
105 std::set
<std::string
>::iterator itr
= wordSet
.begin();
106 while(itr
!= wordSet
.end()) {
110 ofs
<< *itr
<< std::endl
;
118 bool Scanner::makeRandom() {
123 std::random_shuffle(words
.begin(),words
.end(),rd
);
128 } //End of namespace freeRecite