]> git.draconx.ca Git - upkg.git/commitdiff
music: Add simple file type detection to the dummy module.
authorNick Bowler <nbowler@draconx.ca>
Mon, 1 Feb 2010 23:30:04 +0000 (18:30 -0500)
committerNick Bowler <nbowler@draconx.ca>
Mon, 1 Feb 2010 23:30:04 +0000 (18:30 -0500)
The dummy module can be a bit smarter so that the file extension of
exported data is correct.  Add support for xm, it and s3m.

src/engine/music-dummymod.c

index db18fa45482bd067eba7762727b183f7ba44e2f5..a4c86669d3123cdb3aecf7b2bc2f6b5f9bd7ef27 100644 (file)
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "music-module.h"
 #include "upkg.h"
 
 struct music_mod {
        struct upkg_file *f;
+
+       unsigned char header[64];
+       unsigned long hdrlen;
 };
 
 int music_mod_init(void)
@@ -37,12 +41,15 @@ void music_mod_exit(void)
 
 struct music_mod *music_mod_open(struct upkg_file *f)
 {
-       struct music_mod *m = malloc(sizeof *m);
+       struct music_mod *m;
 
-       if (m) {
-               m->f = f;
+       m = malloc(sizeof *m);
+       if (!m) {
+               return NULL;
        }
+       m->f = f;
 
+       m->hdrlen = upkg_export_read(f, m->header, sizeof m->header);
        return m;
 }
 
@@ -85,7 +92,49 @@ int music_mod_dump(struct music_mod *m, FILE *of)
        }
 }
 
+int is_xm(unsigned char *buf, unsigned long len)
+{
+       static const char head[15] =
+               /* ASCII encoding of "Extended Module" */
+               "\x45\x78\x74\x65\x6e\x64\x65\x64\x20\x4d\x6f\x64\x75\x6c\x65";
+
+       if (len >= sizeof head && memcmp(head, buf, sizeof head) == 0)
+               return 1;
+       return 0;
+}
+
+int is_it(unsigned char *buf, unsigned long len)
+{
+       static const char head[4] =
+               /* ASCII encoding of "IMPM" */
+               "\x49\x4d\x50\x4d";
+
+       if (len >= sizeof head && memcmp(head, buf, sizeof head) == 0)
+               return 1;
+       return 0;
+}
+
+int is_s3m(unsigned char *buf, unsigned long len)
+{
+       static const char head[4] =
+               /* ASCII encoding of "SCRM" */
+               "\x53\x43\x52\x4d";
+
+       if (len < 0x2c + sizeof head)
+               return 0;
+       if (memcmp(head, buf+0x2c, sizeof head) == 0)
+               return 1;
+       return 0;
+}
+
 const char *music_mod_type(struct music_mod *mod)
 {
+       if (is_xm(mod->header, mod->hdrlen))
+               return "xm";
+       if (is_it(mod->header, mod->hdrlen))
+               return "it";
+       if (is_s3m(mod->header, mod->hdrlen))
+               return "s3m";
+
        return "unknown";
 }