/* * Cakewalk (WRK) file format test * * Copyright (c) 2004 Pedro Lopez-Cabanillas All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include FILE *file; char *buffer; char *HEADER = "CAKEWALK"; int vma, vme; #define BUFSIZE 1024 #define TRACK_CHUNK 1 #define STREAM_CHUNK 2 #define VARS_CHUNK 3 #define METER_CHUNK 4 #define TEMPO_CHUNK 5 #define SYSEX_CHUNK 6 #define MEMRGN_CHUNK 7 // ??? #define TIMEBASE_CHUNK 0x0A // if present should be the first chunk in the file. #define END_CHUNK 0xff void readBuffer(long count) { if (count > BUFSIZE) fseek(file, count, SEEK_CUR); else fread(buffer, count, 1, file); } void readFileVersion() { fgetc(file); vme = fgetc(file); vma = fgetc(file); } int readChunk() { int len; int ck = fgetc(file); if (ck != END_CHUNK) { fread(&len, sizeof(len), 1, file); // not portable readBuffer(len); } printf(" %02x", ck); return ck; } void cakeTest(char *fileName) { int ck_id; printf("File: %s\n", fileName); file = fopen(fileName, "r"); if (!file) { printf(" Error reading the file\n"); } else { readBuffer(strlen(HEADER)); if (strcmp(buffer, HEADER)) { printf(" Bad file format\n"); } else { readFileVersion(); printf(" file version: %d.%d\n", vma, vme); printf(" file chunks:"); do ck_id = readChunk(); while (ck_id != END_CHUNK); fgetc(file); printf("\n"); if (!feof(file)) { printf(" Corrupted file\n"); } } fclose(file); } } int main(int argc, char *argv[]) { int i; buffer = malloc(BUFSIZE); for (i = 1; i < argc; i++) cakeTest(argv[i]); return EXIT_SUCCESS; }