#include <stdexcept>
void writeFile( const std::string& name, const std::string& content ) {
- std::ofstream file;
-
- file.open( name );
+ std::ofstream file( name );
file << content;
- file.close();
//! \FIXME: Error checking
}
std::string readFile( const std::string& name ) {
std::ifstream t( name );
std::string res = std::string( std::istreambuf_iterator<char>( t ), std::istreambuf_iterator<char>() );
- t.close();
return res;
}
if( 0 != mkdir( filename.c_str(), 0755 ) ) {
if( EEXIST != errno ) {
- return "";
+ throw std::runtime_error("Storage location could not be determined");
}
//! \FIXME: Check this is a directory
return std::pair<bool, time_t>( false, 0 );
}
- std::tm t;
+ std::tm t = {};
t.tm_sec = 0;
t.tm_min = 0;
t.tm_hour = 0;
- t.tm_year = std::stoi( date.substr( 0, 4 ) ) - 1900;
- t.tm_mon = std::stoi( date.substr( 5, 2 ) ) - 1;
t.tm_mday = std::stoi( date.substr( 8, 2 ) );
+ t.tm_mon = std::stoi( date.substr( 5, 2 ) ) - 1;
+ t.tm_year = std::stoi( date.substr( 0, 4 ) ) - 1900;
+
setenv( "TZ", "UTC", 1 );
tzset();
std::time_t res = mktime( &t );
std::string checkS( check, siz );
if( checkS != date ) {
- return std::pair<bool, time_t>( false, 0 );
+ return { false, 0 };
}
return std::pair<bool, time_t>( true, res );
}
return std::pair<bool, time_t>( true, res );
-
}
std::pair<bool, time_t> parseMonthInterval( std::time_t t, const std::string& date ) {
return std::pair<bool, time_t>( false, 0 );
}
}
+
std::pair<bool, time_t> parseYearInterval( std::time_t t, const std::string& date ) {
if( date[date.size() - 1] != 'y' ) {
return std::pair<bool, time_t>( false, 0 );
}
}
-std::shared_ptr<std::ofstream> openLogfile( const std::string name) {
+std::unique_ptr<std::ofstream> openLogfile( const std::string &name ) {
struct stat buffer;
std::string tname = name;
int ctr = 2;
- while(stat (tname.c_str(), &buffer) == 0) {
- tname = name + "_" + std::to_string(ctr++);
+
+ while( stat( tname.c_str(), &buffer ) == 0 ) {
+ tname = name + "_" + std::to_string( ctr++ );
}
- auto res = std::shared_ptr<std::ofstream>(new std::ofstream( tname ),
- [](std::ofstream *p){
- p->close();
- delete p;
- });
- if(! res->good() ){
- throw std::string("Failed to open file for logging: ") + name;
+
+ auto res = std::make_unique<std::ofstream>( tname );
+
+ if( ! res->good() ) {
+ throw std::runtime_error( std::string("Failed to open file for logging: " ) + name );
}
+
return res;
}
+
+std::string timestamp(){
+ time_t c_time;
+ if( time( &c_time ) == -1 ) {
+ throw std::runtime_error( "Error while fetching time?" );
+ }
+ return std::to_string( c_time );
+}