From f9c7a5f62f96fab9ce5e3d2c205bc9da3046c4d4 Mon Sep 17 00:00:00 2001 From: Tomas Bzatek Date: Sun, 8 Jun 2008 17:35:32 +0200 Subject: Format numbers according to used locale --- UConfig.pas | 4 +-- UCoreUtils.pas | 58 +++++++++++++++++++++++++++++----------- UPreferences.pas | 2 +- translations/UTranslation_EN.pas | 2 +- 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/UConfig.pas b/UConfig.pas index 31105c9..48cd1cf 100644 --- a/UConfig.pas +++ b/UConfig.pas @@ -25,8 +25,8 @@ uses Classes, ULocale; resourcestring ConstAppTitle = 'Tux Commander'; - ConstAboutVersion = '0.6.38-dev'; - ConstAboutBuildDate = '2008-06-07'; + ConstAboutVersion = '0.6.39-dev'; + ConstAboutBuildDate = '2008-06-08'; {$IFDEF __FPC__} {$INCLUDE fpcver.inc} diff --git a/UCoreUtils.pas b/UCoreUtils.pas index f297fe0..31c72b1 100644 --- a/UCoreUtils.pas +++ b/UCoreUtils.pas @@ -57,7 +57,7 @@ function GetErrorString(ErrorNo: integer): string; function GetSignalString(SignalNo: integer): string; function UnixTimeToTDateTime(UnixTime: Int64): TDateTime; -function FormatSize(Value: Int64; Base: integer): string; +function FormatSize(Value: Int64; Base: integer; OverrideSizeFormat: integer = -1): string; function AttrToStr(const Mode: Cardinal; IncludeFileType: boolean = True): string; function AttrToOctal(const Mode: Cardinal): integer; @@ -196,14 +196,24 @@ begin Insert(Sep, Result, i); end; -function FormatSize(Value: Int64; Base: integer): string; +function FormatSize(Value: Int64; Base: integer; OverrideSizeFormat: integer = -1): string; var s: string; - i: integer; + f, i: integer; + p: PChar; + x: gdouble; begin if Base < 1 then Base := 1; - case ConfSizeFormat of + f := OverrideSizeFormat; + if f < 0 then f := ConfSizeFormat; + case f of 0 : begin // System default formatting - Result := FormatFloat('###,###,##0', Value div Base); + p := malloc(255); + memset(p, 0, 255); + if sprintf(p, '%''lu', Value div Base) < 1 then begin + DebugMsg(['FormatSize(0): sprintf() failed, using old format function.']); + Result := FormatFloat('###,###,##0', Value div Base); + end else Result := StrToUTF8(p); + Libc.free(p); end; 1 : begin // 123456 Result := IntToStr(Value div Base); @@ -218,17 +228,35 @@ begin Result := FormatFloat64(Value div Base, ''''); end; 5 : begin // 123kB - Grouped - s := '###,###,##0'; - if ConfSizeGroupPrecision > 0 then begin - s := s + '.'; - for i := 1 to ConfSizeGroupPrecision do - if ConfSizeGroupRequestZeroDigits then s := s + '0' - else s := s + '#'; + if (Value >= 1024*1024*1024) or (Base = 1024*1024*1024) then begin + x := Value / (1024*1024*1024); + p := g_strdup_printf(PChar('%''.' + IntToStr(ConfSizeGroupPrecision) + 'f GB'), x); + end else + if (Value >= 1024*1024) or (Base = 1024*1024) then begin + x := Value / (1024*1024); + p := g_strdup_printf(PChar('%''.' + IntToStr(ConfSizeGroupPrecision) + 'f MB'), x); + end else + if (Value >= 1024) or (Base = 1024) then begin + x := Value / 1024; + p := g_strdup_printf(PChar('%''.' + IntToStr(ConfSizeGroupPrecision) + 'f kB'), x); + end else p := g_strdup_printf('%d', Value); + if p = nil then begin + DebugMsg(['FormatSize(5): g_strdup_printf() failed, using old format function.']); + s := '###,###,##0'; + if ConfSizeGroupPrecision > 0 then begin + s := s + '.'; + for i := 1 to ConfSizeGroupPrecision do + if ConfSizeGroupRequestZeroDigits then s := s + '0' + else s := s + '#'; + end; + if (Value >= 1024*1024*1024) or (Base = 1024*1024*1024) then Result := FormatFloat(s + ' GB', Value / (1024*1024*1024)) else + if (Value >= 1024*1024) or (Base = 1024*1024) then Result := FormatFloat(s + ' MB', Value / (1024*1024)) else + if (Value >= 1024) or (Base = 1024) then Result := FormatFloat(s + ' kB', Value / (1024)) else + Result := IntToStr(Value); + end else begin + Result := StrToUTF8(p); + g_free(p); end; - if (Value >= 1024*1024*1024) or (Base = 1024*1024*1024) then Result := FormatFloat(s + ' GB', Value / (1024*1024*1024)) else - if (Value >= 1024*1024) or (Base = 1024*1024) then Result := FormatFloat(s + ' MB', Value / (1024*1024)) else - if (Value >= 1024) or (Base = 1024) then Result := FormatFloat(s + ' kB', Value / (1024)) else - Result := IntToStr(Value); end; end; if ConfSizeFormat in [0..4] then begin diff --git a/UPreferences.pas b/UPreferences.pas index c1c75b0..fa6775d 100644 --- a/UPreferences.pas +++ b/UPreferences.pas @@ -197,7 +197,7 @@ begin SizeFormatOptionMenu := TGTKOptionMenu.Create(Self); SizeFormatLabel.FocusControl := SizeFormatOptionMenu; miSizeFormat1 := TGTKMenuItem.CreateTyped(Self, itLabel); - miSizeFormat1.Caption := Format('%s %s', [LANGPreferencesmiSizeFormat1, FormatFloat('(###,###,###)', 123456)]); + miSizeFormat1.Caption := Format('%s (%s)', [LANGPreferencesmiSizeFormat1, FormatSize(123456, 0, 0)]); miSizeFormat2 := TGTKMenuItem.CreateTyped(Self, itLabel); miSizeFormat2.Caption := '123456'; miSizeFormat3 := TGTKMenuItem.CreateTyped(Self, itLabel); diff --git a/translations/UTranslation_EN.pas b/translations/UTranslation_EN.pas index 9a562a2..fa31434 100644 --- a/translations/UTranslation_EN.pas +++ b/translations/UTranslation_EN.pas @@ -387,7 +387,7 @@ const LANGenF2Button_Caption = 'F2 - Rename'; LANGenPreferencesShowFuncButtonsCheckBox_Caption = 'Show function _key buttons'; LANGenPreferencesSizeFormatLabel_Caption = '_Size format:'; LANGenPreferencesmiSizeFormat1 = 'System'; - LANGenPreferencesmiSizeFormat6 = 'Grouped'; + LANGenPreferencesmiSizeFormat6 = 'Dynamic'; LANGenPreferencesAutodetectXApp = 'Autodetect X app'; LANGenPreferencesAlwaysRunInTerminal = 'Always run in terminal'; LANGenPreferencesNeverRunInTerminal = 'Never run in terminal'; -- cgit v1.2.3