Advertisement
snake5

c++ version

Jul 31st, 2019
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 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)
  8. {
  9.     FILE *fp = fopen(filename, "rb");
  10.     if (fp)
  11.     {
  12.         fseek(fp, 0, SEEK_END);
  13.         int count = ftell(fp);
  14.         char* contents = new char[count];
  15.         rewind(fp);
  16.         fread(contents, 1, count, 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_##type(name, size); \
  26.     printf(#name " = "); \
  27.     dump(name, size); \
  28.     puts("");
  29. #define READ(name, type) \
  30.     type name; \
  31.     read_##type(&name, 1); \
  32.     printf(#name " = "); \
  33.     dump(name); \
  34.     puts("");
  35. typedef int32_t Int32;
  36.  
  37. struct reader
  38. {
  39.     char* data;
  40.     int at = 0;
  41.    
  42.     void read_Int32(int32_t* out, int count)
  43.     {
  44.         memcpy(out, &data[at], 4);
  45.         at += 4;
  46.     }
  47.     void read_char(char* out, int count)
  48.     {
  49.         memcpy(out, &data[at], count);
  50.         at += count;
  51.     }
  52.     void dump(int32_t v)
  53.     {
  54.         printf("%d", v);
  55.     }
  56.     void dump(char* v, int count)
  57.     {
  58.         for (int i = 0; i < count; i++)
  59.             fputc(v[i], stdout);
  60.     }
  61. };
  62.  
  63. struct myreader : reader
  64. {
  65.     myreader()
  66.     {
  67.         data = get_file_contents("loop.wav");
  68.         READA(magic, char, 4);
  69.         READ(size, Int32);
  70.     }
  71. };
  72.  
  73. int main()
  74. {
  75.     myreader();
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement