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.
This commit is contained in:
David Anderson 2015-09-22 11:22:07 -07:00
parent fcb359d2ee
commit 28e18f6b8d
12 changed files with 36 additions and 104 deletions

View File

@ -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");

View File

@ -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

View File

@ -912,7 +912,7 @@ void WORK_FETCH::handle_reply(
void WORK_FETCH::set_initial_work_request(PROJECT* p) {
clear_request();
for (int i=0; i<coprocs.n_rsc; i++) {
if (p->resource_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];

View File

@ -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());

View File

@ -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) {

View File

@ -20,6 +20,7 @@
#endif
#include "stdwx.h"
#include <wx/numformatter.h>
#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);
}

View File

@ -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"

View File

@ -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() {

View File

@ -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);

View File

@ -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

View File

@ -755,64 +755,4 @@ vector<string> 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);
}
}

View File

@ -102,11 +102,4 @@ extern void strip_translation(char* p);
extern std::vector<std::string> 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