Qt 4.8
Public Types | Public Functions | Public Variables | Protected Functions | Private Types | Private Functions | Properties | List of all members
QResourceRoot Class Reference
Inheritance diagram for QResourceRoot:
QDynamicBufferResourceRoot QDynamicFileResourceRoot

Public Types

enum  ResourceRootType { Resource_Builtin, Resource_File, Resource_Buffer }
 

Public Functions

QStringList children (int node) const
 
const uchardata (int node, qint64 *size) const
 
int findNode (const QString &path, const QLocale &locale=QLocale()) const
 
bool isCompressed (int node) const
 
bool isContainer (int node) const
 
virtual QString mappingRoot () const
 
bool mappingRootSubdir (const QString &path, QString *match=0) const
 
bool operator!= (const QResourceRoot &other) const
 
bool operator== (const QResourceRoot &other) const
 
 QResourceRoot ()
 
 QResourceRoot (const uchar *t, const uchar *n, const uchar *d)
 
virtual ResourceRootType type () const
 
virtual ~QResourceRoot ()
 

Public Variables

QAtomicInt ref
 

Protected Functions

void setSource (const uchar *t, const uchar *n, const uchar *d)
 

Private Types

enum  Flags { Compressed = 0x01, Directory = 0x02 }
 

Private Functions

int findOffset (int node) const
 
short flags (int node) const
 
int hash (int node) const
 
QString name (int node) const
 

Properties

const ucharnames
 
const ucharpayloads
 
const uchartree
 

Detailed Description

Definition at line 108 of file qresource.cpp.

Enumerations

◆ Flags

enum QResourceRoot::Flags
private
Enumerator
Compressed 
Directory 

Definition at line 110 of file qresource.cpp.

111  {
112  Compressed = 0x01,
113  Directory = 0x02
114  };

◆ ResourceRootType

Enumerator
Resource_Builtin 
Resource_File 
Resource_Buffer 

Definition at line 137 of file qresource.cpp.

Constructors and Destructors

◆ QResourceRoot() [1/2]

QResourceRoot::QResourceRoot ( )
inline

Definition at line 123 of file qresource.cpp.

123 : tree(0), names(0), payloads(0) {}
const uchar * tree
Definition: qresource.cpp:115
const uchar * names
Definition: qresource.cpp:115
const uchar * payloads
Definition: qresource.cpp:115

◆ QResourceRoot() [2/2]

QResourceRoot::QResourceRoot ( const uchar t,
const uchar n,
const uchar d 
)
inline

Definition at line 124 of file qresource.cpp.

124 { setSource(t, n, d); }
double d
Definition: qnumeric_p.h:62
void setSource(const uchar *t, const uchar *n, const uchar *d)
Definition: qresource.cpp:141

◆ ~QResourceRoot()

virtual QResourceRoot::~QResourceRoot ( )
inlinevirtual

Definition at line 125 of file qresource.cpp.

125 { }

Functions

◆ children()

QStringList QResourceRoot::children ( int  node) const

Definition at line 806 of file qresource.cpp.

Referenced by QResourcePrivate::ensureChildren().

807 {
808  if(node == -1)
809  return QStringList();
810  int offset = findOffset(node) + 4; //jump past name
811 
812  const short flags = (tree[offset+0] << 8) + (tree[offset+1] << 0);
813  offset += 2;
814 
815  QStringList ret;
816  if(flags & Directory) {
817  const int child_count = (tree[offset+0] << 24) + (tree[offset+1] << 16) +
818  (tree[offset+2] << 8) + (tree[offset+3] << 0);
819  offset += 4;
820  const int child_off = (tree[offset+0] << 24) + (tree[offset+1] << 16) +
821  (tree[offset+2] << 8) + (tree[offset+3] << 0);
822  for(int i = child_off; i < child_off+child_count; ++i)
823  ret << name(i);
824  }
825  return ret;
826 }
QString name(int node) const
Definition: qresource.cpp:620
const uchar * tree
Definition: qresource.cpp:115
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
short flags(int node) const
Definition: qresource.cpp:774
int findOffset(int node) const
Definition: qresource.cpp:116

◆ data()

const uchar * QResourceRoot::data ( int  node,
qint64 size 
) const

Definition at line 781 of file qresource.cpp.

Referenced by QResourcePrivate::load().

