riff namespace cleanup (sorta, it's ugly:/)

This commit is contained in:
Bill Currie 2003-04-10 21:45:12 +00:00
parent c58dac3ccf
commit 5e98c762ed
3 changed files with 141 additions and 140 deletions

View file

@ -34,34 +34,34 @@
#include "QF/quakeio.h" #include "QF/quakeio.h"
typedef struct d_chunk_s { typedef struct riff_d_chunk_s {
unsigned char name[4]; unsigned char name[4];
unsigned len; unsigned len;
} d_chunk_t; } riff_d_chunk_t;
typedef struct d_cue_point_s { typedef struct riff_d_cue_point_s {
unsigned name; unsigned name;
unsigned position; unsigned position;
char chunk[4]; char chunk[4];
unsigned chunk_start; unsigned chunk_start;
unsigned block_start; unsigned block_start;
unsigned sample_offset; unsigned sample_offset;
} d_cue_point_t; } riff_d_cue_point_t;
typedef struct d_cue_s { typedef struct riff_d_cue_s {
unsigned count; unsigned count;
d_cue_point_t cue_points[1]; riff_d_cue_point_t cue_points[1];
} d_cue_t; } riff_d_cue_t;
typedef struct d_format_s { typedef struct riff_d_format_s {
unsigned short format_tag; unsigned short format_tag;
unsigned short channels; unsigned short channels;
unsigned samples_pre_sec; unsigned samples_pre_sec;
unsigned byte_per_sec; unsigned byte_per_sec;
unsigned short align; unsigned short align;
} d_format_t; } riff_d_format_t;
typedef struct d_ltxt_s { typedef struct riff_d_ltxt_s {
unsigned name; unsigned name;
unsigned len; unsigned len;
char purpose[4]; char purpose[4];
@ -70,49 +70,50 @@ typedef struct d_ltxt_s {
unsigned dialect; unsigned dialect;
unsigned codepage; unsigned codepage;
unsigned char data[0]; unsigned char data[0];
} d_ltxt_t; } riff_d_ltxt_t;
typedef struct cue_s { typedef struct riff_cue_s {
d_chunk_t ck; riff_d_chunk_t ck;
d_cue_t *cue; riff_d_cue_t *cue;
} cue_t; } riff_cue_t;
typedef struct format_s { typedef struct riff_format_s {
d_chunk_t ck; riff_d_chunk_t ck;
d_format_t format; riff_d_format_t format;
char fdata[0]; char fdata[0];
} format_t; } riff_format_t;
typedef struct ltxt_s { typedef struct riff_ltxt_s {
d_chunk_t ck; riff_d_chunk_t ck;
d_ltxt_t ltxt; riff_d_ltxt_t ltxt;
} ltxt_t; } riff_ltxt_t;
typedef struct label_s { typedef struct riff_label_s {
d_chunk_t ck; riff_d_chunk_t ck;
unsigned ofs; unsigned ofs;
char *label; char *label;
} label_t; } riff_label_t;
typedef struct data_s { typedef struct riff_data_s {
d_chunk_t ck; riff_d_chunk_t ck;
char *data; char *data;
} data_t; } riff_data_t;
typedef struct list_s { typedef struct riff_list_s {
d_chunk_t ck; riff_d_chunk_t ck;
char name[4]; char name[4];
d_chunk_t *chunks[0]; riff_d_chunk_t *chunks[0];
} list_t; } riff_list_t;
typedef riff_list_t riff_t; // a riff file is one huge list chunk
#define SWITCH(name) switch (((name)[0] << 24) | ((name)[1] << 16) \ #define RIFF_SWITCH(name) switch (((name)[0] << 24) | ((name)[1] << 16) \
| ((name)[2] << 8) | (name)[3]) | ((name)[2] << 8) | (name)[3])
#define CASE(a,b,c,d) (((unsigned char)(a) << 24) \ #define RIFF_CASE(a,b,c,d) (((unsigned char)(a) << 24) \
| ((unsigned char)(b) << 16) \ | ((unsigned char)(b) << 16) \
| ((unsigned char)(c) << 8) \ | ((unsigned char)(c) << 8) \
| (unsigned char)(d)) | (unsigned char)(d))
list_t *riff_read (QFile *file); riff_t *riff_read (QFile *file);
void riff_free (list_t *riff); void riff_free (riff_t *riff);
#endif//__QF_riff_h #endif//__QF_riff_h

View file

