OpenTTD
Data Structures | Typedefs | Functions | Variables
fios.cpp File Reference

This file contains functions for building file lists for the save/load dialogs. More...

#include "stdafx.h"
#include "fios.h"
#include "fileio_func.h"
#include "tar_type.h"
#include "screenshot.h"
#include "string_func.h"
#include <sys/stat.h>
#include <unistd.h>
#include "table/strings.h"
#include "safeguards.h"
#include "network/network_content.h"
#include "3rdparty/md5/md5.h"

Go to the source code of this file.

Data Structures

class  FiosFileScanner
 Scanner to scan for a particular type of FIOS file. More...
struct  ScenarioIdentifier
 Basic data to distinguish a scenario. More...
class  ScenarioScanner
 Scanner to find the unique IDs of scenarios. More...

Typedefs

typedef FiosType fios_getlist_callback_proc (SaveLoadDialogMode mode, const char *filename, const char *ext, char *title, const char *last)

Functions

bool FiosIsRoot (const char *path)
bool FiosIsValidFile (const char *path, const struct dirent *ent, struct stat *sb)
bool FiosIsHiddenFile (const struct dirent *ent)
void FiosGetDrives ()
bool FiosGetDiskFreeSpace (const char *path, uint64 *tot)
void GetOldSaveGameName (const char *file, char *title, const char *last)
int CDECL CompareFiosItems (const FiosItem *da, const FiosItem *db)
 Compare two FiosItem's.
void FiosFreeSavegameList ()
 Free the list of savegames.
StringID FiosGetDescText (const char **path, uint64 *total_free)
 Get descriptive texts.
const char * FiosBrowseTo (const FiosItem *item)
 Browse to a new path based on the passed item, starting at #_fios_path.
static void FiosMakeFilename (char *buf, const char *path, const char *name, const char *ext, const char *last)
 Construct a filename from its components in destination buffer buf.
void FiosMakeSavegameName (char *buf, const char *name, const char *last)
 Make a save game or scenario filename from a name.
void FiosMakeHeightmapName (char *buf, const char *name, const char *last)
 Construct a filename for a height map.
bool FiosDelete (const char *name)
 Delete a file.
static void FiosGetFileList (SaveLoadDialogMode mode, fios_getlist_callback_proc *callback_proc, Subdirectory subdir)
 Fill the list of the files in a directory, according to some arbitrary rule.
static void GetFileTitle (const char *file, char *title, const char *last, Subdirectory subdir)
 Get the title of a file, which (if exists) is stored in a file named the same as the data file but with '.title' added to it.
FiosType FiosGetSavegameListCallback (SaveLoadDialogMode mode, const char *file, const char *ext, char *title, const char *last)
 Callback for FiosGetFileList.
void FiosGetSavegameList (SaveLoadDialogMode mode)
 Get a list of savegames.
static FiosType FiosGetScenarioListCallback (SaveLoadDialogMode mode, const char *file, const char *ext, char *title, const char *last)
 Callback for FiosGetFileList.
void FiosGetScenarioList (SaveLoadDialogMode mode)
 Get a list of scenarios.
static FiosType FiosGetHeightmapListCallback (SaveLoadDialogMode mode, const char *file, const char *ext, char *title, const char *last)
void FiosGetHeightmapList (SaveLoadDialogMode mode)
 Get a list of heightmaps.
const char * FiosGetScreenshotDir ()
 Get the directory for screenshots.
const char * FindScenario (const ContentInfo *ci, bool md5sum)
 Find a given scenario based on its unique ID.
bool HasScenario (const ContentInfo *ci, bool md5sum)
 Check whether we've got a given scenario based on its unique ID.
void ScanScenarios ()
 Force a (re)scan of the scenarios.

Variables

SmallVector< FiosItem, 32 > _fios_items
static char * _fios_path
static const char * _fios_path_last
SmallFiosItem _file_to_saveload
SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING
static ScenarioScanner _scanner
 Scanner for scenarios.

Detailed Description

This file contains functions for building file lists for the save/load dialogs.

Definition in file fios.cpp.

Function Documentation

