mirror of https://github.com/PatrickLipka/tt.git
Utility functions moved to util.cpp
This commit is contained in:
parent
416bcbde0a
commit
8625034382
62
src/tt.cpp
62
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);
|
||||
|
|
5
src/tt.h
5
src/tt.h
|
@ -1,14 +1,9 @@
|
|||
#ifndef TT_H
|
||||
#define TT_H
|
||||
#define STRING(s) STR(s)
|
||||
#define STR(s) #s
|
||||
|
||||
#include <string>
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
|
@ -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
|
Loading…
Reference in New Issue