Compact known file extension storage

This commit is contained in:
Kp 2013-11-26 22:19:52 +00:00
parent 4dfceab50b
commit cb4fd7c1b4
8 changed files with 30 additions and 26 deletions

View file

@ -1,9 +1,11 @@
#ifndef __JUKEBOX_H__ #ifndef __JUKEBOX_H__
#define __JUKEBOX_H__ #define __JUKEBOX_H__
#include "physfsx.h"
#ifdef __cplusplus #ifdef __cplusplus
extern const char *const jukebox_exts[7]; extern const file_extension_t jukebox_exts[7];
void jukebox_unload(); void jukebox_unload();
void jukebox_load(); void jukebox_load();

View file

@ -327,7 +327,7 @@ static inline void PHYSFSX_readMatrix(vms_matrix *m,PHYSFS_file *file)
#define PHYSFSX_contfile_init PHYSFSX_addRelToSearchPath #define PHYSFSX_contfile_init PHYSFSX_addRelToSearchPath
#define PHYSFSX_contfile_close PHYSFSX_removeRelFromSearchPath #define PHYSFSX_contfile_close PHYSFSX_removeRelFromSearchPath
typedef const char *file_extension_t; typedef char file_extension_t[5];
int PHYSFSX_checkMatchingExtension(const file_extension_t *exts, const char *filename) __attribute_nonnull(); int PHYSFSX_checkMatchingExtension(const file_extension_t *exts, const char *filename) __attribute_nonnull();
extern int PHYSFSX_addRelToSearchPath(const char *relname, int add_to_end); extern int PHYSFSX_addRelToSearchPath(const char *relname, int add_to_end);
extern int PHYSFSX_removeRelFromSearchPath(const char *relname); extern int PHYSFSX_removeRelFromSearchPath(const char *relname);

View file

@ -35,7 +35,9 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
#define ND_STATE_ONEFRAMEBACKWARD 7 #define ND_STATE_ONEFRAMEBACKWARD 7
#define DEMO_DIR "demos/" #define DEMO_DIR "demos/"
#define DEMO_EXT ".dem" #define DEMO_EXT "dem"
extern const file_extension_t demo_file_extensions[2];
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
#define DEMO_BACKUP_EXT "386" #define DEMO_BACKUP_EXT "386"
#else #else

View file

