xundump: Eliminate global cur_obj

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
Markus Armbruster 2014-02-08 19:28:30 +01:00
parent d81c8c9463
commit 708b8d49d0

View file

@ -69,7 +69,6 @@ static int lineno; /* Current line number */
static int cur_type; /* Current table's file type */ static int cur_type; /* Current table's file type */
static int partno; /* Counts from 0..#parts-1 */ static int partno; /* Counts from 0..#parts-1 */
static void *cur_obj; /* The object being read into */
static int cur_id; /* and its index in the table */ static int cur_id; /* and its index in the table */
static int old_nelem; static int old_nelem;
static unsigned char *idgap; /* idgap && idgap[ID] iff part#0 lacks ID */ static unsigned char *idgap; /* idgap && idgap[ID] iff part#0 lacks ID */
@ -158,7 +157,6 @@ tbl_start(int type)
cur_type = type; cur_type = type;
partno = 0; partno = 0;
cur_id = -1; cur_id = -1;
cur_obj = NULL;
old_nelem = type == EF_BAD ? 0 : ef_nelem(type); old_nelem = type == EF_BAD ? 0 : ef_nelem(type);
idgap = NULL; idgap = NULL;
idgap_len = 0; idgap_len = 0;
@ -173,26 +171,29 @@ tbl_end(void)
} }
/* /*
* Seek to current table's ID-th record. * Seek to current table's ID-th object.
* ID must be acceptable. * Extend the table if necessary.
* Store it in cur_obj, and set cur_id accordingly. * Save ID in cur_id.
* Return 0 on success, -1 on failure. * Return the object on success, NULL on failure.
*/ */
static int static void *
tbl_seek(int id) tbl_seek(int id)
{ {
struct empfile *ep = &empfile[cur_type]; struct empfile *ep = &empfile[cur_type];
void *obj;
if (id >= ef_nelem(cur_type)) { if (id >= ef_nelem(cur_type)) {
if (!ef_ensure_space(cur_type, id, 1)) if (!ef_ensure_space(cur_type, id, 1)) {
return gripe("Can't put ID %d into table %s", id, ep->name); gripe("Can't put ID %d into table %s", id, ep->name);
return NULL;
}
} }
cur_obj = ef_ptr(cur_type, id); obj = ef_ptr(cur_type, id);
if (CANT_HAPPEN(!cur_obj)) if (CANT_HAPPEN(!obj))
return -1; return NULL;
cur_id = id; cur_id = id;
return 0; return obj;
} }
/* /*
@ -263,7 +264,6 @@ tbl_part_done(void)
partno++; partno++;
cur_id = -1; cur_id = -1;
cur_obj = NULL;
return 0; return 0;
} }
@ -344,33 +344,38 @@ rowid(void)
/* /*
* Get the current row's object. * Get the current row's object.
* Store it in cur_obj, and set cur_id accordingly. * Extend the table if necessary.
* Return 0 on success, -1 on failure. * Save ID in cur_id.
* Return the object on success, NULL on failure.
*/ */
static int static void *
rowobj(void) rowobj(void)
{ {
int last_id = cur_id; int last_id = cur_id;
int id; int id;
void *obj;
if (partno) { if (partno) {
id = expected_id(cur_id + 1, empfile[cur_type].fids); id = expected_id(cur_id + 1, empfile[cur_type].fids);
if (id < 0) if (id < 0) {
return gripe("Table's first part doesn't have this row"); gripe("Table's first part doesn't have this row");
return NULL;
}
} else if (ca0_is_id(cur_type)) { } else if (ca0_is_id(cur_type)) {
id = rowid(); id = rowid();
if (id < 0) if (id < 0)
return -1; return NULL;
} else } else
id = last_id + 1; id = last_id + 1;
if (id > ef_id_limit(cur_type)) if (id > ef_id_limit(cur_type)) {
return gripe("Too many rows"); gripe("Too many rows");
if (tbl_seek(id) < 0) return NULL;
return -1; }
if (!partno) obj = tbl_seek(id);
if (obj && !partno)
omit_ids(last_id + 1, id); omit_ids(last_id + 1, id);
return 0; return obj;
} }
/* /*
@ -541,17 +546,19 @@ static int
putrow(void) putrow(void)
{ {
int i, ret = 0; int i, ret = 0;
void *obj;
if (rowobj() < 0) obj = rowobj();
if (!obj)
return -1; return -1;
for (i = 0; i < nflds; i++) { for (i = 0; i < nflds; i++) {
switch (fldval[i].val_type) { switch (fldval[i].val_type) {
case NSC_DOUBLE: case NSC_DOUBLE:
ret |= putnum(cur_obj, i, fldval[i].val_as.dbl); ret |= putnum(obj, i, fldval[i].val_as.dbl);
break; break;
case NSC_STRING: case NSC_STRING:
ret |= putstr(cur_obj, i, fldval[i].val_as.str.base); ret |= putstr(obj, i, fldval[i].val_as.str.base);
free(fldval[i].val_as.str.base); free(fldval[i].val_as.str.base);
break; break;
default: default: