11 #include "ConfigHolder.h"
14 #include "ForgetCurve.h"
18 namespace freeRecite
{
20 bool Manager::load() {
26 std::ifstream
ifs(configHolder
.mgrFile().c_str());
35 bool firstFlag
= true;
36 while(amount
> 0 && ifs
.good()) {
38 allTasks
[tmpTask
.getID()] = tmpTask
;
40 if(tmpTask
.shouldReview())
41 activeID
.push_back(tmpTask
.getID());
43 firstReviewTime
= tmpTask
.getReviewTime();
45 }else if(firstReviewTime
> tmpTask
.getReviewTime())
46 firstReviewTime
= tmpTask
.getReviewTime();
56 bool Manager::save() {
57 std::ofstream
ofs(configHolder
.mgrFile().c_str());
60 ofs
<< static_cast<unsigned>(allTasks
.size())
61 << ',' << maxTaskID
<< '\n';
62 std::map
<time_t,Task
>::const_iterator itr
= allTasks
.begin();
63 while(itr
!= allTasks
.end()){
67 if((itr
->second
).isAvailable())
75 bool Manager::refresh() {
84 bool Manager::hasTask(time_t taskID
) {
85 if(allTasks
.find(taskID
) == allTasks
.end())
91 int Manager::currNumWords() {
93 std::set
<std::string
> allWords
;
95 std::map
<time_t,Task
>::const_iterator itr
= allTasks
.begin();
96 while(itr
!= allTasks
.end()) {
97 ifs
.open(Scanner::getTaskFileName(itr
->first
).c_str());
101 while(std::getline(ifs
,tmpWord
)) {
102 if(!tmpWord
.empty()) {
103 allWords
.insert(tmpWord
);
109 return static_cast<int>(allWords
.size());
112 int Manager::doneNumWords() {
114 std::set
<std::string
> allWords
;
116 ifs
.open(configHolder
.doneFile().c_str());
119 while(std::getline(ifs
,tmpWord
)) {
120 if(!tmpWord
.empty()) {
121 allWords
.insert(tmpWord
);
124 return static_cast<int>(allWords
.size());
127 bool Manager::createTask(const std::set
<std::string
> &words
,
128 const char *taskName
, unsigned maxLimit
) {
131 unsigned tasksNum
= words
.size()/maxLimit
+ 1;
132 maxLimit
= words
.size()/tasksNum
;
133 std::string baseName
;
134 std::string realName
;
135 std::ofstream desFile
;
136 std::ostringstream os
;
139 struct tm
* timeinfo
;
141 std::set
<std::string
>::const_iterator itr
= words
.begin();
147 if(baseName
.empty()){
148 timeinfo
= localtime(&curTime
);
149 strftime(buffer
,20,"%Y%m%d%H%M%S",timeinfo
);
153 for (unsigned i
= 1; i
<= tasksNum
; ++i
) { //[0]
159 realName
= baseName
+ os
.str();
162 if(curTime
> maxTaskID
)
166 desFile
.open(Scanner::getTaskFileName(maxTaskID
).c_str());
167 D_OUTPUT(Scanner::getTaskFileName(maxTaskID
).c_str())
168 if(!desFile
.is_open())
171 //Write words into file.
173 while(j
++ < maxLimit
&& itr
!= words
.end()) {
180 if(dictionary
.lookUp(*itr
))
181 desFile
<< *itr
<< std::endl
;
183 std::string str
= *itr
;
184 //Convert the word to lower
185 for(int i
= 0; i
< str
.size(); ++i
)
187 str
[i
] = tolower(str
[i
]);
188 if(dictionary
.lookUp(str
))
189 desFile
<< str
<< std::endl
;
191 desFile
<< *itr
<< std::endl
;
197 Task
newTask(maxTaskID
,realName
.c_str());
198 //Pass a copy of newTask here.
199 allTasks
[newTask
.getID()] = newTask
;
200 activeID
.push_back(newTask
.getID());
207 void Manager::removeTask(time_t taskID
) {
208 allTasks
.erase(allTasks
.find(taskID
));
210 ::remove(Scanner::getTaskFileName(taskID
).c_str());
215 const std::vector
<time_t> &Manager::getActiveTasks() const{
219 const std::map
<time_t,Task
> &Manager::getAllTasks() const{
224 //Get the number of the active tasks.
225 int Manager::getActiveTaskNum() const {
226 return activeID
.size();
229 //Get the task's name.
230 const std::string
&Manager::getTaskName(time_t taskID
)const {
231 return (allTasks
.find(taskID
)->second
).getName();
235 time_t Manager::getNextTime(time_t taskID
) const {
236 return (allTasks
.find(taskID
)->second
).getReviewTime();
239 //Get the step of the task.
240 int Manager::getTaskStep(time_t taskID
)const {
241 return (allTasks
.find(taskID
)->second
).getStep();
244 int Manager::test(const time_t &taskID
, const int &mark
){
245 int result
= (allTasks
[taskID
]).test(mark
);
247 if(result
!= 1) //If user haven't complish the task,return directly.
250 // This task have been complished, add the words to done.txt and
251 // delete this task in manager.
252 std::ifstream
ifs(Scanner::getTaskFileName(taskID
).c_str());
255 std::set
<std::string
> doneWords
;
258 std::getline(ifs
,word
);
260 doneWords
.insert(word
);
263 ifs
.open(configHolder
.doneFile().c_str());
266 std::getline(ifs
,word
);
268 doneWords
.insert(word
);
272 std::ofstream
ofs(configHolder
.doneFile().c_str());
275 std::set
<std::string
>::const_iterator itr
= doneWords
.begin();
276 while(ofs
.good() && itr
!= doneWords
.end())
277 ofs
<< *itr
++ << '\n';
282 //This is a global variable.
285 } //namespace freeRecite end