- "n" wird nicht mehr als NAECHSTER erkannt.

also:
- misc fscanf() tests.
- fixing --noreports
This commit is contained in:
Enno Rehling 2008-01-08 20:00:19 +00:00
parent 5a3598c1eb
commit 73f354be90
16 changed files with 137 additions and 84 deletions

View file

@ -52,22 +52,26 @@ a_writegive(const attrib * a, FILE * F)
static int static int
a_readgive(attrib * a, FILE * F) a_readgive(attrib * a, FILE * F)
{ {
give_data * gdata = (give_data*)a->data.v; give_data * gdata = (give_data*)a->data.v;
variant var; variant var;
char zText[32]; int result;
char zText[32];
fscanf(F, "%s ", zText); result = fscanf(F, "%s ", zText);
var.i = atoi36(zText); if (result<0) return result;
gdata->building = findbuilding(var.i); var.i = atoi36(zText);
if (gdata->building==NULL) ur_add(var, (void**)&gdata->building, resolve_building); gdata->building = findbuilding(var.i);
for (;;) { if (gdata->building==NULL) ur_add(var, (void**)&gdata->building, resolve_building);
for (;;) {
int i; int i;
fscanf(F, "%s", zText); result = fscanf(F, "%s", zText);
if (!strcmp("end", zText)) break; if (result==EOF) return EOF;
fscanf(F, "%d", &i); if (!strcmp("end", zText)) break;
if (i==0) i_add(&gdata->items, i_new(it_find(zText), i)); result = fscanf(F, "%d", &i);
} if (result==EOF) return EOF;
return AT_READ_OK; if (i==0) i_add(&gdata->items, i_new(it_find(zText), i));
}
return AT_READ_OK;
} }
static void static void

View file

@ -34,7 +34,8 @@ write_moved(const attrib * a, FILE * F)
static int static int
read_moved(attrib * a, FILE * F) read_moved(attrib * a, FILE * F)
{ {
fscanf(F, "%d", &a->data.i); int result = fscanf(F, "%d", &a->data.i);
if (result<0) return result;
if (a->data.i !=0 ) return AT_READ_OK; if (a->data.i !=0 ) return AT_READ_OK;
else return AT_READ_FAIL; else return AT_READ_FAIL;
} }

View file

