Logo Search packages:      
Sourcecode: qcomicbook version File versions  Download package

ImgArchiveSink::ArchiveType ImgArchiveSink::archiveType ( const QString &  filename  )  [static, protected]

Determines archive type.

Determines archive type basing on filename extension. As a last effort, if the check fails, tries to guess archive type basing on well known magic bytes.

Parameters:
filename name of the archive, with path

Definition at line 85 of file imgarchivesink.cpp.

{
      static const struct {
            int offset; //the first byte to compare
            int len; //number of bytes in pattern
            const char *ptrn; //pattern
            ArchiveType type; //archive type
      } magic[] = {
            {0, 4, "\x52\x61\x72\x21", RAR_ARCHIVE},
            {0, 4, "\x50\x4b\x03\x04", ZIP_ARCHIVE},
            {8, 4, "\x2a\x41\x43\x45", ACE_ARCHIVE}
      };

      QFile file(filename);
      if (file.open(IO_ReadOnly))
      {
            for (int i=0; i<3; i++)
            {
                  int j;
                  if (!file.at(magic[i].offset))
                        continue;
                  for (j=0; j<magic[i].len; j++)
                  {
                        int c;
                        if ((c = file.getch()) < 0)
                              break;
                        if (c != magic[i].ptrn[j])
                              break;
                  }
                  if (j == magic[i].len)
                  {
                        file.close();
                        return magic[i].type;
                  }
            }
            file.close();
      }

      //
      // try to match filename extension
      for (QValueList<ArchiveTypeInfo>::const_iterator it = archinfo.begin(); it!=archinfo.end(); it++)
      {
            const ArchiveTypeInfo &inf = *it;
            for (QStringList::const_iterator sit = inf.extensions.begin(); sit!=inf.extensions.end(); sit++)
            {
                  if (filename.endsWith(*sit, false))
                        return inf.type;
            }
      }

      return UNKNOWN_ARCHIVE;
}


Generated by  Doxygen 1.6.0   Back to index