(svn r5764) - Cleanup: - Cleanup: Move the now unified FiosAlloc, compare_FiosItems, FiosFreeSavegameList, FiosMakeSavegameName, FiosDelete and FileExists to newly created file fios.c where it belongs.

- Fix: forgot to remove GetLanguageList from functions.h in previous commit
replace/41b28d7194a279bdc17475d4fbe2ea6ec885a466
Darkvater 18 years ago
parent b5e3718ac4
commit ee7b3de2f5

@ -655,6 +655,7 @@ SRCS += elrail.c
SRCS += engine.c
SRCS += engine_gui.c
SRCS += fileio.c
SRCS += fios.c
SRCS += gfx.c
SRCS += gfxinit.c
SRCS += graph_gui.c

@ -0,0 +1,98 @@
/* $Id$ */
/** @file fios.c
* This file contains functions for building file lists for the save/load dialogs.
*/
#include "stdafx.h"
#include "openttd.h"
#include "hal.h"
#include "string.h"
#include "variables.h"
#include "functions.h"
#include "table/strings.h"
#include "hal.h"
#include <sys/types.h>
#include <sys/stat.h>
#ifdef WIN32
# include <io.h>
#else
# include <unistd.h>
# include <dirent.h>
#endif /* WIN32 */
char *_fios_path;
FiosItem *_fios_items;
int _fios_count, _fios_alloc;
/**
* Allocate a new FiosItem.
* @return A pointer to the newly allocated FiosItem.
*/
FiosItem *FiosAlloc(void)
{
if (_fios_count == _fios_alloc) {
_fios_alloc += 256;
_fios_items = realloc(_fios_items, _fios_alloc * sizeof(FiosItem));
}
return &_fios_items[_fios_count++];
}
/**
* Compare two FiosItem's. Used with qsort when sorting the file list.
* @param a A pointer to the first FiosItem to compare.
* @param a A pointer to the second FiosItem to compare.
* @return -1, 0 or 1, depending on how the two items should be sorted.
*/
int CDECL compare_FiosItems(const void *a, const void *b)
{
const FiosItem *da = (const FiosItem *)a;
const FiosItem *db = (const FiosItem *)b;
int r;
if (_savegame_sort_order & SORT_BY_NAME) {
r = strcasecmp(da->title, db->title);
} else {
r = da->mtime < db->mtime ? -1 : 1;
}
if (_savegame_sort_order & SORT_DESCENDING) r = -r;
return r;
}
/**
* Free the list of savegames
*/
void FiosFreeSavegameList(void)
{
free(_fios_items);
_fios_items = NULL;
_fios_alloc = _fios_count = 0;
}
void FiosMakeSavegameName(char *buf, const char *name, size_t size)
{
const char *extension, *period;
extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
/* Don't append the extension if it is already there */
period = strrchr(name, '.');
if (period != NULL && strcasecmp(period, extension) == 0) extension = "";
snprintf(buf, size, "%s" PATHSEP "%s%s", _fios_path, name, extension);
}
bool FiosDelete(const char *name)
{
char filename[512];
FiosMakeSavegameName(filename, name, lengthof(filename));
return unlink(filename) == 0;
}
bool FileExists(const char *filename)
{
return access(filename, 0) == 0;
}

@ -241,7 +241,6 @@ bool ReadLanguagePack(int index);
void InitializeLanguagePacks(void);
const char *GetCurrentLocale(const char *param);
void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize);
int GetLanguageList(char **languages, int max);
void LoadFromConfig(void);
void SaveToConfig(void);

@ -89,6 +89,8 @@ StringID FiosGetDescText(const char **path, uint32 *tot);
bool FiosDelete(const char *name);
// Make a filename from a name
void FiosMakeSavegameName(char *buf, const char *name, size_t size);
// Allocate a new FiosItem
FiosItem *FiosAlloc(void);
int CDECL compare_FiosItems(const void *a, const void *b);

