forked from University/epr24pr5-ojanssen2
107 lines
3 KiB
C++
107 lines
3 KiB
C++
#include "Manager.h"
|
|
#include "FileError.h"
|
|
#include <fstream>
|
|
|
|
using err::FileError;
|
|
using util::DataType;
|
|
|
|
namespace util {
|
|
Manager::Manager(): users({}), tasks({}), user_task_index({}), filename("tasks") {
|
|
ifstream in (this->filename);
|
|
if (!in)
|
|
{
|
|
// throw FileError("Could not open file input stream!");
|
|
this->save();
|
|
in.clear();
|
|
in.open(this->filename);
|
|
if (!in)
|
|
throw FileError("Could not open file input stream!");
|
|
}
|
|
DataType current_type = DataType::INIT;
|
|
string d;
|
|
char c;
|
|
while(true) {
|
|
in >> c;
|
|
if (in.eof() || c == EOF)
|
|
break;
|
|
if (c == '\n')
|
|
{
|
|
in >> d;
|
|
continue;
|
|
}
|
|
if (c == '['){
|
|
switch (current_type)
|
|
{
|
|
case DataType::INIT:
|
|
current_type = DataType::TASK;
|
|
break;
|
|
case DataType::TASK:
|
|
current_type = DataType::USER;
|
|
break;
|
|
case DataType::USER:
|
|
current_type = DataType::USERTASKINDEX;
|
|
break;
|
|
case DataType::USERTASKINDEX:
|
|
break; // This should not happen. There is nothing after [assignments]
|
|
}
|
|
in >> d;
|
|
continue;
|
|
}
|
|
in.putback(c);
|
|
switch (current_type) {
|
|
case DataType::INIT:
|
|
break;
|
|
case DataType::TASK:
|
|
{
|
|
Task t;
|
|
in >> t;
|
|
this->tasks.push_back(t);
|
|
}
|
|
break;
|
|
case DataType::USER:
|
|
{
|
|
User u;
|
|
in >> u;
|
|
this->users.push_back(u);
|
|
}
|
|
break;
|
|
case DataType::USERTASKINDEX:
|
|
{
|
|
Assignment a;
|
|
in >> a;
|
|
this->user_task_index.push_back(a);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
in.close();
|
|
}
|
|
|
|
void Manager::save() {
|
|
ofstream out = ofstream(this->filename);
|
|
if (!out)
|
|
throw FileError("Could not open file output stream!");
|
|
out << "[tasks]\n";
|
|
for (Task& t : this->tasks)
|
|
t.write(out);
|
|
out << "\n[users]\n";
|
|
for (User& u : this->users)
|
|
u.write(out);
|
|
out << "\n[assignments]\n";
|
|
for (Assignment& uti: this->user_task_index)
|
|
uti.write(out);
|
|
out.close();
|
|
}
|
|
|
|
vector<User> Manager::get_users() const {
|
|
return this->users;
|
|
}
|
|
|
|
vector<Task> Manager::get_task() const {
|
|
return this->tasks;
|
|
}
|
|
|
|
vector<Assignment> Manager::get_user_task_index() const {
|
|
return this->user_task_index;
|
|
}
|
|
}
|