From 28e18f6b8d07387ded65305a591b7bdee865843b Mon Sep 17 00:00:00 2001 From: David Anderson Date: Tue, 22 Sep 2015 11:22:07 -0700 Subject: [PATCH] Manager, client: use WxWidgets functions for number formatting Turns out WxWidgets has functions for localized number formatting. This was added in 2.9 so we weren't able to use it last time I checked. Remove our own number-formatting code. Don't do localized number formatting in client. --- client/client_state.cpp | 4 +-- client/cs_prefs.cpp | 20 +++++------ client/work_fetch.cpp | 2 +- clientgui/BOINCGUIApp.cpp | 7 ---- clientgui/DlgItemProperties.cpp | 16 ++++----- clientgui/MainDocument.cpp | 5 +++ clientgui/MainDocument.h | 1 + clientgui/ViewProjects.cpp | 4 +-- clientgui/ViewStatistics.cpp | 10 +++--- clientgui/sg_ProjectPanel.cpp | 2 +- lib/str_util.cpp | 62 +-------------------------------- lib/str_util.h | 7 ---- 12 files changed, 36 insertions(+), 104 deletions(-) diff --git a/client/client_state.cpp b/client/client_state.cpp index d1ae0d3d71..a85352d3ce 100644 --- a/client/client_state.cpp +++ b/client/client_state.cpp @@ -2178,8 +2178,8 @@ void CLIENT_STATE::log_show_projects() { strcpy(buf, "not assigned yet"); } msg_printf(p, MSG_INFO, - "URL %s; Computer ID %s; resource share %s", - p->master_url, buf, comma_print(p->resource_share, 0).c_str() + "URL %s; Computer ID %s; resource share %.0f", + p->master_url, buf, p->resource_share ); if (p->ended) { msg_printf(p, MSG_INFO, "Project has ended - OK to detach"); diff --git a/client/cs_prefs.cpp b/client/cs_prefs.cpp index d40df9f201..3b2b75c494 100644 --- a/client/cs_prefs.cpp +++ b/client/cs_prefs.cpp @@ -654,18 +654,18 @@ void CLIENT_STATE::read_global_prefs( msg_printf(NULL, MSG_INFO, "Preferences:"); msg_printf(NULL, MSG_INFO, - " max memory usage when active: %sMB", - comma_print((host_info.m_nbytes*global_prefs.ram_max_used_busy_frac)/MEGA, 2).c_str() + " max memory usage when active: %.2fMB", + (host_info.m_nbytes*global_prefs.ram_max_used_busy_frac)/MEGA ); msg_printf(NULL, MSG_INFO, - " max memory usage when idle: %sMB", - comma_print((host_info.m_nbytes*global_prefs.ram_max_used_idle_frac)/MEGA, 2).c_str() + " max memory usage when idle: %.2fMB", + (host_info.m_nbytes*global_prefs.ram_max_used_idle_frac)/MEGA ); #ifndef SIM get_disk_usages(); msg_printf(NULL, MSG_INFO, - " max disk usage: %sGB", - comma_print(allowed_disk_usage(total_disk_usage)/GIGA, 2).c_str() + " max disk usage: %.2fGB", + allowed_disk_usage(total_disk_usage)/GIGA ); #endif // max_cpus, bandwidth limits may have changed @@ -695,14 +695,14 @@ void CLIENT_STATE::read_global_prefs( } if (global_prefs.max_bytes_sec_down) { msg_printf(NULL, MSG_INFO, - " max download rate: %s bytes/sec", - comma_print(global_prefs.max_bytes_sec_down, 0).c_str() + " max download rate: %.0f bytes/sec", + global_prefs.max_bytes_sec_down ); } if (global_prefs.max_bytes_sec_up) { msg_printf(NULL, MSG_INFO, - " max upload rate: %s bytes/sec", - comma_print(global_prefs.max_bytes_sec_up, 0).c_str() + " max upload rate: %.0f bytes/sec", + global_prefs.max_bytes_sec_up, 0 ); } #ifndef SIM diff --git a/client/work_fetch.cpp b/client/work_fetch.cpp index 78931770f2..8d37f2049e 100644 --- a/client/work_fetch.cpp +++ b/client/work_fetch.cpp @@ -912,7 +912,7 @@ void WORK_FETCH::handle_reply( void WORK_FETCH::set_initial_work_request(PROJECT* p) { clear_request(); for (int i=0; iresource_share > 0 and !p->dont_request_more_work) { + if (p->resource_share > 0 && !p->dont_request_more_work) { rsc_work_fetch[i].req_secs = 1; if (i) { RSC_WORK_FETCH& rwf = rsc_work_fetch[i]; diff --git a/clientgui/BOINCGUIApp.cpp b/clientgui/BOINCGUIApp.cpp index 24afb7ba85..25064d273f 100644 --- a/clientgui/BOINCGUIApp.cpp +++ b/clientgui/BOINCGUIApp.cpp @@ -310,13 +310,6 @@ bool CBOINCGUIApp::OnInit() { InitSupportedLanguages(); - // get number formatting chars - // - wxString s = wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT); - nfc.decimal_point = s[0]; - s = wxLocale::GetInfo(wxLOCALE_THOUSANDS_SEP); - nfc.thousands_sep = s[0]; - // Note: JAWS for Windows will only speak the context-sensitive // help if you use this help provider: wxHelpProvider::Set(new wxHelpControllerHelpProvider()); diff --git a/clientgui/DlgItemProperties.cpp b/clientgui/DlgItemProperties.cpp index c9348b56b5..2b21fbbea3 100644 --- a/clientgui/DlgItemProperties.cpp +++ b/clientgui/DlgItemProperties.cpp @@ -252,7 +252,7 @@ void CDlgItemProperties::renderInfos(PROJECT* project_in) { addProperty(_("URL"), wxString(project->master_url, wxConvUTF8)); addProperty(_("User name"), wxString(project->user_name.c_str(), wxConvUTF8)); addProperty(_("Team name"), wxString(project->team_name.c_str(), wxConvUTF8)); - addProperty(_("Resource share"), wxString::Format(wxT("%s"), comma_print(project->resource_share, 0).c_str())); + addProperty(_("Resource share"), wxString::Format(wxT("%s"), format_number(project->resource_share, 0))); if (project->min_rpc_time > dtime()) { addProperty(_("Scheduler RPC deferred for"), FormatTime(project->min_rpc_time - dtime())); } @@ -290,24 +290,24 @@ void CDlgItemProperties::renderInfos(PROJECT* project_in) { if (project->ended) { addProperty(_("Ended"), _("yes")); } - addProperty(_("Tasks completed"), wxString::Format(wxT("%s"), comma_print(project->njobs_success, 0).c_str())); - addProperty(_("Tasks failed"), wxString::Format(wxT("%s"), comma_print(project->njobs_error, 0).c_str())); + addProperty(_("Tasks completed"), wxString::Format(wxT("%s"), format_number(project->njobs_success, 0))); + addProperty(_("Tasks failed"), wxString::Format(wxT("%s"), format_number(project->njobs_error, 0))); addSection(_("Credit")); addProperty(_("User"), // Displays the average and total user credit wxString::Format( _("%s total, %s average"), - comma_print(project->user_total_credit, 0).c_str(), - comma_print(project->user_expavg_credit, 2).c_str() + format_number(project->user_total_credit, 0).c_str(), + format_number(project->user_expavg_credit, 2).c_str() ) ); addProperty(_("Host"), // Displays the average and total host credit wxString::Format( _("%s total, %s average"), - comma_print(project->host_total_credit, 0).c_str(), - comma_print(project->host_expavg_credit, 2).c_str() + format_number(project->host_total_credit, 0).c_str(), + format_number(project->host_expavg_credit, 2).c_str() ) ); @@ -383,7 +383,7 @@ void CDlgItemProperties::renderInfos(RESULT* result) { } if (wup) { addProperty(_("Estimated computation size"), - wxString::Format(wxT("%s GFLOPs"), comma_print(wup->rsc_fpops_est/1e9, 0).c_str()) + wxString::Format(wxT("%s GFLOPs"), format_number(wup->rsc_fpops_est/1e9, 0)) ); } if (result->active_task) { diff --git a/clientgui/MainDocument.cpp b/clientgui/MainDocument.cpp index 76ec043095..c59fba26f5 100644 --- a/clientgui/MainDocument.cpp +++ b/clientgui/MainDocument.cpp @@ -20,6 +20,7 @@ #endif #include "stdwx.h" +#include #include "error_numbers.h" #include "util.h" @@ -2692,3 +2693,7 @@ wxString FormatTime(double secs) { wxTimeSpan ts = wxTimeSpan(iHour, iMin, iSec); return ts.Format((secs>=86400)?"%Dd %H:%M:%S":"%H:%M:%S"); } + +wxString format_number(double x, int nprec) { + return wxNumberFormatter::ToString(x, nprec); +} \ No newline at end of file diff --git a/clientgui/MainDocument.h b/clientgui/MainDocument.h index 8e0ec36af5..3b885718a9 100644 --- a/clientgui/MainDocument.h +++ b/clientgui/MainDocument.h @@ -429,6 +429,7 @@ extern double GetYDPIScaling(); #endif wxBitmap GetScaledBitmapFromXPMData(const char** XPMData); +wxString format_number(double x, int nprec); #ifdef SANDBOX #define BOINC_MASTER_GROUP_NAME "boinc_master" diff --git a/clientgui/ViewProjects.cpp b/clientgui/ViewProjects.cpp index 9ea8aca813..59265e3d39 100644 --- a/clientgui/ViewProjects.cpp +++ b/clientgui/ViewProjects.cpp @@ -73,10 +73,10 @@ static int DefaultShownColumns[] = { COLUMN_PROJECT, COLUMN_ACCOUNTNAME, COLUMN_ #define BTN_PROPERTIES 5 static void format_total_credit(double credit, wxString& strBuffer) { - strBuffer = wxString(comma_print(credit, 0).c_str(), wxConvUTF8); + strBuffer = format_number(credit, 0); } static void format_avg_credit(double credit, wxString& strBuffer) { - strBuffer = wxString(comma_print(credit, 2).c_str(), wxConvUTF8); + strBuffer = format_number(credit, 2); } CProject::CProject() { diff --git a/clientgui/ViewStatistics.cpp b/clientgui/ViewStatistics.cpp index 62d1bc6b69..6706d84bb5 100644 --- a/clientgui/ViewStatistics.cpp +++ b/clientgui/ViewStatistics.cpp @@ -662,8 +662,8 @@ void CPaintStatistics::DrawAxis(wxDC &dc, const double max_val_y, const double m wxCoord w_temp, h_temp, des_temp, lead_temp; wxCoord w_temp2; - dc.GetTextExtent(wxString::Format(wxT(" %s"), comma_print(max_val_y_all, 2).c_str()), &w_temp, &h_temp, &des_temp, &lead_temp); - dc.GetTextExtent(wxString::Format(wxT(" %s"), comma_print(min_val_y_all, 2).c_str()), &w_temp2, &h_temp, &des_temp, &lead_temp); + dc.GetTextExtent(wxString::Format(wxT(" %s"), format_number(max_val_y_all, 2)), &w_temp, &h_temp, &des_temp, &lead_temp); + dc.GetTextExtent(wxString::Format(wxT(" %s"), format_number(min_val_y_all, 2)), &w_temp2, &h_temp, &des_temp, &lead_temp); if (w_temp < w_temp2) w_temp = w_temp2; @@ -740,7 +740,7 @@ void CPaintStatistics::DrawAxis(wxDC &dc, const double max_val_y, const double m d_oy_count = (int)floor((max_val_y - y_start_val) / d_oy_val); for (double ny = 0; ny <= double(d_oy_count); ++ny){ - dc.GetTextExtent(wxString::Format(wxT("%s"), comma_print(y_start_val + ny * d_oy_val, 2).c_str()), &w_temp, &h_temp, &des_temp, &lead_temp); + dc.GetTextExtent(wxString::Format(wxT("%s"), format_number(y_start_val + ny * d_oy_val, 2)), &w_temp, &h_temp, &des_temp, &lead_temp); x0 = wxCoord(m_Graph_X_start + 1.0); y0 = wxCoord(m_Ay_ValToCoord * (y_start_val + ny * d_oy_val) + m_By_ValToCoord); x1 = wxCoord(m_Graph_X_end - 1.0); @@ -753,7 +753,7 @@ void CPaintStatistics::DrawAxis(wxDC &dc, const double max_val_y, const double m y0 = wxCoord(m_Ay_ValToCoord * (y_start_val + ny * d_oy_val) + m_By_ValToCoord - double(h_temp) / 2.0); if (x0 < 0) x0 = 0; if (y0 < 0) y0 = 0; - dc.DrawText(wxString::Format(wxT("%s"), comma_print(y_start_val + ny * d_oy_val, 2).c_str()), x0, y0); + dc.DrawText(wxString::Format(wxT("%s"), format_number(y_start_val + ny * d_oy_val, 2)), x0, y0); } } @@ -1108,7 +1108,7 @@ void CPaintStatistics::DrawMarker(wxDC &dc) { if (y0 < 0) y0 = 0; dc.SetTextForeground (m_pen_AxisYTextColour); - dc.DrawText(wxString::Format(wxT("%s"), comma_print(m_GraphMarker_Y1, 2).c_str()) , x0, y00); + dc.DrawText(wxString::Format(wxT("%s"), format_number(m_GraphMarker_Y1, 2)) , x0, y00); dc.SetTextForeground (m_pen_AxisXTextColour); dc.DrawText(strBuffer1 ,x00, y0); dc.SetBackgroundMode(wxTRANSPARENT); diff --git a/clientgui/sg_ProjectPanel.cpp b/clientgui/sg_ProjectPanel.cpp index cb3d44957d..38ccf9e48f 100644 --- a/clientgui/sg_ProjectPanel.cpp +++ b/clientgui/sg_ProjectPanel.cpp @@ -269,7 +269,7 @@ void CSimpleProjectPanel::UpdateInterface() { if (m_fDisplayedCredit != project->user_total_credit) { str.Printf(wxT("%s: %s"), m_sTotalWorkDoneString.c_str(), - comma_print(project->user_total_credit, 0).c_str() + format_number(project->user_total_credit, 0) ); UpdateStaticText(&m_TotalCreditValue, str); m_TotalCreditValue->SetName(str); // For accessibility on Windows diff --git a/lib/str_util.cpp b/lib/str_util.cpp index 58bd675418..1f7f7c2924 100644 --- a/lib/str_util.cpp +++ b/lib/str_util.cpp @@ -755,64 +755,4 @@ vector split(string s, char delim) { result.push_back(item); } return result; -} - -NUM_FORMAT_CHARS nfc; - -static void get_format_chars() { - if (nfc.thousands_sep) return; -#ifdef _WIN32 - char buf[256]; - GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, buf, sizeof(buf)); - nfc.thousands_sep = buf[0]; - GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, buf, sizeof(buf)); - nfc.decimal_point = buf[0]; -#else - struct lconv *lcp = localeconv(); - if (lcp) { - if (lcp->thousands_sep != NULL && *lcp->thousands_sep) { - nfc.thousands_sep = *lcp->thousands_sep; - nfc.decimal_point = *lcp->decimal_point; - } else { - nfc.thousands_sep = ','; - nfc.decimal_point = '.'; - } - } -#endif -} - -// convert number to string with thousands separators. -// If nfrac is nonzero, following with fractional digits -// -string comma_print(double x, int nfrac) { - static char retbuf[30]; - char *p = &retbuf[sizeof(retbuf)-1]; - int i = 0; - - *p = 0; - - get_format_chars(); - - unsigned long long n = x; - - // do fractional part if requested - // - if (nfrac) { - double frac = x - n; - p -= nfrac+1; - sprintf(p, "%.*f", nfrac, frac); - p++; // skip 0 - *p = nfc.decimal_point; - } - - do { - if (i%3 == 0 && i != 0) { - *--p = nfc.thousands_sep; - } - *--p = '0' + n % 10; - n /= 10; - i++; - } while (n); - - return string(p); -} +} \ No newline at end of file diff --git a/lib/str_util.h b/lib/str_util.h index a91213d6ae..410d695b48 100644 --- a/lib/str_util.h +++ b/lib/str_util.h @@ -102,11 +102,4 @@ extern void strip_translation(char* p); extern std::vector split(std::string, char delim); -struct NUM_FORMAT_CHARS { - char thousands_sep; - char decimal_point; -}; -extern NUM_FORMAT_CHARS nfc; -extern std::string comma_print(double, int); - #endif