1
0
mirror of https://github.com/OpenTTD/OpenTTD.git synced 2025-08-31 02:19:09 +00:00

(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)

This commit is contained in:
tron
2005-03-11 14:14:54 +00:00
parent eab8b9a116
commit d755b7908e
3 changed files with 310 additions and 322 deletions

307
os2.c
View File

@@ -23,31 +23,39 @@
#include <SDL.h>
#endif
static inline int strcasecmp(const char* s1, const char* s2)
{
return stricmp(s1, s2);
}
static 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()
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 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 < 2) // sort by date
r = da->mtime < db->mtime ? -1 : 1;
r = da->mtime < db->mtime ? -1 : 1;
else
r = stricmp(da->title[0] ? da->title : da->name, db->title[0] ? db->title : db->name);
r = strcasecmp(
da->title[0] != '\0' ? da->title : da->name,
db->title[0] != '\0' ? db->title : db->name
);
if (_savegame_sort_order & 1) r = -r;
return r;
@@ -62,9 +70,9 @@ static DIR *my_opendir(char *path, char *file)
return opendir(paths);
}
static void append_path(char *out, char *path, char *file)
static void append_path(char *out, const char *path, const char *file)
{
if ((path[2] == '\\') && (path[3] == 0))
if (path[2] == '\\' && path[3] == '\0')
sprintf(out, "%s%s", path, file);
else
sprintf(out, "%s\\%s", path, file);
@@ -91,27 +99,27 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
_fios_path = _fios_save_path;
// Parent directory, only if not of the type C:\.
if (_fios_path[3] != 0) {
if (_fios_path[3] != '\0') {
fios = FiosAlloc();
fios->type = FIOS_TYPE_PARENT;
fios->mtime = 0;
strcpy(fios->title, ".. (Parent directory)");
}
// Show subdirectories first
dir = my_opendir(_fios_path, "*.*");
if (dir != NULL) {
while ((dirent = readdir(dir))) {
while ((dirent = readdir(dir)) != NULL) {
append_path(filename, _fios_path, dirent->d_name);
if (!stat(filename, &sb)) {
if (S_ISDIR(sb.st_mode)) {
if (!(dirent->d_name[0] == '.' && (dirent->d_name[1] == 0 || (dirent->d_name[1] == '.' && dirent->d_name[2] == 0))))
{
fios = FiosAlloc();
fios->type = FIOS_TYPE_DIR;
strcpy(fios->name, dirent->d_name);
sprintf(fios->title, "%s\\ (Directory)", dirent->d_name);
}
}
if (!stat(filename, &sb) && S_ISDIR(sb.st_mode) &&
strcmp(fd.cFileName, ".") != 0 &&
strcmp(fd.cFileName, "..") != 0) {
fios = FiosAlloc();
fios->type = FIOS_TYPE_DIR;
fios->mtime = 0;
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name));
snprintf(fios->title, lengthof(fios->title),
"%s\\ (Directory)", dirent->d_name);
}
}
closedir(dir);
@@ -128,38 +136,38 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
// this is where to start sorting
sort_start = _fios_count;
/* Show savegame files
* .SAV OpenTTD saved game
* .SS1 Transport Tycoon Deluxe preset game
* .SV1 Transport Tycoon Deluxe (Patch) saved game
* .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game
/* Show savegame files
* .SAV OpenTTD saved game
* .SS1 Transport Tycoon Deluxe preset game
* .SV1 Transport Tycoon Deluxe (Patch) saved game
* .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game
*/
dir = my_opendir(_fios_path, "*.*");
if (dir != NULL) {
while ((dirent = readdir(dir))) {
while ((dirent = readdir(dir)) != NULL) {
char *t;
append_path(filename, _fios_path, dirent->d_name);
if (!stat(filename, &sb)) {
if (!S_ISDIR(sb.st_mode)) {
char *t = strrchr(dirent->d_name, '.');
if (t && !stricmp(t, ".sav")) { // OpenTTD
*t = 0; // cut extension
fios = FiosAlloc();
fios->type = FIOS_TYPE_FILE;
fios->mtime = sb.st_mtime;
fios->title[0] = 0;
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name));
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) {
int ext = 0; // start of savegame extensions in _old_extensions[]
if (t && ((ext++, !stricmp(t, ".ss1")) || (ext++, !stricmp(t, ".sv1")) || (ext++, !stricmp(t, ".sv2"))) ) { // TTDLX(Patch)
*t = 0; // cut extension
fios = FiosAlloc();
fios->old_extension = ext-1;
fios->type = FIOS_TYPE_OLDFILE;
fios->mtime = sb.st_mtime;
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name));
GetOldSaveGameName(fios->title, filename);
}
}
if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) continue;
t = strrchr(dirent->d_name, '.');
if (t != NULL && strcasecmp(t, ".sav") == 0) { // OpenTTD
fios = FiosAlloc();
fios->type = FIOS_TYPE_FILE;
fios->mtime = sb.st_mtime;
fios->title[0] = '\0';
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name));
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) {
if (t != NULL && (
strcasecmp(t, ".ss1") == 0 ||
strcasecmp(t, ".sv1") == 0 ||
strcasecmp(t, ".sv2") == 0
)) { // TTDLX(Patch)
fios = FiosAlloc();
fios->type = FIOS_TYPE_OLDFILE;
fios->mtime = sb.st_mtime;
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name));
GetOldSaveGameName(fios->title, filename);
}
}
}
@@ -170,15 +178,18 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
// Drives
{
unsigned save, disk, disk2, total;
uint save;
uint disk;
uint total;
/* save original drive */
_dos_getdrive(&save);
/* get available drive letters */
for (disk = 1; disk < 27; ++disk)
{
uint disk2;
_dos_setdrive(disk, &total);
_dos_getdrive(&disk2);
@@ -186,9 +197,7 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
{
fios = FiosAlloc();
fios->type = FIOS_TYPE_DRIVE;
fios->title[0] = disk + 'A'-1;
fios->title[1] = ':';
fios->title[2] = 0;
sprintf(fios->title, "%c:", 'A' + disk - 1);
}
}
@@ -218,28 +227,28 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
_fios_path = _fios_scn_path;
// Parent directory, only if not of the type C:\.
if (_fios_path[3] != 0 && mode != SLD_NEW_GAME) {
if (_fios_path[3] != '\0' && mode != SLD_NEW_GAME) {
fios = FiosAlloc();
fios->type = FIOS_TYPE_PARENT;
fios->mtime = 0;
strcpy(fios->title, ".. (Parent directory)");
}
// Show subdirectories first
dir = my_opendir(_fios_path, "*.*");
if (dir != NULL) {
while ((dirent = readdir(dir))) {
while ((dirent = readdir(dir)) != NULL) {
append_path(filename, _fios_path, dirent->d_name);
if (!stat(filename, &sb)) {
if (S_ISDIR(sb.st_mode)) {
if (!(dirent->d_name[0] == '.' && (dirent->d_name[1] == 0 || (dirent->d_name[1] == '.' && dirent->d_name[2] == 0))))
{
fios = FiosAlloc();
fios->mtime = 0;
fios->type = FIOS_TYPE_DIR;
strcpy(fios->name, dirent->d_name);
sprintf(fios->title, "%s\\ (Directory)", dirent->d_name);
}
}
if (!stat(filename, &sb) && (S_ISDIR(sb.st_mode)
strcmp(dirent->d_name, ".") != 0 &&
strcmp(dirent->d_name, "..") != 0 &&
) {
fios = FiosAlloc();
fios->type = FIOS_TYPE_DIR;
fios->mtime = 0;
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name));
snprintf(fios->title, lengthof(fios->title),
"%s\\ (Directory)", dirent->d_name);
}
}
closedir(dir);
@@ -248,37 +257,37 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
// this is where to start sorting
sort_start = _fios_count;
/* Show scenario files
* .SCN OpenTTD style scenario file
* .SV0 Transport Tycoon Deluxe (Patch) scenario
* .SS0 Transport Tycoon Deluxe preset scenario
/* Show scenario files
* .SCN OpenTTD style scenario file
* .SV0 Transport Tycoon Deluxe (Patch) scenario
* .SS0 Transport Tycoon Deluxe preset scenario
*/
dir = my_opendir(_fios_path, "*.*");
if (dir != NULL) {
while ((dirent = readdir(dir))) {
while ((dirent = readdir(dir)) != NULL) {
char *t;
append_path(filename, _fios_path, dirent->d_name);
if (!stat(filename, &sb)) {
if (!S_ISDIR(sb.st_mode)) {
char *t = strrchr(dirent->d_name, '.');
if (t && !stricmp(t, ".scn")) { // OpenTTD
*t = 0; // cut extension
fios = FiosAlloc();
fios->type = FIOS_TYPE_SCENARIO;
fios->mtime = sb.st_mtime;
fios->title[0] = 0;
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3);
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || mode == SLD_NEW_GAME) {
int ext = 3; // start of scenario extensions in _old_extensions[]
if (t && ((ext++, !stricmp(t, ".sv0")) || (ext++, !stricmp(t, ".ss0"))) ) { // TTDLX(Patch)
*t = 0; // cut extension
fios = FiosAlloc();
fios->old_extension = ext-1;
fios->type = FIOS_TYPE_OLD_SCENARIO;
fios->mtime = sb.st_mtime;
GetOldScenarioGameName(fios->title, filename);
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3);
}
}
if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) continue;
t = strrchr(dirent->d_name, '.');
if (t != NULL && strcasecmp(t, ".scn") == 0) { // OpenTTD
fios = FiosAlloc();
fios->type = FIOS_TYPE_SCENARIO;
fios->mtime = sb.st_mtime;
fios->title[0] = '\0';
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name));
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO ||
mode == SLD_NEW_GAME) {
if (t != NULL && (
strcasecmp(t, ".sv0") == 0 ||
strcasecmp(t, ".ss0") == 0
)) { // TTDLX(Patch)
fios = FiosAlloc();
fios->type = FIOS_TYPE_OLD_SCENARIO;
fios->mtime = sb.st_mtime;
GetOldScenarioGameName(fios->title, filename);
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name));
}
}
}
@@ -321,7 +330,7 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
// Free the list of savegames
void FiosFreeSavegameList()
void FiosFreeSavegameList(void)
{
free(_fios_items);
_fios_items = NULL;
@@ -331,50 +340,35 @@ void FiosFreeSavegameList()
// Browse to
char *FiosBrowseTo(const FiosItem *item)
{
static char str_buffr[512];
char *path = _fios_path;
char *s;
switch(item->type) {
case FIOS_TYPE_DRIVE:
sprintf(path, "%c:\\", item->title[0]);
break;
switch (item->type) {
case FIOS_TYPE_DRIVE:
sprintf(path, "%c:\\", item->title[0]);
break;
case FIOS_TYPE_PARENT:
// Skip drive part
path += 3;
s = path;
while (*path) {
if (*path== '\\')
s = path;
path++;
case FIOS_TYPE_PARENT:
s = strrchr(path, '\\');
if (s != NULL) *s = '\0';
break;
case FIOS_TYPE_DIR:
s = strchr(item->name, '\\');
if (s != NULL) *s = '\0';
strcat(path, "\\");
strcat(path, item->name);
break;
case FIOS_TYPE_FILE:
case FIOS_TYPE_OLDFILE:
case FIOS_TYPE_SCENARIO:
case FIOS_TYPE_OLD_SCENARIO: {
static char str_buffr[512];
sprintf(str_buffr, "%s\\%s", path, item->name);
return str_buffr;
}
*s = 0;
break;
case FIOS_TYPE_DIR:
// Scan to end
while (*++path);
// Add backslash?
if (path[-1] != '\\') *path++ = '\\';
strcpy(path, item->name);
break;
case FIOS_TYPE_FILE:
FiosMakeSavegameName(str_buffr, item->name);
return str_buffr;
case FIOS_TYPE_OLDFILE:
sprintf(str_buffr, "%s\\%s.%s", _fios_path, item->name, _old_extensions[item->old_extension]);
return str_buffr;
case FIOS_TYPE_SCENARIO:
sprintf(str_buffr, "%s\\%s.scn", path, item->name);
return str_buffr;
case FIOS_TYPE_OLD_SCENARIO:
sprintf(str_buffr, "%s\\%s.%s", path, item->name, _old_extensions[item->old_extension]);
return str_buffr;
}
return NULL;
@@ -389,7 +383,7 @@ StringID FiosGetDescText(const char **path)
char drive;
*path = _fios_path;
drive = *path[0] - 'A'+1;
drive = *path[0] - 'A' + 1;
_getdiskfree(drive, &free);
@@ -399,7 +393,7 @@ StringID FiosGetDescText(const char **path)
void FiosMakeSavegameName(char *buf, const char *name)
{
if(_game_mode==GM_EDITOR)
if (_game_mode == GM_EDITOR)
sprintf(buf, "%s\\%s.scn", _fios_path, name);
else
sprintf(buf, "%s\\%s.sav", _fios_path, name);
@@ -438,7 +432,7 @@ const DriverDesc _music_driver_descs[] = {
/* GetOSVersion returns the minimal required version of OS to be able to use that driver.
Not needed for OS/2. */
byte GetOSVersion()
byte GetOSVersion(void)
{
return 2; // any arbitrary number bigger then 0
}
@@ -479,7 +473,7 @@ static void ChangeWorkingDirectory(char *exe)
{
char *s = strrchr(exe, '\\');
if (s != NULL) {
*s = 0;
*s = '\0';
chdir(exe);
*s = '\\';
}
@@ -534,21 +528,20 @@ void DeterminePaths(void)
{
char *s;
_path.game_data_dir = malloc( MAX_PATH );
_path.game_data_dir = malloc(MAX_PATH);
ttd_strlcpy(_path.game_data_dir, GAME_DATA_DIR, MAX_PATH);
#if defined SECOND_DATA_DIR
_path.second_data_dir = malloc( MAX_PATH );
ttd_strlcpy( _path.second_data_dir, SECOND_DATA_DIR, MAX_PATH);
_path.second_data_dir = malloc(MAX_PATH);
ttd_strlcpy(_path.second_data_dir, SECOND_DATA_DIR, MAX_PATH);
#endif
#if defined(USE_HOMEDIR)
{
char *homedir;
homedir = getenv("HOME");
const char *homedir = getenv("HOME");
if(!homedir) {
struct passwd *pw = getpwuid(getuid());
if (pw) homedir = pw->pw_dir;
if (homedir == NULL) {
const struct passwd *pw = getpwuid(getuid());
if (pw != NULL) homedir = pw->pw_dir;
}
_path.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR);
@@ -556,14 +549,14 @@ void DeterminePaths(void)
#else /* not defined(USE_HOMEDIR) */
_path.personal_dir = malloc( MAX_PATH );
_path.personal_dir = malloc(MAX_PATH);
ttd_strlcpy(_path.personal_dir, PERSONAL_DIR, MAX_PATH);
// check if absolute or relative path
s = strchr(_path.personal_dir, '\\');
// add absolute path
if (s==NULL || _path.personal_dir != s) {
if (s == NULL || _path.personal_dir != s) {
getcwd(_path.personal_dir, MAX_PATH);
s = strchr(_path.personal_dir, 0);
*s++ = '\\';
@@ -575,7 +568,7 @@ void DeterminePaths(void)
s = strchr(_path.personal_dir, 0);
// append a / ?
if (s[-1] != '\\') { s[0] = '\\'; s[1] = 0; }
if (s[-1] != '\\') strcpy(s, "\\");
_path.save_dir = str_fmt("%ssave", _path.personal_dir);
_path.autosave_dir = str_fmt("%s\\autosave", _path.save_dir);
@@ -586,6 +579,9 @@ void DeterminePaths(void)
if (_config_file == NULL)
_config_file = str_fmt("%sopenttd.cfg", _path.personal_dir);
_highscore_file = str_fmt("%shs.dat", _path.personal_dir);
_log_file = str_fmt("%sopenttd.log", _path.personal_dir);
#if defined CUSTOM_LANG_DIR
// sets the search path for lng files to the custom one
_path.lang_dir = malloc( MAX_PATH );
@@ -606,7 +602,7 @@ void DeterminePaths(void)
// Switches OpenTTD to a console app at run-time, instead of a PM app
// Necessary to see stdout, etc
void OS2_SwitchToConsoleMode()
void OS2_SwitchToConsoleMode(void)
{
PPIB pib;
PTIB tib;
@@ -645,7 +641,7 @@ static void OS2MidiPlaySong(const char *filename)
MidiSendCommand("play song from 0");
}
static void OS2MidiStopSong()
static void OS2MidiStopSong(void)
{
MidiSendCommand("close all");
}
@@ -655,7 +651,7 @@ static void OS2MidiSetVolume(byte vol)
MidiSendCommand("set song audio volume %d", ((vol/127)*100));
}
static bool OS2MidiIsSongPlaying()
static bool OS2MidiIsSongPlaying(void)
{
char buf[16];
mciSendString("status song mode", buf, sizeof(buf), NULL, 0);
@@ -667,7 +663,7 @@ static const char *OS2MidiStart(const char * const *parm)
return 0;
}
static void OS2MidiStop()
static void OS2MidiStop(void)
{
MidiSendCommand("close all");
}
@@ -681,4 +677,7 @@ const HalMusicDriver _os2_music_driver = {
OS2MidiSetVolume,
};
bool InsertTextBufferClipboard(Textbuf *tb) {return false;}
bool InsertTextBufferClipboard(Textbuf *tb)
{
return false;
}