int CDECL CompareFiosItems ( const FiosItem da,
const FiosItem db 
)

Compare two FiosItem's.

Used with sort when sorting the file list.

Parameters
daA pointer to the first FiosItem to compare.
dbA pointer to the second FiosItem to compare.
Returns
-1, 0 or 1, depending on how the two items should be sorted.

Definition at line 54 of file fios.cpp.

Referenced by FiosGetFileList().

const char* FindScenario ( const ContentInfo ci,
bool  md5sum 
)

Find a given scenario based on its unique ID.

Parameters
ciThe content info to compare it to.
md5sumWhether to look at the md5sum or the id.
Returns
The filename of the file, else NULL.

Definition at line 679 of file fios.cpp.

References SmallVector< T, S >::Begin(), SmallVector< T, S >::End(), ContentInfo::md5sum, ScenarioScanner::Scan(), and ContentInfo::unique_id.

Referenced by ContentInfo::GetTextfile(), and HasScenario().

const char* FiosBrowseTo ( const FiosItem item)

Browse to a new path based on the passed item, starting at #_fios_path.

Parameters
*itemItem telling us what to do.
Returns
A filename w/path if we reached a file, otherwise NULL.

Definition at line 93 of file fios.cpp.

References seprintf(), and strecat().

Referenced by SaveLoadWindow::OnClick().

bool FiosDelete ( const char *  name)

Delete a file.

Parameters
nameFilename to delete.
Returns
Whether the file deletion was successful.

Definition at line 210 of file fios.cpp.

References FiosMakeSavegameName(), and lastof.

Referenced by SaveLoadWindow::OnTimeout().

void FiosFreeSavegameList ( )

Free the list of savegames.

Definition at line 69 of file fios.cpp.

References SmallVector< T, S >::Clear(), and SmallVector< T, S >::Compact().

StringID FiosGetDescText ( const char **  path,
uint64 *  total_free 
)

Get descriptive texts.

Returns the path and free space left on the device

Parameters
pathstring describing the path
total_freetotal free space in megabytes, optional (can be NULL)
Returns
StringID describing the path (free space or failure)

Definition at line 82 of file fios.cpp.

Referenced by SaveLoadWindow::DrawWidget().

static void FiosGetFileList ( SaveLoadDialogMode  mode,
fios_getlist_callback_proc *  callback_proc,
Subdirectory  subdir 
)
static

Fill the list of the files in a directory, according to some arbitrary rule.

Parameters
modeThe mode we are in. Some modes don't allow 'parent'.
callback_procThe function that is called where you need to do the filtering.
subdirThe directory from where to start (global) searching.

Definition at line 296 of file fios.cpp.

References SmallVector< T, S >::Append(), SmallVector< T, S >::Begin(), SmallVector< T, S >::Clear(), SmallVector< T, S >::Compact(), CompareFiosItems(), FS2OTTD(), SmallVector< T, S >::Get(), lastof, SmallVector< T, S >::Length(), NO_DIRECTORY, QSortT(), FileScanner::Scan(), seprintf(), str_validate(), strecpy(), and ttd_opendir().

Referenced by FiosGetHeightmapList(), FiosGetSavegameList(), and FiosGetScenarioList().

void FiosGetHeightmapList ( SaveLoadDialogMode  mode)

Get a list of heightmaps.

Parameters
modeSave/load mode.

Definition at line 553 of file fios.cpp.

References FiosGetFileList(), HEIGHTMAP_DIR, lastof, and NO_DIRECTORY.

void FiosGetSavegameList ( SaveLoadDialogMode  mode)

Get a list of savegames.

Parameters
modeSave/load mode.
See Also
FiosGetFileList

Definition at line 430 of file fios.cpp.

References FiosGetFileList(), FiosGetSavegameListCallback(), NO_DIRECTORY, and SAVE_DIR.

FiosType FiosGetSavegameListCallback ( SaveLoadDialogMode  mode,
const char *  file,
const char *  ext,
char *  title,
const char *  last 
)

Callback for FiosGetFileList.

It tells if a file is a savegame or not.