782 {
783  if(node == -1) {
784  *size = 0;
785  return 0;
786  }
787  int offset = findOffset(node) + 4; //jump past name
788 
789  const short flags = (tree[offset+0] << 8) + (tree[offset+1] << 0);
790  offset += 2;
791 
792  offset += 4; //jump past locale
793 
794  if(!(flags & Directory)) {
795  const int data_offset = (tree[offset+0] << 24) + (tree[offset+1] << 16) +
796  (tree[offset+2] << 8) + (tree[offset+3] << 0);
797  const uint data_length = (payloads[data_offset+0] << 24) + (payloads[data_offset+1] << 16) +
798  (payloads[data_offset+2] << 8) + (payloads[data_offset+3] << 0);
799  const uchar *ret = payloads+data_offset+4;
800  *size = data_length;
801  return ret;
802  }
803  *size = 0;
804  return 0;
805 }
const uchar * tree
Definition: qresource.cpp:115
unsigned char uchar
Definition: qglobal.h:994
unsigned int uint
Definition: qglobal.h:996
short flags(int node) const
Definition: qresource.cpp:774
int findOffset(int node) const
Definition: qresource.cpp:116
const uchar * payloads
Definition: qresource.cpp:115

◆ findNode()

int QResourceRoot::findNode ( const QString path,
const QLocale locale = QLocale() 
) const

Definition at line 644 of file qresource.cpp.

Referenced by QResourcePrivate::ensureChildren(), and QResourcePrivate::load().

645 {
646  QString path = _path;
647  {
648  QString root = mappingRoot();
649  if(!root.isEmpty()) {
650  if(root == path) {
651  path = QLatin1Char('/');
652  } else {
653  if(!root.endsWith(QLatin1Char('/')))
654  root += QLatin1Char('/');
655  if(path.size() >= root.size() && path.startsWith(root))
656  path = path.mid(root.length()-1);
657  if(path.isEmpty())
658  path = QLatin1Char('/');
659  }
660  }
661  }
662 #ifdef DEBUG_RESOURCE_MATCH
663  qDebug() << "!!!!" << "START" << path << locale.country() << locale.language();
664 #endif
665 
666  if(path == QLatin1String("/"))
667  return 0;
668 
669  //the root node is always first
670  int child_count = (tree[6] << 24) + (tree[7] << 16) +
671  (tree[8] << 8) + (tree[9] << 0);
672  int child = (tree[10] << 24) + (tree[11] << 16) +
673  (tree[12] << 8) + (tree[13] << 0);
674 
675  //now iterate up the tree
676  int node = -1;
677 
678  QStringSplitter splitter(path);
679  while (child_count && splitter.hasNext()) {
680  QStringRef segment = splitter.next();
681 
682 #ifdef DEBUG_RESOURCE_MATCH
683  qDebug() << " CHILDREN" << segment;
684  for(int j = 0; j < child_count; ++j) {
685  qDebug() << " " << child+j << " :: " << name(child+j);
686  }
687 #endif
688  const int h = qHash(segment);
689 
690  //do the binary search for the hash
691  int l = 0, r = child_count-1;
692  int sub_node = (l+r+1)/2;
693  while(r != l) {
694  const int sub_node_hash = hash(child+sub_node);
695  if(h == sub_node_hash)
696  break;
697  else if(h < sub_node_hash)
698  r = sub_node - 1;
699  else
700  l = sub_node;
701  sub_node = (l + r + 1) / 2;
702  }
703  sub_node += child;
704 
705  //now do the "harder" compares
706  bool found = false;
707  if(hash(sub_node) == h) {
708  while(sub_node > child && hash(sub_node-1) == h) //backup for collisions
709  --sub_node;
710  for(; sub_node < child+child_count && hash(sub_node) == h; ++sub_node) { //here we go...
711  if(name(sub_node) == segment) {
712  found = true;
713  int offset = findOffset(sub_node);
714 #ifdef DEBUG_RESOURCE_MATCH
715  qDebug() << " TRY" << sub_node << name(sub_node) << offset;
716 #endif
717  offset += 4; //jump past name
718 
719  const short flags = (tree[offset+0] << 8) +
720  (tree[offset+1] << 0);
721  offset += 2;
722 
723  if(!splitter.hasNext()) {
724  if(!(flags & Directory)) {
725  const short country = (tree[offset+0] << 8) +
726  (tree[offset+1] << 0);
727  offset += 2;
728 
729  const short language = (tree[offset+0] << 8) +
730  (tree[offset+1] << 0);
731  offset += 2;
732 #ifdef DEBUG_RESOURCE_MATCH
733  qDebug() << " " << "LOCALE" << country << language;
734 #endif
735  if(country == locale.country() && language == locale.language()) {
736 #ifdef DEBUG_RESOURCE_MATCH
737  qDebug() << "!!!!" << "FINISHED" << __LINE__ << sub_node;
738 #endif
739  return sub_node;
740  } else if((country == QLocale::AnyCountry && language == locale.language()) ||
741  (country == QLocale::AnyCountry && language == QLocale::C && node == -1)) {
742  node = sub_node;
743  }
744  continue;
745  } else {
746 #ifdef DEBUG_RESOURCE_MATCH
747  qDebug() << "!!!!" << "FINISHED" << __LINE__ << sub_node;
748 #endif
749 
750  return sub_node;
751  }
752  }
753 
754  if(!(flags & Directory))
755  return -1;
756 
757  child_count = (tree[offset+0] << 24) + (tree[offset+1] << 16) +
758  (tree[offset+2] << 8) + (tree[offset+3] << 0);
759  offset += 4;
760  child = (tree[offset+0] << 24) + (tree[offset+1] << 16) +
761  (tree[offset+2] << 8) + (tree[offset+3] << 0);
762  break;
763  }
764  }
765  }
766  if(!found)
767  break;
768  }
769 #ifdef DEBUG_RESOURCE_MATCH
770  qDebug() << "!!!!" << "FINISHED" << __LINE__ << node;
771 #endif
772  return node;
773 }
uint qHash(const QProcEnvKey &key)
Definition: qprocess_p.h:96
Language language() const
Returns the language of this locale.
Definition: qlocale.cpp:920
QString name(int node) const
Definition: qresource.cpp:620
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
const uchar * tree
Definition: qresource.cpp:115
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual QString mappingRoot() const
Definition: qresource.cpp:131
Q_CORE_EXPORT void qDebug(const char *,...)
QLocale::Language language
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
short flags(int node) const
Definition: qresource.cpp:774
The QStringRef class provides a thin wrapper around QString substrings.
Definition: qstring.h:1099
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
int findOffset(int node) const
Definition: qresource.cpp:116
QFactoryLoader * l
int hash(int node) const
Definition: qresource.cpp:609
Country country() const
Returns the country of this locale.
Definition: qlocale.cpp:945
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
QLocale::Country country
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ findOffset()

