home *** CD-ROM | disk | FTP | other *** search
/ Groovy Bytes: Behind the Moon / groovybytes.iso / GROOVY / SND_TOOL / REVERSE.ZIP / REVERSE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-04  |  2.2 KB  |  120 lines

  1. #include <stdio.h>
  2. #include <mem.h>
  3.  
  4. char nchannels=4;
  5. FILE *f;
  6. FILE *f2;
  7. char orders[128];
  8. char toppattern;
  9.  
  10. void error(char *s)
  11. {
  12.   printf("\n%s", s);
  13.   exit(1);
  14. }
  15.  
  16. void openmod(char *s)
  17. {
  18.   char title[21], channels[5];
  19.   
  20.   f=fopen(s, "rb");
  21.   if(!f) error("Unable to open read file");
  22.   fseek(f, 0, SEEK_SET);
  23.   fread(title, 21, 1, f);
  24.   fseek(f, 1080, SEEK_SET);
  25.   fread(channels, 4, 1, f);
  26.   channels[4]=0;
  27.   if(!strcmp(channels, "M.K.")) nchannels=4;
  28.   if(!strcmp(channels, "FLT4")) nchannels=4;
  29.   if(!strcmp(channels, "FLT8")) nchannels=8;
  30.   if(!strcmp(channels, "6CHN")) nchannels=6;
  31.   if(!strcmp(channels, "8CHN")) nchannels=8;
  32.   printf("\n%d channel module: %s", nchannels, title);
  33. }
  34.  
  35. void copyfile(char *s)
  36. {
  37.   char space[10000];
  38.   int n;
  39.  
  40.   f2=fopen(s, "wb");
  41.   fseek(f, 0, SEEK_SET);
  42.   do
  43.   {
  44.     n=fread(space, 1, 10000, f);
  45.     if(fwrite(space, 1, n, f2)<n) error("Error writing to file (disk full?)");
  46.   } while(n==10000);
  47. }
  48.  
  49. void revorders()
  50. {
  51.   int i;
  52.   char length;
  53.   char revorders[128];
  54.   
  55.   fseek(f, 950, SEEK_SET);
  56.   fread(&length, 1, 1, f);
  57.   fseek(f, 952, SEEK_SET);  
  58.   fread(orders, 1, length, f);
  59.  
  60.   for(i=0; i<length; i++)
  61.   {
  62.     if(orders[i]>toppattern) toppattern=orders[i];
  63.     revorders[i]=orders[length-i-1];
  64.   }
  65.   fseek(f2, 952, SEEK_SET);
  66.   fwrite(revorders, 1, length, f2);
  67. }
  68.  
  69. void revpatterns()
  70. {
  71.   char data[2048];
  72.   char backdata[2048];
  73.   char *rowdata[64];
  74.   int i, j;
  75.   char *p, len;
  76.  
  77.   len=4*nchannels;
  78.  
  79.   for(j=0; j<64; j++)  
  80.     rowdata[j]=data+len*j;
  81.   
  82.   fseek(f, 1084, SEEK_SET);
  83.   fseek(f2, 1084, SEEK_SET);
  84.   for(i=0; i<toppattern+1; i++)
  85.   {
  86.     fread(data, 1, nchannels*4*64, f);
  87.     p=backdata;
  88.     for(j=63; j>=0; j--)
  89.     {
  90.       memmove(p, rowdata[j], len);
  91.       p+=len;
  92.     }
  93.     fwrite(backdata, 1, nchannels*4*64, f2);
  94.   }
  95. }
  96.  
  97. main(int argc, char **argv)
  98. {
  99.   printf("MOD Reverser");
  100.   
  101.   printf("\nCopyright (c) 1995 by Jim Crawford");
  102.   openmod(argv[1]);
  103.   
  104.   printf("\nCreating new file: %s", argv[2]);
  105.   copyfile(argv[2]);
  106.   
  107.   printf("\nReversing orders...");
  108.   revorders();
  109.   
  110.   printf("\nReversing patterns...");
  111.   revpatterns();
  112.  
  113.   fclose(f);
  114.   fclose(f2);
  115.  
  116.   printf("\nDone!");
  117.  
  118.   return argc-argc;
  119. }
  120.