Legacy support re-written by using TT_IO_VERSION as differentiator

This commit is contained in:
Patrick Lipka 2022-04-04 16:43:13 +02:00
parent 5098dc0357
commit e8770bdf39
4 changed files with 29 additions and 15 deletions

View File

@ -3,6 +3,8 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include "project.h" #include "project.h"
#include "tt.h"
#include "util.h"
Project::Project(std::string name){ Project::Project(std::string name){
Project::name = name; Project::name = name;
@ -94,6 +96,8 @@ void ProjectList::save(std::string file_name){
if (!of){ if (!of){
std::cout << "Could not open file " << file_name << " for writing!" << std::endl; 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(); size_t month_len = month.length();
of.write((char*) &month_len, sizeof(size_t)); of.write((char*) &month_len, sizeof(size_t));
of.write((char*) month.c_str(), month_len); 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 // 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); std::ifstream inf(file_name, std::ios::binary);
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;
} }
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 <yyyy>-<mm>'" << 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){ if (!legacy_mode){
// read month string from file // read month string from file
inf.read((char*) &month_len,sizeof(size_t)); inf.read((char*) &month_len,sizeof(size_t));
char *month_tmp = new char[month_len+1]; char *month_tmp = new char[month_len+1];
inf.read(month_tmp,month_len); 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)); inf.read((char*) &num_projects,sizeof(int));
// save copy of num_projects to control loop and apply later as // save copy of num_projects to control loop and apply later as
// num_projects will get bigger when adding projects to the project list // num_projects will get bigger when adding projects to the project list
int number_of_projects = num_projects; int number_of_projects = num_projects;

View File

@ -33,7 +33,7 @@ class ProjectList{
Project *active_project; Project *active_project;
void set_active_project(int id); void set_active_project(int id);
void save(std::string file_name); 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); Project *find_project_by_name(std::string proj_name);
int find_project_id_by_name(std::string proj_name); int find_project_id_by_name(std::string proj_name);
}; };

View File

@ -3,7 +3,8 @@
#include <string> #include <string>
#define TT_VERSION 1.1.0 #define TT_VERSION 1.3.0
#define TT_IO_VERSION 130
// global variables: // global variables:
extern std::string user_name; extern std::string user_name;

View File

@ -588,17 +588,7 @@ void command_report(std::string date_str, ProjectList* proj_list){
}else{ }else{
ProjectList list(month); ProjectList list(month);
std::string file_name = tracking_dir+"/"+date_str; 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){ 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++){