Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <inttypes.h>
- char* get_file_contents(const char* filename, int& size)
- {
- FILE *fp = fopen(filename, "rb");
- if (fp)
- {
- fseek(fp, 0, SEEK_END);
- size = ftell(fp);
- char* contents = new char[size];
- rewind(fp);
- fread(contents, 1, size, fp);
- fclose(fp);
- return contents;
- }
- throw errno;
- }
- #define READA(name, type, size) \
- type name[size]; \
- read(name, size); \
- for (int i = 0; i < level; i++) printf(" "); \
- printf(#name " = "); \
- dump(name, size); \
- puts("");
- #define READ(name, type) \
- type name; \
- read(&name, 1); \
- for (int i = 0; i < level; i++) printf(" "); \
- printf(#name " = "); \
- dump(&name, 1); \
- puts("");
- #define PUSH(name) \
- for (int i = 0; i < level; i++) printf(" "); \
- puts(name); \
- level++;
- #define POP() \
- level--;
- typedef int32_t s32;
- struct reader
- {
- char* data;
- int size = 0;
- int at = 0;
- int level = 0;
- void read(int32_t* out, int count) { readb(out, sizeof(*out) * count); }
- void read(char* out, int count) { readb(out, sizeof(*out) * count); }
- void readb(void* out, int bytes)
- {
- if (at + bytes >= size)
- {
- memset(out, 0, bytes);
- return;
- }
- memcpy(out, &data[at], bytes);
- at += bytes;
- }
- void dump(int32_t* v, int count)
- {
- for (int i = 0; i < count; i++)
- printf("%d ", v[i]);
- }
- void dump(char* v, int count)
- {
- fputc('"', stdout);
- for (int i = 0; i < count; i++)
- fputc(v[i], stdout);
- fputc('"', stdout);
- }
- };
- struct myreader : reader
- {
- myreader()
- {
- data = get_file_contents("loop.wav", size);
- READA(magic, char, 4);
- READ(size, s32);
- int end = at + size;
- READA(riffmagic, char, 4);
- while (at < end)
- {
- readchunk();
- }
- }
- void readchunk()
- {
- PUSH("chunk");
- READA(magic, char, 4);
- READ(size, s32);
- at += size;
- POP();
- }
- };
- int main()
- {
- myreader();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement