// This code written mainly from information provided at // http://www.borg.com/~jglatt/tech/midifile/table.htm #include #include typedef unsigned char uchar; void ex(int d){printf("Bye %d\n", d); exit(4);} int cc = 0; uchar nc(){char x = getc(stdin); if(x == EOF && feof(stdin)) {printf("cc = %d\n", cc); ex(6);} ++cc; return x;} void expect(char * s){while (*s) if(*(s++) != nc()) ex(1);} int gn(int l) {int k = 0; while(l--) k = (k<<8) | nc(); return k;} int main(){expect("MThd"); if(gn(4) != 6) ex(3); if(gn(2) != 1) ex(2); {int tc = gn(2), den = gn(2); printf("tc = %d, den = %d\n", tc, den); while(tc--) {expect("MTrk"); {int ts = gn(4); printf("Chunk: track with %d bytes, (starting at 0x%x)\n", ts, cc); while(ts--) nc();}} if (getc(stdin) != EOF || !feof(stdin)) ex(5); } return 0;} // http://www.borg.com/~jglatt/tech