(ef_init_srv): Call ef_init() before ef_load().

(xundump): Set empfile member cids in addition to fids.

(xundump): Clarify shortcomings: requires EFF_MEM, appends sentinel
always, can't grow dynamic cache.

(xundump): Return table type on success.
This commit is contained in:
Markus Armbruster 2005-12-03 10:09:53 +00:00
parent b8befab521
commit 29c83d1511
2 changed files with 63 additions and 55 deletions

View file

@ -406,10 +406,11 @@ xundump(FILE *fp, char *file, int expected_table)
{ {
char name[64]; char name[64];
char sep; char sep;
struct empfile *ep;
int row, res, rows, ch; int row, res, rows, ch;
struct value values[MAX_NUM_COLUMNS + 1]; struct value values[MAX_NUM_COLUMNS + 1];
int type; int type;
int fixed_rows; int fixed_rows, need_sentinel;
if (strcmp(fname, file) != 0) { if (strcmp(fname, file) != 0) {
fname = file; fname = file;
@ -425,12 +426,16 @@ xundump(FILE *fp, char *file, int expected_table)
type = ef_byname(name); type = ef_byname(name);
if (type < 0) if (type < 0)
return gripe("Unknown table `%s'", name); return gripe("Unknown table `%s'", name);
ep = &empfile[type];
if (CANT_HAPPEN(!(ep->flags & EFF_MEM)))
return -1; /* not implemented */
if (expected_table != EF_BAD && expected_table != type) if (expected_table != EF_BAD && expected_table != type)
return gripe("Expected table `%s', not `%s'", return gripe("Expected table `%s', not `%s'",
ef_nameof(expected_table), name); ef_nameof(expected_table), name);
fixed_rows = has_const(ef_cadef(type)); fixed_rows = has_const(ef_cadef(type));
need_sentinel = !fixed_rows; /* FIXME only approximation */
for (row = 0; ; row++) { for (row = 0; ; row++) {
lineno++; lineno++;
@ -443,12 +448,12 @@ xundump(FILE *fp, char *file, int expected_table)
* Add column count check to the return value of xuflds() * Add column count check to the return value of xuflds()
*/ */
res = xuflds(fp, values); res = xuflds(fp, values);
if (res > 0 && row >= empfile[type].csize - 1) { if (res > 0 && row >= ep->csize - 1) {
/* TODO grow cache unless EFF_STATIC */
gripe("Too many rows for table %s", name); gripe("Too many rows for table %s", name);
res = -1; res = -1;
} }
if (res > 0) { if (res > 0) {
empfile[type].fids = row + 1;
if (!fixed_rows) if (!fixed_rows)
xuinitrow(type, row); xuinitrow(type, row);
res = xuloadrow(type, row, values); res = xuloadrow(type, row, values);
@ -465,14 +470,14 @@ xundump(FILE *fp, char *file, int expected_table)
if (row != rows) if (row != rows)
return gripe("Read %d rows, which doesn't match footer", return gripe("Read %d rows, which doesn't match footer",
row); row);
if (fixed_rows && row != empfile[type].csize -1) if (fixed_rows && row != ep->csize -1)
return gripe("Table %s requires %d rows, got %d", return gripe("Table %s requires %d rows, got %d",
name, empfile[type].csize - 1, row); name, ep->csize - 1, row);
if (sep != '\n') if (sep != '\n')
return gripe("Junk after table footer"); return gripe("Junk after table footer");
if (need_sentinel)
if (!fixed_rows)
xuinitrow(type, row); xuinitrow(type, row);
return 0; ep->fids = ep->cids = row;
return type;
} }

View file

@ -35,6 +35,56 @@
#include "file.h" #include "file.h"
#include "prototypes.h" #include "prototypes.h"
struct fileinit {
int ef_type;
void (*init) (int, char *);
int (*postread) (int, char *);
int (*prewrite) (int, char *);
};
static struct fileinit fileinit[] = {
{EF_SECTOR, NULL, sct_postread, sct_prewrite},
{EF_SHIP, shp_init, shp_postread, shp_prewrite},
{EF_PLANE, pln_init, pln_postread, pln_prewrite},
{EF_LAND, lnd_init, lnd_postread, lnd_prewrite},
{EF_NUKE, nuk_init, nuk_postread, nuk_prewrite}
};
static void ef_open_srv(void);
static void ef_close_srv(void);
/*
* Initialize empfile for full server operations.
* Like ef_init(), but additionally installs the server's callbacks.
* This is separate from ef_init(), so that utility programs like
* files can use empfile.
*/
void
ef_init_srv(void)
{
unsigned i;
ef_init();
if (ef_load() < 0)
exit(EXIT_FAILURE);
for (i = 0; i < sizeof(fileinit) / sizeof(fileinit[0]); i++) {
empfile[fileinit[i].ef_type].init = fileinit[i].init;
empfile[fileinit[i].ef_type].postread = fileinit[i].postread;
empfile[fileinit[i].ef_type].prewrite = fileinit[i].prewrite;
}
ef_open_srv();
if (ef_verify() < 0)
exit(EXIT_FAILURE);
global_init();
}
void
ef_fin_srv(void)
{
ef_close_srv();
}
static void static void
ef_open_srv(void) ef_open_srv(void)
{ {
@ -60,47 +110,6 @@ ef_open_srv(void)
} }
} }
struct fileinit {
int ef_type;
void (*init) (int, char *);
int (*postread) (int, char *);
int (*prewrite) (int, char *);
};
static struct fileinit fileinit[] = {
{EF_SECTOR, NULL, sct_postread, sct_prewrite},
{EF_SHIP, shp_init, shp_postread, shp_prewrite},
{EF_PLANE, pln_init, pln_postread, pln_prewrite},
{EF_LAND, lnd_init, lnd_postread, lnd_prewrite},
{EF_NUKE, nuk_init, nuk_postread, nuk_prewrite}
};
/*
* Initialize empfile for full server operations.
* Like ef_init(), but additionally installs the server's callbacks.
* This is separate from ef_init(), so that utility programs like
* files can use empfile.
*/
void
ef_init_srv(void)
{
unsigned i;
if (ef_load() < 0)
exit(EXIT_FAILURE);
for (i = 0; i < sizeof(fileinit) / sizeof(fileinit[0]); i++) {
empfile[fileinit[i].ef_type].init = fileinit[i].init;
empfile[fileinit[i].ef_type].postread = fileinit[i].postread;
empfile[fileinit[i].ef_type].prewrite = fileinit[i].prewrite;
}
ef_init();
ef_open_srv();
if (ef_verify() < 0)
exit(EXIT_FAILURE);
global_init();
}
static void static void
ef_close_srv(void) ef_close_srv(void)
{ {
@ -120,9 +129,3 @@ ef_close_srv(void)
ef_close(EF_BMAP); ef_close(EF_BMAP);
ef_close(EF_LOST); ef_close(EF_LOST);
} }
void
ef_fin_srv(void)
{
ef_close_srv();
}