X-Git-Url: http://code.wpia.club/?a=blobdiff_plain;f=src%2Futil.cpp;h=30f87b77e1569b53bfc396cdbdb56d2676a2e230;hb=156855b7e12c3a0254590da514b0d0e8efe469f4;hp=c16ba1c6c8d541164fc5ff7fd540aadb74d1e7fa;hpb=bfd47498091c1062c5d2c07884003c2525b60ca1;p=cassiopeia.git diff --git a/src/util.cpp b/src/util.cpp index c16ba1c..30f87b7 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -9,11 +9,8 @@ #include 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 } @@ -21,7 +18,6 @@ void writeFile( const std::string& name, const std::string& content ) { std::string readFile( const std::string& name ) { std::ifstream t( name ); std::string res = std::string( std::istreambuf_iterator( t ), std::istreambuf_iterator() ); - t.close(); return res; } @@ -33,7 +29,7 @@ std::string writeBackFile( const std::string& serial, const std::string& cert, c 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 @@ -113,7 +109,7 @@ std::pair parseDate( const std::string& date ) { std::string checkS( check, siz ); if( checkS != date ) { - return std::pair( false, 0 ); + return { false, 0 }; } return std::pair( true, res ); @@ -138,7 +134,6 @@ std::pair addMonths( std::time_t t, int32_t count ) { } return std::pair( true, res ); - } std::pair parseMonthInterval( std::time_t t, const std::string& date ) { @@ -161,6 +156,7 @@ std::pair parseMonthInterval( std::time_t t, const std::string& da return std::pair( false, 0 ); } } + std::pair parseYearInterval( std::time_t t, const std::string& date ) { if( date[date.size() - 1] != 'y' ) { return std::pair( false, 0 ); @@ -182,20 +178,28 @@ std::pair parseYearInterval( std::time_t t, const std::string& dat } } -std::shared_ptr openLogfile( const std::string name) { +std::unique_ptr 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(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( 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 ); +}