]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/intl.cpp
added ability to set tool alignment (#10174)
[wxWidgets.git] / src / common / intl.cpp
index 0fd36d04d9888ebff956d59233044343ead0a088..023d7cf2ac15553da3d627a9124fd88920cc0a4f 100644 (file)
@@ -66,7 +66,7 @@
 #include "wx/tokenzr.h"
 #include "wx/fontmap.h"
 #include "wx/encconv.h"
-#include "wx/ptr_scpd.h"
+#include "wx/scopedptr.h"
 #include "wx/apptrait.h"
 #include "wx/stdpaths.h"
 #include "wx/hashset.h"
@@ -105,34 +105,6 @@ static const size_t LEN_FULL = LEN_LANG + 1 + LEN_SUBLANG; // 1 for '_'
 // global functions
 // ----------------------------------------------------------------------------
 
-#ifdef __WXDEBUG__
-
-// small class to suppress the translation erros until exit from current scope
-class NoTransErr
-{
-public:
-    NoTransErr() { ms_suppressCount++; }
-    ~NoTransErr() { ms_suppressCount--;  }
-
-    static bool Suppress() { return ms_suppressCount > 0; }
-
-private:
-    static size_t ms_suppressCount;
-};
-
-size_t NoTransErr::ms_suppressCount = 0;
-
-#else // !Debug
-
-class NoTransErr
-{
-public:
-    NoTransErr() { }
-~NoTransErr() { }
-};
-
-#endif // Debug/!Debug
-
 static wxLocale *wxSetLocale(wxLocale *pLocale);
 
 namespace
@@ -868,6 +840,9 @@ wxPluralFormsCalculator* wxPluralFormsCalculator::make(const char* s)
 // wxMsgCatalogFile corresponds to one disk-file message catalog.
 //
 // This is a "low-level" class and is used only by wxMsgCatalog
+// NOTE: for the documentation of the binary catalog (.MO) files refer to
+//       the GNU gettext manual: 
+//       http://www.gnu.org/software/autoconf/manual/gettext/MO-Files.html
 // ----------------------------------------------------------------------------
 
 WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxString, wxMessagesHash);
@@ -881,12 +856,12 @@ public:
 
     // load the catalog from disk (szDirPrefix corresponds to language)
     bool Load(const wxString& szDirPrefix, const wxString& szName,
-            wxPluralFormsCalculatorPtr& rPluralFormsCalculator);
+              wxPluralFormsCalculatorPtr& rPluralFormsCalculator);
 
     // fills the hash with string-translation pairs
-    void FillHash(wxMessagesHash& hash,
-                const wxString& msgIdCharset,
-                bool convertEncoding) const;
+    bool FillHash(wxMessagesHash& hash,
+                  const wxString& msgIdCharset,
+                  bool convertEncoding) const;
 
     // return the charset of the strings in this catalog or empty string if
     // none/unknown
@@ -956,7 +931,7 @@ private:
 
     bool m_bSwapped;   // wrong endianness?
 
-    DECLARE_NO_COPY_CLASS(wxMsgCatalogFile)
+    wxDECLARE_NO_COPY_CLASS(wxMsgCatalogFile);
 };
 
 