@ -20,12 +20,12 @@ typedef struct bim_song_info {
#define SONG_CREDITS 4 #define SONG_CREDITS 4
#define SONG_FIRST_LEVEL_SONG 5 #define SONG_FIRST_LEVEL_SONG 5
#define SONG_EXT_HMP ".hmp" #define SONG_EXT_HMP "hmp"
#ifdef USE_SDLMIXER #ifdef USE_SDLMIXER
#define SONG_EXT_MID ".mid" #define SONG_EXT_MID "mid"
#define SONG_EXT_OGG ".ogg" #define SONG_EXT_OGG "ogg"
#define SONG_EXT_FLAC ".flac" #define SONG_EXT_FLAC "flac"
#define SONG_EXT_MP3 ".mp3" #define SONG_EXT_MP3 "mp3"
#endif #endif
int songs_play_file(const char *filename, int repeat, void (*hook_finished_track)()); int songs_play_file(const char *filename, int repeat, void (*hook_finished_track)());

View file

@ -49,7 +49,7 @@ void jukebox_unload()
JukeboxSongs.num_songs = JukeboxSongs.max_songs = JukeboxSongs.max_buf = 0; JukeboxSongs.num_songs = JukeboxSongs.max_songs = JukeboxSongs.max_buf = 0;
} }
const char *const jukebox_exts[7] = { SONG_EXT_HMP, SONG_EXT_MID, SONG_EXT_OGG, SONG_EXT_FLAC, SONG_EXT_MP3, NULL }; const file_extension_t jukebox_exts[7] = { SONG_EXT_HMP, SONG_EXT_MID, SONG_EXT_OGG, SONG_EXT_FLAC, SONG_EXT_MP3, "" };
static int read_m3u(void) static int read_m3u(void)
{ {

View file

@ -130,6 +130,8 @@ static void do_multi_player_menu();
static void do_sandbox_menu(); static void do_sandbox_menu();
#endif #endif
static int select_file_recursive(const char *title, const char *orig_path, const file_extension_t *ext_list, int select_dir, int (*when_selected)(void *userdata, const char *filename), void *userdata) __attribute_nonnull();
// Hide all menus // Hide all menus
int hide_menus(void) int hide_menus(void)
{ {
@ -315,7 +317,7 @@ int RegisterPlayer()
const char **m; const char **m;
char **f; char **f;
char **list; char **list;
static const char *const types[] = { ".plr", NULL }; static const file_extension_t types[] = { "plr", "" };
int i = 0, NumItems; int i = 0, NumItems;
int citem = 0; int citem = 0;
int allow_abort_flag = 1; int allow_abort_flag = 1;
@ -738,10 +740,9 @@ static int demo_menu_handler( listbox *lb, d_event *event, void *userdata )
int select_demo(void) int select_demo(void)
{ {
char **list; char **list;
static const char *const types[] = { DEMO_EXT, NULL };
int NumItems; int NumItems;
list = PHYSFSX_findFiles(DEMO_DIR, types); list = PHYSFSX_findFiles(DEMO_DIR, demo_file_extensions);
if (!list) if (!list)
return 0; // memory error return 0; // memory error
if ( !*list ) if ( !*list )
@ -1318,7 +1319,7 @@ typedef struct browser
void *userdata; // Whatever you want passed to when_selected void *userdata; // Whatever you want passed to when_selected
char **list; // All menu items char **list; // All menu items
char *list_buf; // Buffer for menu item text: hopefully reduces memory fragmentation this way char *list_buf; // Buffer for menu item text: hopefully reduces memory fragmentation this way
const char *const *ext_list; // List of file extensions we're looking for (if looking for a music file many types are possible) const file_extension_t *ext_list; // List of file extensions we're looking for (if looking for a music file many types are possible)
int select_dir; // Allow selecting the current directory (e.g. for Jukebox level song directory) int select_dir; // Allow selecting the current directory (e.g. for Jukebox level song directory)
int num_files; // Number of list items found (including parent directory and current directory if selectable) int num_files; // Number of list items found (including parent directory and current directory if selectable)
int max_files; // How many entries we can have before having to grow the array int max_files; // How many entries we can have before having to grow the array
@ -1363,8 +1364,6 @@ static int list_directory(browser *b)
return 1; return 1;
} }
static int select_file_recursive(const char *title, const char *orig_path, const char *const *ext_list, int select_dir, int (*when_selected)(void *userdata, const char *filename), void *userdata);
static int select_file_handler(listbox *menu, d_event *event, browser *b) static int select_file_handler(listbox *menu, d_event *event, browser *b)
{ {
char newpath[PATH_MAX]; char newpath[PATH_MAX];
@ -1480,7 +1479,7 @@ static int select_file_handler(listbox *menu, d_event *event, browser *b)
return 0; return 0;
} }
static int select_file_recursive(const char *title, const char *orig_path, const char *const *ext_list, int select_dir, int (*when_selected)(void *userdata, const char *filename), void *userdata) static int select_file_recursive(const char *title, const char *orig_path, const file_extension_t *ext_list, int select_dir, int (*when_selected)(void *userdata, const char *filename), void *userdata)
{ {
browser *b; browser *b;
const char *sep = PHYSFS_getDirSeparator(); const char *sep = PHYSFS_getDirSeparator();
@ -1681,7 +1680,7 @@ static int sound_menuset(newmenu *menu, d_event *event, void *userdata)
#ifdef USE_SDLMIXER #ifdef USE_SDLMIXER
if (citem == opt_sm_mtype3_lmpath) if (citem == opt_sm_mtype3_lmpath)
{ {
static const char *const ext_list[] = { ".m3u", NULL }; // select a directory or M3U playlist static const file_extension_t ext_list[] = { "m3u", "" }; // select a directory or M3U playlist
select_file_recursive( select_file_recursive(
#ifndef _WIN32 #ifndef _WIN32
"Select directory or\nM3U playlist to\n play level music from", "Select directory or\nM3U playlist to\n play level music from",

View file

@ -163,6 +163,8 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
#define DEMO_MAX_LEVELS 29 #define DEMO_MAX_LEVELS 29
const file_extension_t demo_file_extensions[2] = { DEMO_EXT, "" };
// In- and Out-files // In- and Out-files
PHYSFS_file *infile; PHYSFS_file *infile;
PHYSFS_file *outfile = NULL; PHYSFS_file *outfile = NULL;
@ -3730,10 +3732,9 @@ try_again:
int newdemo_count_demos() int newdemo_count_demos()
{ {
char **find, **i; char **find, **i;
static const char *const types[] = { DEMO_EXT, NULL };
int NumFiles=0; int NumFiles=0;
find = PHYSFSX_findFiles(DEMO_DIR, types); find = PHYSFSX_findFiles(DEMO_DIR, demo_file_extensions);
for (i = find; *i != NULL; i++) for (i = find; *i != NULL; i++)
NumFiles++; NumFiles++;
@ -3757,7 +3758,6 @@ void newdemo_start_playback(const char * filename)
{ {
// Randomly pick a filename // Randomly pick a filename
int NumFiles = 0, RandFileNum; int NumFiles = 0, RandFileNum;
static const char *const types[] = { DEMO_EXT, NULL };
rnd_demo = PURPOSE_RANDOM_PLAY; rnd_demo = PURPOSE_RANDOM_PLAY;
NumFiles = newdemo_count_demos(); NumFiles = newdemo_count_demos();
@ -3769,7 +3769,7 @@ void newdemo_start_playback(const char * filename)
RandFileNum = d_rand() % NumFiles; RandFileNum = d_rand() % NumFiles;
NumFiles = 0; NumFiles = 0;
find = PHYSFSX_findFiles(DEMO_DIR, types); find = PHYSFSX_findFiles(DEMO_DIR, demo_file_extensions);
for (i = find; *i != NULL; i++) for (i = find; *i != NULL; i++)
{ {

View file

@ -19,14 +19,17 @@
#include "object.h" #include "object.h"
#include "newdemo.h" #include "newdemo.h"
static const file_extension_t archive_exts[] = { "dxa", "" };
int PHYSFSX_checkMatchingExtension(const file_extension_t *exts, const char *filename) int PHYSFSX_checkMatchingExtension(const file_extension_t *exts, const char *filename)
{ {
const char *ext = strrchr(filename, '.'); const char *ext = strrchr(filename, '.');
if (!ext) if (!ext)
return 0; return 0;
++ext;
for (const file_extension_t *k = exts;; ++k) // see if the file is of a type we want for (const file_extension_t *k = exts;; ++k) // see if the file is of a type we want
{ {
if (!*k) if (!(*k)[0])
return 0; return 0;
if (!d_stricmp(ext, *k)) if (!d_stricmp(ext, *k))
return 1; return 1;
@ -350,7 +353,7 @@ int PHYSFSX_rename(const char *oldpath, const char *newpath)
// Find files at path that have an extension listed in exts // Find files at path that have an extension listed in exts
// The extension list exts must be NULL-terminated, with each ext beginning with a '.' // The extension list exts must be NULL-terminated, with each ext beginning with a '.'
char **PHYSFSX_findFiles(const char *path, const char *const *exts) char **PHYSFSX_findFiles(const char *path, const file_extension_t *exts)
{ {
char **list = PHYSFS_enumerateFiles(path); char **list = PHYSFS_enumerateFiles(path);
char **i, **j = list; char **i, **j = list;
@ -375,7 +378,7 @@ char **PHYSFSX_findFiles(const char *path, const char *const *exts)
// Same function as above but takes a real directory as second argument, only adding files originating from this directory. // Same function as above but takes a real directory as second argument, only adding files originating from this directory.
// This can be used to further seperate files in search path but it must be made sure realpath is properly formatted. // This can be used to further seperate files in search path but it must be made sure realpath is properly formatted.
char **PHYSFSX_findabsoluteFiles(const char *path, const char *realpath, const char *const *exts) char **PHYSFSX_findabsoluteFiles(const char *path, const char *realpath, const file_extension_t *exts)
{ {
char **list = PHYSFS_enumerateFiles(path); char **list = PHYSFS_enumerateFiles(path);
char **i, **j = list; char **i, **j = list;
@ -480,7 +483,6 @@ PHYSFS_file *PHYSFSX_openWriteBuffered(const char *filename)
void PHYSFSX_addArchiveContent() void PHYSFSX_addArchiveContent()
{ {
char **list = NULL; char **list = NULL;
static const char *const archive_exts[] = { ".dxa", NULL };
int i = 0, content_updated = 0; int i = 0, content_updated = 0;
con_printf(CON_DEBUG, "PHYSFS: Adding archives to the game."); con_printf(CON_DEBUG, "PHYSFS: Adding archives to the game.");
@ -531,7 +533,6 @@ void PHYSFSX_addArchiveContent()
void PHYSFSX_removeArchiveContent() void PHYSFSX_removeArchiveContent()
{ {
char **list = NULL; char **list = NULL;
static const char *const archive_exts[] = { ".dxa", NULL };
int i = 0; int i = 0;
// find files in Searchpath ... // find files in Searchpath ...