OSDN Git Service

PATCH: [ 779818 ] Fix number display to be locale & user-pref aware
authorPerry Rapp <elsapo@users.sourceforge.net>
Sat, 30 Aug 2003 01:33:36 +0000 (01:33 +0000)
committerPerry Rapp <elsapo@users.sourceforge.net>
Sat, 30 Aug 2003 01:33:36 +0000 (01:33 +0000)
Src/DirView.cpp
Src/DirViewColItems.cpp
Src/Merge.dsp
Src/locality.cpp [new file with mode: 0644]
Src/locality.h [new file with mode: 0644]
Src/readme.txt

index fdee6bc..0a41d59 100644 (file)
@@ -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;
 }
 
index cceea76..280f290 100644 (file)
@@ -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)
 {
index 566f9ee..9bbb28a 100644 (file)
@@ -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 (file)
index 0000000..2fd2aa4
--- /dev/null
@@ -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 (file)
index 0000000..0bfccdd
--- /dev/null
@@ -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
index 3d7126e..5f5b62d 100644 (file)
@@ -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