int QResourceRoot::findOffset ( int  node) const
inlineprivate

Definition at line 116 of file qresource.cpp.

116 { return node * 14; } //sizeof each tree element

◆ flags()

short QResourceRoot::flags ( int  node) const
private

Definition at line 774 of file qresource.cpp.

775 {
776  if(node == -1)
777  return 0;
778  const int offset = findOffset(node) + 4; //jump past name
779  return (tree[offset+0] << 8) + (tree[offset+1] << 0);
780 }
const uchar * tree
Definition: qresource.cpp:115
int findOffset(int node) const
Definition: qresource.cpp:116

◆ hash()

int QResourceRoot::hash ( int  node) const
inlineprivate

Definition at line 609 of file qresource.cpp.

610 {
611  if(!node) //root
612  return 0;
613  const int offset = findOffset(node);
614  int name_offset = (tree[offset+0] << 24) + (tree[offset+1] << 16) +
615  (tree[offset+2] << 8) + (tree[offset+3] << 0);
616  name_offset += 2; //jump past name length
617  return (names[name_offset+0] << 24) + (names[name_offset+1] << 16) +
618  (names[name_offset+2] << 8) + (names[name_offset+3] << 0);
619 }
const uchar * tree
Definition: qresource.cpp:115
const uchar * names
Definition: qresource.cpp:115
int findOffset(int node) const
Definition: qresource.cpp:116

◆ isCompressed()

bool QResourceRoot::isCompressed ( int  node) const
inline

Definition at line 128 of file qresource.cpp.

Referenced by QResourcePrivate::load().

128 { return flags(node) & Compressed; }
short flags(int node) const
Definition: qresource.cpp:774

◆ isContainer()

bool QResourceRoot::isContainer ( int  node) const
inline

Definition at line 127 of file qresource.cpp.

Referenced by QResourcePrivate::load().

127 { return flags(node) & Directory; }
short flags(int node) const
Definition: qresource.cpp:774

◆ mappingRoot()

