Qt 4.8
Classes | Macros | Enumerations | Functions | Variables
qtranslator.cpp File Reference
#include "qplatformdefs.h"
#include "qtranslator.h"
#include "qfileinfo.h"
#include "qstring.h"
#include "qstringlist.h"
#include "qcoreapplication.h"
#include "qcoreapplication_p.h"
#include "qdatastream.h"
#include "qdir.h"
#include "qfile.h"
#include "qmap.h"
#include "qalgorithms.h"
#include "qhash.h"
#include "qtranslator_p.h"
#include "qlocale.h"
#include "qresource.h"
#include "private/qcore_unix_p.h"
#include <sys/mman.h>
#include <errno.h>
#include <stdlib.h>
#include "qobject_p.h"

Go to the source code of this file.

Classes

class  QTranslatorPrivate
 

Macros

#define CHECK_RANGE
 
#define MAP_FAILED   -1
 
#define MAP_FILE   0
 
#define QT_USE_MMAP
 

Enumerations

enum  Tag {
  Tag_End = 1, Tag_SourceText16, Tag_Translation, Tag_Context16,
  Tag_Obsolete1, Tag_SourceText, Tag_Context, Tag_Comment,
  Tag_Obsolete2
}
 

Functions

static uint elfHash (const char *name)
 
static QString find_translation (const QLocale &locale, const QString &filename, const QString &prefix, const QString &directory, const QString &suffix)
 
static QString getMessage (const uchar *m, const uchar *end, const char *context, const char *sourceText, const char *comment, int numerus)
 
static bool match (const uchar *found, const char *target, uint len)
 
static int numerusHelper (int n, const uchar *rules, int rulesSize)
 
static quint16 read16 (const uchar *data)
 
static quint32 read32 (const uchar *data)
 
static quint8 read8 (const uchar *data)
 

Variables

static const uchar magic [MagicLength]
 
static const int MagicLength = 16
 

Macro Definition Documentation

◆ CHECK_RANGE

#define CHECK_RANGE
Value:
do { \
if (i >= rulesSize) \
return -1; \
} while (0)

Referenced by numerusHelper().

◆ MAP_FAILED

#define MAP_FAILED   -1

◆ MAP_FILE

#define MAP_FILE   0

◆ QT_USE_MMAP

#define QT_USE_MMAP

Definition at line 64 of file qtranslator.cpp.

Enumeration Type Documentation

◆ Tag

enum Tag
Enumerator
Tag_End 
Tag_SourceText16 
Tag_Translation 
Tag_Context16 
Tag_Obsolete1 
Tag_SourceText 
Tag_Context 
Tag_Comment 
Tag_Obsolete2 

Definition at line 86 of file qtranslator.cpp.

Function Documentation

◆ elfHash()

static uint elfHash ( const char *  name)
static

Definition at line 110 of file qtranslator.cpp.

Referenced by QTranslatorPrivate::do_translate().

111 {
112  const uchar *k;
113  uint h = 0;
114  uint g;
115 
116  if (name) {
117  k = (const uchar *) name;
118  while (*k) {
119  h = (h << 4) + *k++;
120  if ((g = (h & 0xf0000000)) != 0)
121  h ^= g >> 24;
122  h &= ~g;
123  }
124  }
125  if (!h)
126  h = 1;
127  return h;
128 }
unsigned char uchar
Definition: qglobal.h:994
const char * name
unsigned int uint
Definition: qglobal.h:996

◆ find_translation()

static QString find_translation ( const QLocale locale,
const QString filename,
const QString prefix,
const QString directory,
const QString suffix 
)
static

Definition at line 586 of file qtranslator.cpp.

Referenced by QTranslator::load().

