diff --git a/camerad/archon.cpp b/camerad/archon.cpp index d3efad3b..8e6cef22 100644 --- a/camerad/archon.cpp +++ b/camerad/archon.cpp @@ -4939,7 +4939,8 @@ namespace Archon { std::cerr << "exposure progress: "; while ((now - (waittime + start_time) < 0) && !this->abort) { - timeout(0.010); // sleep 10 msec = 1e6 Archon ticks + // sleep 10 msec = 1e6 Archon ticks + std::this_thread::sleep_for( std::chrono::milliseconds( 10 )); increment += 1000000; now = get_clock_time(); this->camera_info.exposure_progress = (double)increment / (double)(prediction - this->start_timer); @@ -5002,8 +5003,8 @@ namespace Archon { break; } - timeout( 0.001 ); // a little pause to slow down the requests to Archon - + // a little pause to slow down the requests to Archon + std::this_thread::sleep_for( std::chrono::milliseconds( 1 )); // Added protection against infinite loops, probably will never be invoked // because an Archon error getting the timer would exit the loop. // exposure_timeout_time is in msec, and it's a little more than 1 msec to get diff --git a/utils/utilities.cpp b/utils/utilities.cpp index 448beba5..82845e80 100644 --- a/utils/utilities.cpp +++ b/utils/utilities.cpp @@ -49,7 +49,7 @@ std::mutex generate_tmpfile_mtx; if ( itr != end && ++itr != end ) { return *itr; } - return 0; + return nullptr; } /***** getCmdOption *********************************************************/ @@ -64,9 +64,9 @@ std::mutex generate_tmpfile_mtx; */ int my_hardware_concurrency() { std::ifstream cpuinfo( "/proc/cpuinfo" ); - return std::count( std::istream_iterator(cpuinfo), + return static_cast(std::count( std::istream_iterator(cpuinfo), std::istream_iterator(), - std::string("processor") ); + std::string("processor") )); } /***** my_hardware_concurrency **********************************************/ @@ -80,7 +80,7 @@ std::mutex generate_tmpfile_mtx; * */ int cores_available() { - unsigned int cores = std::thread::hardware_concurrency(); + int cores = static_cast(std::thread::hardware_concurrency()); return cores ? cores : my_hardware_concurrency(); } /***** cores_available ******************************************************/ @@ -120,7 +120,7 @@ std::mutex generate_tmpfile_mtx; tokens.clear(); // If the string is zero length, return now with no tokens - if (str.length() == 0) { return(0); } + if (str.empty()) { return(0); } // Skip delimiters at beginning. std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); @@ -132,7 +132,7 @@ std::mutex generate_tmpfile_mtx; unsigned int quote_start = str.find(quote); //finds first quote mark bool quotes_found = false; - if (quote_start != std::string::npos) { + if (quote_start <= str.length()) { } else { quote_start = -1; @@ -155,7 +155,7 @@ std::mutex generate_tmpfile_mtx; // If the next character is a quote, grab between the quotes if (std::string::npos != lastPos && lastPos == quote_start){ - pos = str.find_first_of("\"", lastPos + 1) + 1; + pos = str.find_first_of('\"', lastPos + 1) + 1; quotes_found = true; } // Otherwise, find next "non-delimiter" @@ -163,7 +163,7 @@ std::mutex generate_tmpfile_mtx; pos = str.find_first_of(delimiters, lastPos); } } - return(tokens.size()); + return static_cast(tokens.size()); } /***** Tokenize *************************************************************/ @@ -190,12 +190,12 @@ std::mutex generate_tmpfile_mtx; devlist.clear(); ndev=0; // empty the dev and arg list vectors arglist.clear(); narg=0; - std::size_t devdelim = str.find( ":" ); // position of device delimiter + std::size_t devdelim = str.find( ':' ); // position of device delimiter // If there is a device delimiter then build a vector of the device numbers // if ( devdelim != std::string::npos ) { - std::string dev_str = str.substr( 0, str.find( ":" ) ); + std::string dev_str = str.substr( 0, str.find( ':' ) ); std::vector tokens; Tokenize( dev_str, tokens, "," ); // Tokenize the dev string on the comma "," for ( const auto &tok : tokens ) { @@ -211,7 +211,7 @@ std::mutex generate_tmpfile_mtx; return; } } - ndev = devlist.size(); + ndev = static_cast(devlist.size()); } // Anything left, look for space-delimited tokens for the arg list @@ -222,10 +222,8 @@ std::mutex generate_tmpfile_mtx; for ( const auto &tok : tokens ) { arglist.push_back( tok ); } - narg = arglist.size(); - - return; - } + narg = static_cast(arglist.size()); + } /***** Tokenize *************************************************************/ @@ -317,8 +315,8 @@ std::mutex generate_tmpfile_mtx; long get_time( const std::string &tmzone_in, int &year, int &mon, int &mday, int &hour, int &min, int &sec, int &usec ) { std::stringstream current_time; // String to contain the time std::time_t t = std::time(nullptr); // Container for system time - struct timespec timenow; // Time of day container - struct tm mytime; // GMT time container + timespec timenow{}; // Time of day container + tm mytime{}; // GMT time container long error = 0; // Get the system time, return a bad timestamp on error @@ -401,7 +399,7 @@ std::mutex generate_tmpfile_mtx; std::string timestamp_from( const std::string &tmzone_in, struct timespec &time_in ) { std::stringstream current_time; // String to contain the time std::time_t t=std::time(nullptr); // Container for system time - struct tm time; // time container + tm time{}; // time container // Convert the input time to local or GMT // @@ -452,8 +450,8 @@ std::mutex generate_tmpfile_mtx; std::string get_system_date( const std::string &tmzone_in ) { std::stringstream current_date; // String to contain the return value std::time_t t=std::time(nullptr); // Container for system time - struct timespec timenow;; // Time of day container - struct tm mytime; // time container + timespec timenow{}; // Time of day container + tm mytime{}; // time container // Get the system time, return a bad datestamp on error // @@ -502,8 +500,8 @@ std::mutex generate_tmpfile_mtx; std::string get_file_time( const std::string &tmzone_in ) { std::stringstream current_time; // String to contain the time std::time_t t=std::time(nullptr); // Container for system time - struct timespec timenow; // Time of day container - struct tm mytime; // time container + timespec timenow{}; // Time of day container + tm mytime{}; // time container // Get the system time, return a bad timestamp on error // @@ -536,9 +534,9 @@ std::mutex generate_tmpfile_mtx; * */ double get_clock_time() { - struct timespec data; // Container for the current time + timespec data{}; // Container for the current time if (clock_gettime(CLOCK_REALTIME, &data) != 0) return 0; - return ( data.tv_sec + (data.tv_nsec / 1000000000.0) ); + return ( static_cast(data.tv_sec) + (static_cast(data.tv_nsec) / 1000000000.0) ); } /***** get_clock_time *******************************************************/ @@ -551,13 +549,14 @@ std::mutex generate_tmpfile_mtx; * @return 0 on success, 1 on error * */ - long timeout( int wholesec, std::string next ) { + long timeout( int wholesec, const std::string &next ) { std::time_t t=std::time(nullptr); // Container for system time - struct timespec timenow; // Time of day container - struct tm mytime; // GMT time container + timespec timenow{}; // Time of day container + tm mytime{}; // GMT time container long error=0; - int nsec, sec; + int nsec=0; + int sec=0; // sleep for any requested whole number of seconds // @@ -573,7 +572,7 @@ std::mutex generate_tmpfile_mtx; t = timenow.tv_sec; if ( gmtime_r( &t, &mytime ) == nullptr ) error = 1; sec = mytime.tm_sec; // current second - nsec = timenow.tv_nsec; // current nanosecond + nsec = static_cast(timenow.tv_nsec); // current nanosecond } // sleep for the required fraction to get to the next whole number @@ -583,9 +582,7 @@ std::mutex generate_tmpfile_mtx; if (nsec < 999999999) { std::this_thread::sleep_for( std::chrono::nanoseconds( 999999999-nsec ) ); } - } - else - if ( !error && next == "min" ) { + } else if ( !error && next == "min" ) { if (sec < 59) { std::this_thread::sleep_for( std::chrono::seconds( 59-sec ) ); } @@ -593,7 +590,6 @@ std::mutex generate_tmpfile_mtx; std::this_thread::sleep_for( std::chrono::nanoseconds( 999999999-nsec ) ); } } - return error; } /***** timeout **************************************************************/ @@ -615,32 +611,30 @@ std::mutex generate_tmpfile_mtx; * @return double, 0 on error * */ - double mjd_from( struct timespec &time_in ) { + double mjd_from( timespec &time_in ) { std::time_t t=std::time(nullptr); // Container for system time - struct tm time; // GMT time container - double a, y, m; - double jdn, jd; + tm time{}; // GMT time container // Convert the input time to GMT // t = time_in.tv_sec; if ( gmtime_r( &t, &time ) == nullptr ) return 0.; - a = std::floor((14 - (time.tm_mon + 1)) / 12); - y = (time.tm_year + 1900) +4800 - a; - m = (time.tm_mon + 1) + 12 * a - 3; - jdn = time.tm_mday - + std::floor((153 * m + 2) / 5) - + 365 * y - + std::floor(y / 4) - - std::floor(y / 100) - + std::floor(y / 400) - - 32045; - - jd = jdn + (time.tm_hour - 12) / 24. - + time.tm_min / 1440. - + time.tm_sec / 86400. - + (time_in.tv_nsec/1000000000.)/86400.; + double a = std::floor((14 - (time.tm_mon + 1)) / 12); + double y = (time.tm_year + 1900) + 4800 - a; + double m = (time.tm_mon + 1) + 12 * a - 3; + double jdn = time.tm_mday + + std::floor((153 * m + 2) / 5) + + 365 * y + + std::floor(y / 4) + - std::floor(y / 100) + + std::floor(y / 400) + - 32045; + + double jd = jdn + (time.tm_hour - 12) / 24. + + time.tm_min / 1440. + + time.tm_sec / 86400. + + (static_cast(time_in.tv_nsec) / 1000000000.) / 86400.; return( jd - 2400000.5 ); } @@ -675,7 +669,7 @@ std::mutex generate_tmpfile_mtx; Tokenize( v2, tokens2, "." ); // Compare each token. - // As soon as one is greater than the other then return. + // As soon as one is greater than the other, then return. // for (size_t i=0,j=0; ( i < tokens1.size() && j < tokens2.size() ); i++,j++) { try { @@ -750,8 +744,8 @@ std::mutex generate_tmpfile_mtx; // convert result to a string // std::stringstream str; - for (int i = 0; i < MD5_BLOCK_SIZE; ++i) { - str << std::hex << std::setw(2) << std::setfill('0') << static_cast(result[i]); + for (unsigned char i : result) { + str << std::hex << std::setw(2) << std::setfill('0') << static_cast(i); } hash = str.str(); @@ -770,7 +764,7 @@ std::mutex generate_tmpfile_mtx; * */ bool is_owner( const std::filesystem::path &filename ) { - struct stat fstat; + struct stat fstat{}; if ( stat( filename.c_str(), &fstat ) == 0 ) { // Check if the effective user ID matches the file's owner ID return geteuid() == fstat.st_uid; @@ -789,7 +783,7 @@ std::mutex generate_tmpfile_mtx; * */ bool has_write_permission( const std::filesystem::path &filename ) { - struct stat fstat; + struct stat fstat{}; if ( stat( filename.c_str(), &fstat ) == 0 ) { // Check if the effective user ID has write permission return fstat.st_mode & S_IWUSR; @@ -914,3 +908,13 @@ std::mutex generate_tmpfile_mtx; } } /***** generate_temp_filename ***********************************************/ + +/***** rtrim ***********************************************/ +/** + * @s string from which to trim trailing whitespaces + * + */ +void rtrim(std::string &s) { /// trim off trailing whitespace from a string + s.erase( std::find_if( s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); } ).base(), s.end() ); + } +/***** rtrim ***********************************************/ \ No newline at end of file diff --git a/utils/utilities.h b/utils/utilities.h index e8126ff9..892ced01 100644 --- a/utils/utilities.h +++ b/utils/utilities.h @@ -63,7 +63,7 @@ std::string timestamp_from( const std::string &tmzone_in, struct timespec &time_ inline std::string get_timestamp(const std::string &tz) { /// return current time in formatted string "YYYY-MM-DDTHH:MM:SS.sss" - struct timespec timenow; + struct timespec timenow{}; clock_gettime( CLOCK_REALTIME, &timenow ); return timestamp_from( tz, timenow ); } @@ -80,12 +80,12 @@ std::string get_file_time( const std::string &tmzone_in ); double get_clock_time(); -long timeout( int wholesec=0, std::string next="" ); /// wait until next integral second or minute +long timeout( int wholesec=0, const std::string &next="" ); /// wait until next integral second or minute double mjd_from( struct timespec &time_n ); /// modified Julian date from input timespec struct inline double mjd_now() { /// modified Julian date now - struct timespec timenow; + timespec timenow{}; clock_gettime( CLOCK_REALTIME, &timenow ); return( mjd_from( timenow ) ); } @@ -103,9 +103,7 @@ bool ends_with( const std::string &str, std::string_view suffix ); std::string generate_temp_filename( const std::string &prefix ); -static inline void rtrim(std::string &s) { /// trim off trailing whitespace from a string - s.erase( std::find_if( s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); } ).base(), s.end() ); -} +void rtrim(std::string &s); inline bool caseCompareChar( char a, char b ) { return ( std::toupper(a) == std::toupper(b) ); } @@ -145,12 +143,12 @@ std::string to_string_prec( const T value_in, const int prec = 6 ) { class InterruptableSleepTimer { private: std::timed_mutex _mut; - std::atomic _locked; // track whether the mutex is locked + std::atomic _locked{}; // track whether the mutex is locked std::atomic _run; - inline void _lock() { _mut.lock(); _locked = true; } // lock mutex + void _lock() { _mut.lock(); _locked = true; } // lock mutex - inline void _unlock() { _locked = false; _mut.unlock(); } // unlock mutex + void _unlock() { _locked = false; _mut.unlock(); } // unlock mutex public: // lock on creation @@ -168,7 +166,7 @@ class InterruptableSleepTimer { } } - inline bool running() { return _run; } + bool running() { return _run; } // called by any thread except the creator // waits until wake is called or the specified time passes @@ -186,13 +184,13 @@ class InterruptableSleepTimer { // where wake has already been called. // should only be called by the creating thread // - inline void stop() { + void stop() { if ( _locked ) { _run = false; _unlock(); } } - inline void start() { + void start() { if ( ! _locked ) { _lock(); _run = true; @@ -213,7 +211,7 @@ class InterruptableSleepTimer { class Time { public: static timespec getTimeNow() { - struct timespec timenow; + timespec timenow{}; clock_gettime( CLOCK_REALTIME, &timenow ); return timenow; }