Parameters
modeSave/load mode.
fileName of the file to check.
extA pointer to the extension identifier inside file
titleBuffer if a callback wants to lookup the title of the file; NULL to skip the lookup
lastLast available byte in buffer (to prevent buffer overflows); not used when title == NULL
Returns
a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a savegame
See Also
FiosGetFileList
FiosGetSavegameList

Definition at line 398 of file fios.cpp.

References GetFileTitle(), SAVE_DIR, SLD_LOAD_GAME, and SLD_LOAD_SCENARIO.

Referenced by FiosGetSavegameList(), and openttd_main().

void FiosGetScenarioList ( SaveLoadDialogMode  mode)

Get a list of scenarios.

Parameters
modeSave/load mode.
See Also
FiosGetFileList

Definition at line 484 of file fios.cpp.

References FiosGetFileList(), FiosGetScenarioListCallback(), lastof, NO_DIRECTORY, SCENARIO_DIR, and SLD_LOAD_SCENARIO.

static FiosType FiosGetScenarioListCallback ( SaveLoadDialogMode  mode,
const char *  file,
const char *  ext,
char *  title,
const char *  last 
)
static

Callback for FiosGetFileList.

It tells if a file is a scenario or not.

Parameters
modeSave/load mode.
fileName of the file to check.
extA pointer to the extension identifier inside file
titleBuffer if a callback wants to lookup the title of the file
lastLast available byte in buffer (to prevent buffer overflows)
Returns
a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a scenario
See Also
FiosGetFileList
FiosGetScenarioList

Definition at line 458 of file fios.cpp.

References GetFileTitle(), SCENARIO_DIR, SLD_LOAD_GAME, and SLD_LOAD_SCENARIO.

Referenced by FiosGetScenarioList().

const char* FiosGetScreenshotDir ( )

Get the directory for screenshots.

Returns
path to screenshots

Definition at line 577 of file fios.cpp.

References SCREENSHOT_DIR.

Referenced by MakeScreenshotName().

static void FiosMakeFilename ( char *  buf,
const char *  path,
const char *  name,
const char *  ext,
const char *  last 
)
static

Construct a filename from its components in destination buffer buf.

Parameters
bufDestination buffer.
pathDirectory path, may be NULL.
nameFilename.
extFilename extension (use "" for no extension).
lastLast element of buffer buf.

Definition at line 153 of file fios.cpp.

References seprintf().

Referenced by FiosMakeHeightmapName(), and FiosMakeSavegameName().

void FiosMakeHeightmapName ( char *  buf,
const char *  name,
const char *  last 
)

Construct a filename for a height map.

Parameters
bufDestination buffer.
nameFilename.
lastLast element of buffer buf.

Definition at line 196 of file fios.cpp.

References FiosMakeFilename(), GetCurrentScreenshotExtension(), lastof, and strecpy().

Referenced by SaveLoadWindow::OnTimeout().

void FiosMakeSavegameName ( char *  buf,
const char *  name,
const char *  last 
)

Make a save game or scenario filename from a name.

Parameters
bufDestination buffer for saving the filename.
nameName of the file.
lastLast element of buffer buf.

Definition at line 183 of file fios.cpp.

References FiosMakeFilename().

Referenced by FiosDelete(), and SaveLoadWindow::OnTimeout().

static void GetFileTitle ( const char *  file,
char *  title,
const char *  last,
Subdirectory  subdir 
)
static

Get the title of a file, which (if exists) is stored in a file named the same as the data file but with '.title' added to it.

Parameters
filefilename to get the title for
titlethe title buffer to fill
lastthe last element in the title buffer
subdirthe sub directory to search in

Definition at line 371 of file fios.cpp.

References FioFCloseFile(), FioFOpenFile(), lastof, str_validate(), strecat(), and strecpy().

Referenced by FiosGetSavegameListCallback(), and FiosGetScenarioListCallback().

bool HasScenario ( const ContentInfo ci,
bool  md5sum 
)

Check whether we've got a given scenario based on its unique ID.

Parameters
ciThe content info to compare it to.
md5sumWhether to look at the md5sum or the id.
Returns
True iff we've got the scenario.

Definition at line 699 of file fios.cpp.

References FindScenario().