diff --git a/src/tt.cpp b/src/tt.cpp index d8138f2..fbf41a1 100644 --- a/src/tt.cpp +++ b/src/tt.cpp @@ -11,70 +11,10 @@ #include "track.h" #include "ui.h" #include "tt.h" - -std::string get_date(){ - time_t t = time(NULL); - tm *date = localtime(&t); - int month = date->tm_mon+1; - int year = date->tm_year+1900; - std::string month_str; - if (month < 10){ - month_str = "0"+std::to_string(month); - }else{ - month_str = std::to_string(month); - } - std::string date_str = std::to_string(year)+"-"+month_str; - return date_str; -} - -std::string get_last_date(){ - time_t t = time(NULL); - tm *date = localtime(&t); - int month = date->tm_mon; - int year = date->tm_year+1900; - - if(month == 0){ - month = 12; - year--; - } - - std::string month_str; - if (month < 10){ - month_str = "0"+std::to_string(month); - }else{ - month_str = std::to_string(month); - } - std::string date_str = std::to_string(year)+"-"+month_str; - return date_str; -} - -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(); -} +#include "util.h" 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); diff --git a/src/tt.h b/src/tt.h index 58c5acc..a1e92d2 100644 --- a/src/tt.h +++ b/src/tt.h @@ -1,14 +1,9 @@ #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(); -std::string get_last_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 617e287..3dcaf23 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -13,6 +13,7 @@ #include "track.h" #include "string_utils.h" #include "tt.h" +#include "util.h" // Uses GNU readline library for autocompletion and command history diff --git a/src/util.cpp b/src/util.cpp new file mode 100644 index 0000000..166a6d0 --- /dev/null +++ b/src/util.cpp @@ -0,0 +1,62 @@ +#include +#include +#include "util.h" + +std::string get_date(){ + time_t t = time(NULL); + tm *date = localtime(&t); + int month = date->tm_mon+1; + int year = date->tm_year+1900; + std::string month_str; + if (month < 10){ + month_str = "0"+std::to_string(month); + }else{ + month_str = std::to_string(month); + } + std::string date_str = std::to_string(year)+"-"+month_str; + return date_str; +} + +std::string get_last_date(){ + time_t t = time(NULL); + tm *date = localtime(&t); + int month = date->tm_mon; + int year = date->tm_year+1900; + + if(month == 0){ + month = 12; + year--; + } + + std::string month_str; + if (month < 10){ + month_str = "0"+std::to_string(month); + }else{ + month_str = std::to_string(month); + } + std::string date_str = std::to_string(year)+"-"+month_str; + return date_str; +} + +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(); +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..6678b35 --- /dev/null +++ b/src/util.h @@ -0,0 +1,12 @@ +#ifndef UTIL_H +#define UTIL_H + +#define STRING(s) STR(s) +#define STR(s) #s +#include + +std::string get_date(); +std::string get_last_date(); +void parse_config_file(std::string file_name, std::string *user_name, std::string *tracking_dir); + +#endif