int getByte(FILE *in) { int v, i; v = 0; for(i = 0; i < 8; i++) { v += (fgetc2(in) == '1' ? 1 << (7-i) : 0); } return v; }
int tag() { int c = 0, cc = 0, c0 = 0, c1 = 0, c2 = 0; while((c = fgetc2(IN)) >= 0) { if (c == EOF) return -1; c1 += (cc == c && c == '1'); c0 += (cc == c && c == '0'); if (cc == '0' && c == '1') if (c1 > 18 && c0 > 18) break; else c1 = c2 = c0 = 0; cc = c; // if (c2 == 2) printf("c0=%d,c1=%d,c2=%d\n", c0, c1, c2); } if (fgetc2(IN) == EOF) return -1; return 0; }
void main(int argc, char **argv) { if(argc < 1) return; // get path int dat, l = 0, h = 0, w = 0, n = 0; bool prev = false; FILE *fs, *fd; char root[_MAX_PATH], file[_MAX_PATH]; char tmp1[_MAX_PATH], tmp2[_MAX_PATH], dest[_MAX_PATH]; GetModuleFileName(NULL, root, _MAX_PATH); int p = strlen(root); while(root[p] != '\\') p--; root[p] = '\0'; strcpy(tmp1, argv[1]); p = strlen(tmp1); while(p != 0 && tmp1[p] != '\\') p--; if(p) strcpy(file, &tmp1[p + 1]); else strcpy(file, argv[1]); sprintf(tmp1, "%s\\tmp1.txt", root); sprintf(tmp2, "%s\\tmp2.txt", root); sprintf(dest, "%s\\dest.cas", root); if(strcmp(file, "tmp1.txt") == 0) { printf("skip wav->pulse\n"); goto label1; } if(strcmp(file, "tmp2.txt") == 0) { printf("skip wav->pulse\n"); printf("skip pulse->bit\n"); goto label2; } // convert #1 (wav -> pulse) fs = fopen(argv[1], "rb"); fd = fopen(tmp1, "w"); while((dat = fgetc(fs)) != EOF) { bool current = dat & 0x80 ? false : true; if(prev && !current) { if(NARROW(l) && (NARROW(h) || WIDE(h))) fputc('N', fd); else if(WIDE(l) && (WIDE(h) || NARROW(h))) fputc('w', fd); else fprintf(fd, "[%d,%d]", l, h); l = h = 0; } if(current) h++; else l++; prev = current; } fclose(fd); fclose(fs); label1: // convert #2 (pulse -> bit) fs = fopen(tmp1, "rb"); fd = fopen(tmp2, "w"); prev = true; while((dat = fgetc(fs)) != EOF) { if(dat != 'w' && dat != 'N') { fputc(dat, fd); continue; } bool current = (dat == 'w') ? true : false; if(prev != current) { if(w) fprintf(fd, "[W%d]", w); if(n) fprintf(fd, "[n%d]", n); w = n = 0; } prev = current; if(current) w++; else n++; if(w == 2) { fputc('w', fd); w = n = 0; } if(n == 4) { fputc('N', fd); w = n = 0; } } fclose(fd); fclose(fs); label2: // convert #3 (bit -> byte) fs = fopen(tmp2, "rb"); fd = fopen(dest, "wb"); while((dat = fgetc2(fs)) != EOF) { if(dat != 'N') continue; if(fgetc2(fs) != 'N') continue; label3: dat = fgetc2(fs); if(dat == EOF) break; if(dat != 'w') goto label3; int d = 0; d |= (fgetc2(fs) == 'N') ? 0x01 : 0; d |= (fgetc2(fs) == 'N') ? 0x02 : 0; d |= (fgetc2(fs) == 'N') ? 0x04 : 0; d |= (fgetc2(fs) == 'N') ? 0x08 : 0; d |= (fgetc2(fs) == 'N') ? 0x10 : 0; d |= (fgetc2(fs) == 'N') ? 0x20 : 0; d |= (fgetc2(fs) == 'N') ? 0x40 : 0; d |= (fgetc2(fs) == 'N') ? 0x80 : 0; fputc(d, fd); } fclose(fd); fclose(fs); }