@ -225,6 +225,9 @@
<File
RelativePath=".\fileio.c">
</File>
<File
RelativePath=".\fios.c">
</File>
<File
RelativePath=".\gfx.c">
</File>

@ -309,6 +309,10 @@
RelativePath=".\fileio.c"
>
</File>
<File
RelativePath=".\fios.c"
>
</File>
<File
RelativePath=".\gfx.c"
>

68
os2.c

@ -24,37 +24,11 @@
#include <os2.h>
#include <i86.h>
static char *_fios_path;
extern char *_fios_path;
static char *_fios_save_path;
static char *_fios_scn_path;
static FiosItem *_fios_items;
static int _fios_count, _fios_alloc;
static FiosItem *FiosAlloc(void)
{
if (_fios_count == _fios_alloc) {
_fios_alloc += 256;
_fios_items = realloc(_fios_items, _fios_alloc * sizeof(FiosItem));
}
return &_fios_items[_fios_count++];
}
int compare_FiosItems(const void *a, const void *b)
{
const FiosItem *da = (const FiosItem *)a;
const FiosItem *db = (const FiosItem *)b;
int r;
if (_savegame_sort_order & SORT_BY_NAME) {
r = strcasecmp(da->title, db->title);
} else {
r = da->mtime < db->mtime ? -1 : 1;
}
if (_savegame_sort_order & SORT_DESCENDING) r = -r;
return r;
}
extern FiosItem *_fios_items;
extern int _fios_count, _fios_alloc;
static void append_path(char *out, const char *path, const char *file)
{
@ -320,15 +294,6 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
return _fios_items;
}
// Free the list of savegames
void FiosFreeSavegameList(void)
{
free(_fios_items);
_fios_items = NULL;
_fios_alloc = _fios_count = 0;
}
// Browse to
char *FiosBrowseTo(const FiosItem *item)
{
@ -397,33 +362,6 @@ StringID FiosGetDescText(const char **path, uint32 *tot)
return STR_4006_UNABLE_TO_READ_DRIVE;
}
void FiosMakeSavegameName(char *buf, const char *name, size_t size)
{
const char* extension;
const char* period;
extension = (_game_mode == GM_EDITOR ? ".scn" : ".sav");
// Don't append the extension, if it is already there
period = strrchr(name, '.');
if (period != NULL && strcasecmp(period, extension) == 0) extension = "";
snprintf(buf, size, "%s\\%s%s", _fios_path, name, extension);
}
bool FiosDelete(const char *name)
{
char path[512];
FiosMakeSavegameName(path, name, sizeof(path));
return unlink(path) == 0;
}
bool FileExists(const char *filename)
{
return access(filename, 0) == 0;
}
static void ChangeWorkingDirectory(char *exe)
{
char *s = strrchr(exe, '\\');

@ -48,36 +48,11 @@ ULONG __stack = (1024*1024)*2; // maybe not that much is needed actually ;)
#include <SDL.h>
#endif
#endif
static char *_fios_path;
extern char *_fios_path;
static char *_fios_save_path;
static char *_fios_scn_path;
static FiosItem *_fios_items;
static int _fios_count, _fios_alloc;
static FiosItem *FiosAlloc(void)
{
if (_fios_count == _fios_alloc) {
_fios_alloc += 256;
_fios_items = realloc(_fios_items, _fios_alloc * sizeof(FiosItem));
}
return &_fios_items[_fios_count++];
}
int compare_FiosItems(const void *a, const void *b)
{
const FiosItem *da = (const FiosItem *)a;
const FiosItem *db = (const FiosItem *)b;
int r;
if (_savegame_sort_order & SORT_BY_NAME) {
r = strcasecmp(da->title, db->title);
} else {
r = da->mtime < db->mtime ? -1 : 1;
}
if (_savegame_sort_order & SORT_DESCENDING) r = -r;
return r;
}
extern FiosItem *_fios_items;
extern int _fios_count, _fios_alloc;
#if !defined(__MORPHOS__) && !defined(__AMIGAOS__)
#define ISROOT(__p) (__p[1] == '\0')
@ -297,15 +272,6 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
return _fios_items;
}
// Free the list of savegames
void FiosFreeSavegameList(void)
{
free(_fios_items);
_fios_items = NULL;
_fios_alloc = _fios_count = 0;
}
// Browse to
char *FiosBrowseTo(const FiosItem *item)
{
@ -387,33 +353,6 @@ StringID FiosGetDescText(const char **path, uint32 *tot)
return STR_4005_BYTES_FREE;
}
void FiosMakeSavegameName(char *buf, const char *name, size_t size)
{
const char* extension;
const char* period;
extension = (_game_mode == GM_EDITOR ? ".scn" : ".sav");
// Don't append the extension, if it is already there
period = strrchr(name, '.');
if (period != NULL && strcasecmp(period, extension) == 0) extension = "";
snprintf(buf, size, "%s/%s%s", _fios_path, name, extension);
}
bool FiosDelete(const char *name)
{
char path[512];
FiosMakeSavegameName(path, name, sizeof(path));
return unlink(path) == 0;
}
bool FileExists(const char *filename)
{
return access(filename, 0) == 0;
}
#if defined(__BEOS__) || defined(__linux__)
static void ChangeWorkingDirectory(char *exe)
{

@ -707,20 +707,11 @@ int closedir(DIR *d)
return 0;
}
static char *_fios_path;
extern char *_fios_path;
static char *_fios_save_path;
static char *_fios_scn_path;
static FiosItem *_fios_items;
static int _fios_count, _fios_alloc;
static FiosItem *FiosAlloc(void)
{
if (_fios_count == _fios_alloc) {
_fios_alloc += 256;
_fios_items = realloc(_fios_items, _fios_alloc * sizeof(FiosItem));
}
return &_fios_items[_fios_count++];
}
extern FiosItem *_fios_items;
extern int _fios_count, _fios_alloc;
static HANDLE MyFindFirstFile(const char *path, const char *file, WIN32_FIND_DATA *fd)
{
@ -736,23 +727,6 @@ static HANDLE MyFindFirstFile(const char *path, const char *file, WIN32_FIND_DAT
return h;
}
int CDECL compare_FiosItems(const void *a, const void *b)
{
const FiosItem *da = (const FiosItem *)a;
const FiosItem *db = (const FiosItem *)b;
int r;
if (_savegame_sort_order & SORT_BY_NAME) {
r = strcasecmp(da->title, db->title);
} else {
r = da->mtime < db->mtime ? -1 : 1;
}
if (_savegame_sort_order & SORT_DESCENDING) r = -r;
return r;
}
// Get a list of savegames
FiosItem *FiosGetSavegameList(int *num, int mode)
{
@ -985,15 +959,6 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
return _fios_items;
}
// Free the list of savegames
void FiosFreeSavegameList(void)
{
free(_fios_items);
_fios_items = NULL;
_fios_alloc = _fios_count = 0;
}
// Browse to
char *FiosBrowseTo(const FiosItem *item)
{
@ -1067,36 +1032,6 @@ StringID FiosGetDescText(const char **path, uint32 *tot)
return sid;
}
void FiosMakeSavegameName(char *buf, const char *name, size_t size)
{
const char* extension;
const char* period;
extension = (_game_mode == GM_EDITOR ? ".scn" : ".sav");
// Don't append the extension, if it is already there
period = strrchr(name, '.');
if (period != NULL && strcasecmp(period, extension) == 0) extension = "";
snprintf(buf, size, "%s\\%s%s", _fios_path, name, extension);
}
bool FiosDelete(const char *name)
{
char path[512];
FiosMakeSavegameName(path, name, sizeof(path));
return DeleteFile(path) != 0;
}
bool FileExists(const char *filename)
{
HANDLE hand = CreateFile(filename, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hand == INVALID_HANDLE_VALUE) return false;
CloseHandle(hand);
return true;
}
static int ParseCommandLine(char *line, char **argv, int max_argc)
{
int n = 0;

Loading…
Cancel
Save