Read user name and tracking directory from settings file in PREFIX/etc and use them for load/save operations

This commit is contained in:
Patrick Lipka 2021-12-16 16:00:40 +01:00
parent 12376d3e4b
commit 183717c956
4 changed files with 50 additions and 8 deletions

View File

@ -3,6 +3,8 @@ CXXFLAGS := -g -Wall -pedantic
OBJFLAGS := $(CXXFLAGS) -c OBJFLAGS := $(CXXFLAGS) -c
LDFLAGS := -lreadline LDFLAGS := -lreadline
PREFIX ?= /usr/local
BIN_PATH := bin BIN_PATH := bin
SRC_PATH := src SRC_PATH := src
@ -12,10 +14,10 @@ OBJ := $(addprefix $(SRC_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))
default: makedir all default: makedir all
$(BIN_PATH)/tt: $(OBJ) $(BIN_PATH)/tt: $(OBJ)
$(CXX) $(CXXFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(CXX) $(CXXFLAGS) -o $@ $(OBJ) $(LDFLAGS)
$(SRC_PATH)/%.o: $(SRC_PATH)/%.cpp $(SRC_PATH)/%.o: $(SRC_PATH)/%.cpp
$(CXX) $(OBJFLAGS) -o $@ $< $(CXX) -DPREFIX=$(PREFIX) $(OBJFLAGS) -o $@ $<
makedir: makedir:
@mkdir -p $(BIN_PATH) @mkdir -p $(BIN_PATH)

View File

@ -1,4 +1,5 @@
#include <iostream> #include <iostream>
#include <fstream>
#include <vector> #include <vector>
#include <signal.h> #include <signal.h>
#include <unistd.h> #include <unistd.h>
@ -25,13 +26,41 @@ std::string get_date(){
std::string date_str = std::to_string(year)+"-"+month_str; std::string date_str = std::to_string(year)+"-"+month_str;
return date_str; return date_str;
} }
std::string user_name;
void parse_config_file(std::string file_name, std::string *user_name, std::string *tracking_dir){
std::ifstream f(file_name);
if (!f){
std::cout << "ABORT: Could not open config file " << file_name << "for reading." << std::endl;
f.close();
exit(1);
}
std::string line;
while(std::getline(f,line)){
size_t place_of_eq = line.find("=");
std::string key = line.substr(0,place_of_eq);
std::string val = line.substr(place_of_eq+1);
if (key == "user_name") *user_name = val;
if (key == "tracking_directory") *tracking_dir = val;
}
if (user_name->length() == 0 || tracking_dir->length() == 0){
std::cout << "ABORT: Malformed config file " << file_name << std::endl;
f.close();
exit(1);
}
f.close();
}
std::string user_name, tracking_dir, config_file;
int main(){ int main(){
// connect SIGINT signal (CTRL-C) to signal handler from track.h to use it to stop tracking of projects // connect SIGINT signal (CTRL-C) to signal handler from track.h to use it to stop tracking of projects
signal(SIGINT, handler); signal(SIGINT, handler);
tracking = 0; tracking = 0;
user_name = "Patrick"; std::string prefix=STRING(PREFIX);
config_file = prefix+"/etc/tt.conf";
parse_config_file(config_file, &user_name, &tracking_dir);
// TEST: set up test list // TEST: set up test list
ProjectList proj_list("dec"); ProjectList proj_list("dec");

View File

@ -1,6 +1,13 @@
#ifndef TT_H #ifndef TT_H
#define TT_H #define TT_H
#define STRING(s) STR(s)
#define STR(s) #s
#include <string> #include <string>
extern std::string user_name; extern std::string user_name;
extern std::string tracking_dir;
extern std::string config_file;
std::string get_date(); std::string get_date();
void parse_config_file(std::string file_name, std::string *user_name, std::string *tracking_dir);
#endif #endif

View File

@ -7,6 +7,7 @@
#include <ctime> #include <ctime>
#include <iomanip> #include <iomanip>
#include <iterator> #include <iterator>
#include <unistd.h>
#include "ui.h" #include "ui.h"
#include "project.h" #include "project.h"
#include "track.h" #include "track.h"
@ -542,7 +543,6 @@ void command_report(std::string date_str, ProjectList* proj_list){
if(date_str.length() == 0){ if(date_str.length() == 0){
// report for current month // report for current month
// get date:
std::string date = get_date(); std::string date = get_date();
std::cout << "Report for " << user_name << ", month: " << date << std::endl << std::endl; std::cout << "Report for " << user_name << ", month: " << date << std::endl << std::endl;
@ -561,7 +561,8 @@ void command_report(std::string date_str, ProjectList* proj_list){
} }
}else{ }else{
ProjectList list(month); ProjectList list(month);
list.load(date_str); std::string file_name = tracking_dir+"/"+date_str;
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++){
@ -583,9 +584,12 @@ void command_report(std::string date_str, ProjectList* proj_list){
} }
} }
// save project list to file - POSIX only
void command_save(ProjectList *proj_list){ void command_save(ProjectList *proj_list){
std::string date_str = get_date(); std::string date_str = get_date();
std::string file_name = date_str; std::string file_name = tracking_dir+"/"+date_str;
std::string command = "mkdir -p "+tracking_dir;
system (command.c_str());
proj_list->save(file_name); proj_list->save(file_name);
std::cout << "Tracking data saved to file " << file_name << std::endl; std::cout << "Tracking data saved to file " << file_name << std::endl;
} }