fios.c
changeset 4223 d4c9ef555f43
parent 4222 36e9bd216011
child 4235 f5ad8ebce258
equal deleted inserted replaced
4222:36e9bd216011 4223:d4c9ef555f43
     9 #include "hal.h"
     9 #include "hal.h"
    10 #include "string.h"
    10 #include "string.h"
    11 #include "variables.h"
    11 #include "variables.h"
    12 #include "functions.h"
    12 #include "functions.h"
    13 #include "table/strings.h"
    13 #include "table/strings.h"
    14 #include "hal.h"
       
    15 #include "fios.h"
    14 #include "fios.h"
    16 #include <sys/types.h>
    15 #include <sys/types.h>
    17 #include <sys/stat.h>
    16 #include <sys/stat.h>
    18 
    17 
    19 #ifdef WIN32
    18 #ifdef WIN32
    21 #else
    20 #else
    22 # include <unistd.h>
    21 # include <unistd.h>
    23 # include <dirent.h>
    22 # include <dirent.h>
    24 #endif /* WIN32 */
    23 #endif /* WIN32 */
    25 
    24 
    26 char *_fios_path;
    25 /* Variables to display file lists */
    27 FiosItem *_fios_items;
    26 int _fios_num;
    28 int _fios_count, _fios_alloc;
    27 
       
    28 static char *_fios_path;
       
    29 static FiosItem *_fios_items;
       
    30 static int _fios_count, _fios_alloc;
    29 
    31 
    30 /* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */
    32 /* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */
    31 extern bool FiosIsRoot(const char *path);
    33 extern bool FiosIsRoot(const char *path);
    32 extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
    34 extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
    33 extern void FiosGetDrives(void);
    35 extern void FiosGetDrives(void);
   182 /** Create a list of the files in a directory, according to some arbitrary rule.
   184 /** Create a list of the files in a directory, according to some arbitrary rule.
   183  *  @param num Will be filled with the amount of items.
   185  *  @param num Will be filled with the amount of items.
   184  *  @param mode The mode we are in. Some modes don't allow 'parent'.
   186  *  @param mode The mode we are in. Some modes don't allow 'parent'.
   185  *  @param callback The function that is called where you need to do the filtering.
   187  *  @param callback The function that is called where you need to do the filtering.
   186  *  @return Return the list of files. */
   188  *  @return Return the list of files. */
   187 static FiosItem *FiosGetFileList(int *num, int mode, fios_getlist_callback_proc *callback_proc)
   189 static FiosItem *FiosGetFileList(int mode, fios_getlist_callback_proc *callback_proc)
   188 {
   190 {
   189 	struct stat sb;
   191 	struct stat sb;
   190 	struct dirent *dirent;
   192 	struct dirent *dirent;
   191 	DIR *dir;
   193 	DIR *dir;
   192 	FiosItem *fios;
   194 	FiosItem *fios;
   263 	qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems);
   265 	qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems);
   264 
   266 
   265 	/* Show drives */
   267 	/* Show drives */
   266 	if (mode != SLD_NEW_GAME) FiosGetDrives();
   268 	if (mode != SLD_NEW_GAME) FiosGetDrives();
   267 
   269 
   268 	*num = _fios_count;
   270 	_fios_num = _fios_count;
   269 	return _fios_items;
   271 	return _fios_items;
   270 }
   272 }
   271 
   273 
   272 /**
   274 /**
   273  * Callback for FiosGetFileList. It tells if a file is a savegame or not.
   275  * Callback for FiosGetFileList. It tells if a file is a savegame or not.
   303  * Get a list of savegames.
   305  * Get a list of savegames.
   304  * @param mode Save/load mode.
   306  * @param mode Save/load mode.
   305  * @return A pointer to an array of FiosItem representing all the files to be shown in the save/load dialog.
   307  * @return A pointer to an array of FiosItem representing all the files to be shown in the save/load dialog.
   306  * @see FiosGetFileList
   308  * @see FiosGetFileList
   307  */
   309  */
   308 FiosItem *FiosGetSavegameList(int *num, int mode)
   310 FiosItem *FiosGetSavegameList(int mode)
   309 {
   311 {
   310 	static char *_fios_save_path = NULL;
   312 	static char *_fios_save_path = NULL;
   311 
   313 
   312 	if (_fios_save_path == NULL) {
   314 	if (_fios_save_path == NULL) {
   313 		_fios_save_path = malloc(MAX_PATH);
   315 		_fios_save_path = malloc(MAX_PATH);
   314 		ttd_strlcpy(_fios_save_path, _path.save_dir, MAX_PATH);
   316 		ttd_strlcpy(_fios_save_path, _path.save_dir, MAX_PATH);
   315 	}
   317 	}
   316 
   318 
   317 	_fios_path = _fios_save_path;
   319 	_fios_path = _fios_save_path;
   318 
   320 
   319 	return FiosGetFileList(num, mode, &FiosGetSavegameListCallback);
   321 	return FiosGetFileList(mode, &FiosGetSavegameListCallback);
   320 }
   322 }
   321 
   323 
   322 /**
   324 /**
   323  * Callback for FiosGetFileList. It tells if a file is a scenario or not.
   325  * Callback for FiosGetFileList. It tells if a file is a scenario or not.
   324  * @param mode Save/load mode.
   326  * @param mode Save/load mode.
   351  * Get a list of scenarios.
   353  * Get a list of scenarios.
   352  * @param mode Save/load mode.
   354  * @param mode Save/load mode.
   353  * @return A pointer to an array of FiosItem representing all the files to be shown in the save/load dialog.
   355  * @return A pointer to an array of FiosItem representing all the files to be shown in the save/load dialog.
   354  * @see FiosGetFileList
   356  * @see FiosGetFileList
   355  */
   357  */
   356 FiosItem *FiosGetScenarioList(int *num, int mode)
   358 FiosItem *FiosGetScenarioList(int mode)
   357 {
   359 {
   358 	static char *_fios_scn_path = NULL;
   360 	static char *_fios_scn_path = NULL;
   359 
   361 
   360 	if (_fios_scn_path == NULL) {
   362 	if (_fios_scn_path == NULL) {
   361 		_fios_scn_path = malloc(MAX_PATH);
   363 		_fios_scn_path = malloc(MAX_PATH);
   362 		ttd_strlcpy(_fios_scn_path, _path.scenario_dir, MAX_PATH);
   364 		ttd_strlcpy(_fios_scn_path, _path.scenario_dir, MAX_PATH);
   363 	}
   365 	}
   364 
   366 
   365 	_fios_path = _fios_scn_path;
   367 	_fios_path = _fios_scn_path;
   366 
   368 
   367 	return FiosGetFileList(num, mode, &FiosGetScenarioListCallback);
   369 	return FiosGetFileList(mode, &FiosGetScenarioListCallback);
   368 }
   370 }