Utility functions moved to util.cpp

This commit is contained in:
Patrick Lipka 2021-12-16 16:42:31 +01:00
parent 416bcbde0a
commit 8625034382
5 changed files with 76 additions and 66 deletions

View File

@ -11,70 +11,10 @@
#include "track.h" #include "track.h"
#include "ui.h" #include "ui.h"
#include "tt.h" #include "tt.h"
#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();
}
std::string user_name, tracking_dir, config_file; 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);

View File

@ -1,14 +1,9 @@
#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 tracking_dir;
extern std::string config_file; 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 #endif

View File

@ -13,6 +13,7 @@
#include "track.h" #include "track.h"
#include "string_utils.h" #include "string_utils.h"
#include "tt.h" #include "tt.h"
#include "util.h"
// Uses GNU readline library for autocompletion and command history // Uses GNU readline library for autocompletion and command history

62
src/util.cpp Normal file
View File

@ -0,0 +1,62 @@
#include <fstream>
#include <iostream>
#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();
}

12
src/util.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef UTIL_H
#define UTIL_H
#define STRING(s) STR(s)
#define STR(s) #s
#include <string>
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