Clean up how a view's base table is defined

New struct empfile member base replaces ef_open_view() parameter base.
Cleaner, because the base table is a property of the view, not of how
it's used.

Use it to clean up verify_fail()'s base table access, and for extra
sanity checks in ef_open() and ef_open_view().
This commit is contained in:
Markus Armbruster 2011-05-01 13:59:17 +02:00
parent 2b0b53992f
commit 44f97c3297
5 changed files with 56 additions and 50 deletions

View file

@ -42,6 +42,7 @@ struct empfile {
char *file; /* file name, relative to gamedir for char *file; /* file name, relative to gamedir for
game state, to builtindir for config */ game state, to builtindir for config */
struct castr *cadef; /* table column selectors (column meta-data) */ struct castr *cadef; /* table column selectors (column meta-data) */
int base; /* view's base table, else EF_BAD */
int size; /* size of a table entry */ int size; /* size of a table entry */
int flags; /* only EFF_IMMUTABLE immutable, see below int flags; /* only EFF_IMMUTABLE immutable, see below
for use of remaining bits */ for use of remaining bits */
@ -199,7 +200,7 @@ enum {
}; };
#define EF_IS_GAME_STATE(type) (EF_SECTOR <= (type) && (type) <= EF_DYNMAX) #define EF_IS_GAME_STATE(type) (EF_SECTOR <= (type) && (type) <= EF_DYNMAX)
#define EF_IS_VIEW(type) (EF_COUNTRY <= (type) && (type) < EF_MAX) #define EF_IS_VIEW(type) (empfile[(type)].base != EF_BAD)
extern struct castr *ef_cadef(int); extern struct castr *ef_cadef(int);
extern int ef_read(int, int, void *); extern int ef_read(int, int, void *);
@ -209,7 +210,7 @@ extern void *ef_ptr(int, int);
extern char *ef_nameof(int); extern char *ef_nameof(int);
extern time_t ef_mtime(int); extern time_t ef_mtime(int);
extern int ef_open(int, int, int); extern int ef_open(int, int, int);
extern int ef_open_view(int, int); extern int ef_open_view(int);
extern int ef_close(int); extern int ef_close(int);
extern int ef_flush(int); extern int ef_flush(int);
extern void ef_blank(int, int, void *); extern void ef_blank(int, int, void *);

View file

