From 03216073e02e76dd2193977f2039c86419adcd37 Mon Sep 17 00:00:00 2001 From: Perry Rapp Date: Sat, 30 Aug 2003 01:33:36 +0000 Subject: [PATCH] PATCH: [ 779818 ] Fix number display to be locale & user-pref aware --- Src/DirView.cpp | 4 +- Src/DirViewColItems.cpp | 7 ++- Src/Merge.dsp | 8 ++++ Src/locality.cpp | 115 ++++++++++++++++++++++++++++++++++++++++++++++++ Src/locality.h | 19 ++++++++ Src/readme.txt | 5 +++ 6 files changed, 152 insertions(+), 6 deletions(-) create mode 100644 Src/locality.cpp create mode 100644 Src/locality.h diff --git a/Src/DirView.cpp b/Src/DirView.cpp index fdee6bc2c..0a41d59b7 100644 --- a/Src/DirView.cpp +++ b/Src/DirView.cpp @@ -35,6 +35,7 @@ #include "coretools.h" #include "WaitStatusCursor.h" #include "dllver.h" +#include "locality.h" #ifdef _DEBUG #define new DEBUG_NEW @@ -358,8 +359,7 @@ void CDirView::HeaderContextMenu(CPoint point, int /*i*/) // How to do this with locale sensitivity ? CString NumToStr(int n) { - CString s; - s.Format(_T("%d"), n); + CString s = locality::NumToLocaleStr(n); return s; } diff --git a/Src/DirViewColItems.cpp b/Src/DirViewColItems.cpp index cceea7676..280f29043 100644 --- a/Src/DirViewColItems.cpp +++ b/Src/DirViewColItems.cpp @@ -15,9 +15,8 @@ #include "DirDoc.h" #include "MainFrm.h" #include "resource.h" -//#include "coretools.h" -//#include "dllver.h" #include "DirViewColItems.h" +#include "locality.h" #ifdef _DEBUG #define new DEBUG_NEW @@ -125,13 +124,13 @@ static CString ColLsizeGet(const DIFFITEM & di) { CString s; s.Format(_T("%I64d"), di.left.size); - return s; + return locality::GetLocaleStr(s); } static CString ColRsizeGet(const DIFFITEM & di) { CString s; s.Format(_T("%I64d"), di.right.size); - return s; + return locality::GetLocaleStr(s); } static CString ColNewerGet(const DIFFITEM & di) { diff --git a/Src/Merge.dsp b/Src/Merge.dsp index 566f9eee1..9bbb28ae9 100644 --- a/Src/Merge.dsp +++ b/Src/Merge.dsp @@ -380,6 +380,10 @@ SOURCE=..\common\listvwex.cpp # End Source File # Begin Source File +SOURCE=.\locality.cpp +# End Source File +# Begin Source File + SOURCE=..\common\LogFile.cpp # End Source File # Begin Source File @@ -764,6 +768,10 @@ SOURCE=..\common\listvwex.h # End Source File # Begin Source File +SOURCE=.\locality.h +# End Source File +# Begin Source File + SOURCE=..\common\LogFile.h # End Source File # Begin Source File diff --git a/Src/locality.cpp b/Src/locality.cpp new file mode 100644 index 000000000..2fd2aa456 --- /dev/null +++ b/Src/locality.cpp @@ -0,0 +1,115 @@ +/** + * @file locality.h + * + * @brief Implementation of helper functions involving locale + */ +// RCS ID line follows -- this is updated by CVS +// $Id$ + +#include "StdAfx.h" +#include "locality.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +namespace locality { + +/** + * @brief Get numeric value from an LC_ entry in windows locale (NLS) database + */ +static UINT getLocaleUint(int lctype, int defval) +{ + TCHAR buff[64]; + if (!GetLocaleInfo(LOCALE_USER_DEFAULT, lctype, buff, sizeof(buff)/sizeof(buff[0]))) + return defval; + return _ttol(buff); + +} + +/** + * @brief Get string value from LC_ entry in windows locale (NLS) database + */ +static CString getLocaleStr(int lctype, LPCTSTR defval) +{ + CString out; + LPTSTR outbuff = out.GetBuffer(64); + int rt = GetLocaleInfo(LOCALE_USER_DEFAULT, lctype, outbuff, 64); + out.ReleaseBuffer(); + if (!rt) + out = defval; + return out; + +} + +/** + * @brief Get numeric value for LOCALE_SGROUPING + */ +static UINT GetLocaleGrouping(int defval) +{ + TCHAR buff[64]; + if (!GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SGROUPING, buff, sizeof(buff)/sizeof(buff[0]))) + return defval; + // handling for Indic 3;2 + if (!_tcscmp(buff, _T("3;2;0"))) + return 32; + return _ttol(buff); +} + +/** + * @brief Print an integer into a CString, in appropriate fashion for locale & user preferences + * + * NB: We are not converting digits from ASCII via LOCALE_SNATIVEDIGITS + * So we always use ASCII digits, instead of, eg, the Chinese digits + */ +CString NumToLocaleStr(UINT n) +{ + TCHAR numbuff[34]; + _ltot(n, numbuff, 10); + return GetLocaleStr(numbuff); +} + +/** + * @brief Insert commas (or periods) into string, as appropriate for locale & preferences + * + * NB: We are not converting digits from ASCII via LOCALE_SNATIVEDIGITS + * So we always use ASCII digits, instead of, eg, the Chinese digits + */ +CString GetLocaleStr(const CString & str) +{ + // Fill in currency format with locale info + // except we hardcode for no decimal + NUMBERFMT NumFormat; + memset(&NumFormat, 0, sizeof(NumFormat)); + NumFormat.NumDigits = 0; // LOCALE_IDIGITS + NumFormat.LeadingZero = getLocaleUint(LOCALE_ILZERO, 0); + NumFormat.Grouping = GetLocaleGrouping(3); + NumFormat.lpDecimalSep = _T("."); // should not be used + CString sep = getLocaleStr(LOCALE_STHOUSAND, _T(",")); + NumFormat.lpThousandSep = (LPTSTR)(LPCTSTR)sep; + NumFormat.NegativeOrder = getLocaleUint(LOCALE_INEGNUMBER , 0); + + CString out; + LPTSTR outbuff = out.GetBuffer(48); + int rt = GetNumberFormat(LOCALE_USER_DEFAULT // a predefined value for user locale + , 0 // operation option (allow overrides) + , str // input number (see MSDN for legal chars) + , &NumFormat // formatting specifications + , outbuff // output buffer + , 48 + ); // size of output buffer + out.ReleaseBuffer(); + if (!rt) { + int nerr = GetLastError(); + CString msg; + msg.Format(_T("Error %d in NumToStr(): %s"), nerr, GetSysError(nerr)); + TRACE(_T("%s\n"), msg); + out = str; + } + return out; +} + + +}; // namespace locality diff --git a/Src/locality.h b/Src/locality.h new file mode 100644 index 000000000..0bfccdda3 --- /dev/null +++ b/Src/locality.h @@ -0,0 +1,19 @@ +/** + * @file locality.h + * + * @brief Declaration of helper functions involving locale + */ +// RCS ID line follows -- this is updated by CVS +// $Id$ + +#ifndef locality_h_included +#define locality_h_included + +namespace locality { + +CString NumToLocaleStr(UINT n); +CString GetLocaleStr(const CString & str); + +}; + +#endif // locality_h_included diff --git a/Src/readme.txt b/Src/readme.txt index 3d7126e95..5f5b62d43 100644 --- a/Src/readme.txt +++ b/Src/readme.txt @@ -1,3 +1,8 @@ +2003-08-29 Perry + PATCH: [ 779818 ] Fix number display to be locale & user-pref aware + (extended to handle larger numbers than 32 bit, for size) + WinMerge: DirView.cpp DirViewColItems.cpp locality.cpp locality.h Merge.dsp + 2003-08-29 Kimmo PATCH: [ 796756 ] Diff API WinMerge: DiffWrapper.h DiffWrapper.cpp DirDoc.h DirDoc.cpp DirView.cpp -- 2.11.0