src/fios.cpp
branchcpp_gui
changeset 6268 4b5241e5dd10
parent 6008 1ed2f76ac388
child 6285 187e3ef04cc9
equal deleted inserted replaced
6267:7c8ec33959b1 6268:4b5241e5dd10
     1 /* $Id$ */
     1 /* $Id$ */
     2 
     2 
     3 /** @file fios.c
     3 /** @file fios.cpp
     4  * This file contains functions for building file lists for the save/load dialogs.
     4  * This file contains functions for building file lists for the save/load dialogs.
     5  */
     5  */
     6 
     6 
     7 #include "stdafx.h"
     7 #include "stdafx.h"
     8 #include "openttd.h"
     8 #include "openttd.h"
    19 
    19 
    20 #ifdef WIN32
    20 #ifdef WIN32
    21 # include <io.h>
    21 # include <io.h>
    22 #else
    22 #else
    23 # include <unistd.h>
    23 # include <unistd.h>
    24 # include <dirent.h>
       
    25 #endif /* WIN32 */
    24 #endif /* WIN32 */
    26 
    25 
    27 /* Variables to display file lists */
    26 /* Variables to display file lists */
    28 int _fios_num;
    27 int _fios_num;
    29 
    28 
   185 
   184 
   186 bool FileExists(const char *filename)
   185 bool FileExists(const char *filename)
   187 {
   186 {
   188 #if defined(WINCE)
   187 #if defined(WINCE)
   189 	/* There is always one platform that doesn't support basic commands... */
   188 	/* There is always one platform that doesn't support basic commands... */
   190 	HANDLE hand;
   189 	HANDLE hand = CreateFile(OTTD2FS(filename), 0, 0, NULL, OPEN_EXISTING, 0, NULL);
   191 
       
   192 	hand = CreateFile(OTTD2FS(filename), 0, 0, NULL, OPEN_EXISTING, 0, NULL);
       
   193 	if (hand == INVALID_HANDLE_VALUE) return 1;
   190 	if (hand == INVALID_HANDLE_VALUE) return 1;
   194 	CloseHandle(hand);
   191 	CloseHandle(hand);
   195 	return 0;
   192 	return 0;
   196 #else
   193 #else
   197 	return access(filename, 0) == 0;
   194 	return access(filename, 0) == 0;
   210 	struct stat sb;
   207 	struct stat sb;
   211 	struct dirent *dirent;
   208 	struct dirent *dirent;
   212 	DIR *dir;
   209 	DIR *dir;
   213 	FiosItem *fios;
   210 	FiosItem *fios;
   214 	int sort_start;
   211 	int sort_start;
       
   212 	char d_name[sizeof(fios->name)];
   215 
   213 
   216 	/* A parent directory link exists if we are not in the root directory */
   214 	/* A parent directory link exists if we are not in the root directory */
   217 	if (!FiosIsRoot(_fios_path) && mode != SLD_NEW_GAME) {
   215 	if (!FiosIsRoot(_fios_path) && mode != SLD_NEW_GAME) {
   218 		fios = FiosAlloc();
   216 		fios = FiosAlloc();
   219 		fios->type = FIOS_TYPE_PARENT;
   217 		fios->type = FIOS_TYPE_PARENT;
   221 		ttd_strlcpy(fios->name, "..", lengthof(fios->name));
   219 		ttd_strlcpy(fios->name, "..", lengthof(fios->name));
   222 		ttd_strlcpy(fios->title, ".. (Parent directory)", lengthof(fios->title));
   220 		ttd_strlcpy(fios->title, ".. (Parent directory)", lengthof(fios->title));
   223 	}
   221 	}
   224 
   222 
   225 	/* Show subdirectories */
   223 	/* Show subdirectories */
   226 	if (mode != SLD_NEW_GAME && (dir = opendir(_fios_path)) != NULL) {
   224 	if (mode != SLD_NEW_GAME && (dir = ttd_opendir(_fios_path)) != NULL) {
   227 		while ((dirent = readdir(dir)) != NULL) {
   225 		while ((dirent = readdir(dir)) != NULL) {
   228 			const char *d_name = FS2OTTD(dirent->d_name);
   226 			ttd_strlcpy(d_name, FS2OTTD(dirent->d_name), sizeof(d_name));
   229 
   227 
   230 			/* found file must be directory, but not '.' or '..' */
   228 			/* found file must be directory, but not '.' or '..' */
   231 			if (FiosIsValidFile(_fios_path, dirent, &sb) && (sb.st_mode & S_IFDIR) &&
   229 			if (FiosIsValidFile(_fios_path, dirent, &sb) && (sb.st_mode & S_IFDIR) &&
   232 				strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
   230 				strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
   233 				fios = FiosAlloc();
   231 				fios = FiosAlloc();
   251 
   249 
   252 	/* This is where to start sorting for the filenames */
   250 	/* This is where to start sorting for the filenames */
   253 	sort_start = _fios_count;
   251 	sort_start = _fios_count;
   254 
   252 
   255 	/* Show files */
   253 	/* Show files */
   256 	dir = opendir(_fios_path);
   254 	dir = ttd_opendir(_fios_path);
   257 	if (dir != NULL) {
   255 	if (dir != NULL) {
   258 		while ((dirent = readdir(dir)) != NULL) {
   256 		while ((dirent = readdir(dir)) != NULL) {
   259 			char fios_title[64];
   257 			char fios_title[64];
   260 			char *t;
   258 			char *t;
   261 			char *d_name = (char*)FS2OTTD(dirent->d_name);
   259 			ttd_strlcpy(d_name, FS2OTTD(dirent->d_name), sizeof(d_name));
   262 			byte type;
       
   263 
   260 
   264 			if (!FiosIsValidFile(_fios_path, dirent, &sb) || !(sb.st_mode & S_IFREG)) continue;
   261 			if (!FiosIsValidFile(_fios_path, dirent, &sb) || !(sb.st_mode & S_IFREG)) continue;
   265 
   262 
   266 			/* File has no extension, skip it */
   263 			/* File has no extension, skip it */
   267 			if ((t = strrchr(d_name, '.')) == NULL) continue;
   264 			if ((t = strrchr(d_name, '.')) == NULL) continue;
   268 			fios_title[0] = '\0'; // reset the title;
   265 			fios_title[0] = '\0'; // reset the title;
   269 
   266 
   270 			type = callback_proc(mode, d_name, t, fios_title);
   267 			byte type = callback_proc(mode, d_name, t, fios_title);
   271 			if (type != FIOS_TYPE_INVALID) {
   268 			if (type != FIOS_TYPE_INVALID) {
   272 				fios = FiosAlloc();
   269 				fios = FiosAlloc();
   273 				fios->mtime = sb.st_mtime;
   270 				fios->mtime = sb.st_mtime;
   274 				fios->type = type;
   271 				fios->type = type;
   275 				ttd_strlcpy(fios->name, d_name, lengthof(fios->name));
   272 				ttd_strlcpy(fios->name, d_name, lengthof(fios->name));