archivers/qpak.c
changeset 464 21c8e0d1578c
parent 457 92e898299c7d
child 467 99664d9842cb
equal deleted inserted replaced
463:98455f2c89c6 464:21c8e0d1578c
   181 
   181 
   182     return(retval);
   182     return(retval);
   183 } /* QPAK_isArchive */
   183 } /* QPAK_isArchive */
   184 
   184 
   185 
   185 
   186 static void qpak_insertion_sort(QPAKentry *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi)
       
   187 {
       
   188     PHYSFS_uint32 i;
       
   189     PHYSFS_uint32 j;
       
   190     QPAKentry tmp;
       
   191 
       
   192     for (i = lo + 1; i <= hi; i++)
       
   193     {
       
   194         memcpy(&tmp, &a[i], sizeof (QPAKentry));
       
   195         j = i;
       
   196         while ((j > lo) && (strcmp(a[j - 1].name, tmp.name) > 0))
       
   197         {
       
   198             memcpy(&a[j], &a[j - 1], sizeof (QPAKentry));
       
   199             j--;
       
   200         } /* while */
       
   201         memcpy(&a[j], &tmp, sizeof (QPAKentry));
       
   202     } /* for */
       
   203 } /* qpak_insertion_sort */
       
   204 
       
   205 
       
   206 static void qpak_sort_entries(QPAKentry *entries, PHYSFS_uint32 max)
       
   207 {
       
   208     qpak_insertion_sort(entries, 0, max - 1);
       
   209 } /* qpak_sort_entries */
       
   210 
       
   211 
       
   212 static int qpak_loadEntries(void *fh, int dirOffset, int numEntries,
   186 static int qpak_loadEntries(void *fh, int dirOffset, int numEntries,
   213                             QPAKentry *entries)
   187                             QPAKentry *entries)
   214 {
   188 {
   215     PHYSFS_uint32 i;
   189     PHYSFS_uint32 i;
   216 
   190 
   434 
   408 
   435     return(1);
   409     return(1);
   436 } /* qpak_populateDirectories */
   410 } /* qpak_populateDirectories */
   437 
   411 
   438 
   412 
   439 static void qpak_deletePakInfo (QPAKinfo *pakInfo)
   413 static void qpak_deletePakInfo(QPAKinfo *pakInfo)
   440 {
   414 {
   441     if (pakInfo->handle != NULL)
   415     if (pakInfo->handle != NULL)
   442         __PHYSFS_platformClose(pakInfo->handle);
   416         __PHYSFS_platformClose(pakInfo->handle);
   443 
   417 
   444     if (pakInfo->filename != NULL)
   418     if (pakInfo->filename != NULL)
   449 
   423 
   450     qpak_deleteDirectory(pakInfo->root);
   424     qpak_deleteDirectory(pakInfo->root);
   451 
   425 
   452     free(pakInfo);
   426     free(pakInfo);
   453 } /* qpak_deletePakInfo */
   427 } /* qpak_deletePakInfo */
       
   428 
       
   429 
       
   430 static int qpak_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
       
   431 {
       
   432     QPAKentry *a = (QPAKentry *) _a;
       
   433     return(strcmp(a[one].name, a[two].name));
       
   434 } /* qpak_entry_cmp */
       
   435 
       
   436 
       
   437 static void qpak_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
       
   438 {
       
   439     QPAKentry tmp;
       
   440     QPAKentry *first = &(((QPAKentry *) _a)[one]);
       
   441     QPAKentry *second = &(((QPAKentry *) _a)[two]);
       
   442     memcpy(&tmp, first, sizeof (QPAKentry));
       
   443     memcpy(first, second, sizeof (QPAKentry));
       
   444     memcpy(second, &tmp, sizeof (QPAKentry));
       
   445 } /* qpak_entry_swap */
   454 
   446 
   455 
   447 
   456 static DirHandle *QPAK_openArchive(const char *name, int forWriting)
   448 static DirHandle *QPAK_openArchive(const char *name, int forWriting)
   457 {
   449 {
   458     void *fh = NULL;
   450     void *fh = NULL;
   504     } /* if */
   496     } /* if */
   505 
   497 
   506     if (qpak_loadEntries(fh, dirOffset, pi->totalEntries, pi->entries) == 0)
   498     if (qpak_loadEntries(fh, dirOffset, pi->totalEntries, pi->entries) == 0)
   507         goto QPAK_openArchive_failed;
   499         goto QPAK_openArchive_failed;
   508 
   500 
   509     qpak_sort_entries(pi->entries, pi->totalEntries);
   501     __PHYSFS_sort(pi->entries, pi->totalEntries,
       
   502                   qpak_entry_cmp, qpak_entry_swap);
   510 
   503 
   511     pi->root = qpak_newDirectory("");
   504     pi->root = qpak_newDirectory("");
   512     if (pi->root == NULL)
   505     if (pi->root == NULL)
   513         goto QPAK_openArchive_failed;
   506         goto QPAK_openArchive_failed;
   514 
   507