@ -118,7 +118,7 @@ read_data (QFile *f, int len)
} }
static int static int
read_ltxt (QFile *f, int len, d_ltxt_t *ltxt) read_ltxt (QFile *f, int len, riff_d_ltxt_t *ltxt)
{ {
return Qread (f, ltxt, len) == len; return Qread (f, ltxt, len) == len;
} }
@ -126,14 +126,14 @@ read_ltxt (QFile *f, int len, d_ltxt_t *ltxt)
static void static void
read_adtl (dstring_t *list_buf, QFile *f, int len) read_adtl (dstring_t *list_buf, QFile *f, int len)
{ {
d_chunk_t ck, *chunk = 0; riff_d_chunk_t ck, *chunk = 0;
ltxt_t *ltxt; riff_ltxt_t *ltxt;
label_t *label; riff_label_t *label;
data_t *data; riff_data_t *data;
list_t *list; riff_list_t *list;
int r; int r;
list = (list_t *) list_buf->str; list = (riff_list_t *) list_buf->str;
while (len) { while (len) {
r = Rread (f, &ck, sizeof (ck)); r = Rread (f, &ck, sizeof (ck));
if (!r) { if (!r) {
@ -141,16 +141,16 @@ read_adtl (dstring_t *list_buf, QFile *f, int len)
break; break;
} }
len -= r; len -= r;
SWITCH (ck.name) { RIFF_SWITCH (ck.name) {
case CASE ('l','t','x','t'): case RIFF_CASE ('l','t','x','t'):
ltxt = calloc (1, sizeof (ltxt_t)); ltxt = calloc (1, sizeof (riff_ltxt_t));
ltxt->ck = ck; ltxt->ck = ck;
read_ltxt (f, ck.len, &ltxt->ltxt); read_ltxt (f, ck.len, &ltxt->ltxt);
chunk = &ltxt->ck; chunk = &ltxt->ck;
break; break;
case CASE ('l','a','b','l'): case RIFF_CASE ('l','a','b','l'):
case CASE ('n','o','t','e'): case RIFF_CASE ('n','o','t','e'):
label = malloc (sizeof (label_t)); label = malloc (sizeof (riff_label_t));
label->ck = ck; label->ck = ck;
if (!Rread (f, &label->ofs, 4)) { if (!Rread (f, &label->ofs, 4)) {
label->ofs = 0; label->ofs = 0;
@ -167,24 +167,24 @@ read_adtl (dstring_t *list_buf, QFile *f, int len)
} }
len -= ck.len + (ck.len & 1); len -= ck.len + (ck.len & 1);
dstring_append (list_buf, (char *)&chunk, sizeof (chunk)); dstring_append (list_buf, (char *)&chunk, sizeof (chunk));
list = (list_t *) list_buf->str; list = (riff_list_t *) list_buf->str;
chunk = 0; chunk = 0;
} }
dstring_append (list_buf, (char *)&chunk, sizeof (chunk)); dstring_append (list_buf, (char *)&chunk, sizeof (chunk));
list = (list_t *) list_buf->str; list = (riff_list_t *) list_buf->str;
} }
static list_t * static riff_list_t *
read_list (d_chunk_t *ck, QFile *f, int len) read_list (riff_d_chunk_t *ck, QFile *f, int len)
{ {
d_chunk_t *chunk = 0; riff_d_chunk_t *chunk = 0;
dstring_t *list_buf; dstring_t *list_buf;
list_t *list; riff_list_t *list;
list_buf = dstring_new (); list_buf = dstring_new ();
list_buf->size = sizeof (list_t); list_buf->size = sizeof (riff_list_t);
dstring_adjust (list_buf); dstring_adjust (list_buf);
list = (list_t *)list_buf->str; list = (riff_list_t *)list_buf->str;
list->ck = *ck; list->ck = *ck;
if (!Rread (f, list->name, sizeof (list->name))) { if (!Rread (f, list->name, sizeof (list->name))) {
@ -193,10 +193,10 @@ read_list (d_chunk_t *ck, QFile *f, int len)
} }
len -= sizeof (list->name); len -= sizeof (list->name);
while (len > 0) { while (len > 0) {
SWITCH (list->name) { RIFF_SWITCH (list->name) {
case CASE ('I','N','F','O'): case RIFF_CASE ('I','N','F','O'):
{ {
data_t *data = malloc (sizeof (data_t)); riff_data_t *data = malloc (sizeof (riff_data_t));
if (!Rread (f, &data->ck, sizeof (data->ck))) { if (!Rread (f, &data->ck, sizeof (data->ck))) {
len = 0; len = 0;
free (data); free (data);
@ -205,9 +205,9 @@ read_list (d_chunk_t *ck, QFile *f, int len)
chunk = &data->ck; chunk = &data->ck;
//printf ("%.4s %d\n", data->ck.name, data->ck.len); //printf ("%.4s %d\n", data->ck.name, data->ck.len);
len -= sizeof (data->ck); len -= sizeof (data->ck);
SWITCH (data->ck.name) { RIFF_SWITCH (data->ck.name) {
case CASE ('I','C','R','D'): case RIFF_CASE ('I','C','R','D'):
case CASE ('I','S','F','T'): case RIFF_CASE ('I','S','F','T'):
data->data = read_string (f, data->ck.len); data->data = read_string (f, data->ck.len);
break; break;
default: default:
@ -217,13 +217,13 @@ read_list (d_chunk_t *ck, QFile *f, int len)
len -= data->ck.len + (data->ck.len & 1); len -= data->ck.len + (data->ck.len & 1);
} }
break; break;
case CASE ('a','d','t','l'): case RIFF_CASE ('a','d','t','l'):
read_adtl (list_buf, f, len); read_adtl (list_buf, f, len);
len = 0; len = 0;
break; break;
default: default:
{ {
data_t *data = malloc (sizeof (data_t)); riff_data_t *data = malloc (sizeof (riff_data_t));
if (!Rread (f, &data->ck, sizeof (data->ck))) { if (!Rread (f, &data->ck, sizeof (data->ck))) {
free (data); free (data);
} else { } else {
@ -237,20 +237,20 @@ read_list (d_chunk_t *ck, QFile *f, int len)
} }
if (chunk) { if (chunk) {
dstring_append (list_buf, (char *)&chunk, sizeof (chunk)); dstring_append (list_buf, (char *)&chunk, sizeof (chunk));
list = (list_t *) list_buf->str; list = (riff_list_t *) list_buf->str;
} }
chunk = 0; chunk = 0;
} }
dstring_append (list_buf, (char *)&chunk, sizeof (chunk)); dstring_append (list_buf, (char *)&chunk, sizeof (chunk));
list = (list_t *) list_buf->str; list = (riff_list_t *) list_buf->str;
free (list_buf); free (list_buf);
return list; return list;
} }
static d_cue_t * static riff_d_cue_t *
read_cue (QFile *f, int len) read_cue (QFile *f, int len)
{ {
d_cue_t *cue = malloc (len); riff_d_cue_t *cue = malloc (len);
if (!Rread (f, cue, len)) { if (!Rread (f, cue, len)) {
free (cue); free (cue);
@ -260,19 +260,19 @@ read_cue (QFile *f, int len)
return cue; return cue;
} }
list_t * riff_t *
riff_read (QFile *f) riff_read (QFile *f)
{ {
dstring_t *riff_buf; dstring_t *riff_buf;
list_t *riff; riff_list_t *riff;
d_chunk_t *chunk = 0; riff_d_chunk_t *chunk = 0;
d_chunk_t ck; riff_d_chunk_t ck;
int file_len, len; int file_len, len;
riff_buf = dstring_new (); riff_buf = dstring_new ();
riff_buf->size = sizeof (list_t); riff_buf->size = sizeof (riff_list_t);
dstring_adjust (riff_buf); dstring_adjust (riff_buf);
riff = (list_t *)riff_buf->str; riff = (riff_list_t *)riff_buf->str;
file_len = Qfilesize (f); file_len = Qfilesize (f);
@ -296,25 +296,25 @@ riff_read (QFile *f)
//puts ("bling"); //puts ("bling");
ck.len = len = file_len - Qtell (f); ck.len = len = file_len - Qtell (f);
} }
SWITCH (ck.name) { RIFF_SWITCH (ck.name) {
case CASE ('c','u','e',' '): case RIFF_CASE ('c','u','e',' '):
{ {
cue_t *cue = malloc (sizeof (cue_t)); riff_cue_t *cue = malloc (sizeof (riff_cue_t));
cue->ck = ck; cue->ck = ck;
cue->cue = read_cue (f, len); cue->cue = read_cue (f, len);
chunk = &cue->ck; chunk = &cue->ck;
} }
break; break;
case CASE ('L','I','S','T'): case RIFF_CASE ('L','I','S','T'):
{ {
list_t *list; riff_list_t *list;
list = read_list (&ck, f, len); list = read_list (&ck, f, len);
chunk = &list->ck; chunk = &list->ck;
} }
break; break;
case CASE ('d','a','t','a'): case RIFF_CASE ('d','a','t','a'):
{ {
data_t *data = malloc (sizeof (data_t)); riff_data_t *data = malloc (sizeof (riff_data_t));
int c; int c;
data->ck = ck; data->ck = ck;
@ -328,7 +328,7 @@ riff_read (QFile *f)
break; break;
default: default:
{ {
data_t *data = malloc (sizeof (data_t)); riff_data_t *data = malloc (sizeof (riff_data_t));
data->ck = ck; data->ck = ck;
data->data = read_data (f, len); data->data = read_data (f, len);
chunk = &data->ck; chunk = &data->ck;
@ -336,11 +336,11 @@ riff_read (QFile *f)
break; break;
} }
dstring_append (riff_buf, (char *)&chunk, sizeof (chunk)); dstring_append (riff_buf, (char *)&chunk, sizeof (chunk));
riff = (list_t *) riff_buf->str; riff = (riff_list_t *) riff_buf->str;
chunk = 0; chunk = 0;
} }
dstring_append (riff_buf, (char *)&chunk, sizeof (chunk)); dstring_append (riff_buf, (char *)&chunk, sizeof (chunk));
riff = (list_t *) riff_buf->str; riff = (riff_list_t *) riff_buf->str;
free (riff_buf); free (riff_buf);
return riff; return riff;
} }
@ -349,16 +349,16 @@ riff_read (QFile *f)
*****************************************************************************/ *****************************************************************************/
static void static void
free_adtl (d_chunk_t *adtl) free_adtl (riff_d_chunk_t *adtl)
{ {
ltxt_t *ltxt; riff_ltxt_t *ltxt;
label_t *label; riff_label_t *label;
data_t *data; riff_data_t *data;
//printf (" %.4s\n", adtl->name); //printf (" %.4s\n", adtl->name);
SWITCH (adtl->name) { RIFF_SWITCH (adtl->name) {
case CASE ('l','t','x','t'): case RIFF_CASE ('l','t','x','t'):
ltxt = (ltxt_t *) adtl; ltxt = (riff_ltxt_t *) adtl;
/*printf (" %d %d %4s %d %d %d %d\n", /*printf (" %d %d %4s %d %d %d %d\n",
ltxt->ltxt.name, ltxt->ltxt.name,
ltxt->ltxt.len, ltxt->ltxt.len,
@ -369,16 +369,16 @@ free_adtl (d_chunk_t *adtl)
ltxt->ltxt.codepage);*/ ltxt->ltxt.codepage);*/
free (ltxt); free (ltxt);
break; break;
case CASE ('l','a','b','l'): case RIFF_CASE ('l','a','b','l'):
case CASE ('n','o','t','e'): case RIFF_CASE ('n','o','t','e'):
label = (label_t *) adtl; label = (riff_label_t *) adtl;
//printf (" %-8d %s\n", label->ofs, label->label); //printf (" %-8d %s\n", label->ofs, label->label);
if (label->label) if (label->label)
free (label->label); free (label->label);
free (label); free (label);
break; break;
default: default:
data = (data_t *) adtl; data = (riff_data_t *) adtl;
if (data->data) if (data->data)
free (data->data); free (data->data);
free (data); free (data);
@ -387,20 +387,20 @@ free_adtl (d_chunk_t *adtl)
} }
static void static void
free_list (list_t *list) free_list (riff_list_t *list)
{ {
d_chunk_t **ck; riff_d_chunk_t **ck;
data_t *data; riff_data_t *data;
//printf (" %.4s\n", list->name); //printf (" %.4s\n", list->name);
for (ck = list->chunks; *ck; ck++) { for (ck = list->chunks; *ck; ck++) {
SWITCH (list->name) { RIFF_SWITCH (list->name) {
case CASE ('I','N','F','O'): case RIFF_CASE ('I','N','F','O'):
data = (data_t *) *ck; data = (riff_data_t *) *ck;
//printf (" %.4s\n", data->ck.name); //printf (" %.4s\n", data->ck.name);
SWITCH (data->ck.name) { RIFF_SWITCH (data->ck.name) {
case CASE ('I','C','R','D'): case RIFF_CASE ('I','C','R','D'):
case CASE ('I','S','F','T'): case RIFF_CASE ('I','S','F','T'):
//printf (" %s\n", data->data); //printf (" %s\n", data->data);
default: default:
if (data->data) if (data->data)
@ -409,11 +409,11 @@ free_list (list_t *list)
break; break;
} }
break; break;
case CASE ('a','d','t','l'): case RIFF_CASE ('a','d','t','l'):
free_adtl (*ck); free_adtl (*ck);
break; break;
default: default:
data = (data_t *) *ck; data = (riff_data_t *) *ck;
if (data->data) if (data->data)
free (data->data); free (data->data);
free (data); free (data);
@ -424,18 +424,18 @@ free_list (list_t *list)
} }
void void
riff_free (list_t *riff) riff_free (riff_t *riff)
{ {
d_chunk_t **ck; riff_d_chunk_t **ck;
cue_t *cue; riff_cue_t *cue;
list_t *list; riff_list_t *list;
data_t *data; riff_data_t *data;
for (ck = riff->chunks; *ck; ck++) { for (ck = riff->chunks; *ck; ck++) {
//printf ("%.4s\n", (*ck)->name); //printf ("%.4s\n", (*ck)->name);
SWITCH ((*ck)->name) { RIFF_SWITCH ((*ck)->name) {
case CASE ('c','u','e',' '): case RIFF_CASE ('c','u','e',' '):
cue = (cue_t *) *ck; cue = (riff_cue_t *) *ck;
if (cue->cue) { if (cue->cue) {
/*int i; /*int i;
for (i = 0; i < cue->cue->count; i++) { for (i = 0; i < cue->cue->count; i++) {
@ -451,12 +451,12 @@ riff_free (list_t *riff)
} }
free (cue); free (cue);
break; break;
case CASE ('L','I','S','T'): case RIFF_CASE ('L','I','S','T'):
list = (list_t *) *ck; list = (riff_list_t *) *ck;
free_list (list); free_list (list);
break; break;
default: default:
data = (data_t *) *ck; data = (riff_data_t *) *ck;
if (data->data) if (data->data)
free (data->data); free (data->data);
free (data); free (data);

View file

@ -10,8 +10,8 @@ main (int argc, char **argv)
while (*++argv) { while (*++argv) {
QFile *f; QFile *f;
list_t *riff = 0; riff_t *riff = 0;
d_chunk_t **ck; riff_d_chunk_t **ck;
int sample_start, sample_count; int sample_start, sample_count;
f = Qopen (*argv, "rbz"); f = Qopen (*argv, "rbz");
@ -26,12 +26,12 @@ main (int argc, char **argv)
sample_start = -1; sample_start = -1;
sample_count = -1; sample_count = -1;
for (ck = riff->chunks; *ck; ck++) { for (ck = riff->chunks; *ck; ck++) {
SWITCH ((*ck)->name) { RIFF_SWITCH ((*ck)->name) {
case CASE ('c', 'u', 'e', ' '): case RIFF_CASE ('c', 'u', 'e', ' '):
{ {
cue_t *cue = (cue_t *)*ck; riff_cue_t *cue = (riff_cue_t *)*ck;
d_cue_t *dcue = cue->cue; riff_d_cue_t *dcue = cue->cue;
d_cue_point_t *cp = dcue->cue_points; riff_d_cue_point_t *cp = dcue->cue_points;
int i; int i;
for (i = 0; i < dcue->count; i++) for (i = 0; i < dcue->count; i++)
@ -47,19 +47,19 @@ main (int argc, char **argv)
#endif #endif
} }
break; break;
case CASE ('L','I','S','T'): case RIFF_CASE ('L','I','S','T'):
{ {
list_t *list = (list_t *)*ck; riff_list_t *list = (riff_list_t *)*ck;
SWITCH (list->name) { RIFF_SWITCH (list->name) {
case CASE ('a','d','t','l'): case RIFF_CASE ('a','d','t','l'):
{ {
d_chunk_t **ck; riff_d_chunk_t **ck;
for (ck = list->chunks; *ck; ck++) { for (ck = list->chunks; *ck; ck++) {
SWITCH ((*ck)->name) { RIFF_SWITCH ((*ck)->name) {
case CASE ('l', 't', 'x', 't'): case RIFF_CASE ('l', 't', 'x', 't'):
{ {
ltxt_t *ltxt = (ltxt_t *)*ck; riff_ltxt_t *ltxt = (riff_ltxt_t *)*ck;
d_ltxt_t *dltxt = &ltxt->ltxt; riff_d_ltxt_t *dltxt = &ltxt->ltxt;
sample_count = dltxt->len; sample_count = dltxt->len;
#if 0 #if 0
printf ("ltxt: %d %d %4s %d %d %d %d\n", printf ("ltxt: %d %d %4s %d %d %d %d\n",
@ -80,7 +80,7 @@ main (int argc, char **argv)
} }
} }
break; break;
case CASE ('d','a','t','a'): case RIFF_CASE ('d','a','t','a'):
#if 0 #if 0
printf ("data: %d %d\n", *(int*)((data_t*)(*ck))->data, (*ck)->len); printf ("data: %d %d\n", *(int*)((data_t*)(*ck))->data, (*ck)->len);
#endif #endif