Advertisement
snake5

c++ version v2

Jul 31st, 2019
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <inttypes.h>
  6.  
  7. char* get_file_contents(const char* filename, int& size)
  8. {
  9.     FILE *fp = fopen(filename, "rb");
  10.     if (fp)
  11.     {
  12.         fseek(fp, 0, SEEK_END);
  13.         size = ftell(fp);
  14.         char* contents = new char[size];
  15.         rewind(fp);
  16.         fread(contents, 1, size, fp);
  17.         fclose(fp);
  18.         return contents;
  19.     }
  20.     throw errno;
  21. }
  22.  
  23. #define READA(name, type, size) \
  24.     type name[size]; \
  25.     read(name, size); \
  26.     for (int i = 0; i < level; i++) printf("    "); \
  27.     printf(#name " = "); \
  28.     dump(name, size); \
  29.     puts("");
  30. #define READ(name, type) \
  31.     type name; \
  32.     read(&name, 1); \
  33.     for (int i = 0; i < level; i++) printf("    "); \
  34.     printf(#name " = "); \
  35.     dump(&name, 1); \
  36.     puts("");
  37. #define PUSH(name) \
  38.     for (int i = 0; i < level; i++) printf("    "); \
  39.     puts(name); \
  40.     level++;
  41. #define POP() \
  42.     level--;
  43. typedef int32_t s32;
  44.  
  45. struct reader
  46. {
  47.     char* data;
  48.     int size = 0;
  49.     int at = 0;
  50.     int level = 0;
  51.    
  52.     void read(int32_t* out, int count) { readb(out, sizeof(*out) * count); }
  53.     void read(char* out, int count) { readb(out, sizeof(*out) * count); }
  54.     void readb(void* out, int bytes)
  55.     {
  56.         if (at + bytes >= size)
  57.         {
  58.             memset(out, 0, bytes);
  59.             return;
  60.         }
  61.         memcpy(out, &data[at], bytes);
  62.         at += bytes;
  63.     }
  64.     void dump(int32_t* v, int count)
  65.     {
  66.         for (int i = 0; i < count; i++)
  67.             printf("%d ", v[i]);
  68.     }
  69.     void dump(char* v, int count)
  70.     {
  71.         fputc('"', stdout);
  72.         for (int i = 0; i < count; i++)
  73.             fputc(v[i], stdout);
  74.         fputc('"', stdout);
  75.     }
  76. };
  77.  
  78. struct myreader : reader
  79. {
  80.     myreader()
  81.     {
  82.         data = get_file_contents("loop.wav", size);
  83.         READA(magic, char, 4);
  84.         READ(size, s32);
  85.        
  86.         int end = at + size;
  87.         READA(riffmagic, char, 4);
  88.        
  89.         while (at < end)
  90.         {
  91.             readchunk();
  92.         }
  93.     }
  94.    
  95.     void readchunk()
  96.     {
  97.         PUSH("chunk");
  98.         READA(magic, char, 4);
  99.         READ(size, s32);
  100.         at += size;
  101.         POP();
  102.     }
  103. };
  104.  
  105. int main()
  106. {
  107.     myreader();
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement