| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/common/numformatter.cpp |
| 3 | // Purpose: wxNumberFormatter |
| 4 | // Author: Fulvio Senore, Vadim Zeitlin |
| 5 | // Created: 2010-11-06 |
| 6 | // Copyright: (c) 2010 wxWidgets team |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | // ---------------------------------------------------------------------------- |
| 11 | // headers |
| 12 | // ---------------------------------------------------------------------------- |
| 13 | |
| 14 | // For compilers that support precompilation, includes "wx.h". |
| 15 | #include "wx/wxprec.h" |
| 16 | |
| 17 | #ifdef __BORLANDC__ |
| 18 | #pragma hdrstop |
| 19 | #endif |
| 20 | |
| 21 | #include "wx/numformatter.h" |
| 22 | #include "wx/intl.h" |
| 23 | |
| 24 | #include <locale.h> // for setlocale and LC_ALL |
| 25 | |
| 26 | // ---------------------------------------------------------------------------- |
| 27 | // local helpers |
| 28 | // ---------------------------------------------------------------------------- |
| 29 | |
| 30 | namespace |
| 31 | { |
| 32 | |
| 33 | // Contains information about the locale which was used to initialize our |
| 34 | // cached values of the decimal and thousands separators. Notice that it isn't |
| 35 | // enough to store just wxLocale because the user code may call setlocale() |
| 36 | // directly and storing just C locale string is not enough because we can use |
| 37 | // the OS API directly instead of the CRT ones on some platforms. So just store |
| 38 | // both. |
| 39 | class LocaleId |
| 40 | { |
| 41 | public: |
| 42 | LocaleId() |
| 43 | { |
| 44 | #if wxUSE_INTL |
| 45 | m_wxloc = NULL; |
| 46 | #endif // wxUSE_INTL |
| 47 | m_cloc = NULL; |
| 48 | } |
| 49 | |
| 50 | ~LocaleId() |
| 51 | { |
| 52 | Free(); |
| 53 | } |
| 54 | |
| 55 | #if wxUSE_INTL |
| 56 | // Return true if this is the first time this function is called for this |
| 57 | // object or if the program locale has changed since the last time it was |
| 58 | // called. Otherwise just return false indicating that updating locale- |
| 59 | // dependent information is not necessary. |
| 60 | bool NotInitializedOrHasChanged() |
| 61 | { |
| 62 | wxLocale * const wxloc = wxGetLocale(); |
| 63 | const char * const cloc = setlocale(LC_ALL, NULL); |
| 64 | if ( m_wxloc || m_cloc ) |
| 65 | { |
| 66 | if ( m_wxloc == wxloc && strcmp(m_cloc, cloc) == 0 ) |
| 67 | return false; |
| 68 | |
| 69 | Free(); |
| 70 | } |
| 71 | //else: Not initialized yet. |
| 72 | |
| 73 | m_wxloc = wxloc; |
| 74 | m_cloc = wxCRT_StrdupA(cloc); |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | #endif // wxUSE_INTL |
| 79 | |
| 80 | private: |
| 81 | void Free() |
| 82 | { |
| 83 | #if wxUSE_INTL |
| 84 | free(m_cloc); |
| 85 | #endif // wxUSE_INTL |
| 86 | } |
| 87 | |
| 88 | #if wxUSE_INTL |
| 89 | // Non-owned pointer to wxLocale which was used. |
| 90 | wxLocale *m_wxloc; |
| 91 | #endif // wxUSE_INTL |
| 92 | |
| 93 | // Owned pointer to the C locale string. |
| 94 | char *m_cloc; |
| 95 | |
| 96 | wxDECLARE_NO_COPY_CLASS(LocaleId); |
| 97 | }; |
| 98 | |
| 99 | } // anonymous namespace |
| 100 | |
| 101 | // ============================================================================ |
| 102 | // wxNumberFormatter implementation |
| 103 | // ============================================================================ |
| 104 | |
| 105 | // ---------------------------------------------------------------------------- |
| 106 | // Locale information accessors |
| 107 | // ---------------------------------------------------------------------------- |
| 108 | |
| 109 | wxChar wxNumberFormatter::GetDecimalSeparator() |
| 110 | { |
| 111 | #if wxUSE_INTL |
| 112 | // Notice that while using static variable here is not MT-safe, the worst |
| 113 | // that can happen is that we redo the initialization if we're called |
| 114 | // concurrently from more than one thread so it's not a real problem. |
| 115 | static wxChar s_decimalSeparator = 0; |
| 116 | |
| 117 | // Remember the locale which was current when we initialized, we must redo |
| 118 | // the initialization if the locale changed. |
| 119 | static LocaleId s_localeUsedForInit; |
| 120 | |
| 121 | if ( s_localeUsedForInit.NotInitializedOrHasChanged() ) |
| 122 | { |
| 123 | const wxString |
| 124 | s = wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER); |
| 125 | if ( s.empty() ) |
| 126 | { |
| 127 | // We really must have something for decimal separator, so fall |
| 128 | // back to the C locale default. |
| 129 | s_decimalSeparator = '.'; |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | // To the best of my knowledge there are no locales like this. |
| 134 | wxASSERT_MSG( s.length() == 1, |
| 135 | "Multi-character decimal separator?" ); |
| 136 | |
| 137 | s_decimalSeparator = s[0]; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return s_decimalSeparator; |
| 142 | #else // !wxUSE_INTL |
| 143 | return wxT('.'); |
| 144 | #endif // wxUSE_INTL/!wxUSE_INTL |
| 145 | } |
| 146 | |
| 147 | bool wxNumberFormatter::GetThousandsSeparatorIfUsed(wxChar *sep) |
| 148 | { |
| 149 | #if wxUSE_INTL |
| 150 | static wxChar s_thousandsSeparator = 0; |
| 151 | static LocaleId s_localeUsedForInit; |
| 152 | |
| 153 | if ( s_localeUsedForInit.NotInitializedOrHasChanged() ) |
| 154 | { |
| 155 | const wxString |
| 156 | s = wxLocale::GetInfo(wxLOCALE_THOUSANDS_SEP, wxLOCALE_CAT_NUMBER); |
| 157 | if ( !s.empty() ) |
| 158 | { |
| 159 | wxASSERT_MSG( s.length() == 1, |
| 160 | "Multi-character thousands separator?" ); |
| 161 | |
| 162 | s_thousandsSeparator = s[0]; |
| 163 | } |
| 164 | //else: Unlike above it's perfectly fine for the thousands separator to |
| 165 | // be empty if grouping is not used, so just leave it as 0. |
| 166 | } |
| 167 | |
| 168 | if ( !s_thousandsSeparator ) |
| 169 | return false; |
| 170 | |
| 171 | if ( sep ) |
| 172 | *sep = s_thousandsSeparator; |
| 173 | |
| 174 | return true; |
| 175 | #else // !wxUSE_INTL |
| 176 | wxUnusedVar(sep); |
| 177 | return false; |
| 178 | #endif // wxUSE_INTL/!wxUSE_INTL |
| 179 | } |
| 180 | |
| 181 | // ---------------------------------------------------------------------------- |
| 182 | // Conversion to string and helpers |
| 183 | // ---------------------------------------------------------------------------- |
| 184 | |
| 185 | wxString wxNumberFormatter::PostProcessIntString(wxString s, int style) |
| 186 | { |
| 187 | if ( style & Style_WithThousandsSep ) |
| 188 | AddThousandsSeparators(s); |
| 189 | |
| 190 | wxASSERT_MSG( !(style & Style_NoTrailingZeroes), |
| 191 | "Style_NoTrailingZeroes can't be used with integer values" ); |
| 192 | |
| 193 | return s; |
| 194 | } |
| 195 | |
| 196 | wxString wxNumberFormatter::ToString(long val, int style) |
| 197 | { |
| 198 | return PostProcessIntString(wxString::Format("%ld", val), style); |
| 199 | } |
| 200 | |
| 201 | #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG |
| 202 | |
| 203 | wxString wxNumberFormatter::ToString(wxLongLong_t val, int style) |
| 204 | { |
| 205 | return PostProcessIntString(wxString::Format("%" wxLongLongFmtSpec "d", val), |
| 206 | style); |
| 207 | } |
| 208 | |
| 209 | #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG |
| 210 | |
| 211 | wxString wxNumberFormatter::ToString(double val, int precision, int style) |
| 212 | { |
| 213 | wxString s = wxString::FromDouble(val,precision); |
| 214 | |
| 215 | if ( style & Style_WithThousandsSep ) |
| 216 | AddThousandsSeparators(s); |
| 217 | |
| 218 | if ( style & Style_NoTrailingZeroes ) |
| 219 | RemoveTrailingZeroes(s); |
| 220 | |
| 221 | return s; |
| 222 | } |
| 223 | |
| 224 | void wxNumberFormatter::AddThousandsSeparators(wxString& s) |
| 225 | { |
| 226 | wxChar thousandsSep; |
| 227 | if ( !GetThousandsSeparatorIfUsed(&thousandsSep) ) |
| 228 | return; |
| 229 | |
| 230 | size_t pos = s.find(GetDecimalSeparator()); |
| 231 | if ( pos == wxString::npos ) |
| 232 | { |
| 233 | // Start grouping at the end of an integer number. |
| 234 | pos = s.length(); |
| 235 | } |
| 236 | |
| 237 | // End grouping at the beginning of the digits -- there could be at a sign |
| 238 | // before their start. |
| 239 | const size_t start = s.find_first_of("0123456789"); |
| 240 | |
| 241 | // We currently group digits by 3 independently of the locale. This is not |
| 242 | // the right thing to do and we should use lconv::grouping (under POSIX) |
| 243 | // and GetLocaleInfo(LOCALE_SGROUPING) (under MSW) to get information about |
| 244 | // the correct grouping to use. This is something that needs to be done at |
| 245 | // wxLocale level first and then used here in the future (TODO). |
| 246 | const size_t GROUP_LEN = 3; |
| 247 | |
| 248 | while ( pos > start + GROUP_LEN ) |
| 249 | { |
| 250 | pos -= GROUP_LEN; |
| 251 | s.insert(pos, thousandsSep); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void wxNumberFormatter::RemoveTrailingZeroes(wxString& s) |
| 256 | { |
| 257 | const size_t posDecSep = s.find(GetDecimalSeparator()); |
| 258 | wxCHECK_RET( posDecSep != wxString::npos, |
| 259 | wxString::Format("No decimal separator in \"%s\"", s) ); |
| 260 | wxCHECK_RET( posDecSep, "Can't start with decimal separator" ); |
| 261 | |
| 262 | // Find the last character to keep. |
| 263 | size_t posLastNonZero = s.find_last_not_of("0"); |
| 264 | |
| 265 | // If it's the decimal separator itself, don't keep it neither. |
| 266 | if ( posLastNonZero == posDecSep ) |
| 267 | posLastNonZero--; |
| 268 | |
| 269 | s.erase(posLastNonZero + 1); |
| 270 | } |
| 271 | |
| 272 | // ---------------------------------------------------------------------------- |
| 273 | // Conversion from strings |
| 274 | // ---------------------------------------------------------------------------- |
| 275 | |
| 276 | void wxNumberFormatter::RemoveThousandsSeparators(wxString& s) |
| 277 | { |
| 278 | wxChar thousandsSep; |
| 279 | if ( !GetThousandsSeparatorIfUsed(&thousandsSep) ) |
| 280 | return; |
| 281 | |
| 282 | s.Replace(wxString(thousandsSep), wxString()); |
| 283 | } |
| 284 | |
| 285 | bool wxNumberFormatter::FromString(wxString s, long *val) |
| 286 | { |
| 287 | RemoveThousandsSeparators(s); |
| 288 | return s.ToLong(val); |
| 289 | } |
| 290 | |
| 291 | #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG |
| 292 | |
| 293 | bool wxNumberFormatter::FromString(wxString s, wxLongLong_t *val) |
| 294 | { |
| 295 | RemoveThousandsSeparators(s); |
| 296 | return s.ToLongLong(val); |
| 297 | } |
| 298 | |
| 299 | #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG |
| 300 | |
| 301 | bool wxNumberFormatter::FromString(wxString s, double *val) |
| 302 | { |
| 303 | RemoveThousandsSeparators(s); |
| 304 | return s.ToDouble(val); |
| 305 | } |