@ -48,14 +48,11 @@ static void verify_fail(int, int, struct castr *, int, char *, ...)
static void static void
verify_fail(int type, int row, struct castr *ca, int idx, char *fmt, ...) verify_fail(int type, int row, struct castr *ca, int idx, char *fmt, ...)
{ {
int i; int base = empfile[type].base < 0 ? type : empfile[type].base;
va_list ap; va_list ap;
/* Find base table of view, if any */
for (i = 0; empfile[i].cache == empfile[type].cache; i++) ;
fprintf(stderr, "%s %s uid %d", fprintf(stderr, "%s %s uid %d",
EF_IS_GAME_STATE(i) ? "File" : "Config", EF_IS_GAME_STATE(base) ? "File" : "Config",
ef_nameof(type), row); ef_nameof(type), row);
if (ca) { if (ca) {
fprintf(stderr, " field %s", ca->ca_name); fprintf(stderr, " field %s", ca->ca_name);

View file

@ -83,7 +83,7 @@ ef_open(int type, int how, int nelt)
/* open file */ /* open file */
ep = &empfile[type]; ep = &empfile[type];
if (CANT_HAPPEN(ep->fd >= 0)) if (CANT_HAPPEN(!ep->file || ep->base != EF_BAD || ep->fd >= 0))
return 0; return 0;
oflags = O_RDWR; oflags = O_RDWR;
if (how & EFF_PRIVATE) if (how & EFF_PRIVATE)
@ -220,20 +220,28 @@ ef_realloc_cache(struct empfile *ep, int count)
} }
/* /*
* Open the table TYPE as view of table BASE. * Open the table TYPE, which is a view of a base table
* The table must not be already open.
* Return non-zero on success, zero on failure. * Return non-zero on success, zero on failure.
* Beware: views work only as long as BASE doesn't change size! * Beware: views work only as long as the base table doesn't change size!
* You must call ef_close(TYPE) before closing BASE. * You must close the view before closing its base table.
*/ */
int int
ef_open_view(int type, int base) ef_open_view(int type)
{ {
struct empfile *ep; struct empfile *ep;
int base;
if (CANT_HAPPEN(!EF_IS_VIEW(type))) if (ef_check(type) < 0)
return -1; return 0;
ep = &empfile[type]; ep = &empfile[type];
if (CANT_HAPPEN(!(ef_flags(base) & EFF_MEM))) base = ep->base;
if (ef_check(base) < 0)
return 0;
if (CANT_HAPPEN(!(ef_flags(base) & EFF_MEM)
|| ep->file || ep->size != empfile[base].size
|| ep->cache || ep->oninit || ep->postread
|| ep->prewrite || ep->onresize))
return -1; return -1;
ep->cache = empfile[base].cache; ep->cache = empfile[base].cache;

View file

@ -118,42 +118,42 @@ struct empfile empfile[] = {
* All caches unmapped. EF_MAP and EF_BMAP get a bogus size here. * All caches unmapped. EF_MAP and EF_BMAP get a bogus size here.
* Fixed up by empfile_fixup(). * Fixed up by empfile_fixup().
*/ */
{EF_SECTOR, "sect", "sector", sect_ca, {EF_SECTOR, "sect", "sector", sect_ca, EF_BAD,
UNMAPPED_CACHE(struct sctstr, EFF_TYPED | EFF_XY | EFF_OWNER)}, UNMAPPED_CACHE(struct sctstr, EFF_TYPED | EFF_XY | EFF_OWNER)},
{EF_SHIP, "ship", "ship", ship_ca, {EF_SHIP, "ship", "ship", ship_ca, EF_BAD,
UNMAPPED_CACHE(struct shpstr, UNMAPPED_CACHE(struct shpstr,
EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP)}, EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP)},
{EF_PLANE, "plane", "plane", plane_ca, {EF_PLANE, "plane", "plane", plane_ca, EF_BAD,
UNMAPPED_CACHE(struct plnstr, UNMAPPED_CACHE(struct plnstr,
EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP)}, EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP)},
{EF_LAND, "land", "land", land_ca, {EF_LAND, "land", "land", land_ca, EF_BAD,
UNMAPPED_CACHE(struct lndstr, UNMAPPED_CACHE(struct lndstr,
EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP)}, EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP)},
{EF_NUKE, "nuke", "nuke", nuke_ca, {EF_NUKE, "nuke", "nuke", nuke_ca, EF_BAD,
UNMAPPED_CACHE(struct nukstr, EFF_TYPED | EFF_XY | EFF_OWNER)}, UNMAPPED_CACHE(struct nukstr, EFF_TYPED | EFF_XY | EFF_OWNER)},
{EF_NEWS, "news", "news", news_ca, {EF_NEWS, "news", "news", news_ca, EF_BAD,
UNMAPPED_CACHE(struct nwsstr, 0)}, UNMAPPED_CACHE(struct nwsstr, 0)},
{EF_TREATY, "treaty", "treaty", treaty_ca, {EF_TREATY, "treaty", "treaty", treaty_ca, EF_BAD,
UNMAPPED_CACHE(struct trtstr, EFF_TYPED)}, UNMAPPED_CACHE(struct trtstr, EFF_TYPED)},
{EF_TRADE, "trade", "trade", trade_ca, {EF_TRADE, "trade", "trade", trade_ca, EF_BAD,
UNMAPPED_CACHE(struct trdstr, EFF_TYPED | EFF_OWNER)}, UNMAPPED_CACHE(struct trdstr, EFF_TYPED | EFF_OWNER)},
{EF_POWER, "pow", "power", NULL, {EF_POWER, "pow", "power", NULL, EF_BAD,
UNMAPPED_CACHE(struct powstr, 0)}, UNMAPPED_CACHE(struct powstr, 0)},
{EF_NATION, "nat", "nation", nat_ca, {EF_NATION, "nat", "nation", nat_ca, EF_BAD,
UNMAPPED_CACHE(struct natstr, EFF_TYPED | EFF_OWNER)}, UNMAPPED_CACHE(struct natstr, EFF_TYPED | EFF_OWNER)},
{EF_LOAN, "loan", "loan", loan_ca, {EF_LOAN, "loan", "loan", loan_ca, EF_BAD,
UNMAPPED_CACHE(struct lonstr, EFF_TYPED)}, UNMAPPED_CACHE(struct lonstr, EFF_TYPED)},
{EF_MAP, "map", "map", NULL, {EF_MAP, "map", "map", NULL, EF_BAD,
0, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL}, 0, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
{EF_BMAP, "bmap", "bmap", NULL, {EF_BMAP, "bmap", "bmap", NULL, EF_BAD,
0, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL}, 0, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
{EF_COMM, "commodity", "commodity", commodity_ca, {EF_COMM, "commodity", "commodity", commodity_ca, EF_BAD,
UNMAPPED_CACHE(struct comstr, EFF_TYPED | EFF_OWNER)}, UNMAPPED_CACHE(struct comstr, EFF_TYPED | EFF_OWNER)},
{EF_LOST, "lost", "lostitems", lost_ca, {EF_LOST, "lost", "lostitems", lost_ca, EF_BAD,
UNMAPPED_CACHE(struct loststr, EFF_TYPED | EFF_OWNER)}, UNMAPPED_CACHE(struct loststr, EFF_TYPED | EFF_OWNER)},
{EF_REALM, "realm", "realms", realm_ca, {EF_REALM, "realm", "realms", realm_ca, EF_BAD,
UNMAPPED_CACHE(struct realmstr, EFF_TYPED | EFF_OWNER)}, UNMAPPED_CACHE(struct realmstr, EFF_TYPED | EFF_OWNER)},
{EF_GAME, "game", "game", game_ca, {EF_GAME, "game", "game", game_ca, EF_BAD,
UNMAPPED_CACHE(struct gamestr, EFF_TYPED)}, UNMAPPED_CACHE(struct gamestr, EFF_TYPED)},
/* /*
@ -164,40 +164,40 @@ struct empfile empfile[] = {
* member are compiled in. The others are empty; use * member are compiled in. The others are empty; use
* read_builtin_tables() to fill them. * read_builtin_tables() to fill them.
*/ */
{EF_ITEM, "item", "item.config", ichr_ca, {EF_ITEM, "item", "item.config", ichr_ca, EF_BAD,
ARRAY_CACHE(ichr, EFF_CFG)}, ARRAY_CACHE(ichr, EFF_CFG)},
{EF_PRODUCT, "product", "product.config", pchr_ca, {EF_PRODUCT, "product", "product.config", pchr_ca, EF_BAD,
ARRAY_CACHE(pchr, EFF_CFG)}, ARRAY_CACHE(pchr, EFF_CFG)},
{EF_SECTOR_CHR, "sect-chr", "sect.config", dchr_ca, {EF_SECTOR_CHR, "sect-chr", "sect.config", dchr_ca, EF_BAD,
ARRAY_CACHE(dchr, EFF_CFG)}, ARRAY_CACHE(dchr, EFF_CFG)},
{EF_SHIP_CHR, "ship-chr", "ship.config", mchr_ca, {EF_SHIP_CHR, "ship-chr", "ship.config", mchr_ca, EF_BAD,
ARRAY_CACHE(mchr, EFF_CFG)}, ARRAY_CACHE(mchr, EFF_CFG)},
{EF_PLANE_CHR, "plane-chr", "plane.config", plchr_ca, {EF_PLANE_CHR, "plane-chr", "plane.config", plchr_ca, EF_BAD,
ARRAY_CACHE(plchr, EFF_CFG)}, ARRAY_CACHE(plchr, EFF_CFG)},
{EF_LAND_CHR, "land-chr", "land.config", lchr_ca, {EF_LAND_CHR, "land-chr", "land.config", lchr_ca, EF_BAD,
ARRAY_CACHE(lchr, EFF_CFG)}, ARRAY_CACHE(lchr, EFF_CFG)},
{EF_NUKE_CHR, "nuke-chr", "nuke.config", nchr_ca, {EF_NUKE_CHR, "nuke-chr", "nuke.config", nchr_ca, EF_BAD,
ARRAY_CACHE(nchr, EFF_CFG)}, ARRAY_CACHE(nchr, EFF_CFG)},
{EF_NEWS_CHR, "news-chr", NULL, rpt_ca, {EF_NEWS_CHR, "news-chr", NULL, rpt_ca, EF_BAD,
ARRAY_TABLE(rpt, EFF_CFG)}, ARRAY_TABLE(rpt, EFF_CFG)},
{EF_INFRASTRUCTURE, "infrastructure", "infra.config", intrchr_ca, {EF_INFRASTRUCTURE, "infrastructure", "infra.config", intrchr_ca, EF_BAD,
ARRAY_CACHE(intrchr, EFF_CFG)}, ARRAY_CACHE(intrchr, EFF_CFG)},
/* /*
* Update schedule table. Use read_schedule() to fill. * Update schedule table. Use read_schedule() to fill.
*/ */
{EF_UPDATES, "updates", NULL, update_ca, {EF_UPDATES, "updates", NULL, update_ca, EF_BAD,
ARRAY_CACHE(update_time, EFF_CFG)}, ARRAY_CACHE(update_time, EFF_CFG)},
/* /*
* Special tables. EF_META gets bogus size, cids and fids here. * Special tables. EF_META gets bogus size, cids and fids here.
* Fixed up by empfile_init(). EF_VERSION's cadef is set by * Fixed up by empfile_init(). EF_VERSION's cadef is set by
* nsc_init(). * nsc_init().
*/ */
{EF_TABLE, "table", NULL, empfile_ca, {EF_TABLE, "table", NULL, empfile_ca, EF_BAD,
ARRAY_TABLE(empfile, EFF_CFG)}, ARRAY_TABLE(empfile, EFF_CFG)},
{EF_VERSION, "version", NULL, NULL, {EF_VERSION, "version", NULL, NULL, EF_BAD,
sizeof(PACKAGE_STRING), EFF_STATIC, version, 1, 0, 1, 1, -1, sizeof(PACKAGE_STRING), EFF_STATIC, version, 1, 0, 1, 1, -1,
NULL, NULL, NULL, NULL}, NULL, NULL, NULL, NULL},
{EF_META, "meta", NULL, mdchr_ca, {EF_META, "meta", NULL, mdchr_ca, EF_BAD,
PTR_CACHE(mdchr_ca, EFF_CFG)}, PTR_CACHE(mdchr_ca, EFF_CFG)},
/* /*
@ -206,8 +206,8 @@ struct empfile empfile[] = {
* These get bogus csize, cids and fids here. Fixed up by * These get bogus csize, cids and fids here. Fixed up by
* empfile_init(). * empfile_init().
*/ */
#define SYMTAB(type, name, tab) \ #define SYMTAB(type, name, tab) {(type), (name), NULL, symbol_ca, EF_BAD, \
{(type), (name), NULL, symbol_ca, PTR_CACHE((tab), EFF_CFG)} PTR_CACHE((tab), EFF_CFG)}
SYMTAB(EF_AGREEMENT_STATUS, "agreement-status", agreement_statuses), SYMTAB(EF_AGREEMENT_STATUS, "agreement-status", agreement_statuses),
SYMTAB(EF_LAND_CHR_FLAGS, "land-chr-flags", land_chr_flags), SYMTAB(EF_LAND_CHR_FLAGS, "land-chr-flags", land_chr_flags),
SYMTAB(EF_LEVEL, "level", level), SYMTAB(EF_LEVEL, "level", level),
@ -231,11 +231,11 @@ struct empfile empfile[] = {
SYMTAB(EF_TREATY_FLAGS, "treaty-flags", treaty_flags), SYMTAB(EF_TREATY_FLAGS, "treaty-flags", treaty_flags),
/* Views */ /* Views */
{EF_COUNTRY, "country", NULL, cou_ca, {EF_COUNTRY, "country", NULL, cou_ca, EF_NATION,
UNMAPPED_CACHE(struct natstr, EFF_TYPED | EFF_OWNER)}, UNMAPPED_CACHE(struct natstr, EFF_TYPED | EFF_OWNER)},
/* Sentinel */ /* Sentinel */
{EF_BAD, NULL, NULL, NULL, {EF_BAD, NULL, NULL, NULL, EF_BAD,
0, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL}, 0, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
}; };

View file

@ -110,7 +110,7 @@ ef_open_srv(void)
failed |= !ef_open(EF_LOST, 0, -1); failed |= !ef_open(EF_LOST, 0, -1);
failed |= !ef_open(EF_REALM, EFF_MEM, MAXNOC * MAXNOR); failed |= !ef_open(EF_REALM, EFF_MEM, MAXNOC * MAXNOR);
if (!failed) if (!failed)
failed |= ef_open_view(EF_COUNTRY, EF_NATION); failed |= ef_open_view(EF_COUNTRY);
if (failed) { if (failed) {
logerror("Missing files, giving up"); logerror("Missing files, giving up");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);