From 6118371c0edcb29a37a6f2292641dc1a341e6528 Mon Sep 17 00:00:00 2001 From: Per Inge Mathisen Date: Mon, 10 Dec 2007 21:48:27 +0000 Subject: [PATCH] Add methods to read/write byte arrays to tagfile format. git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@3036 4a71c877-e1ca-e34f-864e-861f7616d084 --- data/testdata/tagfile_basic.def | 1 + data/testdata/tagfile_map.def | 6 ++++ lib/framework/tagfile.c | 58 +++++++++++++++++++++++++++++++++ lib/framework/tagfile.h | 2 ++ 4 files changed, 67 insertions(+) diff --git a/data/testdata/tagfile_basic.def b/data/testdata/tagfile_basic.def index 4fcdf7820..dc31e014e 100644 --- a/data/testdata/tagfile_basic.def +++ b/data/testdata/tagfile_basic.def @@ -10,6 +10,7 @@ 02 US 03 - # Droid position in (x,y,z) coordinates 03 FP 01 - # RETIRED (had unknown usage) 04 FP 03 - # RETIRED (had unknown usage) + 05 US 500 - # Long unsigned byte array 09 GR 00 - # Droid weapon group 01 US 01 - # Weapon type 02 US 01 10 # Ammo left diff --git a/data/testdata/tagfile_map.def b/data/testdata/tagfile_map.def index 692772554..56a872872 100644 --- a/data/testdata/tagfile_map.def +++ b/data/testdata/tagfile_map.def @@ -212,3 +212,9 @@ 04 SI 01 -1 # Object being rearmed, if any, -1 if none FF EN 00 - FF EN 00 - +10 GR 00 - # GATEWAY ZONE LINES + 01 US 01 - # Ugly binary blob of data. Someone please decode me! + FF EN 00 - +11 GR 00 - # GATEWAY ZONES + 01 US 01 - # Ugly binary blob of data. Someone please decode me! + FF EN 00 - diff --git a/lib/framework/tagfile.c b/lib/framework/tagfile.c index 188ab772f..2af6a4a3f 100644 --- a/lib/framework/tagfile.c +++ b/lib/framework/tagfile.c @@ -753,6 +753,37 @@ bool tagReadfv(element_t tag, uint16_t size, float *vals) return true; } +bool tagRead8v(element_t tag, uint16_t size, uint8_t *vals) +{ + uint8_t tagtype; + uint16_t count; + int i; + + if (!scanforward(tag)) + { + return false; + } + if (!PHYSFS_readUBE8(handle, &tagtype) || tagtype != TF_INT_U8_ARRAY) + { + TF_ERROR("tagread8v: Tag type not found: %x", (unsigned int)tag); + return false; + } + if (!PHYSFS_readUBE16(handle, &count) || count != size) + { + TF_ERROR("tagread8v: Bad size: %x", (unsigned int)tag); + return false; + } + for (i = 0; i < size; i++) + { + if (!PHYSFS_readUBE8(handle, &vals[i])) + { + TF_ERROR("tagread8v: Error reading idx %d, tag %x", i, (unsigned int)tag); + return false; + } + } + return true; +} + bool tagRead16v(element_t tag, uint16_t size, uint16_t *vals) { uint8_t tagtype; @@ -1105,6 +1136,25 @@ bool tagWritefv(element_t tag, uint16_t count, float *vals) return true; } +bool tagWrite8v(element_t tag, uint16_t count, uint8_t *vals) +{ + int i; + + assert(tag != TAG_SEPARATOR && tag != TAG_GROUP_END); + if (!scan_to(tag) || !write_tag(tag)) + { + return false; + } + ASSERT(current->vr[0] == 'U' && current->vr[1] == 'S', "Wrong type in writing %x", (unsigned int)tag); + (void) PHYSFS_writeUBE8(handle, TF_INT_U8_ARRAY); + (void) PHYSFS_writeUBE16(handle, count); + for (i = 0; i < count; i++) + { + (void) PHYSFS_writeUBE8(handle, vals[i]); + } + return true; +} + bool tagWrite16v(element_t tag, uint16_t count, uint16_t *vals) { int i; @@ -1171,6 +1221,7 @@ bool tagWriteString(element_t tag, const char *buffer) /********* TAGFILE UNIT TEST *********/ +#define BLOB_SIZE 11 // unit test function void tagTest() { @@ -1178,8 +1229,11 @@ void tagTest() const char *cformat = "WZTAGFILE1"; char format[300], *formatdup; uint16_t droidpos[3]; + uint8_t blob[BLOB_SIZE]; float fv[3]; + memset(blob, 1, BLOB_SIZE); // 1111111... + tagOpenWrite("testdata/tagfile_virtual.def", writename); tagWrites(0x05, 11); tagWriteString(0x06, cformat); @@ -1212,6 +1266,7 @@ void tagTest() fv[1] = 1.1f; fv[2] = -1.3f; tagWritefv(0x03, 3, fv); + tagWrite8v(0x05, BLOB_SIZE, blob); tagWriteEnter(0x09, 1); { int32_t v[3] = { -1, 0, 1 }; @@ -1225,6 +1280,7 @@ void tagTest() memset(droidpos, 0, 6); memset(fv, 0, 6); + memset(blob, 0, BLOB_SIZE); tagOpenRead("testdata/tagfile_basic.def", writename); tagReadString(0x01, 200, format); assert(strncmp(format, cformat, 9) == 0); @@ -1241,6 +1297,8 @@ void tagTest() assert(fv[0] - 0.1f < 0.001); assert(fv[1] - 1.1f < 0.001); assert(fv[2] + 1.3f < 0.001); + tagRead8v(0x05, BLOB_SIZE, blob); + assert(blob[BLOB_SIZE / 2] == 1); tagReadEnter(0x09); tagReads32v(0x05, 3, v); assert(v[0] == -1); diff --git a/lib/framework/tagfile.h b/lib/framework/tagfile.h index a939a6dee..236423d09 100644 --- a/lib/framework/tagfile.h +++ b/lib/framework/tagfile.h @@ -67,6 +67,7 @@ bool tagWrite(element_t tag, uint32_t val); bool tagWrites(element_t tag, int32_t val); bool tagWritef(element_t tag, float val); bool tagWritefv(element_t tag, uint16_t count, float *vals); +bool tagWrite8v(element_t tag, uint16_t count, uint8_t *vals); bool tagWrite16v(element_t tag, uint16_t count, uint16_t *vals); bool tagWrites32v(element_t tag, uint16_t count, int32_t *vals); bool tagWriteString(element_t tag, const char *string); @@ -85,6 +86,7 @@ int32_t tagReads(element_t tag); bool tagReadBool(element_t tag); float tagReadf(element_t tag); bool tagReadfv(element_t tag, uint16_t size, float *vals); +bool tagRead8v(element_t tag, uint16_t size, uint8_t *vals); bool tagRead16v(element_t tag, uint16_t size, uint16_t *vals); bool tagReads16v(element_t tag, uint16_t size, int16_t *vals); bool tagReadString(element_t tag, uint16_t size, char *buffer);