static int building_action_read(variant *var, void *owner, gamedata *data) { struct storage *store = data->store; UNUSED_ARG(owner); UNUSED_ARG(var); if (data->version < ATTRIBOWNER_VERSION) { READ_INT(data->store, NULL); } READ_TOK(store, NULL, 0); READ_TOK(store, NULL, 0); return AT_READ_DEPR; }
static int createcurse_read(trigger * t, gamedata *data) { createcurse_data *td = (createcurse_data *)t->data.v; char zText[128]; float flt; read_reference(&td->mage, data, read_unit_reference, resolve_unit); read_reference(&td->target, data, read_unit_reference, resolve_unit); READ_TOK(data->store, zText, sizeof(zText)); td->type = ct_find(zText); READ_FLT(data->store, &flt); td->vigour = flt; READ_INT(data->store, &td->duration); if (data->version < CURSEFLOAT_VERSION) { int n; READ_INT(data->store, &n); td->effect = (float)n; } else { READ_FLT(data->store, &flt); td->effect = flt; } READ_INT(data->store, &td->men); return AT_READ_OK; }
int read_triggers(struct storage *store, trigger ** tp) { for (;;) { trigger_type *ttype; char zText[128]; READ_TOK(store, zText, sizeof(zText)); if (!strcmp(zText, "end")) break; ttype = tt_find(zText); assert(ttype || !"unknown trigger-type"); *tp = t_new(ttype); if (ttype->read) { int i = ttype->read(*tp, store); switch (i) { case AT_READ_OK: tp = &(*tp)->next; break; case AT_READ_FAIL: t_free(*tp); *tp = NULL; break; default: assert(!"invalid return value"); break; } } } return 0; }
static int a_readdirection(attrib * a, void *owner, struct gamedata *data) { struct storage *store = data->store; spec_direction *d = (spec_direction *)(a->data.v); char lbuf[32]; unused_arg(owner); READ_INT(store, &d->x); READ_INT(store, &d->y); READ_INT(store, &d->duration); READ_TOK(store, lbuf, sizeof(lbuf)); d->desc = _strdup(lbuf); READ_TOK(store, lbuf, sizeof(lbuf)); d->keyword = _strdup(lbuf); d->active = true; return AT_READ_OK; }
static int read_gmcreate(attrib * a, void *owner, struct storage *store) { char zText[32]; READ_TOK(store, zText, sizeof(zText)); a->data.v = it_find(zText); if (a->data.v == NULL) { log_error("unknown itemtype %s in gmcreate attribute\n", zText); return AT_READ_FAIL; } return AT_READ_OK; }
static int read_handler(attrib * a, void *owner, struct storage *store) { char zText[128]; handler_info *hi = (handler_info *)a->data.v; READ_TOK(store, zText, sizeof(zText)); hi->event = _strdup(zText); read_triggers(store, &hi->triggers); if (hi->triggers != NULL) { return AT_READ_OK; } return AT_READ_FAIL; }
variant read_race_reference(struct storage *store) { variant result; char zName[20]; READ_TOK(store, zName, sizeof(zName)); if (strcmp(zName, "none") == 0) { result.v = NULL; return result; } else { result.v = rc_find_i(zName); } assert(result.v != NULL); return result; }
static int lc_read(struct attrib *a, void *owner, gamedata *data) { struct storage *store = data->store; char name[NAMESIZE]; building_action *bd = (building_action *)a->data.v; building *b = (building *)owner; int result = 0; if (data->version < ATTRIBOWNER_VERSION) { result = read_reference(&b, data, read_building_reference, resolve_building); assert(b == owner); } READ_TOK(store, name, sizeof(name)); if (strcmp(name, "tunnel_action") == 0) { /* E2: Weltentor has a new module, doesn't need this any longer */ result = 0; b = 0; } else { bd->fname = _strdup(name); } READ_TOK(store, name, sizeof(name)); if (strcmp(name, "tnnL") == 0) { /* tunnel_action was the old Weltentore, their code has changed. ignore this object */ result = 0; b = 0; } if (strcmp(name, NULLSTRING) == 0) bd->param = 0; else { bd->param = _strdup(name); } if (result == 0 && !b) { return AT_READ_FAIL; } return AT_READ_OK; }
static int unitmessage_read(trigger * t, struct storage *store) { unitmessage_data *td = (unitmessage_data *) t->data.v; char zText[256]; int result = read_reference(&td->target, store, read_unit_reference, resolve_unit); READ_TOK(store, zText, sizeof(zText)); td->string = _strdup(zText); READ_INT(store, &td->type); READ_INT(store, &td->level); if (result == 0 && td->target == NULL) { return AT_READ_FAIL; } return AT_READ_OK; }
static int giveitem_read(trigger * t, gamedata *data) { giveitem_data *td = (giveitem_data *)t->data.v; char zText[128]; int result; result = read_unit_reference(data, &td->u, NULL); READ_INT(data->store, &td->number); READ_TOK(data->store, zText, sizeof(zText)); td->itype = it_find(zText); assert(td->itype); if (result == 0) { return AT_READ_FAIL; } return AT_READ_OK; }
static int a_readeffect(attrib * a, void *owner, struct storage *store) { int power; const resource_type *rtype; effect_data *edata = (effect_data *) a->data.v; char zText[32]; READ_TOK(store, zText, sizeof(zText)); rtype = rt_find(zText); READ_INT(store, &power); if (rtype == NULL || rtype->ptype == NULL || power <= 0) { return AT_READ_FAIL; } edata->type = rtype->ptype; edata->value = power; return AT_READ_OK; }
static void test_memstream(CuTest *tc) { storage store; stream out = { 0 }; char buf[1024]; int val=0; mstream_init(&out); binstore_init(&store, &out); store.handle.data = &out; WRITE_INT(&store, 999999); WRITE_TOK(&store, "fortytwo"); WRITE_INT(&store, 42); out.api->rewind(out.handle); READ_INT(&store, &val); READ_TOK(&store, buf, 1024); CuAssertIntEquals(tc, 999999, val); CuAssertStrEquals(tc, "fortytwo", buf); READ_INT(&store, &val); CuAssertIntEquals(tc, 42, val); mstream_done(&out); }
/** reads curses that have been removed from the code */ static int read_ccompat(const char *cursename, struct storage *store) { struct compat { const char *name; const char *tokens; } *seek, old_curses[] = { { "disorientationzone", ""}, { "shipdisorientation", ""}, { NULL, NULL}}; for (seek = old_curses; seek->name; ++seek) { if (strcmp(seek->tokens, cursename) == 0) { const char *p; for (p = seek->name; p; ++p) { switch (*p) { case 'd': READ_INT(store, 0); break; case 's': READ_STR(store, 0, 0); break; case 't': READ_TOK(store, 0, 0); break; case 'i': READ_INT(store, 0); break; case 'f': READ_FLT(store, 0); break; } } return 0; } } return -1; }
int read_borders(gamedata *data) { struct storage *store = data->store; for (;;) { int bid = 0; char zText[32]; region *from, *to; border_type *type; READ_TOK(store, zText, sizeof(zText)); if (!strcmp(zText, "end")) break; READ_INT(store, &bid); if (data->version < UIDHASH_VERSION) { int fx, fy, tx, ty; READ_INT(store, &fx); READ_INT(store, &fy); READ_INT(store, &tx); READ_INT(store, &ty); from = findregion(fx, fy); to = findregion(tx, ty); } else { int fid, tid; READ_INT(store, &fid); READ_INT(store, &tid); from = findregionbyid(fid); to = findregionbyid(tid); if (!to || !from) { log_warning("%s connection between incomplete regions %d and %d", zText, fid, tid); continue; } } type = find_bordertype(zText); if (type == NULL) { log_error("[read_borders] unknown connection type '%s' in %s\n", zText, regionname(from, NULL)); assert(type || !"connection type not registered"); } if (to == from && type && from) { direction_t dir = (direction_t)(rng_int() % MAXDIRECTIONS); region *r = rconnect(from, dir); log_error("[read_borders] invalid %s in %s\n", type->__name, regionname(from, NULL)); if (r != NULL) to = r; } if ((type->read && !type->write)) { log_warning("ignore invalid border '%s' between '%s' and '%s'\n", zText, regionname(from, 0), regionname(to, 0)); } else { connection *b = new_border(type, from, to); nextborder--; /* new_border erhöht den Wert */ b->id = bid; assert(bid <= nextborder); if (type->read) { type->read(b, data); } if (data->version < NOBORDERATTRIBS_VERSION) { attrib *a = NULL; int result = read_attribs(data, &a, b); if (border_convert_cb) { border_convert_cb(b, a); } while (a) { a_remove(&a, a); } if (result < 0) { return result; } } } } return 0; }
int curse_read(attrib * a, void *owner, struct storage *store) { curse *c = (curse *) a->data.v; int ur; char cursename[64]; int n; int flags; float flt; READ_INT(store, &c->no); chash(c); READ_TOK(store, cursename, sizeof(cursename)); READ_INT(store, &flags); READ_INT(store, &c->duration); if (global.data_version >= CURSEVIGOURISFLOAT_VERSION) { READ_FLT(store, &flt); c->vigour = flt; } else { READ_INT(store, &n); c->vigour = (float)n; } if (global.data_version < INTPAK_VERSION) { ur = read_reference(&c->magician, store, read_int, resolve_unit); } else { ur = read_reference(&c->magician, store, read_unit_reference, resolve_unit); } if (global.data_version < CURSEFLOAT_VERSION) { READ_INT(store, &n); c->effect = (float)n; } else { READ_FLT(store, &flt); c->effect = flt; } c->type = ct_find(cursename); if (c->type == NULL) { int result = read_ccompat(cursename, store); if (result != 0) { log_error("missing curse %s, no compatibility code either.\n", cursename); } assert(result == 0); return AT_READ_FAIL; } if (global.data_version < CURSEFLAGS_VERSION) { c_setflag(c, flags); } else { c->flags = flags; } c_clearflag(c, CURSE_ISNEW); if (c->type->read) c->type->read(store, c, owner); else if (c->type->typ == CURSETYP_UNIT) { READ_INT(store, &c->data.i); } if (c->type->typ == CURSETYP_REGION) { int rr = read_reference(&c->data.v, store, read_region_reference, RESOLVE_REGION(global.data_version)); if (ur == 0 && rr == 0 && !c->data.v) { return AT_READ_FAIL; } } return AT_READ_OK; }
static int read_gmcreate(attrib * a, void *owner, struct gamedata *data) { char zText[32]; READ_TOK(data->store, zText, sizeof(zText)); return AT_READ_OK; }
void ConvolutionMatrixStock::reloadStock() { #define READ_TOK() { \ if (!tok_read(f, buf, leavings, sizeof (leavings))) \ break; \ } #define READ_INT(var) { \ READ_TOK(); \ var = strtol(buf, NULL, 10); \ } const char *names[] = { "convmatr.usr", "convmatr.gen", "convmatr.def", NULL }; char *s, buf[256], leavings[4096]; int i, c, x, y, w, h, div, bias; SharedPtr<ConvolutionMatrix> matrix; base::UniquePtr<FILE, int(*)(FILE*)> f; std::string name; cleanStock(); for (i=0; names[i]; i++) { ResourceFinder rf; rf.findInDataDir(names[i]); while (const char* path = rf.next()) { // Open matrices stock file f.reset(fopen(path, "r"), fclose); if (!f) continue; tok_reset_line_num(); strcpy(leavings, ""); // Read the matrix name while (tok_read(f, buf, leavings, sizeof(leavings))) { // Name of the matrix name = buf; // Width and height READ_INT(w); READ_INT(h); if ((w <= 0) || (w > 32) || (h <= 0) || (h > 32)) break; // Create the matrix data matrix.reset(new ConvolutionMatrix(w, h)); matrix->setName(name.c_str()); // Centre READ_INT(x); READ_INT(y); if ((x < 0) || (x >= w) || (y < 0) || (y >= h)) break; matrix->setCenterX(x); matrix->setCenterY(y); // Data READ_TOK(); // Jump the `{' char if (*buf != '{') break; c = 0; div = 0; for (y=0; y<h; ++y) { for (x=0; x<w; ++x) { READ_TOK(); int value = strtod(buf, NULL) * ConvolutionMatrix::Precision; div += value; matrix->value(x, y) = value; } } READ_TOK(); // Jump the `}' char if (*buf != '}') break; if (div > 0) bias = 0; else if (div == 0) { div = ConvolutionMatrix::Precision; bias = 128; } else { div = ABS(div); bias = 255; } // Div READ_TOK(); if (strcmp(buf, "auto") != 0) div = strtod(buf, NULL) * ConvolutionMatrix::Precision; matrix->setDiv(div); // Bias READ_TOK(); if (strcmp(buf, "auto") != 0) bias = strtod(buf, NULL); matrix->setBias(bias); // Target READ_TOK(); Target target = 0; for (s=buf; *s; s++) { switch (*s) { case 'r': target |= TARGET_RED_CHANNEL; break; case 'g': target |= TARGET_GREEN_CHANNEL; break; case 'b': target |= TARGET_BLUE_CHANNEL; break; case 'a': target |= TARGET_ALPHA_CHANNEL; break; } } if ((target & (TARGET_RED_CHANNEL | TARGET_GREEN_CHANNEL | TARGET_BLUE_CHANNEL)) != 0) { target |= TARGET_GRAY_CHANNEL; } matrix->setDefaultTarget(target); // Insert the new matrix in the list m_matrices.push_back(matrix); } } } }
static int read_gmcreate(attrib * a, void *owner, struct storage *store) { char zText[32]; READ_TOK(store, zText, sizeof(zText)); return AT_READ_OK; }
int a_read(struct storage *store, attrib ** attribs, void *owner) { int key, retval = AT_READ_OK; char zText[128]; zText[0] = 0; key = -1; READ_TOK(store, zText, sizeof(zText)); if (strcmp(zText, "end") == 0) return retval; else key = __at_hashkey(zText); while (key != -1) { int(*reader)(attrib *, void *, struct storage *) = 0; attrib_type *at = at_find(key); attrib * na = 0; if (at) { reader = at->read; na = a_new(at); } else { void * kv = 0; cb_find_prefix(&cb_deprecated, zText, strlen(zText) + 1, &kv, 1, 0); if (kv) { cb_get_kv(kv, &reader, sizeof(reader)); } else { fprintf(stderr, "attribute hash: %d (%s)\n", key, zText); assert(at || !"attribute not registered"); } } if (reader) { int i = reader(na, owner, store); if (na) { switch (i) { case AT_READ_OK: a_add(attribs, na); break; case AT_READ_FAIL: retval = AT_READ_FAIL; a_free(na); break; default: assert(!"invalid return value"); break; } } } else { assert(!"error: no registered callback can read attribute"); } READ_TOK(store, zText, sizeof(zText)); if (!strcmp(zText, "end")) break; key = __at_hashkey(zText); } return retval; }