+// ============================================================================
+// wxLocaleIsUtf8
+// ============================================================================
+
+#if wxUSE_UNICODE_UTF8
+
+#if !wxUSE_UTF8_LOCALE_ONLY
+bool wxLocaleIsUtf8 = false; // the safer setting if not known
+#endif
+
+static bool wxIsLocaleUtf8()
+{
+ // NB: we intentionally don't use wxLocale::GetSystemEncodingName(),
+ // because a) it may be unavailable in some builds and b) has slightly
+ // different semantics (default locale instead of current)
+
+#if defined(HAVE_LANGINFO_H) && defined(CODESET)
+ // GNU libc provides current character set this way (this conforms to
+ // Unix98)
+ const char *charset = nl_langinfo(CODESET);
+ if ( charset )
+ {
+ // "UTF-8" is used by modern glibc versions, but test other variants
+ // as well, just in case:
+ if ( strcmp(charset, "UTF-8") == 0 ||
+ strcmp(charset, "utf-8") == 0 ||
+ strcmp(charset, "UTF8") == 0 ||
+ strcmp(charset, "utf8") == 0 )
+ {
+ return true;
+ }
+ }
+#endif // HAVE_LANGINFO_H
+
+ // check if we're running under the "C" locale: it is 7bit subset
+ // of UTF-8, so it can be safely used with the UTF-8 build:
+ const char *lc_ctype = setlocale(LC_CTYPE, NULL);
+ if ( lc_ctype &&
+ (strcmp(lc_ctype, "C") == 0 || strcmp(lc_ctype, "POSIX") == 0) )
+ {
+ return true;
+ }
+
+ // we don't know what charset libc is using, so assume the worst
+ // to be safe:
+ return false;
+}
+
+void wxUpdateLocaleIsUtf8()
+{
+#if wxUSE_UTF8_LOCALE_ONLY
+ if ( !wxIsLocaleUtf8() )
+ {
+ wxLogFatalError(wxT("This program requires UTF-8 locale to run."));
+ }
+#else // !wxUSE_UTF8_LOCALE_ONLY
+ wxLocaleIsUtf8 = wxIsLocaleUtf8();
+#endif
+}
+
+#endif // wxUSE_UNICODE_UTF8
+
+// ============================================================================
+// wx wrappers for CRT functions
+// ============================================================================
+
+#if wxUSE_UNICODE_WCHAR
+ #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callW
+#elif wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
+ #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) \
+ return_kw wxLocaleIsUtf8 ? callA : callW
+#else // ANSI or UTF8 only
+ #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callA
+#endif
+
+int wxPuts(const wxString& s)
+{
+ // under IRIX putws() takes a non-const argument so use wchar_str() instead
+ // of wc_str()
+ CALL_ANSI_OR_UNICODE(return,
+ wxCRT_PutsA(s.mb_str()),
+ wxCRT_PutsW(s.wchar_str()));
+}