This commit removes maplist.c and replaces it with filelist.c, which

contains general functions for creating lists of a type of file and
provides the commands maplist, skinlist, skyboxlist, and demolist.
This commit is contained in:
Brian Koropoff 2001-11-06 07:14:29 +00:00
parent ceb307aead
commit ccd665e80c
8 changed files with 309 additions and 207 deletions

View file

@ -129,5 +129,9 @@ void Con_Shutdown (void);
void Con_ProcessInput (void); void Con_ProcessInput (void);
void Con_Maplist_f (void); void Con_Maplist_f (void);
void Con_Skinlist_f (void);
void Con_Skyboxlist_f (void);
void Con_Demolist_QWD_f (void);
void Con_Demolist_DEM_f (void);
#endif // __console_h #endif // __console_h

View file

@ -3,7 +3,7 @@ INCLUDES= -I$(top_srcdir)/include
lib_LTLIBRARIES= libQFconsole.la lib_LTLIBRARIES= libQFconsole.la
plugin_LTLIBRARIES= libconsole_server.la #libconsole_client.la plugin_LTLIBRARIES= libconsole_server.la #libconsole_client.la
common_sources= buffer.c complete.c console.c inputline.c list.c maplist.c common_sources= buffer.c complete.c console.c inputline.c list.c filelist.c
client_sources= client.c client_sources= client.c
server_sources= server.c server_sources= server.c

297
libs/console/filelist.c Normal file
View file

@ -0,0 +1,297 @@
/*
filelist.c
filelist commands
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
static const char rcsid[] =
"$Id$";
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdarg.h>
#ifdef HAVE_FNMATCH_H
# define model_t sunmodel_t
# include <fnmatch.h>
# undef model_t
#else
# ifdef WIN32
# include "fnmatch.h"
# endif
#endif
#ifdef HAVE_IO_H
# include <io.h>
#endif
#ifdef _MSC_VER
# define _POSIX_
#endif
#include <limits.h>
#include "QF/cmd.h"
#include "QF/console.h"
#include "QF/cvar.h"
#include "QF/hash.h"
#include "QF/qargs.h"
#include "QF/qendian.h"
#include "QF/qtypes.h"
#include "QF/sys.h"
#include "QF/va.h"
#include "QF/vfs.h"
#include "QF/zone.h"
#include "compat.h"
#ifndef HAVE_FNMATCH_PROTO
int fnmatch (const char *__pattern, const char *__string, int __flags);
#endif
struct filelist {
char **list;
int count;
int size;
};
static struct filelist *
filelist_new (void)
{
return calloc (1, sizeof (struct filelist));
}
static void
filelist_free (struct filelist *filelist)
{
int i;
for (i = 0; i < filelist->count; i++)
free (filelist->list[i]);
free (filelist->list);
free (filelist);
}
static void
filelist_add_file (struct filelist *filelist, char *fname, const char *ext)
{
char **new_list;
char *s;
while ((s = strchr(fname, '/')))
fname = s+1;
if (filelist->count == filelist->size) {
filelist->size += 32;
new_list = realloc (filelist->list, filelist->size * sizeof (char *));
if (!new_list) {
filelist->size -= 32;
return;
}
filelist->list = new_list;
}
fname = strdup (fname);
if (ext && (s = strstr(fname, va(".%s", ext))))
*s = 0;
filelist->list[filelist->count++] = fname;
}
static int
filelist_cmp (const void *_a, const void *_b)
{
char *a = *(char **) _a;
char *b = *(char **) _b;
return strcmp (a, b);
}
static void
filelist_print (struct filelist *filelist)
{
int i;
const char **list;
if (filelist->count) {
//qsort (filelist->list, filelist->count, sizeof (char *), filelist_cmp);
if (0) filelist_cmp (0, 0);
list = (const char **)malloc ((filelist->count + 1)*sizeof(char **));
list[filelist->count] = 0;
for (i = 0; i < filelist->count; i++)
list[i] = filelist->list[i];
Con_DisplayList (list, con_linewidth);
free (list);
}
}
/*
filelist_fill
Fills a list with files of a specific extension.
*/
void
filelist_fill (struct filelist *filelist, const char *path, const char *ext)
{
searchpath_t *search;
DIR *dir_ptr;
struct dirent *dirent;
char buf[MAX_OSPATH];
for (search = com_searchpaths; search != NULL; search = search->next) {
if (search->pack) {
int i;
pack_t *pak = search->pack;
for (i = 0; i < pak->numfiles; i++) {
char *name = pak->files[i].name;
if (!fnmatch (va("%s*.%s", path, ext), name, FNM_PATHNAME)
|| !fnmatch (va("%s*.%s.gz", path, ext), name, FNM_PATHNAME))
filelist_add_file (filelist, name, ext);
}
} else {
snprintf (buf, sizeof (buf), "%s/%s", search->filename, path);
dir_ptr = opendir (buf);
if (!dir_ptr)
continue;
while ((dirent = readdir (dir_ptr)))
if (!fnmatch (va("*.%s", ext), dirent->d_name, 0)
|| !fnmatch (va("*.%s.gz", ext), dirent->d_name, 0))
filelist_add_file (filelist, dirent->d_name, ext);
closedir (dir_ptr);
}
}
}
void
Con_Maplist_f (void)
{
struct filelist *maplist = filelist_new ();
filelist_fill (maplist, "maps/", "bsp");
filelist_print (maplist);
filelist_free (maplist);
}
void
Con_Skinlist_f (void)
{
struct filelist *skinlist = filelist_new ();
filelist_fill (skinlist, "skins/", "pcx");
filelist_print (skinlist);
filelist_free (skinlist);
}
char *sb_endings[] = {
"bk",
"dn",
"ft",
"lf",
"rt",
"up",
0
};
void
Con_Skyboxlist_f (void)
{
int i, j, k, c, b;
char basename[256];
struct filelist *skyboxlist = filelist_new ();
struct filelist *cutlist = filelist_new ();
filelist_fill(skyboxlist, "env/", "tga");
filelist_fill(skyboxlist, "env/", "pcx");
for (i = 0; i < skyboxlist->count; i++) {
if (strlen(skyboxlist->list[i]) > strlen(sb_endings[0]) && strcmp(skyboxlist->list[i] + strlen(skyboxlist->list[i]) - strlen(sb_endings[0]), sb_endings[0]) == 0) {
strncpy(basename, skyboxlist->list[i], sizeof(basename));
basename[strlen(skyboxlist->list[i]) - strlen(sb_endings[0])] = 0;
c = 0;
for (j = 1; sb_endings[j]; j++) {
b = 0;
for (k = 0; k < skyboxlist->count; k++) {
if (strcmp(va("%s%s", basename, sb_endings[j]), skyboxlist->list[k]) == 0) {
b = 1;
*skyboxlist->list[k] = 0;
}
}
c += b;
}
if (c == 5)
filelist_add_file(cutlist, basename, 0);
}
}
filelist_print(cutlist);
filelist_free(cutlist);
filelist_free(skyboxlist);
}
void
Con_Demolist_QWD_f (void)
{
struct filelist *demolist = filelist_new ();
filelist_fill(demolist, "", "qwd");
filelist_print(demolist);
filelist_free(demolist);
return;
}
void
Con_Demolist_DEM_f (void)
{
struct filelist *demolist = filelist_new ();
filelist_fill(demolist, "", "dem");
filelist_print(demolist);
filelist_free(demolist);
return;
}

