physfs.c
changeset 12 a4041c91d715
parent 11 677e01f5109e
child 15 418eacc97ac8
equal deleted inserted replaced
11:677e01f5109e 12:a4041c91d715
   706 const char *PHYSFS_getRealDir(const char *filename)
   706 const char *PHYSFS_getRealDir(const char *filename)
   707 {
   707 {
   708 } /* PHYSFS_getRealDir */
   708 } /* PHYSFS_getRealDir */
   709 
   709 
   710 
   710 
   711 
   711 static void countList(LinkedStringList *list)
   712 /**
   712 {
   713  * Get a file listing of a search path's directory. Matching directories are
   713     int retval = 0;
   714  *  interpolated. That is, if "C:\mypath" is in the search path and contains a
   714     LinkedStringList *i;
   715  *  directory "savegames" that contains "x.sav", "y.sav", and "z.sav", and
   715 
   716  *  there is also a "C:\userpath" in the search path that has a "savegames"
   716     assert(list != NULL);
   717  *  subdirectory with "w.sav", then the following code:
   717 
   718  *
   718     for (i = list; i != NULL; i = i->next)
   719  * ------------------------------------------------
   719         retval++;
   720  * char **rc = PHYSFS_enumerateFiles("savegames");
   720 
   721  * char **i;
   721     return(retval);
   722  *
   722 } /* countList */
   723  * for (i = rc; *i != NULL; i++)
   723 
   724  *     printf("We've got [%s].\n", *i);
   724 
   725  *
   725 static char **convertStringListToPhysFSList(LinkedStringList *finalList)
   726  * PHYSFS_freeList(rc);
   726 {
   727  * ------------------------------------------------
   727     int i;
   728  *
   728     LinkedStringList *next = NULL;
   729  *  ...will print:
   729     int len = countList(finalList);
   730  *
   730     char **retval = (char **) malloc((len + 1) * sizeof (char *));
   731  * ------------------------------------------------
   731 
   732  * We've got [x.sav].
   732     if (retval == NULL)
   733  * We've got [y.sav].
   733         __PHYSFS_setError(ERR_OUT_OF_MEMORY);
   734  * We've got [z.sav].
   734 
   735  * We've got [w.sav].
   735     for (i = 0; i < len; i++)
   736  * ------------------------------------------------
   736     {
   737  *
   737         next = finalList->next;
   738  * Don't forget to call PHYSFS_freeList() with the return value from this
   738         if (retval == NULL)
   739  *  function when you are done with it.
   739             free(finalList->str);
   740  *
   740         else
   741  *    @param path directory in platform-independent notation to enumerate.
   741             retval[i] = finalList->str;
   742  *   @return Null-terminated array of null-terminated strings.
   742         free(finalList);
   743  */
   743         finalList = next;
       
   744     } /* for */
       
   745 
       
   746     if (retval != NULL);
       
   747         retval[i] = NULL;
       
   748 
       
   749     return(retval);
       
   750 } /* convertStringListToPhysFSList */
       
   751 
       
   752 
       
   753 static void insertStringListItem(LinkedStringList **final,
       
   754                                  LinkedStringList *item)
       
   755 {
       
   756     LinkedStringList *i;
       
   757     LinkedStringList *prev = NULL;
       
   758     int rc;
       
   759 
       
   760     for (i = *final; i != NULL; i = i->next)
       
   761     {
       
   762         rc = strcmp(i->str, item->str);
       
   763         if (rc == 0)      /* already in list. */
       
   764         {
       
   765             free(item->str);
       
   766             free(item);
       
   767             return;
       
   768         } /* if */
       
   769         else if (rc > 0)  /* insertion point. */
       
   770         {
       
   771             if (prev == NULL)
       
   772                 *final = item;
       
   773             else
       
   774                 prev->next = item;
       
   775             item->next = i;
       
   776             return;
       
   777         } /* else if */
       
   778         prev = i;
       
   779     } /* for */
       
   780 } /* insertStringListItem */
       
   781 
       
   782 
       
   783 /* if we run out of memory anywhere in here, we give back what we can. */
       
   784 static void interpolateStringLists(LinkedStringList **final,
       
   785                                     LinkedStringList *newList)
       
   786 {
       
   787     LinkedStringList *next = NULL;
       
   788 
       
   789     while (newList != NULL)
       
   790     {
       
   791         next = newList->next;
       
   792         insertStringListItem(final, newList);
       
   793         newList = next;
       
   794     } /* while */
       
   795 } /* interpolateStringLists */
       
   796 
       
   797 
   744 char **PHYSFS_enumerateFiles(const char *path)
   798 char **PHYSFS_enumerateFiles(const char *path)
   745 {
   799 {
       
   800     SearchDirInfo *i;
       
   801     char **retval = NULL;
       
   802     LinkedStringList *rc;
       
   803     LinkedStringList *finalList = NULL;
       
   804 
       
   805     for (i = searchPath; i != NULL; i = i->next)
       
   806     {
       
   807         assert(i->reader->funcs->enumerateFiles != NULL);
       
   808         rc = i->reader->funcs->enumerateFiles(path);
       
   809         interpolateStringLists(&finalList, rc);
       
   810     } /* for */
       
   811 
       
   812     retval = convertStringListToPhysFSList(finalList);
       
   813     return(retval);
   746 } /* PHYSFS_enumerateFiles */
   814 } /* PHYSFS_enumerateFiles */
   747 
   815 
   748 
   816 
   749 /**
   817 /**
   750  * Open a file for writing, in platform-independent notation and in relation
   818  * Open a file for writing, in platform-independent notation and in relation