@@ -1198,7 +1173,7 @@ bool wxMsgCatalogFile::Load(const wxString& szDirPrefix, const wxString& szName,
 
 
     searchPath += GetFullSearchPath(szDirPrefix);
-    if ( szDirPrefix[LEN_LANG] == wxS('_') )
+    if ( szDirPrefix.length() > LEN_LANG && szDirPrefix[LEN_LANG] == wxS('_') )
     {
         // also add just base locale name: for things like "fr_BE" (Belgium
         // French) we should use fall back on plain "fr" if no Belgium-specific
@@ -1207,16 +1182,8 @@ bool wxMsgCatalogFile::Load(const wxString& szDirPrefix, const wxString& szName,
                     << GetFullSearchPath(ExtractLang(szDirPrefix));
     }
 
-    // don't give translation errors here because the wxstd catalog might
-    // not yet be loaded (and it's normal)
-    //
-    // (we're using an object because we have several return paths)
-
-    NoTransErr noTransErr;
-    wxLogVerbose(_("looking for catalog '%s' in path '%s'."),
-                szName, searchPath.c_str());
-    wxLogTrace(TRACE_I18N, wxS("Looking for \"%s.mo\" in \"%s\""),
-                szName, searchPath.c_str());
+    wxLogTrace(TRACE_I18N, wxS("Looking for \"%s.mo\" in search path \"%s\""),
+                szName, searchPath);
 
     wxFileName fn(szName);
     fn.SetExt(wxS("mo"));
@@ -1304,59 +1271,63 @@ bool wxMsgCatalogFile::Load(const wxString& szDirPrefix, const wxString& szName,
     // plural forms formula from it:
 
     const char* headerData = StringAtOfs(m_pOrigTable, 0);
-    if (headerData && headerData[0] == 0)
+    if ( headerData && headerData[0] == '\0' )
     {
         // Extract the charset:
-        wxString header = wxString::FromAscii(StringAtOfs(m_pTransTable, 0));
-        int begin = header.Find(wxS("Content-Type: text/plain; charset="));
-        if (begin != wxNOT_FOUND)
+        const char * const header = StringAtOfs(m_pTransTable, 0);
+        const char *
+            cset = strstr(header, "Content-Type: text/plain; charset=");
+        if ( cset )
         {
-            begin += 34; //strlen("Content-Type: text/plain; charset=")
-            size_t end = header.find('\n', begin);
-            if (end != size_t(-1))
+            cset += 34; // strlen("Content-Type: text/plain; charset=")
+
+            const char * const csetEnd = strchr(cset, '\n');
+            if ( csetEnd )
             {
-                m_charset.assign(header, begin, end - begin);
-                if (m_charset == wxS("CHARSET"))
+                m_charset = wxString(cset, csetEnd - cset);
+                if ( m_charset == wxS("CHARSET") )
                 {
                     // "CHARSET" is not valid charset, but lazy translator
-                    m_charset.Clear();
+                    m_charset.empty();
                 }
             }
         }
         // else: incorrectly filled Content-Type header
 
         // Extract plural forms:
-        begin = header.Find(wxS("Plural-Forms:"));
-        if (begin != wxNOT_FOUND)
+        const char * plurals = strstr(header, "Plural-Forms:");
+        if ( plurals )
         {
-            begin += 13;
-            size_t end = header.find('\n', begin);
-            if (end != size_t(-1))
+            plurals += 13; // strlen("Plural-Forms:")
+            const char * const pluralsEnd = strchr(plurals, '\n');
+            if ( pluralsEnd )
             {
-                wxString pfs(header, begin, end - begin);
-                wxPluralFormsCalculator* pCalculator = wxPluralFormsCalculator
-                    ::make(pfs.ToAscii());
-                if (pCalculator != 0)
+                const size_t pluralsLen = pluralsEnd - plurals;
+                wxCharBuffer buf(pluralsLen);
+                strncpy(buf.data(), plurals, pluralsLen);
+                wxPluralFormsCalculator * const
+                    pCalculator = wxPluralFormsCalculator::make(buf);
+                if ( pCalculator )
                 {
                     rPluralFormsCalculator.reset(pCalculator);
                 }
                 else
                 {
-                    wxLogVerbose(_("Cannot parse Plural-Forms:'%s'"), pfs.c_str());
+                    wxLogVerbose(_("Failed to parse Plural-Forms: '%s'"),
+                                 buf.data());
                 }
             }
         }
-        if (rPluralFormsCalculator.get() == NULL)
-        {
+
+        if ( !rPluralFormsCalculator.get() )
             rPluralFormsCalculator.reset(wxPluralFormsCalculator::make());
-        }
     }
 
     // everything is fine
     return true;
 }
 
-void wxMsgCatalogFile::FillHash(wxMessagesHash& hash,
+bool wxMsgCatalogFile::FillHash(wxMessagesHash& hash,
                                 const wxString& msgIdCharset,
                                 bool convertEncoding) const
 {
@@ -1444,6 +1415,8 @@ void wxMsgCatalogFile::FillHash(wxMessagesHash& hash,
     for (size_t32 i = 0; i < m_numStrings; i++)
     {
         const char *data = StringAtOfs(m_pOrigTable, i);
+        if (!data)
+            return false; // may happen for invalid MO files
 
         wxString msgid;
 #if wxUSE_UNICODE
@@ -1458,6 +1431,9 @@ void wxMsgCatalogFile::FillHash(wxMessagesHash& hash,
 #endif // wxUSE_UNICODE
 
         data = StringAtOfs(m_pTransTable, i);
+        if (!data)
+            return false; // may happen for invalid MO files
+
         size_t length = Swap(m_pTransTable[i].nLen);
         size_t offset = 0;
         size_t index = 0;
@@ -1488,7 +1464,12 @@ void wxMsgCatalogFile::FillHash(wxMessagesHash& hash,
             }
 
             // skip this string
-            offset += strlen(str) + 1;
+            // IMPORTANT: accesses to the 'data' pointer are valid only for 
+            //            the first 'length+1' bytes (GNU specs says that the
+            //            final NUL is not counted in length); using wxStrnlen() 
+            //            we make sure we don't access memory beyond the valid range
+            //            (which otherwise may happen for invalid MO files):
+            offset += wxStrnlen(str, length - offset) + 1;
             ++index;
         }
     }
@@ -1497,6 +1478,8 @@ void wxMsgCatalogFile::FillHash(wxMessagesHash& hash,
     delete sourceConv;
     delete inputConvPtr;
 #endif // wxUSE_WCHAR_T
+
+    return true;
 }
 
 
@@ -1531,7 +1514,8 @@ bool wxMsgCatalog::Load(const wxString& dirPrefix, const wxString& name,
     if ( !file.Load(dirPrefix, name, m_pluralFormsCalculator) )
         return false;
 
-    file.FillHash(m_messages, msgIdCharset, bConvertEncoding);
+    if ( !file.FillHash(m_messages, msgIdCharset, bConvertEncoding) )
+        return false;
 
 #if !wxUSE_UNICODE
     // we should use a conversion compatible with the message catalog encoding
@@ -2180,7 +2164,7 @@ wxString wxLocale::GetSystemEncodingName()
         // the environment variables (in most cases this won't work, but I was
         // out of ideas)
         char *lang = getenv( "LC_ALL");
-        char *dot = lang ? strchr(lang, '.') : (char *)NULL;
+        char *dot = lang ? strchr(lang, '.') : NULL;
         if (!dot)
         {
             lang = getenv( "LC_CTYPE" );
@@ -2422,18 +2406,11 @@ const wxString& wxLocale::GetString(const wxString& origString,
 
     if ( trans == NULL )
     {
-#ifdef __WXDEBUG__
-        if ( !NoTransErr::Suppress() )
-        {
-            NoTransErr noTransErr;
-
-            wxLogTrace(TRACE_I18N,
-                    wxS("string \"%s\"[%ld] not found in %slocale '%s'."),
-                    origString, (long)n,
-                    wxString::Format(wxS("domain '%s' "), domain).c_str(),
-                    m_strLocale.c_str());
-        }
-#endif // __WXDEBUG__
+        wxLogTrace(TRACE_I18N,
+                wxS("string \"%s\"[%ld] not found in %slocale '%s'."),
+                origString, (long)n,
+                wxString::Format(wxS("domain '%s' "), domain).c_str(),
+                m_strLocale.c_str());
 
         if (n == size_t(-1))
             return GetUntranslatedString(origString);
@@ -2573,7 +2550,7 @@ bool wxLocale::AddCatalog(const wxString& szDomain,
                         const wxString& msgIdCharset)
 
 {
-    wxCHECK_MSG( IsOk(), false, "must initialize catalog first" );
+    wxCHECK_MSG( !m_strShort.empty(), false, "must initialize catalog first" );
 
 
     // It is OK to not load catalog if the msgid language and m_language match,