implement a proper (and optionally secure) QFile interace for ruamoko.

This commit is contained in:
Bill Currie 2003-02-14 18:17:17 +00:00
parent 9d3d4e0638
commit fd7636137d
11 changed files with 382 additions and 53 deletions

View file

@ -40,14 +40,24 @@ struct inputline_s;
void Cbuf_Progs_Init (struct progs_s *pr);
void Cbuf_Progs_SetCbuf (struct progs_s *pr, struct cbuf_s *cbuf);
void Cmd_Progs_Init (struct progs_s *pr);
void Cvar_Progs_Init (struct progs_s *pr);
void File_Progs_Init (struct progs_s *pr);
void InputLine_Progs_Init (struct progs_s *pr);
void InputLine_Progs_SetDraw (struct progs_s *pr,
void (*draw)(struct inputline_s*));
void Key_Progs_Init (struct progs_s *pr);
void QFile_Progs_Init (struct progs_s *pr, int secure);
int QFile_open (struct progs_s *pr, const char *path, const char *mode);
void String_Progs_Init (struct progs_s *pr);
void StringHash_Progs_Init (struct progs_s *pr);
#endif//__QF_csqc_h

View file

@ -448,6 +448,7 @@ Menu_Init (void)
File_Progs_Init (&menu_pr_state);
InputLine_Progs_Init (&menu_pr_state);
Key_Progs_Init (&menu_pr_state);
QFile_Progs_Init (&menu_pr_state, 1);
PR_Cmds_Init (&menu_pr_state);
R_Progs_Init (&menu_pr_state);
String_Progs_Init (&menu_pr_state);

View file

@ -11,4 +11,4 @@ libQFgamecode_builtins_la_SOURCES= pr_cmds.c
libQFcsqc_la_LDFLAGS= -version-info 1:0:0
libQFcsqc_la_SOURCES=\
bi_cbuf.c bi_cmd.c bi_cvar.c bi_file.c bi_init.c bi_inputline.c \
bi_string.c bi_strhash.c
bi_qfile.c bi_string.c bi_strhash.c

View file

@ -59,9 +59,6 @@ int fnmatch (const char *__pattern, const char *__string, int __flags);
#include "QF/va.h"
#include "QF/zone.h"
#define MAX_HANDLES 20
static QFile *handles[MAX_HANDLES];
static const char *file_ban_list[] = {
"default.cfg{,.gz}",
"demo1.dem{,.gz}",
@ -123,7 +120,6 @@ bi_File_Open (progs_t *pr)
const char *mode = P_STRING (pr, 1);
char *path;
char *p;
int h;
int do_write = 0;
int do_read = 0;
@ -162,49 +158,16 @@ bi_File_Open (progs_t *pr)
goto error;
if (do_write && !file_writeable (path))
goto error;
for (h = 0; h < MAX_HANDLES && handles[h]; h++)
;
if (h == MAX_HANDLES)
goto error;
if (!(handles[h] = Qopen (va ("%s/%s", com_gamedir, path), mode)))
goto error;
R_INT (pr) = QFile_open (pr, va ("%s/%s", com_gamedir, path), mode);
free (path);
R_INT (pr) = h + 1;
return;
error:
free (path);
R_INT (pr) = 0;
}
static void
bi_File_Close (progs_t *pr)
{
int h = P_INT (pr, 0) - 1;
if (h < 0 || h >= MAX_HANDLES || !handles[h])
return;
Qclose (handles[h]);
handles[h] = 0;
}
static void
bi_File_GetLine (progs_t *pr)
{
int h = P_INT (pr, 0) - 1;
const char *s;
if (h < 0 || h >= MAX_HANDLES || !handles[h]) {
R_INT (pr) = 0;
return;
}
s = Qgetline (handles[h]);
RETURN_STRING (pr, s);
}
void
File_Progs_Init (progs_t *pr)
{
PR_AddBuiltin (pr, "File_Open", bi_File_Open, -1);
PR_AddBuiltin (pr, "File_Close", bi_File_Close, -1);
PR_AddBuiltin (pr, "File_GetLine", bi_File_GetLine, -1);
}