virtual QString QResourceRoot::mappingRoot ( ) const
inlinevirtual

Reimplemented in QDynamicBufferResourceRoot.

Definition at line 131 of file qresource.cpp.

131 { return QString(); }
The QString class provides a Unicode character string.
Definition: qstring.h:83

◆ mappingRootSubdir()

bool QResourceRoot::mappingRootSubdir ( const QString path,
QString match = 0 
) const

Definition at line 827 of file qresource.cpp.

Referenced by QResourcePrivate::ensureChildren(), and QResourcePrivate::load().

828 {
829  const QString root = mappingRoot();
830  if(!root.isEmpty()) {
831  const QStringList root_segments = root.split(QLatin1Char('/'), QString::SkipEmptyParts),
832  path_segments = path.split(QLatin1Char('/'), QString::SkipEmptyParts);
833  if(path_segments.size() <= root_segments.size()) {
834  int matched = 0;
835  for(int i = 0; i < path_segments.size(); ++i) {
836  if(root_segments[i] != path_segments[i])
837  break;
838  ++matched;
839  }
840  if(matched == path_segments.size()) {
841  if(match && root_segments.size() > matched)
842  *match = root_segments.at(matched);
843  return true;
844  }
845  }
846  }
847  return false;
848 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual QString mappingRoot() const
Definition: qresource.cpp:131
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
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ name()

QString QResourceRoot::name ( int  node) const
inlineprivate

Definition at line 620 of file qresource.cpp.

621 {
622  if(!node) // root
623  return QString();
624  const int offset = findOffset(node);
625 
626  QString ret;
627  int name_offset = (tree[offset+0] << 24) + (tree[offset+1] << 16) +
628  (tree[offset+2] << 8) + (tree[offset+3] << 0);
629  const short name_length = (names[name_offset+0] << 8) +
630  (names[name_offset+1] << 0);
631  name_offset += 2;
632  name_offset += 4; //jump past hash
633 
634  ret.resize(name_length);
635  QChar *strData = ret.data();
636  for(int i = 0; i < name_length*2; i+=2) {
637  QChar c(names[name_offset+i+1], names[name_offset+i]);
638  *strData = c;
639  ++strData;
640  }
641  return ret;
642 }
unsigned char c[8]
Definition: qnumeric_p.h:62
const uchar * tree
Definition: qresource.cpp:115
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QChar * data()
Returns a pointer to the data stored in the QString.
Definition: qstring.h:710
const uchar * names
Definition: qresource.cpp:115
void resize(int size)
Sets the size of the string to size characters.
Definition: qstring.cpp:1353
int findOffset(int node) const
Definition: qresource.cpp:116

◆ operator!=()

bool QResourceRoot::operator!= ( const QResourceRoot other) const
inline

Definition at line 135 of file qresource.cpp.

136  { return !operator==(other); }
bool operator==(const QResourceRoot &other) const
Definition: qresource.cpp:133

◆ operator==()

bool QResourceRoot::operator== ( const QResourceRoot other) const
inline

Definition at line 133 of file qresource.cpp.

134  { return tree == other.tree && names == other.names && payloads == other.payloads; }
const uchar * tree
Definition: qresource.cpp:115
const uchar * names
Definition: qresource.cpp:115
const uchar * payloads
Definition: qresource.cpp:115

◆ setSource()

void QResourceRoot::setSource ( const uchar t,
const uchar n,
const uchar d 
)
inlineprotected

Definition at line 141 of file qresource.cpp.

141  {
142  tree = t;
143  names = n;
144  payloads = d;
145  }
double d
Definition: qnumeric_p.h:62
const uchar * tree
Definition: qresource.cpp:115
const uchar * names
Definition: qresource.cpp:115
const uchar * payloads
Definition: qresource.cpp:115

◆ type()

virtual ResourceRootType QResourceRoot::type ( ) const
inlinevirtual

Properties

◆ names

const uchar * QResourceRoot::names
private

Definition at line 115 of file qresource.cpp.

Referenced by operator==().

◆ payloads

const uchar * QResourceRoot::payloads
private

Definition at line 115 of file qresource.cpp.

Referenced by operator==().

◆ ref

QAtomicInt QResourceRoot::ref
mutable

◆ tree

const uchar* QResourceRoot::tree
private

Definition at line 115 of file qresource.cpp.

Referenced by operator==().


The documentation for this class was generated from the following file: