Solved problems with project list corruption due to read of wrong length and redundant setting of current task

This commit is contained in:
Patrick Lipka 2021-12-15 12:16:03 +01:00
parent 10615ed0fa
commit 85fd6e563f
3 changed files with 16 additions and 20 deletions

View File

@ -65,7 +65,6 @@ ProjectList::ProjectList(std::string month){
void ProjectList::add_project(Project proj){ void ProjectList::add_project(Project proj){
projects.push_back(proj); projects.push_back(proj);
set_active_project(num_projects); set_active_project(num_projects);
active_project->set_active_task(active_project->active_task_id);
num_projects++; num_projects++;
} }
@ -91,13 +90,13 @@ void ProjectList::save(std::string file_name){
std::cout << "Saving: num_projects=" << num_projects << std::endl; std::cout << "Saving: num_projects=" << num_projects << std::endl;
of.write((char*) &active_project_id, sizeof(int)); of.write((char*) &active_project_id, sizeof(int));
for (int i=0; i<num_projects; i++){ for (int i=0; i<num_projects; i++){
size_t len = projects[i].name.size(); size_t len = projects[i].name.length();
of.write((char*) &len, sizeof(size_t)); of.write((char*) &len, sizeof(size_t));
of.write((char*) projects[i].name.c_str(),len); of.write((char*) projects[i].name.c_str(),len);
of.write((char*) &(projects[i].num_tasks), sizeof(int)); of.write((char*) &(projects[i].num_tasks), sizeof(int));
of.write((char*) &(projects[i].active_task_id), sizeof(int)); of.write((char*) &(projects[i].active_task_id), sizeof(int));
for (int j=0; j<projects[i].num_tasks; j++){ for (int j=0; j<projects[i].num_tasks; j++){
size_t task_name_len = projects[i].tasks[j].name.size(); size_t task_name_len = projects[i].tasks[j].name.length();
of.write((char*) &task_name_len, sizeof(size_t)); of.write((char*) &task_name_len, sizeof(size_t));
of.write((char*) projects[i].tasks[j].name.c_str(), task_name_len); of.write((char*) projects[i].tasks[j].name.c_str(), task_name_len);
of.write((char*) &(projects[i].tasks[j].work_time), sizeof(int)); of.write((char*) &(projects[i].tasks[j].work_time), sizeof(int));
@ -111,10 +110,8 @@ void ProjectList::load(std::string file_name){
int active; int active;
if (!inf){ if (!inf){
std::cout << "Could not open file " << file_name << " for reading!" << std::endl; std::cout << "Could not open file " << file_name << " for reading!" << std::endl;
//exit(1);
} }
inf.read((char*) &num_projects,sizeof(int)); inf.read((char*) &num_projects,sizeof(int));
std::cout << "Reading: num_projects=" << num_projects << std::endl;
int number_of_projects = num_projects; int number_of_projects = num_projects;
inf.read((char*) &active, sizeof(int)); inf.read((char*) &active, sizeof(int));
int np = num_projects; int np = num_projects;
@ -131,6 +128,7 @@ void ProjectList::load(std::string file_name){
Project proj(proj_name); Project proj(proj_name);
delete [] tmp_name; delete [] tmp_name;
inf.read((char*) &(proj.num_tasks), sizeof(int)); inf.read((char*) &(proj.num_tasks), sizeof(int));
int number_of_tasks = proj.num_tasks;
inf.read((char*) &active_t, sizeof(int)); inf.read((char*) &active_t, sizeof(int));
int nt = proj.num_tasks; int nt = proj.num_tasks;
for (int j=0; j<nt; j++){ for (int j=0; j<nt; j++){
@ -139,17 +137,18 @@ void ProjectList::load(std::string file_name){
inf.read((char*) &task_name_len, sizeof(size_t)); inf.read((char*) &task_name_len, sizeof(size_t));
char* tmp_task_name = new char[task_name_len+1]; char* tmp_task_name = new char[task_name_len+1];
inf.read(tmp_task_name, task_name_len); inf.read(tmp_task_name, task_name_len);
tmp_task_name[len] = '\0'; tmp_task_name[task_name_len] = '\0';
task_name = tmp_task_name; task_name = tmp_task_name;
delete [] tmp_task_name; delete [] tmp_task_name;
Task task(task_name); Task task(task_name);
int work_time=0; int work_time=0;
inf.read((char*) &(work_time), sizeof(int)); inf.read((char*) &(work_time), sizeof(int));
task.add_time(work_time); task.add_time(work_time);
proj.add_task(task); proj.add_task(task);
} }
// needed to get the active_task pointers right _after_ adding tasks to projetcs // needed to get the active_task pointers right _after_ adding tasks to projetcs
active_task_arr[i] = active_t; active_task_arr[i] = active_t;
proj.num_tasks = number_of_tasks;
add_project(proj); add_project(proj);
} }
set_active_project(active); set_active_project(active);

View File

@ -14,7 +14,7 @@
std::string get_date(){ std::string get_date(){
time_t t = time(NULL); time_t t = time(NULL);
tm *date = localtime(&t); tm *date = localtime(&t);
int month = 5;//date->tm_mon+1; int month = date->tm_mon+1;
int year = date->tm_year+1900; int year = date->tm_year+1900;
std::string month_str; std::string month_str;
if (month < 10){ if (month < 10){
@ -40,7 +40,9 @@ int main(){
proj.add_task(task1); proj.add_task(task1);
proj.add_task(task2); proj.add_task(task2);
proj.add_task(task3); proj.add_task(task3);
proj_list.add_project(proj); proj_list.add_project(proj);
// proj_list.load(get_date());
init_autocomplete(&proj_list); init_autocomplete(&proj_list);
// use GNU readline for auto completion and history when parsing command input // use GNU readline for auto completion and history when parsing command input
while(1) { while(1) {
@ -54,6 +56,5 @@ int main(){
free(buffer); free(buffer);
} }
} }
return 0; return 0;
} }

View File

@ -37,17 +37,14 @@ char **tt_name_completion(const char* text, int start, int end){
char *tt_name_generator(const char *text, int state){ char *tt_name_generator(const char *text, int state){
static int idx, len; static int idx, len;
const char* name; const char *name;
if (!state){ if (!state){
idx = 0; idx = 0;
len = strlen(text); len = strlen(text);
} }
while ((name = autocomplete_names[idx++].c_str())){ while ((name = autocomplete_names[idx++].c_str())){
if (strncmp(name, text, len) == 0){ if (strncmp(name, text, len) == 0){
return strdup(name); return strdup(name);
} }
} }
return NULL; return NULL;
@ -458,7 +455,6 @@ void command_at(std::string input, int wtime, ProjectList *proj_list){
// add time to active task // add time to active task
proj_list->active_project->active_task->add_time(wtime); proj_list->active_project->active_task->add_time(wtime);
std::cout << wtime << "s added to task " << proj_list->active_project->name << "/" << proj_list->active_project->active_task->name << std::endl; std::cout << wtime << "s added to task " << proj_list->active_project->name << "/" << proj_list->active_project->active_task->name << std::endl;
std::cout << "New time: "<< proj_list->active_project->active_task->work_time << std::endl;
return; return;
}else{ }else{
// add time to task in same project // add time to task in same project
@ -551,13 +547,13 @@ void command_report(std::string date_str, ProjectList* proj_list){
} }
} }
}else{ }else{
ProjectList *list = new ProjectList(month); ProjectList list(month);
list->load(date_str); list.load(date_str);
if (list->num_projects > 0){ if (list.num_projects > 0){
std::cout << "Report for " << user_name << ", month: " << date_str << std::endl << std::endl; std::cout << "Report for " << user_name << ", month: " << date_str << std::endl << std::endl;
for (int i=0; i<list->num_projects; i++){ for (int i=0; i<list.num_projects; i++){
// compute task and total times // compute task and total times
Project *proj = &(list->projects[i]); Project *proj = &(list.projects[i]);
float wtime_proj = proj->get_total_work_time() / 3600.0; float wtime_proj = proj->get_total_work_time() / 3600.0;
if(wtime_proj >= 0.01){ if(wtime_proj >= 0.01){
std::cout << "Project: " << proj->name << std::endl; std::cout << "Project: " << proj->name << std::endl;