View file

@ -0,0 +1,318 @@
/*
bi_file.c
CSQC file builtins
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
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
static __attribute__ ((unused)) const char rcsid[] =
"$Id$";
#include <stdlib.h>
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_FNMATCH_H
# define model_t sunmodel_t
# include <fnmatch.h>
# undef model_t
#else
# ifdef WIN32
# include "fnmatch.h"
# endif
#endif
#ifndef HAVE_FNMATCH_PROTO
int fnmatch (const char *__pattern, const char *__string, int __flags);
#endif
#include "QF/csqc.h"
#include "QF/progs.h"
#include "QF/quakefs.h"
#include "QF/va.h"
#include "QF/zone.h"
#define MAX_HANDLES 20
static QFile *handles[MAX_HANDLES];
int
QFile_open (struct progs_s *pr, const char *path, const char *mode)
{
int h;
for (h = 0; h < MAX_HANDLES && handles[h]; h++)
;
if (h == MAX_HANDLES)
goto error;
if (!(handles[h] = Qopen (path, mode)))
goto error;
return h + 1;
error:
return 0;
}
static void
secured (progs_t *pr)
{
PR_RunError (pr, "Secured function called");
}
static void
bi_Qrename (progs_t *pr)
{
const char *old = P_STRING (pr, 0);
const char *new = P_STRING (pr, 1);
R_INT (pr) = Qrename (old, new);
}
static void
bi_Qremove (progs_t *pr)
{
const char *path = P_STRING (pr, 0);
R_INT (pr) = Qremove (path);
}
static void
bi_Qopen (progs_t *pr)
{
const char *path = P_STRING (pr, 0);
const char *mode = P_STRING (pr, 1);
R_INT (pr) = QFile_open (pr, path, mode);
}
static void
bi_Qclose (progs_t *pr)
{
int h = P_INT (pr, 0) - 1;
if (h < 0 || h >= MAX_HANDLES || !handles[h])
return;
Qclose (handles[h]);
handles[h] = 0;
}
static void
bi_Qgetline (progs_t *pr)
{
int h = P_INT (pr, 0) - 1;
const char *s;
if (h < 0 || h >= MAX_HANDLES || !handles[h]) {
R_INT (pr) = 0;
return;
}
s = Qgetline (handles[h]);
RETURN_STRING (pr, s);
}
static void
check_buffer (progs_t *pr, pr_type_t *buf, int count, const char *name)
{
int len;
len = (count + sizeof (pr_type_t) - 1) / sizeof (pr_type_t);
if (buf < pr->pr_globals || buf + len > pr->pr_globals + pr->globals_size)
PR_RunError (pr, "%s: bad buffer", name);
}
static void
bi_Qread (progs_t *pr)
{
int h = P_INT (pr, 0) - 1;
pr_type_t *buf = P_POINTER (pr, 1);
int count = P_INT (pr, 2);
if (h < 0 || h >= MAX_HANDLES || !handles[h]) {
R_INT (pr) = -1;
return;
}
check_buffer (pr, buf, count, "Qread");
R_INT (pr) = Qread (handles[h], buf, count);
}
static void
bi_Qwrite (progs_t *pr)
{
int h = P_INT (pr, 0) - 1;
pr_type_t *buf = P_POINTER (pr, 1);
int count = P_INT (pr, 2);
if (h < 0 || h >= MAX_HANDLES || !handles[h]) {
R_INT (pr) = -1;
return;
}
check_buffer (pr, buf, count, "Qwrite");
R_INT (pr) = Qwrite (handles[h], buf, count);
}
static void
bi_Qputs (progs_t *pr)
{
int h = P_INT (pr, 0) - 1;
const char *str = P_STRING (pr, 1);
if (h < 0 || h >= MAX_HANDLES || !handles[h]) {
R_INT (pr) = -1;
return;
}
R_INT (pr) = Qputs (handles[h], str);
}
#if 0
static void
bi_Qgets (progs_t *pr)
{
int h = P_INT (pr, 0) - 1;
pr_type_t *buf = P_POINTER (pr, 1);
int count = P_INT (pr, 2);
if (h < 0 || h >= MAX_HANDLES || !handles[h]) {
R_INT (pr) = -1;
return;
}
check_buffer (pr, buf, count, "Qgets");
R_INT (pr) = POINTER_TO_PROG (pr, Qgets (handles[h], (char *) buf, count));
}
#endif
static void
bi_Qgetc (progs_t *pr)
{
int h = P_INT (pr, 0) - 1;
if (h < 0 || h >= MAX_HANDLES || !handles[h]) {
R_INT (pr) = -1;
return;
}
R_INT (pr) = Qgetc (handles[h]);
}
static void
bi_Qputc (progs_t *pr)
{
int h = P_INT (pr, 0) - 1;
int c = P_INT (pr, 1);
if (h < 0 || h >= MAX_HANDLES || !handles[h]) {
R_INT (pr) = -1;
return;
}
R_INT (pr) = Qputc (handles[h], c);
}
static void
bi_Qseek (progs_t *pr)
{
int h = P_INT (pr, 0) - 1;
int offset = P_INT (pr, 1);
int whence = P_INT (pr, 2);
if (h < 0 || h >= MAX_HANDLES || !handles[h]) {
R_INT (pr) = -1;
return;
}
R_INT (pr) = Qseek (handles[h], offset, whence);
}
static void
bi_Qtell (progs_t *pr)
{
int h = P_INT (pr, 0) - 1;
if (h < 0 || h >= MAX_HANDLES || !handles[h]) {
R_INT (pr) = -1;
return;
}
R_INT (pr) = Qtell (handles[h]);
}
static void
bi_Qflush (progs_t *pr)
{
int h = P_INT (pr, 0) - 1;
if (h < 0 || h >= MAX_HANDLES || !handles[h]) {
R_INT (pr) = -1;
return;
}
R_INT (pr) = Qflush (handles[h]);
}
static void
bi_Qeof (progs_t *pr)
{
int h = P_INT (pr, 0) - 1;
if (h < 0 || h >= MAX_HANDLES || !handles[h]) {
R_INT (pr) = -1;
return;
}
R_INT (pr) = Qeof (handles[h]);
}
static void
bi_Qfilesize (progs_t *pr)
{
int h = P_INT (pr, 0) - 1;
if (h < 0 || h >= MAX_HANDLES || !handles[h]) {
R_INT (pr) = -1;
return;
}
R_INT (pr) = Qfilesize (handles[h]);
}
void
QFile_Progs_Init (progs_t *pr, int secure)
{
if (secure) {
PR_AddBuiltin (pr, "Qrename", secured, -1);
PR_AddBuiltin (pr, "Qremove", secured, -1);
PR_AddBuiltin (pr, "Qopen", secured, -1);
} else {
PR_AddBuiltin (pr, "Qrename", bi_Qrename, -1);
PR_AddBuiltin (pr, "Qremove", bi_Qremove, -1);
PR_AddBuiltin (pr, "Qopen", bi_Qopen, -1);
}
PR_AddBuiltin (pr, "Qclose", bi_Qclose, -1);
PR_AddBuiltin (pr, "Qgetline", bi_Qgetline, -1);
PR_AddBuiltin (pr, "Qread", bi_Qread, -1);
PR_AddBuiltin (pr, "Qwrite", bi_Qwrite, -1);
PR_AddBuiltin (pr, "Qputs", bi_Qputs, -1);
// PR_AddBuiltin (pr, "Qgets", bi_Qgets, -1);
PR_AddBuiltin (pr, "Qgetc", bi_Qgetc, -1);
PR_AddBuiltin (pr, "Qputc", bi_Qputc, -1);
PR_AddBuiltin (pr, "Qseek", bi_Qseek, -1);
PR_AddBuiltin (pr, "Qtell", bi_Qtell, -1);
PR_AddBuiltin (pr, "Qflush", bi_Qflush, -1);
PR_AddBuiltin (pr, "Qeof", bi_Qeof, -1);
PR_AddBuiltin (pr, "Qfilesize", bi_Qfilesize, -1);
}

View file

@ -94,17 +94,17 @@ integer save_cursor;
void () scan_saves =
{
local integer i;
local file_t f;
local QFile f;
for (i = 0; i < MAX_SAVEGAMES; i++) {
loadable[i] = 0;
filenames[i] = "--- UNUSED SLOT ---";
f = File_Open (sprintf ("s%i.sav", i), "rz");
if (!f)
continue;
File_GetLine (f);
filenames[i] = String_ReplaceChar ('_', ' ', File_GetLine (f));
Qgetline (f);
filenames[i] = String_ReplaceChar ('_', ' ', Qgetline (f));
loadable[i] = 1;
File_Close (f);
Qclose (f);
}
};

View file

@ -1,11 +1,8 @@
#ifndef __ruamoko_file_h
#define __ruamoko_file_h
struct _file_t = {};
typedef _file_t [] file_t;
#include "qfile.h"
@extern file_t (string path, string mode) File_Open;
@extern void (file_t file) File_Close;
@extern string (file_t file) File_GetLine;
@extern QFile (string path, string mode) File_Open;
#endif//__ruamoko_file_h

24
ruamoko/include/qfile.h Normal file
View file

@ -0,0 +1,24 @@
#ifndef __ruamoko_qfile_h
#define __ruamoko_qfile_h
struct _qfile_t = {};
typedef _qfile_t [] QFile;
@extern integer (string old, string new) Qrename;
@extern integer (string path) Qremove;
@extern QFile (string path, string mode) Qopen;
@extern void (QFile file) Qclose;
@extern string (QFile file) Qgetline;
@extern integer (QFile file, void [] buf, integer count) Qread;
@extern integer (QFile file, void [] buf, integer count) Qwrite;
@extern integer (QFile file, string str) Qputs;
//@extern integer (QFile file, void [] buf, integer count) Qgets;
@extern integer (QFile file) Qgetc;
@extern integer (QFile file, integer c) Qputc;
@extern integer (QFile file, integer offset, integer whence) Qseek;
@extern integer (QFile file) Qtell;
@extern integer (QFile file) Qflush;
@extern integer (QFile file) Qeof;
@extern integer (QFile file) Qfilesize;
#endif//__ruamoko_qfile_h

View file

@ -28,8 +28,8 @@ EXTRA_LIBRARIES= $(ruamoko_libs)
libr_a_SOURCES=\
crudefile.r debug.r entities.r infokey.r math.r message.r nq_message.r \
physics.r qw_message.r qw_physics.r qw_sys.r sound.r string.r system.r \
Object.r Array.r Entity.r Point.r Size.r Rect.r
physics.r qfile.r qw_message.r qw_physics.r qw_sys.r sound.r string.r \
system.r Object.r Array.r Entity.r Point.r Size.r Rect.r
libr_a_AR=$(PAK) -cf
libgui_a_SOURCES=\

View file

@ -1,5 +1,3 @@
#include "file.h"
file_t (string path, string mode) File_Open = #0;
void (file_t file) File_Close = #0;
string (file_t file) File_GetLine = #0;
QFile (string path, string mode) File_Open = #0;

18
ruamoko/lib/qfile.r Normal file
View file

@ -0,0 +1,18 @@
#include "qfile.h"
integer (string old, string new) Qrename = #0;
integer (string path) Qremove = #0;
QFile (string path, string mode) Qopen = #0;
void (QFile file) Qclose = #0;
string (QFile file) Qgetline = #0;
integer (QFile file, void [] buf, integer count) Qread = #0;
integer (QFile file, void [] buf, integer count) Qwrite = #0;
integer (QFile file, string str) Qputs = #0;
//integer (QFile file, void [] buf, integer count) Qgets = #0;
integer (QFile file) Qgetc = #0;
integer (QFile file, integer c) Qputc = #0;
integer (QFile file, integer offset, integer whence) Qseek = #0;
integer (QFile file) Qtell = #0;
integer (QFile file) Qflush = #0;
integer (QFile file) Qeof = #0;
integer (QFile file) Qfilesize = #0;