Beispiel #1
0
TQString KStringHandler::center( const TQString &text , uint width )
{
    const TQString s = text.stripWhiteSpace();
    const unsigned int length = s.length();
    if ( width <= length ) {
        return s;
     }

    TQString result;
    result.fill( ' ', ( width - length ) / 2 );
    result += s;

    return result.leftJustify( width );
}
KMimeType * KServiceTypeFactory::findFromPattern(const TQString &_filename, TQString *match)
{
   // Assume we're NOT building a database
   if (!m_str) return 0;

   // Get stream to the header
   TQDataStream *str = m_str;

   str->device()->at( m_fastPatternOffset );

   TQ_INT32 nrOfEntries;
   (*str) >> nrOfEntries;
   TQ_INT32 entrySize;
   (*str) >> entrySize;

   TQ_INT32 fastOffset =  str->device()->at( );

   TQ_INT32 matchingOffset = 0;

   // Let's go for a binary search in the "fast" pattern index
   TQ_INT32 left = 0;
   TQ_INT32 right = nrOfEntries - 1;
   TQ_INT32 middle;
   // Extract extension
   int lastDot = _filename.findRev('.');
   int ext_len = _filename.length() - lastDot - 1;
   if (lastDot != -1 && ext_len <= 4) // if no '.', skip the extension lookup
   {
      TQString extension = _filename.right( ext_len );
      extension = extension.leftJustify(4);

      TQString pattern;
      while (left <= right) {
         middle = (left + right) / 2;
         // read pattern at position "middle"
         str->device()->at( middle * entrySize + fastOffset );
         KSycocaEntry::read(*str, pattern);
         int cmp = pattern.compare( extension );
         if (cmp < 0)
            left = middle + 1;
         else if (cmp == 0) // found
         {
            (*str) >> matchingOffset;
            // don't return newServiceType - there may be an "other" pattern that
            // matches best this file, like *.tar.bz
            if (match)
                *match = "*."+pattern.stripWhiteSpace();
            break; // but get out of the fast patterns
         }
         else