View file

@ -1,203 +0,0 @@
/*
maplist.c
maplist command
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
static const char rcsid[] =
"$Id$";
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdarg.h>
#ifdef HAVE_FNMATCH_H
# define model_t sunmodel_t
# include <fnmatch.h>
# undef model_t
#else
# ifdef WIN32
# include "fnmatch.h"
# endif
#endif
#ifdef HAVE_IO_H
# include <io.h>
#endif
#ifdef _MSC_VER
# define _POSIX_
#endif
#include <limits.h>
#include "QF/cmd.h"
#include "QF/console.h" //FIXME maplist really shouldn't be in here
#include "QF/cvar.h"
#include "QF/hash.h"
#include "QF/qargs.h"
#include "QF/qendian.h"
#include "QF/qtypes.h"
#include "QF/sys.h"
#include "QF/va.h"
#include "QF/vfs.h"
#include "QF/zone.h"
#include "compat.h"
#ifndef HAVE_FNMATCH_PROTO
int fnmatch (const char *__pattern, const char *__string, int __flags);
#endif
struct maplist {
char **list;
int count;
int size;
};
static struct maplist *
maplist_new (void)
{
return calloc (1, sizeof (struct maplist));
}
static void
maplist_free (struct maplist *maplist)
{
int i;
for (i = 0; i < maplist->count; i++)
free (maplist->list[i]);
free (maplist->list);
free (maplist);
}
static void
maplist_add_map (struct maplist *maplist, char *fname)
{
char **new_list;
if (maplist->count == maplist->size) {
maplist->size += 32;
new_list = realloc (maplist->list, maplist->size * sizeof (char *));
if (!new_list) {
maplist->size -= 32;
return;
}
maplist->list = new_list;
}
fname = strdup (fname);
*strstr (fname, ".bsp") = 0;
maplist->list[maplist->count++] = fname;
}
static int
maplist_cmp (const void *_a, const void *_b)
{
char *a = *(char **) _a;
char *b = *(char **) _b;
return strcmp (a, b);
}
static void
maplist_print (struct maplist *maplist)
{
int i;
const char **list;
if (maplist->count) {
qsort (maplist->list, maplist->count, sizeof (char *), maplist_cmp);
list = (const char **)malloc (maplist->count + 1);
list[maplist->count] = 0;
for (i = 0; i < maplist->count; i++)
list[i] = maplist->list[i];
Con_DisplayList (list, con_linewidth);
free (list);
}
}
/*
Con_Maplist_f
List map files in gamepaths.
*/
void
Con_Maplist_f (void)
{
searchpath_t *search;
DIR *dir_ptr;
struct dirent *dirent;
char buf[MAX_OSPATH];
for (search = com_searchpaths; search != NULL; search = search->next) {
if (search->pack) {
int i;
pack_t *pak = search->pack;
struct maplist *maplist = maplist_new ();
Sys_Printf ("Looking in %s...\n", search->pack->filename);
for (i = 0; i < pak->numfiles; i++) {
char *name = pak->files[i].name;
if (!fnmatch ("maps/*.bsp", name, FNM_PATHNAME)
|| !fnmatch ("maps/*.bsp.gz", name, FNM_PATHNAME))
maplist_add_map (maplist, name + 5);
}
maplist_print (maplist);
maplist_free (maplist);
} else {
struct maplist *maplist = maplist_new ();
snprintf (buf, sizeof (buf), "%s/maps", search->filename);
dir_ptr = opendir (buf);
Sys_Printf ("Looking in %s...\n", buf);
if (!dir_ptr)
continue;
while ((dirent = readdir (dir_ptr)))
if (!fnmatch ("*.bsp", dirent->d_name, 0)
|| !fnmatch ("*.bsp.gz", dirent->d_name, 0))
maplist_add_map (maplist, dirent->d_name);
closedir (dir_ptr);
maplist_print (maplist);
maplist_free (maplist);
}
}
}

