src/fios.cpp
changeset 9031 445046ca06af
parent 9030 096c2ae42927
child 9032 0412a7e3c1d9
equal deleted inserted replaced
9030:096c2ae42927 9031:445046ca06af
   150 		{
   150 		{
   151 			static char str_buffr[512];
   151 			static char str_buffr[512];
   152 			snprintf(str_buffr, lengthof(str_buffr), "%s%s", path, item->name);
   152 			snprintf(str_buffr, lengthof(str_buffr), "%s%s", path, item->name);
   153 			return str_buffr;
   153 			return str_buffr;
   154 		}
   154 		}
       
   155 
       
   156 		case FIOS_TYPE_DRIVE:
       
   157 		case FIOS_TYPE_INVALID:
       
   158 			break;
   155 	}
   159 	}
   156 
   160 
   157 	return NULL;
   161 	return NULL;
   158 }
   162 }
   159 
   163 
   206 #else
   210 #else
   207 	return access(OTTD2FS(filename), 0) == 0;
   211 	return access(OTTD2FS(filename), 0) == 0;
   208 #endif
   212 #endif
   209 }
   213 }
   210 
   214 
   211 typedef byte fios_getlist_callback_proc(int mode, const char *filename, const char *ext, char *title);
   215 typedef FiosType fios_getlist_callback_proc(SaveLoadDialogMode mode, const char *filename, const char *ext, char *title);
   212 
   216 
   213 /** Create a list of the files in a directory, according to some arbitrary rule.
   217 /** Create a list of the files in a directory, according to some arbitrary rule.
   214  *  @param mode The mode we are in. Some modes don't allow 'parent'.
   218  *  @param mode The mode we are in. Some modes don't allow 'parent'.
   215  *  @param callback_proc The function that is called where you need to do the filtering.
   219  *  @param callback_proc The function that is called where you need to do the filtering.
   216  *  @return Return the list of files. */
   220  *  @return Return the list of files. */
   217 static FiosItem *FiosGetFileList(int mode, fios_getlist_callback_proc *callback_proc)
   221 static FiosItem *FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_proc *callback_proc)
   218 {
   222 {
   219 	struct stat sb;
   223 	struct stat sb;
   220 	struct dirent *dirent;
   224 	struct dirent *dirent;
   221 	DIR *dir;
   225 	DIR *dir;
   222 	FiosItem *fios;
   226 	FiosItem *fios;
   275 
   279 
   276 			/* File has no extension, skip it */
   280 			/* File has no extension, skip it */
   277 			if ((t = strrchr(d_name, '.')) == NULL) continue;
   281 			if ((t = strrchr(d_name, '.')) == NULL) continue;
   278 			fios_title[0] = '\0'; // reset the title;
   282 			fios_title[0] = '\0'; // reset the title;
   279 
   283 
   280 			byte type = callback_proc(mode, d_name, t, fios_title);
   284 			FiosType type = callback_proc(mode, d_name, t, fios_title);
   281 			if (type != FIOS_TYPE_INVALID) {
   285 			if (type != FIOS_TYPE_INVALID) {
   282 				fios = FiosAlloc();
   286 				fios = FiosAlloc();
   283 				fios->mtime = sb.st_mtime;
   287 				fios->mtime = sb.st_mtime;
   284 				fios->type = type;
   288 				fios->type = type;
   285 				ttd_strlcpy(fios->name, d_name, lengthof(fios->name));
   289 				ttd_strlcpy(fios->name, d_name, lengthof(fios->name));
   311  * @param title Buffer if a callback wants to lookup the title of the file
   315  * @param title Buffer if a callback wants to lookup the title of the file
   312  * @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a savegame
   316  * @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a savegame
   313  * @see FiosGetFileList
   317  * @see FiosGetFileList
   314  * @see FiosGetSavegameList
   318  * @see FiosGetSavegameList
   315  */
   319  */
   316 static byte FiosGetSavegameListCallback(int mode, const char *file, const char *ext, char *title)
   320 static FiosType FiosGetSavegameListCallback(SaveLoadDialogMode mode, const char *file, const char *ext, char *title)
   317 {
   321 {
   318 	/* Show savegame files
   322 	/* Show savegame files
   319 	 * .SAV OpenTTD saved game
   323 	 * .SAV OpenTTD saved game
   320 	 * .SS1 Transport Tycoon Deluxe preset game
   324 	 * .SS1 Transport Tycoon Deluxe preset game
   321 	 * .SV1 Transport Tycoon Deluxe (Patch) saved game
   325 	 * .SV1 Transport Tycoon Deluxe (Patch) saved game
   337  * Get a list of savegames.
   341  * Get a list of savegames.
   338  * @param mode Save/load mode.
   342  * @param mode Save/load mode.
   339  * @return A pointer to an array of FiosItem representing all the files to be shown in the save/load dialog.
   343  * @return A pointer to an array of FiosItem representing all the files to be shown in the save/load dialog.
   340  * @see FiosGetFileList
   344  * @see FiosGetFileList
   341  */
   345  */
   342 FiosItem *FiosGetSavegameList(int mode)
   346 FiosItem *FiosGetSavegameList(SaveLoadDialogMode mode)
   343 {
   347 {
   344 	static char *_fios_save_path = NULL;
   348 	static char *fios_save_path = NULL;
   345 
   349 
   346 	if (_fios_save_path == NULL) {
   350 	if (fios_save_path == NULL) {
   347 		_fios_save_path = MallocT<char>(MAX_PATH);
   351 		fios_save_path = MallocT<char>(MAX_PATH);
   348 		FioGetDirectory(_fios_save_path, MAX_PATH, SAVE_DIR);
   352 		FioGetDirectory(fios_save_path, MAX_PATH, SAVE_DIR);
   349 	}
   353 	}
   350 
   354 
   351 	_fios_path = _fios_save_path;
   355 	_fios_path = fios_save_path;
   352 
   356 
   353 	return FiosGetFileList(mode, &FiosGetSavegameListCallback);
   357 	return FiosGetFileList(mode, &FiosGetSavegameListCallback);
   354 }
   358 }
   355 
   359 
   356 /**
   360 /**
   361  * @param title Buffer if a callback wants to lookup the title of the file
   365  * @param title Buffer if a callback wants to lookup the title of the file
   362  * @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a scenario
   366  * @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a scenario
   363  * @see FiosGetFileList
   367  * @see FiosGetFileList
   364  * @see FiosGetScenarioList
   368  * @see FiosGetScenarioList
   365  */
   369  */
   366 static byte FiosGetScenarioListCallback(int mode, const char *file, const char *ext, char *title)
   370 static FiosType FiosGetScenarioListCallback(SaveLoadDialogMode mode, const char *file, const char *ext, char *title)
   367 {
   371 {
   368 	/* Show scenario files
   372 	/* Show scenario files
   369 	 * .SCN OpenTTD style scenario file
   373 	 * .SCN OpenTTD style scenario file
   370 	 * .SV0 Transport Tycoon Deluxe (Patch) scenario
   374 	 * .SV0 Transport Tycoon Deluxe (Patch) scenario
   371 	 * .SS0 Transport Tycoon Deluxe preset scenario */
   375 	 * .SS0 Transport Tycoon Deluxe preset scenario */
   385  * Get a list of scenarios.
   389  * Get a list of scenarios.
   386  * @param mode Save/load mode.
   390  * @param mode Save/load mode.
   387  * @return A pointer to an array of FiosItem representing all the files to be shown in the save/load dialog.
   391  * @return A pointer to an array of FiosItem representing all the files to be shown in the save/load dialog.
   388  * @see FiosGetFileList
   392  * @see FiosGetFileList
   389  */
   393  */
   390 FiosItem *FiosGetScenarioList(int mode)
   394 FiosItem *FiosGetScenarioList(SaveLoadDialogMode mode)
   391 {
   395 {
   392 	static char *_fios_scn_path = NULL;
   396 	static char *fios_scn_path = NULL;
   393 
   397 
   394 	/* Copy the default path on first run or on 'New Game' */
   398 	/* Copy the default path on first run or on 'New Game' */
   395 	if (_fios_scn_path == NULL) {
   399 	if (fios_scn_path == NULL) {
   396 		_fios_scn_path = MallocT<char>(MAX_PATH);
   400 		fios_scn_path = MallocT<char>(MAX_PATH);
   397 		FioGetDirectory(_fios_scn_path, MAX_PATH, SCENARIO_DIR);
   401 		FioGetDirectory(fios_scn_path, MAX_PATH, SCENARIO_DIR);
   398 	}
   402 	}
   399 
   403 
   400 	_fios_path = _fios_scn_path;
   404 	_fios_path = fios_scn_path;
   401 
   405 
   402 	return FiosGetFileList(mode, &FiosGetScenarioListCallback);
   406 	return FiosGetFileList(mode, &FiosGetScenarioListCallback);
   403 }
   407 }
   404 
   408 
   405 static byte FiosGetHeightmapListCallback(int mode, const char *file, const char *ext, char *title)
   409 static FiosType FiosGetHeightmapListCallback(SaveLoadDialogMode mode, const char *file, const char *ext, char *title)
   406 {
   410 {
   407 	/* Show heightmap files
   411 	/* Show heightmap files
   408 	 * .PNG PNG Based heightmap files
   412 	 * .PNG PNG Based heightmap files
   409 	 * .BMP BMP Based heightmap files
   413 	 * .BMP BMP Based heightmap files
   410 	 */
   414 	 */
   417 
   421 
   418 	return FIOS_TYPE_INVALID;
   422 	return FIOS_TYPE_INVALID;
   419 }
   423 }
   420 
   424 
   421 /* Get a list of Heightmaps */
   425 /* Get a list of Heightmaps */
   422 FiosItem *FiosGetHeightmapList(int mode)
   426 FiosItem *FiosGetHeightmapList(SaveLoadDialogMode mode)
   423 {
   427 {
   424 	static char *_fios_hmap_path = NULL;
   428 	static char *fios_hmap_path = NULL;
   425 
   429 
   426 	if (_fios_hmap_path == NULL) {
   430 	if (fios_hmap_path == NULL) {
   427 		_fios_hmap_path = MallocT<char>(MAX_PATH);
   431 		fios_hmap_path = MallocT<char>(MAX_PATH);
   428 		FioGetDirectory(_fios_hmap_path, MAX_PATH, HEIGHTMAP_DIR);
   432 		FioGetDirectory(fios_hmap_path, MAX_PATH, HEIGHTMAP_DIR);
   429 	}
   433 	}
   430 
   434 
   431 	_fios_path = _fios_hmap_path;
   435 	_fios_path = fios_hmap_path;
   432 
   436 
   433 	return FiosGetFileList(mode, &FiosGetHeightmapListCallback);
   437 	return FiosGetFileList(mode, &FiosGetHeightmapListCallback);
   434 }
   438 }