591 {
592  QString path;
593  if (QFileInfo(filename).isRelative()) {
594  path = directory;
595  if (!path.isEmpty() && !path.endsWith(QLatin1Char('/')))
596  path += QLatin1Char('/');
597  }
598 
599  QFileInfo fi;
600  QString realname;
601  QStringList fuzzyLocales;
602 
603  // see http://www.unicode.org/reports/tr35/#LanguageMatching for inspiration
604 
605  QStringList languages = locale.uiLanguages();
606 #if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
607  for (int i = languages.size()-1; i >= 0; --i) {
608  QString lang = languages.at(i);
609  QString lowerLang = lang.toLower();
610  if (lang != lowerLang)
611  languages.insert(i+1, lowerLang);
612  }
613 #endif
614 
615  // try explicit locales names first
616  foreach (QString localeName, languages) {
617  localeName.replace(QLatin1Char('-'), QLatin1Char('_'));
618 
619  realname = path + filename + prefix + localeName + (suffix.isNull() ? QLatin1String(".qm") : suffix);
620  fi.setFile(realname);
621  if (fi.isReadable() && fi.isFile())
622  return realname;
623 
624  realname = path + filename + prefix + localeName;
625  fi.setFile(realname);
626  if (fi.isReadable() && fi.isFile())
627  return realname;
628 
629  fuzzyLocales.append(localeName);
630  }
631 
632  // start guessing
633  foreach (QString localeName, fuzzyLocales) {
634  for (;;) {
635  int rightmost = localeName.lastIndexOf(QLatin1Char('_'));
636  // no truncations? fail
637  if (rightmost <= 0)
638  break;
639  localeName.truncate(rightmost);
640 
641  realname = path + filename + prefix + localeName + (suffix.isNull() ? QLatin1String(".qm") : suffix);
642  fi.setFile(realname);
643  if (fi.isReadable() && fi.isFile())
644  return realname;
645 
646  realname = path + filename + prefix + localeName;
647  fi.setFile(realname);
648  if (fi.isReadable() && fi.isFile())
649  return realname;
650  }
651  }
652 
653  if (!suffix.isNull()) {
654  realname = path + filename + suffix;
655  fi.setFile(realname);
656  if (fi.isReadable() && fi.isFile())
657  return realname;
658  }
659 
660  realname = path + filename + prefix;
661  fi.setFile(realname);
662  if (fi.isReadable() && fi.isFile())
663  return realname;
664 
665  realname = path + filename;
666  fi.setFile(realname);
667  if (fi.isReadable() && fi.isFile())
668  return realname;
669 
670  return QString();
671 }
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
void insert(int i, const T &t)
Inserts value at index position i in the list.
Definition: qlist.h:575
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
QStringList uiLanguages() const
Returns an ordered list of locale names for translation purposes in preference order.
Definition: qlocale.cpp:3410
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
void truncate(int pos)
Truncates the string at the given position index.
Definition: qstring.cpp:4603
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void setFile(const QString &file)
Sets the file that the QFileInfo provides information about to file.
Definition: qfileinfo.cpp:468
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition: qstring.h:505
int lastIndexOf(QChar c, int from=-1, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:3000
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
QByteArray suffix
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
bool isFile() const
Returns true if this object points to a file or to a symbolic link to a file.
Definition: qfileinfo.cpp:971
bool isReadable() const
Returns true if the user can read the file; otherwise returns false.
Definition: qfileinfo.cpp:896
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ getMessage()

static QString getMessage ( const uchar m,
const uchar end,
const char *  context,
const char *  sourceText,
const char *  comment,
int  numerus 
)
static

Definition at line 806 of file qtranslator.cpp.

Referenced by QTranslatorPrivate::do_translate().

808 {
809  const uchar *tn = 0;
810  uint tn_length = 0;
811  int currentNumerus = -1;
812 
813  for (;;) {
814  uchar tag = 0;
815  if (m < end)
816  tag = read8(m++);
817  switch((Tag)tag) {
818  case Tag_End:
819  goto end;
820  case Tag_Translation: {
821  int len = read32(m);
822  if (len % 1)
823  return QString();
824  m += 4;
825  if (++currentNumerus == numerus) {
826  tn_length = len;
827  tn = m;
828  }
829  m += len;
830  break;
831  }
832  case Tag_Obsolete1:
833  m += 4;
834  break;
835  case Tag_SourceText: {
836  quint32 len = read32(m);
837  m += 4;
838  if (!match(m, sourceText, len))
839  return QString();
840  m += len;
841  }
842  break;
843  case Tag_Context: {
844  quint32 len = read32(m);
845  m += 4;
846  if (!match(m, context, len))
847  return QString();
848  m += len;
849  }
850  break;
851  case Tag_Comment: {
852  quint32 len = read32(m);
853  m += 4;
854  if (*m && !match(m, comment, len))
855  return QString();
856  m += len;
857  }
858  break;
859  default:
860  return QString();
861  }
862  }
863 end:
864  if (!tn)
865  return QString();
866  QString str = QString((const QChar *)tn, tn_length/2);
867  if (QSysInfo::ByteOrder == QSysInfo::LittleEndian) {
868  for (int i = 0; i < str.length(); ++i)
869  str[i] = QChar((str.at(i).unicode() >> 8) + ((str.at(i).unicode() << 8) & 0xff00));
870  }
871  return str;
872 }
Tag
Definition: qtranslator.cpp:86
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
static bool match(const uchar *found, const char *target, uint len)
The QString class provides a Unicode character string.
Definition: qstring.h:83
static quint32 read32(const uchar *data)
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
unsigned char uchar
Definition: qglobal.h:994
unsigned int uint
Definition: qglobal.h:996
unsigned int quint32
Definition: qglobal.h:938
static quint8 read8(const uchar *data)
static const KeyPair *const end

◆ match()

static bool match ( const uchar found,
const char *  target,
uint  len 
)
static

◆ numerusHelper()

static int numerusHelper ( int  n,
const uchar rules,
int  rulesSize 
)
static

Definition at line 130 of file qtranslator.cpp.

Referenced by QTranslatorPrivate::do_translate().

131 {
132 #define CHECK_RANGE \
133  do { \
134  if (i >= rulesSize) \
135  return -1; \
136  } while (0)
137 
138  int result = 0;
139  int i = 0;
140 
141  if (rulesSize == 0)
142  return 0;
143 
144  for (;;) {
145  bool orExprTruthValue = false;
146 
147  for (;;) {
148  bool andExprTruthValue = true;
149 
150  for (;;) {
151  bool truthValue = true;
152 
153  CHECK_RANGE;
154  int opcode = rules[i++];
155 
156  int leftOperand = n;
157  if (opcode & Q_MOD_10) {
158  leftOperand %= 10;
159  } else if (opcode & Q_MOD_100) {
160  leftOperand %= 100;
161  } else if (opcode & Q_LEAD_1000) {
162  while (leftOperand >= 1000)
163  leftOperand /= 1000;
164  }
165 
166  int op = opcode & Q_OP_MASK;
167 
168  CHECK_RANGE;
169  int rightOperand = rules[i++];
170 
171  switch (op) {
172  default:
173  return -1;
174  case Q_EQ:
175  truthValue = (leftOperand == rightOperand);
176  break;
177  case Q_LT:
178  truthValue = (leftOperand < rightOperand);
179  break;
180  case Q_LEQ:
181  truthValue = (leftOperand <= rightOperand);
182  break;
183  case Q_BETWEEN:
184  int bottom = rightOperand;
185  CHECK_RANGE;
186  int top = rules[i++];
187  truthValue = (leftOperand >= bottom && leftOperand <= top);
188  }
189 
190  if (opcode & Q_NOT)
191  truthValue = !truthValue;
192 
193  andExprTruthValue = andExprTruthValue && truthValue;
194 
195  if (i == rulesSize || rules[i] != Q_AND)
196  break;
197  ++i;
198  }
199 
200  orExprTruthValue = orExprTruthValue || andExprTruthValue;
201 
202  if (i == rulesSize || rules[i] != Q_OR)
203  break;
204  ++i;
205  }
206 
207  if (orExprTruthValue)
208  return result;
209 
210  ++result;
211 
212  if (i == rulesSize)
213  return result;
214 
215  if (rules[i++] != Q_NEWRULE)
216  break;
217  }
218  return -1;
219 }
#define CHECK_RANGE

◆ read16()

static quint16 read16 ( const uchar data)
static

Definition at line 752 of file qtranslator.cpp.

Referenced by QTranslatorPrivate::do_translate().

753 {
754  return (data[0] << 8) | (data[1]);
755 }
static const char * data(const QByteArray &arr)

◆ read32()

static quint32 read32 ( const uchar data)
static

Definition at line 757 of file qtranslator.cpp.

Referenced by QTranslatorPrivate::do_load(), QTranslatorPrivate::do_translate(), and getMessage().

758 {
759  return (data[0] << 24)
760  | (data[1] << 16)
761  | (data[2] << 8)
762  | (data[3]);
763 }
static const char * data(const QByteArray &arr)

◆ read8()

static quint8 read8 ( const uchar data)
static

Definition at line 747 of file qtranslator.cpp.

Referenced by QTranslatorPrivate::do_load(), QTranslatorPrivate::do_translate(), and getMessage().

748 {
749  return *data;
750 }
static const char * data(const QByteArray &arr)

Variable Documentation

◆ magic

const uchar magic[MagicLength]
static
Initial value:
= {
0x3c, 0xb8, 0x64, 0x18, 0xca, 0xef, 0x9c, 0x95,
0xcd, 0x21, 0x1c, 0xbf, 0x60, 0xa1, 0xbd, 0xdd
}

Definition at line 96 of file qtranslator.cpp.

Referenced by QTranslatorPrivate::do_load().

◆ MagicLength

const int MagicLength = 16
static

Definition at line 95 of file qtranslator.cpp.

Referenced by QTranslatorPrivate::do_load().