View file

@ -64,7 +64,7 @@ client_LIBFILES= \
$(top_builddir)/libs/video/targets/libQFjs.la \ $(top_builddir)/libs/video/targets/libQFjs.la \
$(top_builddir)/libs/audio/libQFcd.la \ $(top_builddir)/libs/audio/libQFcd.la \
$(top_builddir)/libs/audio/libQFsound.la \ $(top_builddir)/libs/audio/libQFsound.la \
$(top_builddir)/libs/console/maplist.o $(top_builddir)/libs/console/filelist.o
server_LIBFILES= \ server_LIBFILES= \
$(top_builddir)/libs/models/libQFmodels.la \ $(top_builddir)/libs/models/libQFmodels.la \

View file

@ -738,7 +738,8 @@ CL_Init (void)
Cmd_AddCommand ("stop", CL_Stop_f, "No Description"); Cmd_AddCommand ("stop", CL_Stop_f, "No Description");
Cmd_AddCommand ("playdemo", CL_PlayDemo_f, "No Description"); Cmd_AddCommand ("playdemo", CL_PlayDemo_f, "No Description");
Cmd_AddCommand ("timedemo", CL_TimeDemo_f, "No Description"); Cmd_AddCommand ("timedemo", CL_TimeDemo_f, "No Description");
Cmd_AddCommand ("maplist", Con_Maplist_f, "No Description"); Cmd_AddCommand ("maplist", Con_Maplist_f, "List available maps");
Cmd_AddCommand ("demolist", Con_Demolist_DEM_f, "List available demos");
Cmd_AddCommand ("force_centerview", Force_CenterView_f, "force the view " Cmd_AddCommand ("force_centerview", Force_CenterView_f, "force the view "
"to be level"); "to be level");

View file

@ -103,7 +103,7 @@ qf_client_LIBS= \
$(top_builddir)/libs/gamecode/builtins/libQFgamecode_builtins.la \ $(top_builddir)/libs/gamecode/builtins/libQFgamecode_builtins.la \
$(top_builddir)/libs/audio/libQFcd.la \ $(top_builddir)/libs/audio/libQFcd.la \
$(top_builddir)/libs/audio/libQFsound.la \ $(top_builddir)/libs/audio/libQFsound.la \
$(top_builddir)/libs/console/maplist.o \ $(top_builddir)/libs/console/filelist.o \
$(top_builddir)/libs/util/libQFutil.la $(top_builddir)/libs/util/libQFutil.la
client_LIBS= libqfnet.la libasm.la $(qf_client_LIBS) $(NET_LIBS) client_LIBS= libqfnet.la libasm.la $(qf_client_LIBS) $(NET_LIBS)

View file

@ -1131,6 +1131,9 @@ CL_Init (void)
Cmd_AddCommand ("timedemo", CL_TimeDemo_f, "Play a demo as fast as your " Cmd_AddCommand ("timedemo", CL_TimeDemo_f, "Play a demo as fast as your "
"hardware can. Useful for benchmarking."); "hardware can. Useful for benchmarking.");
Cmd_AddCommand ("maplist", Con_Maplist_f, "List maps available"); Cmd_AddCommand ("maplist", Con_Maplist_f, "List maps available");
Cmd_AddCommand ("skinlist", Con_Skinlist_f, "List skins available");
Cmd_AddCommand ("skyboxlist", Con_Skyboxlist_f, "List skyboxes available");
Cmd_AddCommand ("demolist", Con_Demolist_QWD_f, "List demos available");
Cmd_AddCommand ("quit", CL_Quit_f, "Exit the program"); Cmd_AddCommand ("quit", CL_Quit_f, "Exit the program");
Cmd_AddCommand ("connect", CL_Connect_f, "Connect to a server 'connect " Cmd_AddCommand ("connect", CL_Connect_f, "Connect to a server 'connect "
"hostname:port'"); "hostname:port'");