forked from University/epr24pr5-ojanssen2
187 lines
5.3 KiB
C++
187 lines
5.3 KiB
C++
#include "Manager.h"
|
|
#include "Error.h"
|
|
#include <fstream>
|
|
#include "DataType.h"
|
|
|
|
using err::Error;
|
|
using util::DataType;
|
|
|
|
namespace util
|
|
{
|
|
Manager::Manager(): users({}), tasks({}), assignments({}), user_id_index(0), task_id_index(0), filename("tasks")
|
|
{
|
|
//Open File
|
|
ifstream in(this->filename, ios_base::in);
|
|
if (!in)
|
|
{
|
|
this->save();
|
|
in.clear();
|
|
in.open(this->filename);
|
|
if (!in)
|
|
throw Error(601, "Datei kann nicht geöffnet werden.");
|
|
}
|
|
map<DataType, vector<string>> buffer;
|
|
DataType current_type = DataType::INIT;
|
|
string d; // dump
|
|
char c;
|
|
string line, section;
|
|
//Parse all Lines until EOF
|
|
while (getline(in, line))
|
|
{
|
|
// Check if the line starts with '['
|
|
if (!line.empty() && line[0] == '[')
|
|
{
|
|
if (line == "[tasks]")
|
|
current_type = DataType::TASK;
|
|
else if (line == "[users]")
|
|
current_type = DataType::USER;
|
|
else if (line == "[assignments]")
|
|
current_type = DataType::USERTASKINDEX;
|
|
else
|
|
throw Error(602, "Datei hat ein unbekanntes Format.");
|
|
continue;
|
|
}
|
|
//Lehre Zeilen überspringen.
|
|
if (line.empty())
|
|
continue;
|
|
//Speichern im Buffer
|
|
buffer[current_type].push_back(line);
|
|
} //Data is parsed
|
|
in.close();
|
|
|
|
// Reihenfolge der Verabeitung
|
|
vector<DataType> sections = {DataType::USER, DataType::TASK, DataType::USERTASKINDEX};
|
|
for (DataType type : sections)
|
|
{
|
|
// Keine Data -> Continue
|
|
if (buffer.find(type) == buffer.end()) continue;
|
|
const vector<string>& lines = buffer[type];
|
|
//Iteriere über die Gebufferten Strings.
|
|
for (const string& bufferd_line : lines)
|
|
{
|
|
switch (type)
|
|
{
|
|
case DataType::TASK:
|
|
{
|
|
Task task;
|
|
in >> task;
|
|
this->tasks[task.get_id()] = new Task(task);
|
|
}
|
|
break;
|
|
case DataType::USER:
|
|
{
|
|
User user;
|
|
in >> user;
|
|
this->users[user.get_id()] = new User(user);
|
|
}
|
|
break;
|
|
case DataType::USERTASKINDEX:
|
|
{
|
|
Assignment assignment;
|
|
in >> assignment;
|
|
this->assignments.push_back(new Assignment(assignment));
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
this->update_indexes();
|
|
}
|
|
|
|
void Manager::update_user_id_index()
|
|
{
|
|
int i = 0;
|
|
for (const auto& user_pair : this->users)
|
|
if (user_pair.second && user_pair.second->get_id() >= i)
|
|
i = user_pair.second->get_id() + 1;
|
|
this->user_id_index = i;
|
|
}
|
|
|
|
void Manager::update_task_id_index()
|
|
{
|
|
int i = 0;
|
|
for (const auto& task_pair : this->users)
|
|
if (task_pair.second && task_pair.second->get_id() >= i)
|
|
i = task_pair.second->get_id() + 1;
|
|
this->task_id_index = i;
|
|
}
|
|
|
|
void Manager::update_indexes()
|
|
{
|
|
this->update_user_id_index();
|
|
this->update_task_id_index();
|
|
}
|
|
|
|
int Manager::get_user_id()
|
|
{
|
|
int value = this->user_id_index;
|
|
++this->user_id_index;
|
|
return value;
|
|
}
|
|
|
|
int Manager::get_task_id()
|
|
{
|
|
int value = this->task_id_index;
|
|
++this->task_id_index;
|
|
return value;
|
|
}
|
|
|
|
Manager* Manager::INSTANCE;
|
|
|
|
Manager& Manager::get_instance()
|
|
{
|
|
if (!INSTANCE)
|
|
INSTANCE = new Manager();
|
|
return *INSTANCE;
|
|
}
|
|
vector<User*> const Manager::get_users() {
|
|
vector<User*> user_vector;
|
|
for (const auto& pair : this->users) {
|
|
if (pair.second) { // Ensure it's not a nullptr
|
|
user_vector.push_back(pair.second);
|
|
}
|
|
}
|
|
return user_vector;
|
|
|
|
};
|
|
vector<Task*> get_tasks();
|
|
vector<Assignment*> get_assignments();
|
|
void Manager::save()
|
|
{
|
|
ofstream out = ofstream(this->filename, ios_base::trunc);
|
|
if (!out)
|
|
throw Error(603, "Datei kann nicht geschrieben werden.");
|
|
out << "[tasks]\n";
|
|
for (const auto& task_pair : this->users)
|
|
{
|
|
//Nullpointer Check
|
|
if (task_pair.second)
|
|
{
|
|
task_pair.second->write(out);
|
|
}
|
|
}
|
|
out << "\n[users]\n";
|
|
for (const auto& user_pair : this->users)
|
|
{
|
|
//Nullpointer Check
|
|
if (user_pair.second)
|
|
{
|
|
user_pair.second->write(out);
|
|
}
|
|
}
|
|
|
|
out << "\n[assignments]\n";
|
|
for (Assignment* assignment : this->assignments)
|
|
{
|
|
//Nullpointer Check
|
|
if (assignment)
|
|
{
|
|
assignment->write(out);
|
|
}
|
|
}
|
|
out.close();
|
|
}
|
|
|
|
} // util
|