@ -27,9 +27,10 @@ write_movement(const attrib * a, FILE * F)
static int static int
read_movement(attrib * a, FILE * F) read_movement(attrib * a, FILE * F)
{ {
fscanf(F, "%d", &a->data.i); int result = fscanf(F, "%d", &a->data.i);
if (a->data.i !=0 ) return AT_READ_OK; if (result<0) return result;
else return AT_READ_FAIL; if (a->data.i !=0 ) return AT_READ_OK;
else return AT_READ_FAIL;
} }
attrib_type at_movement = { attrib_type at_movement = {

View file

@ -88,20 +88,21 @@ static int
object_read(attrib *a, FILE *F) object_read(attrib *a, FILE *F)
{ {
object_data * data = (object_data *)a->data.v; object_data * data = (object_data *)a->data.v;
int type; int type, result;
char buffer[128]; char buffer[128];
fscanf(F, "%s %d", buffer, &type); result = fscanf(F, "%s %d", buffer, &type);
if (result<0) return result;
data->name = strdup(buffer); data->name = strdup(buffer);
data->type = (object_type)type; data->type = (object_type)type;
switch (data->type) { switch (data->type) {
case TINTEGER: case TINTEGER:
fscanf(F, "%d", &data->data.i); result = fscanf(F, "%d", &data->data.i);
break; break;
case TREAL: case TREAL:
fscanf(F, "%lf", &data->data.real); result = fscanf(F, "%lf", &data->data.real);
break; break;
case TSTRING: case TSTRING:
freadstr(F, enc_gamedata, buffer, sizeof(buffer)); result = freadstr(F, enc_gamedata, buffer, sizeof(buffer));
data->data.str = strdup(buffer); data->data.str = strdup(buffer);
break; break;
case TBUILDING: case TBUILDING:
@ -121,6 +122,7 @@ object_read(attrib *a, FILE *F)
default: default:
return AT_READ_FAIL; return AT_READ_FAIL;
} }
if (result<0) return result;
return AT_READ_OK; return AT_READ_OK;
} }

View file

@ -35,12 +35,13 @@ write_of(const struct attrib * a, FILE* F)
int int
read_of(struct attrib * a, FILE* F) /* return 1 on success, 0 if attrib needs removal */ read_of(struct attrib * a, FILE* F) /* return 1 on success, 0 if attrib needs removal */
{ {
int of; int of, result;
fscanf(F, "%d", &of); result = fscanf(F, "%d", &of);
a->data.v = findfaction(of); if (result<0) return result;
if (a->data.v) return AT_READ_OK; a->data.v = findfaction(of);
return AT_READ_FAIL; if (a->data.v) return AT_READ_OK;
return AT_READ_FAIL;
} }
attrib_type at_otherfaction = { attrib_type at_otherfaction = {

View file

@ -51,15 +51,17 @@ write_variable(const struct attrib * a, FILE *F)
static int static int
read_variable(struct attrib *a, FILE *F) read_variable(struct attrib *a, FILE *F)
{ {
char localBuffer[1024]; char localBuffer[1024];
fscanf(F, "%s", localBuffer); int result = fscanf(F, "%s", localBuffer);
((variable *)(a->data.v))->key = strdup(localBuffer); if (result<0) return result;
((variable *)(a->data.v))->key = strdup(localBuffer);
freadstr(F, enc_gamedata, localBuffer, sizeof(localBuffer)); result = freadstr(F, enc_gamedata, localBuffer, sizeof(localBuffer));
((variable *)(a->data.v))->value = strdup(localBuffer); if (result<0) return result;
((variable *)(a->data.v))->value = strdup(localBuffer);
return AT_READ_OK; return AT_READ_OK;
} }
attrib_type at_variable = { attrib_type at_variable = {

View file

@ -33,7 +33,8 @@ a_readfunction(struct attrib *a, FILE *F)
/* return 1 on success, 0 if attrib needs removal */ /* return 1 on success, 0 if attrib needs removal */
{ {
char buf[64]; char buf[64];
fscanf(F, "%s ", buf); int result = fscanf(F, "%s ", buf);
if (result<0) return result;
a->data.f = get_function(buf); a->data.f = get_function(buf);
return AT_READ_OK; return AT_READ_OK;
} }

View file

@ -1559,7 +1559,7 @@ creport_init(void)
tsf_register("items", &cr_resources); tsf_register("items", &cr_resources);
tsf_register("regions", &cr_regions); tsf_register("regions", &cr_regions);
register_reporttype("cr", &report_computer, 1<<O_COMPUTER); if (!nocr) register_reporttype("cr", &report_computer, 1<<O_COMPUTER);
} }
void void

View file

@ -2446,8 +2446,8 @@ kann_finden(faction * f1, faction * f2)
void void
report_init(void) report_init(void)
{ {
register_reporttype("nr", &report_plaintext, 1<<O_REPORT); if (!nocr) register_reporttype("nr", &report_plaintext, 1<<O_REPORT);
register_reporttype("txt", &report_template, 1<<O_ZUGVORLAGE); if (!nonr) register_reporttype("txt", &report_template, 1<<O_ZUGVORLAGE);
} }
void void

View file

@ -181,18 +181,20 @@ a_writeeffect(const attrib *a, FILE *f)
static int static int
a_readeffect(attrib *a, FILE *f) a_readeffect(attrib *a, FILE *f)
{ {
int power; int power, result;
const item_type * itype; const item_type * itype;
effect_data * edata = (effect_data*)a->data.v; effect_data * edata = (effect_data*)a->data.v;
char zText[32]; char zText[32];
fscanf(f, "%s %d", zText, &power);
itype = it_find(zText); result = fscanf(f, "%s %d", zText, &power);
if (result<0) return result;
itype = it_find(zText);
if (itype==NULL || itype->rtype==NULL || itype->rtype->ptype==NULL || power<=0) { if (itype==NULL || itype->rtype==NULL || itype->rtype->ptype==NULL || power<=0) {
return AT_READ_FAIL; return AT_READ_FAIL;
} }
edata->type = itype->rtype->ptype; edata->type = itype->rtype->ptype;
edata->value = power; edata->value = power;
return AT_READ_OK; return AT_READ_OK;
} }
attrib_type at_effect = { attrib_type at_effect = {

View file

@ -164,20 +164,23 @@ find_bordertype(const char * name)
void void
b_read(border * b, FILE *f) b_read(border * b, FILE *f)
{ {
int result;
switch (b->type->datatype) { switch (b->type->datatype) {
case VAR_NONE: case VAR_NONE:
case VAR_INT: case VAR_INT:
fscanf(f, "%x ", &b->data.i); result = fscanf(f, "%x ", &b->data.i);
break; break;
case VAR_VOIDPTR: case VAR_VOIDPTR:
fscanf(f, "%p ", &b->data.v); result = fscanf(f, "%p ", &b->data.v);
break; break;
case VAR_SHORTA: case VAR_SHORTA:
fscanf(f, "%hd %hd ", &b->data.sa[0], &b->data.sa[1]); result = fscanf(f, "%hd %hd ", &b->data.sa[0], &b->data.sa[1]);
break; break;
default: default:
assert(!"unhandled variant type in border"); assert(!"unhandled variant type in border");
result = 0;
} }
assert(result>=0 || "EOF encountered?");
} }
void void
@ -531,7 +534,7 @@ write_borders(FILE * f)
fputs("end ", f); fputs("end ", f);
} }
void int
read_borders(FILE * f) read_borders(FILE * f)
{ {
for (;;) { for (;;) {
@ -541,10 +544,13 @@ read_borders(FILE * f)
border * b; border * b;
region * from, * to; region * from, * to;
border_type * type; border_type * type;
int result;
fscanf(f, "%s", zText); result = fscanf(f, "%s", zText);
if (result<0) return result;
if (!strcmp(zText, "end")) break; if (!strcmp(zText, "end")) break;
fscanf(f, "%u %hd %hd %hd %hd", &bid, &fx, &fy, &tx, &ty); result = fscanf(f, "%u %hd %hd %hd %hd", &bid, &fx, &fy, &tx, &ty);
if (result<0) return result;
from = findregion(fx, fy); from = findregion(fx, fy);
if (!incomplete_data && from==NULL) { if (!incomplete_data && from==NULL) {
@ -574,9 +580,11 @@ read_borders(FILE * f)
b->id = bid; b->id = bid;
assert(bid<=nextborder); assert(bid<=nextborder);
if (type->read) type->read(b, f); if (type->read) type->read(b, f);
a_read(f, &b->attribs); result = a_read(f, &b->attribs);
if (result<0) return result;
if (!to || !from) { if (!to || !from) {
erase_border(b); erase_border(b);
} }
} }
return 0;
} }

View file

@ -101,7 +101,7 @@ extern "C" {
extern void register_bordertype(border_type * type); extern void register_bordertype(border_type * type);
/* register a new bordertype */ /* register a new bordertype */
extern void read_borders(FILE * f); extern int read_borders(FILE * f);
extern void write_borders(FILE * f); extern void write_borders(FILE * f);
extern void age_borders(void); extern void age_borders(void);

View file

@ -1216,6 +1216,9 @@ write_reports(faction * f, time_t ltime)
struct report_context ctx; struct report_context ctx;
const char * encoding = "UTF-8"; const char * encoding = "UTF-8";
if (noreports) {
return false;
}
ctx.f = f; ctx.f = f;
ctx.report_time = time(NULL); ctx.report_time = time(NULL);
ctx.seen = prepare_report(f); ctx.seen = prepare_report(f);

View file

@ -431,6 +431,11 @@ unitorders(FILE * F, int enc, struct faction * f)
i = getid(); i = getid();
u = findunitg(i, NULL); u = findunitg(i, NULL);
if (quiet==0) {
log_stdio(stdout, "-%4s;", unitid(u));
fflush(stdout);
}
if (u && u->race == new_race[RC_SPELL]) return NULL; if (u && u->race == new_race[RC_SPELL]) return NULL;
if (u && u->faction == f) { if (u && u->faction == f) {
order ** ordp; order ** ordp;
@ -472,13 +477,22 @@ unitorders(FILE * F, int enc, struct faction * f)
if (stok) { if (stok) {
boolean quit = false; boolean quit = false;
switch (findparam(stok, u->faction->locale)) { param_t param = findparam(stok, u->faction->locale);
switch (param) {
case P_UNIT: case P_UNIT:
case P_REGION: case P_REGION:
quit = true;
break;
case P_FACTION: case P_FACTION:
case P_NEXT: case P_NEXT:
case P_GAMENAME: case P_GAMENAME:
quit = true; /* these terminate the orders, so we apply extra checking */
if (strlen(stok)>=3) {
quit = true;
break;
} else {
quit = false;
}
} }
if (quit) break; if (quit) break;
} }
@ -1021,7 +1035,9 @@ writeunit(FILE * F, const unit * u)
wi36(F, u->no); wi36(F, u->no);
wi36(F, u->faction->no); wi36(F, u->faction->no);
fwritestr(F, (const char *)u->name); fwritestr(F, (const char *)u->name);
fputc(' ', F);
fwritestr(F, u->display?(const char *)u->display:""); fwritestr(F, u->display?(const char *)u->display:"");
fputc(' ', F);
wi(F, u->number); wi(F, u->number);
wi(F, u->age); wi(F, u->age);
ws(F, u->race->_name[0]); ws(F, u->race->_name[0]);
@ -1243,6 +1259,7 @@ void
writeregion(FILE * F, const region * r) writeregion(FILE * F, const region * r)
{ {
fwritestr(F, r->display?(const char *)r->display:""); fwritestr(F, r->display?(const char *)r->display:"");
fputc(' ', F);
ws(F, r->terrain->_name); ws(F, r->terrain->_name);
wi(F, r->flags & RF_SAVEMASK); wi(F, r->flags & RF_SAVEMASK);
wi(F, r->age); wi(F, r->age);
@ -1252,6 +1269,7 @@ writeregion(FILE * F, const region * r)
struct demand * demand; struct demand * demand;
rawmaterial * res = r->resources; rawmaterial * res = r->resources;
fwritestr(F, (const char *)r->land->name); fwritestr(F, (const char *)r->land->name);
fputc(' ', F);
assert(rtrees(r,0)>=0); assert(rtrees(r,0)>=0);
assert(rtrees(r,1)>=0); assert(rtrees(r,1)>=0);
assert(rtrees(r,2)>=0); assert(rtrees(r,2)>=0);
@ -1462,7 +1480,9 @@ writefaction(FILE * F, const faction * f)
} }
fwritestr(F, (const char *)f->name); fwritestr(F, (const char *)f->name);
fputc(' ', F);
fwritestr(F, (const char *)f->banner); fwritestr(F, (const char *)f->banner);
fputc(' ', F);
ws(F, f->email); ws(F, f->email);
ws(F, (const char *)f->passw); ws(F, (const char *)f->passw);
ws(F, (const char *)f->override); ws(F, (const char *)f->override);
@ -1866,6 +1886,7 @@ writegame(const char *filename, int quiet)
watcher * w; watcher * w;
wi(F, pl->id); wi(F, pl->id);
fwritestr(F, pl->name); fwritestr(F, pl->name);
fputc(' ', F);
wi(F, pl->minx); wi(F, pl->minx);
wi(F, pl->maxx); wi(F, pl->maxx);
wi(F, pl->miny); wi(F, pl->miny);
@ -1924,7 +1945,9 @@ writegame(const char *filename, int quiet)
for (b = r->buildings; b; b = b->next) { for (b = r->buildings; b; b = b->next) {
wi36(F, b->no); wi36(F, b->no);
fwritestr(F, b->name); fwritestr(F, b->name);
fputc(' ', F);
fwritestr(F, b->display?b->display:""); fwritestr(F, b->display?b->display:"");
fputc(' ', F);
wi(F, b->size); wi(F, b->size);
ws(F, b->type->_name); ws(F, b->type->_name);
wnl(F); wnl(F);
@ -1938,7 +1961,9 @@ writegame(const char *filename, int quiet)
assert(sh->region == r); assert(sh->region == r);
wi36(F, sh->no); wi36(F, sh->no);
fwritestr(F, (const char *)sh->name); fwritestr(F, (const char *)sh->name);
fputc(' ', F);
fwritestr(F, sh->display?(const char *)sh->display:""); fwritestr(F, sh->display?(const char *)sh->display:"");
fputc(' ', F);
ws(F, sh->type->name[0]); ws(F, sh->type->name[0]);
wi(F, sh->size); wi(F, sh->size);
wi(F, sh->damage); wi(F, sh->damage);

View file

@ -38,33 +38,35 @@ write_triggers(FILE * F, const trigger * t)
fputs("end ", F); fputs("end ", F);
} }
void int
read_triggers(FILE * F, trigger ** tp) read_triggers(FILE * F, trigger ** tp)
{ {
for (;;) { for (;;) {
trigger_type * ttype; trigger_type * ttype;
char zText[128]; char zText[128];
fscanf(F, "%s", zText); int result = fscanf(F, "%s", zText);
if (!strcmp(zText, "end")) break; if (result<0) return result;
ttype = tt_find(zText); if (!strcmp(zText, "end")) break;
assert(ttype || !"unknown trigger-type"); ttype = tt_find(zText);
*tp = t_new(ttype); assert(ttype || !"unknown trigger-type");
if (ttype->read) { *tp = t_new(ttype);
int i = ttype->read(*tp, F); if (ttype->read) {
switch (i) { int i = ttype->read(*tp, F);
case AT_READ_OK: switch (i) {
tp = &(*tp)->next; case AT_READ_OK:
break; tp = &(*tp)->next;
case AT_READ_FAIL: break;
t_free(*tp); case AT_READ_FAIL:
*tp = NULL; t_free(*tp);
break; *tp = NULL;
default: break;
assert(!"invalid return value"); default:
break; assert(!"invalid return value");
} break;
} }
} }
}
return 0;
} }
trigger * trigger *
@ -142,7 +144,8 @@ read_handler(attrib * a, FILE * F)
{ {
char zText[128]; char zText[128];
handler_info *hi = (handler_info*)a->data.v; handler_info *hi = (handler_info*)a->data.v;
fscanf(F, "%s ", zText); int result = fscanf(F, "%s", zText);
if (result<0) return result;
hi->event = strdup(zText); hi->event = strdup(zText);
read_triggers(F, &hi->triggers); read_triggers(F, &hi->triggers);
if (hi->triggers!=NULL) { if (hi->triggers!=NULL) {

View file

@ -63,7 +63,7 @@ extern void handle_event(struct attrib * attribs, const char * eventname, void *
/* functions for making complex triggers: */ /* functions for making complex triggers: */
extern void free_triggers(trigger * triggers); /* release all these triggers */ extern void free_triggers(trigger * triggers); /* release all these triggers */
extern void write_triggers(FILE * F, const trigger * t); extern void write_triggers(FILE * F, const trigger * t);
extern void read_triggers(FILE * F, trigger ** tp); extern int read_triggers(FILE * F, trigger ** tp);
extern int handle_triggers(trigger ** triggers, void * data); extern int handle_triggers(trigger ** triggers, void * data);
extern struct attrib_type at_eventhandler; extern struct attrib_type at_eventhandler;