(360/PS3) Attempt to use the same file browsing code for 360 and

PS3
This commit is contained in:
TwinAphex51224 2012-03-29 16:09:43 +02:00
parent db7be5736b
commit 765ba65188
3 changed files with 108 additions and 29 deletions

View File

@ -112,10 +112,8 @@
/*============================================================
FILE
============================================================ */
#if defined(__CELLOS_LV2__)
#if defined(__CELLOS_LV2__) || defined(_XBOX)
#include "../../ps3/file_browser.c"
#elif defined(_XBOX)
#include "../../360/file_browser.c"
#endif
#include "../../file.c"

View File

@ -16,11 +16,14 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#ifdef _XBOX
#include <xtl.h>
#endif
#include "file_browser.h"
static int less_than_key(const void * a, const void * b)
{
#ifdef __CELLOS_LV2__
DirectoryEntry * a_dir = (DirectoryEntry*)a;
DirectoryEntry * b_dir = (DirectoryEntry*)b;
@ -31,6 +34,9 @@ static int less_than_key(const void * a, const void * b)
return 1;
return strcasecmp(a_dir->d_name, b_dir->d_name);
#else
return 0;
#endif
}
static const char * filebrowser_get_extension(const char * filename)
@ -45,7 +51,7 @@ static const char * filebrowser_get_extension(const char * filename)
static void filebrowser_clear_current_entries(filebrowser_t * filebrowser)
{
for(uint32_t i = 0; i < MAX_FILE_LIMIT_CFS; i++)
for(uint32_t i = 0; i < MAX_FILE_LIMIT; i++)
{
filebrowser->cur[filebrowser->file_count].d_type = 0;
filebrowser->cur[filebrowser->file_count].d_namlen = 0;
@ -53,14 +59,81 @@ static void filebrowser_clear_current_entries(filebrowser_t * filebrowser)
}
}
static bool filebrowser_parse_directory(filebrowser_t * filebrowser,
static void filebrowser_parse_directory(filebrowser_t * filebrowser,
const char * path, const char * extensions)
{
int error = 0;
#if defined(_XBOX)
filebrowser->file_count = 0;
WIN32_FIND_DATA ffd;
HANDLE hFind = INVALID_HANDLE_VALUE;
char path_buf[PATH_MAX];
if (strlcpy(path_buf, path, sizeof(path_buf)) >= sizeof(path_buf))
{
error = 1;
goto error;
}
if (strlcat(path_buf, "\\*", sizeof(path_buf)) >= sizeof(path_buf))
{
error = 1;
goto error;
}
hFind = FindFirstFile(path_buf, &ffd);
if (hFind == INVALID_HANDLE_VALUE)
{
error = 1;
goto error;
}
do
{
strcpy(filebrowser->dir[filebrowser->directory_stack_size], path);
bool found_dir = false;
if(!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
char tmp_extensions[512];
strncpy(tmp_extensions, extensions, sizeof(tmp_extensions));
const char * current_extension = filebrowser_get_extension(ffd.cFileName);
bool found_rom = false;
if(current_extension)
{
char * pch = strtok(tmp_extensions, "|");
while (pch != NULL)
{
if(strcmp(current_extension, pch) == 0)
{
found_rom = true;
break;
}
pch = strtok(NULL, "|");
}
}
if(!found_rom)
continue;
}
else if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
found_dir = true;
filebrowser->cur[filebrowser->file_count].d_type = found_dir ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL;
sprintf(filebrowser->cur[filebrowser->file_count].d_name, ffd.cFileName);
filebrowser->file_count++;
}while (FindNextFile(hFind, &ffd) != 0 && (filebrowser->file_count + 1) < MAX_FILE_LIMIT);
#elif defined(__CELLOS_LV2__)
int fd;
/* bad path*/
if (strcmp(path,"") == 0)
return false;
{
error = 1;
goto error;
}
/* delete old path*/
filebrowser_clear_current_entries(filebrowser);
@ -122,18 +195,29 @@ const char * path, const char * extensions)
cellFsClosedir(fd);
}
else
return false;
{
error = 1;
goto error;
}
qsort(filebrowser->cur, filebrowser->file_count, sizeof(DirectoryEntry), less_than_key);
return true;
#endif
error:
if(error)
{
SSNES_ERR("Failed to open directory: \"%s\"\n", path);
}
#ifdef _XBOX
FindClose(hFind);
#endif
}
void filebrowser_new(filebrowser_t * filebrowser, const char * start_dir,
const char * extensions)
{
filebrowser_clear_current_entries(filebrowser);
filebrowser->directory_stack_size = 0;
strncpy(filebrowser->extensions, extensions, sizeof(filebrowser->extensions));
strlcpy(filebrowser->extensions, extensions, sizeof(filebrowser->extensions));
filebrowser_parse_directory(filebrowser, start_dir, filebrowser->extensions);
}
@ -142,9 +226,8 @@ const char * extensions)
void filebrowser_reset_start_directory(filebrowser_t * filebrowser, const char * start_dir,
const char * extensions)
{
filebrowser_clear_current_entries(filebrowser);
filebrowser->directory_stack_size = 0;
strncpy(filebrowser->extensions, extensions, sizeof(filebrowser->extensions));
strlcpy(filebrowser->extensions, extensions, sizeof(filebrowser->extensions));
filebrowser_parse_directory(filebrowser, start_dir, filebrowser->extensions);
}
@ -153,7 +236,6 @@ void filebrowser_push_directory(filebrowser_t * filebrowser, const char * path,
bool with_extension)
{
filebrowser->directory_stack_size++;
if(with_extension)
filebrowser_parse_directory(filebrowser, path, filebrowser->extensions);
else

View File

@ -41,24 +41,27 @@
#define MAXJOLIET 255
#define MAX_PATH_LENGTH 1024
#define CELL_FS_MAX_FS_FILE_NAME_LENGTH (255)
#define MAX_FILE_LIMIT_CFS 30000
#include <stdint.h>
#include <stdlib.h>
#ifdef __CELLOS_LV2__
#include <stdbool.h>
#include <cell/cell_fs.h>
#include <string.h>
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#define FS_MAX_PATH 256
#define FS_MAX_FS_PATH_LENGTH 255
#define MAX_FILE_LIMIT 30000
#elif defined(_XBOX)
#define FS_MAX_PATH MAX_PATH
#define FS_MAX_FS_PATH_LENGTH 2048
#define MAX_FILE_LIMIT 4096
#endif
typedef struct {
uint8_t d_type;
uint8_t d_namlen;
char d_name[CELL_FS_MAX_FS_FILE_NAME_LENGTH+1];
char d_name[FS_MAX_PATH];
} DirectoryEntry;
typedef struct
@ -66,8 +69,8 @@ typedef struct
uint32_t file_count; /* amount of files in current dir*/
uint32_t currently_selected; /* currently select browser entry*/
uint32_t directory_stack_size;
char dir[128][CELL_FS_MAX_FS_PATH_LENGTH]; /* info of the current directory*/
DirectoryEntry cur[MAX_FILE_LIMIT_CFS]; /* current file listing*/
char dir[128][FS_MAX_FS_PATH_LENGTH]; /* info of the current directory*/
DirectoryEntry cur[MAX_FILE_LIMIT]; /* current file listing*/
char extensions[512]; /* allowed extensions*/
} filebrowser_t;
@ -113,8 +116,4 @@ void filebrowser_pop_directory (filebrowser_t * filebrowser);
#define FILEBROWSER_IS_CURRENT_A_FILE(filebrowser) (filebrowser.cur[filebrowser.currently_selected].d_type == CELL_FS_TYPE_REGULAR)
#define FILEBROWSER_IS_CURRENT_A_DIRECTORY(filebrowser) (filebrowser.cur[filebrowser.currently_selected].d_type == CELL_FS_TYPE_DIRECTORY)
#ifdef __cplusplus
}
#endif
#endif /* FILEBROWSER_H_ */