mirror of https://github.com/PatrickLipka/tt.git
Added saving to binary, implementation of loading started
This commit is contained in:
parent
1f5778e190
commit
32daf02351
|
@ -1,5 +1,7 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
#include "project.h"
|
#include "project.h"
|
||||||
|
|
||||||
Project::Project(std::string name){
|
Project::Project(std::string name){
|
||||||
|
@ -9,25 +11,68 @@ Project::Project(std::string name){
|
||||||
}
|
}
|
||||||
|
|
||||||
void Project::add_task(Task task){
|
void Project::add_task(Task task){
|
||||||
Project::tasks.push_back(task);
|
tasks.push_back(task);
|
||||||
Project::active_task = Project::num_tasks;
|
active_task = num_tasks;
|
||||||
Project::num_tasks++;
|
num_tasks++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Project::remove_task(int id){
|
void Project::remove_task(int id){
|
||||||
Project::tasks.erase(tasks.begin()+id);
|
tasks.erase(tasks.begin()+id);
|
||||||
Project::active_task = std::max(id-1,0);
|
active_task = std::max(id-1,0);
|
||||||
Project::num_tasks--;
|
num_tasks--;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProjectList::add_project(Project proj){
|
void ProjectList::add_project(Project proj){
|
||||||
ProjectList::projects.push_back(proj);
|
projects.push_back(proj);
|
||||||
ProjectList::active_project = ProjectList::num_projects;
|
active_project = num_projects;
|
||||||
ProjectList::num_projects++;
|
num_projects++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProjectList::remove_project(int id){
|
void ProjectList::remove_project(int id){
|
||||||
ProjectList::projects.erase(projects.begin()+id);
|
projects.erase(projects.begin()+id);
|
||||||
ProjectList::active_project = std::max(id-1,0);
|
active_project = std::max(id-1,0);
|
||||||
ProjectList::num_projects--;
|
num_projects--;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectList::save(std::string file_name){
|
||||||
|
std::ofstream of(file_name, std::ios::out | std::ios::binary);
|
||||||
|
if (!of){
|
||||||
|
std::cout << "Could not open file " << file_name << " for writing!" << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
std::cout << num_projects << " " << num_projects << std::endl;
|
||||||
|
of.write((char*) &num_projects, sizeof(int));
|
||||||
|
of.write((char*) &active_project, sizeof(int));
|
||||||
|
for (int i=0; i<num_projects; i++){
|
||||||
|
int len = projects[i].name.length();
|
||||||
|
of.write((char*) &len, sizeof(int));
|
||||||
|
of.write((char*) &(projects[i].name),ProjectList::projects[i].name.length());
|
||||||
|
of.write((char*) &(projects[i].num_tasks), sizeof(int));
|
||||||
|
of.write((char*) &(projects[i].active_task), sizeof(int));
|
||||||
|
for (int j=0; j<projects[i].num_tasks; j++){
|
||||||
|
of.write((char*) &(projects[i].tasks[j].name), projects[i].tasks[j].name.length());
|
||||||
|
of.write((char*) &(projects[i].tasks[j].work_time), sizeof(int));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
of.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectList::load(std::string file_name){
|
||||||
|
std::ifstream inf(file_name, std::ios::in | std::ios::binary);
|
||||||
|
if (!inf){
|
||||||
|
std::cout << "Could not open file " << file_name << " for reading!" << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
inf.read((char*) &num_projects,sizeof(int));
|
||||||
|
std::cout << "Num projects read:" << num_projects << std::endl;
|
||||||
|
inf.read((char*) &active_project, sizeof(int));
|
||||||
|
for (int i=0; i<num_projects;i++){
|
||||||
|
int tmp_len;
|
||||||
|
std::string tmp_name;
|
||||||
|
inf.read((char*) &tmp_len, sizeof(int));
|
||||||
|
std::cout << "tmp_len read: "<< tmp_len <<std::endl;
|
||||||
|
inf.read((char*) &tmp_name, tmp_len);
|
||||||
|
std::cout << "name read: " << tmp_name << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,8 @@ class ProjectList{
|
||||||
void add_project(Project proj);
|
void add_project(Project proj);
|
||||||
void remove_project(int id);
|
void remove_project(int id);
|
||||||
int active_project;
|
int active_project;
|
||||||
|
void save(std::string file_name);
|
||||||
|
void load(std::string file_name);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -21,7 +21,12 @@ int main(){
|
||||||
proj_list.add_project(proj);
|
proj_list.add_project(proj);
|
||||||
std::cout << "Project List no of projects: " << proj_list.num_projects << " Name of active proj: " << proj_list.projects[proj_list.active_project].name << std::endl;
|
std::cout << "Project List no of projects: " << proj_list.num_projects << " Name of active proj: " << proj_list.projects[proj_list.active_project].name << std::endl;
|
||||||
|
|
||||||
proj_list.remove_project(0);
|
std::cout << "Write Project List to file" << std::endl;
|
||||||
std::cout << "Project List no of projects: " << proj_list.num_projects << std::endl;
|
proj_list.save("test.bin");
|
||||||
|
|
||||||
|
std::cout << "Read Project List from file" << std::endl;
|
||||||
|
ProjectList list2;
|
||||||
|
list2.load("test.bin");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue