diff --git a/Makefile b/Makefile index 4dcf08b..5c0471b 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,8 @@ CXXFLAGS := -g -Wall -pedantic OBJFLAGS := $(CXXFLAGS) -c LDFLAGS := -lreadline +PREFIX ?= /usr/local + BIN_PATH := bin SRC_PATH := src @@ -12,10 +14,10 @@ OBJ := $(addprefix $(SRC_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC))))) default: makedir all $(BIN_PATH)/tt: $(OBJ) - $(CXX) $(CXXFLAGS) -o $@ $(OBJ) $(LDFLAGS) + $(CXX) $(CXXFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(SRC_PATH)/%.o: $(SRC_PATH)/%.cpp - $(CXX) $(OBJFLAGS) -o $@ $< + $(CXX) -DPREFIX=$(PREFIX) $(OBJFLAGS) -o $@ $< makedir: @mkdir -p $(BIN_PATH) diff --git a/src/tt.cpp b/src/tt.cpp index adffb15..0d9a8e1 100644 --- a/src/tt.cpp +++ b/src/tt.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -25,14 +26,42 @@ std::string get_date(){ std::string date_str = std::to_string(year)+"-"+month_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(){ // connect SIGINT signal (CTRL-C) to signal handler from track.h to use it to stop tracking of projects signal(SIGINT, handler); 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 ProjectList proj_list("dec"); Project proj("Test Project"); diff --git a/src/tt.h b/src/tt.h index 84e2355..0552abc 100644 --- a/src/tt.h +++ b/src/tt.h @@ -1,6 +1,13 @@ #ifndef TT_H #define TT_H +#define STRING(s) STR(s) +#define STR(s) #s + #include extern std::string user_name; +extern std::string tracking_dir; +extern std::string config_file; + std::string get_date(); +void parse_config_file(std::string file_name, std::string *user_name, std::string *tracking_dir); #endif diff --git a/src/ui.cpp b/src/ui.cpp index 60156a8..617e287 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "ui.h" #include "project.h" #include "track.h" @@ -542,7 +543,6 @@ void command_report(std::string date_str, ProjectList* proj_list){ if(date_str.length() == 0){ // report for current month - // get date: std::string date = get_date(); 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{ ProjectList list(month); - list.load(date_str); + std::string file_name = tracking_dir+"/"+date_str; + 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; isave(file_name); std::cout << "Tracking data saved to file " << file_name << std::endl; }