]> git.draconx.ca Git - upkg.git/commitdiff
Simplify music I/O.
authorNick Bowler <nbowler@draconx.ca>
Fri, 3 Jul 2009 21:25:25 +0000 (17:25 -0400)
committerNick Bowler <nbowler@draconx.ca>
Fri, 3 Jul 2009 21:25:25 +0000 (17:25 -0400)
src/engine/music.c

index 7d57bdb9401b3bb36d6e4c10261ecbc1bad8d218..87a549b7e6d538766a2832ff7248afca41bfe50a 100644 (file)
        G_TYPE_INSTANCE_GET_PRIVATE(o, ENGINE_MUSIC_TYPE, struct music_priv)
 
 struct music_priv {
-       unsigned type;
-
        struct upkg_file *f;
-       size_t base, len;
-
-       unsigned char buf[2048];
-       unsigned long nbuf;
 };
 
 static void exportable_init(UObjectExportable *);
@@ -29,23 +23,20 @@ G_DEFINE_DYNAMIC_TYPE_EXTENDED(EngineMusic, engine_music, U_OBJECT_TYPE, 0,
 static int export(GObject *o, FILE *f)
 {
        struct music_priv *priv = MUSIC_GET_PRIV(o);
-       size_t left = priv->len, rc;
-
        unsigned char buf[1024];
 
-       if (!priv->f)
-               return -1;
-
-       if (upkg_export_seek(priv->f, priv->base, SEEK_SET) != 0)
+       if (!priv->f || upkg_export_seek(priv->f, 0, SEEK_SET) != 0)
                return -1;
 
-       while (left > 0) {
-               rc = upkg_export_read(priv->f, buf, MIN(left, sizeof buf));
-               if (rc == 0)
+       while (!priv->f->eof) {
+               size_t rc = upkg_export_read(priv->f, buf, sizeof buf);
+               if (rc == 0) {
+                       if (priv->f->eof) break;
                        return -1;
+               }
+
                if (fwrite(buf, 1, rc, f) != rc)
                        return -1;
-               left -= rc;
        }
 
        return 0;
@@ -67,40 +58,37 @@ static int deserialize(UObject *o, struct upkg_file *f)
 {
        struct music_priv *priv = MUSIC_GET_PRIV(o);
        EngineMusic *m = ENGINE_MUSIC(o);
+       size_t rc, pos = 0, buflen;
+       unsigned char buf[32];
        long size;
 
-       size_t rc;
-
        U_OBJECT_CLASS(engine_music_parent_class)->deserialize(o, f);
 
-       priv->nbuf = upkg_export_read(f, priv->buf, sizeof priv->buf);
-       priv->base = 0;
+       buflen = upkg_export_read(f, buf, sizeof buf);
 
        /* Random field #1 */
-       if (priv->nbuf - priv->base < 1)
+       if (buflen - pos < 1)
                return -1;
-       // unpack_16_le(priv->buf+0);
-       priv->base += 1;
+       pos += 1;
 
        if (f->pkg->version > 61) {
                /* Random field #2 */
-               if (priv->nbuf - priv->base < 4)
+               if (buflen - pos < 4)
                        return -1;
-               // unpack_32_le(priv->buf+2);
-               priv->base += 4;
+               pos += 4;
        }
 
-       rc = upkg_decode_index(&size, priv->buf+priv->base, priv->nbuf-priv->base);
+       rc = upkg_decode_index(&size, buf+pos, buflen-pos);
        if (rc == 0 || size < 0)
                return -1;
+       pos += rc;
 
-       priv->base += rc;
-       priv->len   = size;
-
-       priv->nbuf -= priv->base;
-       memmove(priv->buf, priv->buf+priv->base, priv->nbuf);
+       f->base += pos;
+       f->len   = size;
+       upkg_export_seek(f, 0, SEEK_SET);
 
        priv->f = f;
+
        return 0;
 }