int main(int argc, char **argv) { char **args = argv, *flags, *out; int sym; FILE *inFile; UInt reportSpace = 0, traceFlags = 0; LZWCmp cmp; Config cfg = {PRINTS_PER_LINE, PRINT_FORMAT, NULL}; while (--argc) { args++; if (**args == '-') { flags = *args; while (*++flags) { if (*flags == 't') { //Dump BST at each code transmit traceFlags |= TRACE_TREE; } else if (*flags == 'c') { //Announce each transmission of a code traceFlags |= TRACE_CODES; } else if (*flags == 'b') { //Announce each bump of bits per code traceFlags |= TRACE_BUMPS; } else if (*flags == 'r') { //Announce each dictionary recycle traceFlags |= TRACE_RECYCLES; } else if (*flags == 's') { //Report space after run reportSpace = 1; } } } else { inFile = fopen(*args, "r"); out = calloc(strlen(*args) + strlen(".Z") + 1, 1); strcpy(out, *args); strcat(out, ".Z"); cfg.outFile = fopen(out, "w"); cfg.printsLeft = PRINTS_PER_LINE; LZWCmpInit(&cmp, Sink, &cfg, RECYCLE_CODE, traceFlags); for (sym = fgetc(inFile); sym != EOF; sym = fgetc(inFile)) LZWCmpEncode(&cmp, (UChar)sym); LZWCmpStop(&cmp); if (reportSpace) // flag that results from the -s commandline option printf("Space after LZWCmpStop for %s: %ld\n", *args, report_space()); fclose(inFile); fclose(cfg.outFile); free(out); LZWCmpDestruct(&cmp); } } LZWCmpClearFreelist(); if (reportSpace) // flag that results from the -s commandline option printf("Final space: %ld\n", report_space()); return 0; }
int main(int argc, char *argv[]) { int ndxCount = 1, traceFlags = 0, loopCount = 0, flagCount, tmpInt; char *nameC, *tempC; int fIndex; unsigned long space; LZWCmp cmp; UInt ucharC; FILE *fp, *wp; State states; traceFlags = GetFlags(argc, argv); for (ndxCount = 1; ndxCount < argc && *argv[ndxCount] == '-'; ndxCount++) ; fIndex = argc - ndxCount; for (loopCount = 1; loopCount <= fIndex; loopCount++) { if (!(fp = fopen(argv[ndxCount], "r"))) { printf("Cannot open %s\n", argv[ndxCount]); return 1; } nameC = malloc(strlen(argv[ndxCount]) + FILE_SIZE); strcpy(nameC, argv[ndxCount]); ndxCount++; strcat(nameC, ".Z\0"); wp = fopen(nameC, "w+"); states.count = 0; states.fp = wp; LZWCmpInit(&cmp, CodeS, &states, RECYCLE_SIZE, traceFlags); while ((ucharC = fgetc(fp)) != EOF) { LZWCmpEncode(&cmp, ucharC); } LZWCmpStop(&cmp); if (ReportSpace) { tmpInt = strlen(nameC) - 2; space = report_space(); printf("Space after LZWCmpStop for %.*s: %ld\n", tmpInt, nameC, space); } free(nameC); LZWCmpDestruct(&cmp); fclose(fp); fclose(wp); } LZWCmpClearFreelist(); if (ReportSpace) printf("Final space: %ld\n", report_space()); return 0; }