From e8770bdf392e9ec91ae85906d59347616adb0864 Mon Sep 17 00:00:00 2001 From: Patrick Lipka Date: Mon, 4 Apr 2022 16:43:13 +0200 Subject: [PATCH] Legacy support re-written by using TT_IO_VERSION as differentiator --- src/project.cpp | 27 +++++++++++++++++++++++++-- src/project.h | 2 +- src/tt.h | 3 ++- src/ui.cpp | 12 +----------- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/project.cpp b/src/project.cpp index 25545ad..67197bd 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -3,6 +3,8 @@ #include #include #include "project.h" +#include "tt.h" +#include "util.h" Project::Project(std::string name){ Project::name = name; @@ -94,6 +96,8 @@ void ProjectList::save(std::string file_name){ if (!of){ std::cout << "Could not open file " << file_name << " for writing!" << std::endl; } + int io_version_number= std::stoi(STRING(TT_IO_VERSION)); + of.write((char*) &io_version_number, sizeof(int)); size_t month_len = month.length(); of.write((char*) &month_len, sizeof(size_t)); of.write((char*) month.c_str(), month_len); @@ -116,16 +120,34 @@ void ProjectList::save(std::string file_name){ } // reads project list from binary file -void ProjectList::load(std::string file_name, bool ignore_worktimes, bool legacy_mode){ +void ProjectList::load(std::string file_name, bool ignore_worktimes){ std::ifstream inf(file_name, std::ios::binary); int active; if (!inf){ std::cout << "Could not open file " << file_name << " for reading!" << std::endl; } - size_t month_len; + bool legacy_mode=false; + int io_version_number= std::stoi(STRING(TT_IO_VERSION)); + int io_ver_num=-1; + inf.read((char*) &io_ver_num,sizeof(int)); + + // check for I/O version number in tracking file, toggle legacy mode depending on it + if(io_ver_num < io_version_number){ + std::cout << "Reading file in legacy mode." << std::endl; + std::cout << "Support for files from tt v.<=1.3.0 will be dropped in the future." << std::endl; + std::cout << "Consider converting it using 'convert -'" << std::endl; + std::cout << std::endl; + + legacy_mode=true; + // re-read file from the beginning + inf.clear(); + inf.seekg(0); + } + size_t month_len; if (!legacy_mode){ // read month string from file + inf.read((char*) &month_len,sizeof(size_t)); char *month_tmp = new char[month_len+1]; inf.read(month_tmp,month_len); @@ -135,6 +157,7 @@ void ProjectList::load(std::string file_name, bool ignore_worktimes, bool legacy } inf.read((char*) &num_projects,sizeof(int)); + // save copy of num_projects to control loop and apply later as // num_projects will get bigger when adding projects to the project list int number_of_projects = num_projects; diff --git a/src/project.h b/src/project.h index e4c4708..ab47439 100644 --- a/src/project.h +++ b/src/project.h @@ -33,7 +33,7 @@ class ProjectList{ Project *active_project; void set_active_project(int id); void save(std::string file_name); - void load(std::string file_name, bool ignore_worktimes=false, bool legacy_mode=false); + void load(std::string file_name, bool ignore_worktimes=false); Project *find_project_by_name(std::string proj_name); int find_project_id_by_name(std::string proj_name); }; diff --git a/src/tt.h b/src/tt.h index d63635c..fd0a45a 100644 --- a/src/tt.h +++ b/src/tt.h @@ -3,7 +3,8 @@ #include -#define TT_VERSION 1.1.0 +#define TT_VERSION 1.3.0 +#define TT_IO_VERSION 130 // global variables: extern std::string user_name; diff --git a/src/ui.cpp b/src/ui.cpp index 3ee1730..27efd5e 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -588,17 +588,7 @@ void command_report(std::string date_str, ProjectList* proj_list){ }else{ ProjectList list(month); std::string file_name = tracking_dir+"/"+date_str; - // legacy support for old tracking file format prior to version 1.3.0 - // to be dropped with next major release - if (std::stoi(year) <= 2022 && std::stoi(month) <= 4){ - std::cout << "Warning: possibly reading old data file of tt v.<1.3.0." << std::endl; - std::cout << "Support for these files will be dropped with the next major release!" << std::endl; - std::cout << "Consider convertig them using the convert command." << std::endl << std::endl; - list.load(file_name,false, true); - }else{ - list.load(file_name); - } - + list.load(file_name); if (list.num_projects > 0){ std::cout << "Report for " << user_name << ", month: " << date_str << std::endl << std::endl; for (int i=0; i