From 7261b34ace3d50dd3d324b88ab329a9828909bc2 Mon Sep 17 00:00:00 2001 From: Justin Crawford Date: Mon, 15 Apr 2013 11:33:28 -0700 Subject: [PATCH] Don't use namespace std. It makes things too confusing. --- CMakeLists.txt | 4 ---- tmux-mem-cpu-load.cpp | 50 +++++++++++++++++++++---------------------- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bc0d70d..ea96196 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,10 +11,6 @@ if(NOT CMAKE_BUILD_TYPE) FORCE) endif(NOT CMAKE_BUILD_TYPE) -if(APPLE) - message(SEND_ERROR "OSX is currently not supported. Patches are welcome.") -endif() - add_executable(tmux-mem-cpu-load tmux-mem-cpu-load.cpp) install(TARGETS tmux-mem-cpu-load RUNTIME DESTINATION bin diff --git a/tmux-mem-cpu-load.cpp b/tmux-mem-cpu-load.cpp index 94ffcb5..30c090f 100644 --- a/tmux-mem-cpu-load.cpp +++ b/tmux-mem-cpu-load.cpp @@ -18,13 +18,11 @@ #include #include #include -using namespace std; - #include // sleep float cpu_percentage( unsigned int cpu_usage_delay ) { - string stat_line; + std::string stat_line; size_t line_start_pos; size_t line_end_pos; unsigned long long current_user; @@ -39,10 +37,10 @@ float cpu_percentage( unsigned int cpu_usage_delay ) unsigned long long diff_system; unsigned long long diff_nice; unsigned long long diff_idle; - istringstream iss; + std::istringstream iss; - ifstream stat_file("/proc/stat"); - getline(stat_file, stat_line); + std::ifstream stat_file("/proc/stat"); + std::getline(stat_file, stat_line); stat_file.close(); // skip "cpu" @@ -59,7 +57,7 @@ float cpu_percentage( unsigned int cpu_usage_delay ) usleep( cpu_usage_delay ); stat_file.open("/proc/stat"); - getline(stat_file, stat_line); + std::getline(stat_file, stat_line); stat_file.close(); // skip "cpu" @@ -81,16 +79,16 @@ float cpu_percentage( unsigned int cpu_usage_delay ) return static_cast(diff_user + diff_system + diff_nice)/static_cast(diff_user + diff_system + diff_nice + diff_idle)*100.0; } -string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines ) +std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines ) { - string meter( graph_lines + 2, ' ' ); + std::string meter( graph_lines + 2, ' ' ); meter[0] = '['; meter[meter.length() - 1] = ']'; int meter_count = 0; float percentage; - ostringstream oss; + std::ostringstream oss; oss.precision( 1 ); - oss.setf( ios::fixed | ios::right ); + oss.setf( std::ios::fixed | std::ios::right ); percentage = cpu_percentage( cpu_usage_delay ); float meter_step = 99.9 / graph_lines; @@ -109,19 +107,19 @@ string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines ) return oss.str(); } -string mem_string() +std::string mem_string() { unsigned int total_mem; unsigned int used_mem; unsigned int unused_mem; size_t line_start_pos; size_t line_end_pos; - istringstream iss; - ostringstream oss; - string mem_line; + std::istringstream iss; + std::ostringstream oss; + std::string mem_line; - ifstream meminfo_file( "/proc/meminfo" ); - getline( meminfo_file, mem_line ); + std::ifstream meminfo_file( "/proc/meminfo" ); + std::getline( meminfo_file, mem_line ); line_start_pos = mem_line.find_first_of( ':' ); line_start_pos++; line_end_pos = mem_line.find_first_of( 'k' ); @@ -132,7 +130,7 @@ string mem_string() for( unsigned int i = 0; i < 3; i++ ) { - getline( meminfo_file, mem_line ); + std::getline( meminfo_file, mem_line ); line_start_pos = mem_line.find_first_of( ':' ); line_start_pos++; line_end_pos = mem_line.find_first_of( 'k' ); @@ -147,11 +145,11 @@ string mem_string() return oss.str(); } -string load_string() +std::string load_string() { - ifstream loadavg_file( "/proc/loadavg" ); - string load_line; - getline( loadavg_file, load_line ); + std::ifstream loadavg_file( "/proc/loadavg" ); + std::string load_line; + std::getline( loadavg_file, load_line ); loadavg_file.close(); return load_line.substr( 0, 14 ); @@ -163,8 +161,8 @@ int main(int argc, char** argv) unsigned int graph_lines = 10; try { - istringstream iss; - iss.exceptions ( ifstream::failbit | ifstream::badbit ); + std::istringstream iss; + iss.exceptions ( std::ifstream::failbit | std::ifstream::badbit ); if( argc > 1 ) { iss.str( argv[1] ); @@ -179,9 +177,9 @@ int main(int argc, char** argv) iss >> graph_lines; } } - catch(const exception &e) + catch(const std::exception &e) { - cerr << "Usage: " << argv[0] << " [tmux_status-interval(seconds)] [graph lines]" << endl; + std::cerr << "Usage: " << argv[0] << " [tmux_status-interval(seconds)] [graph lines]" << std::endl; return 1; }