📄 lzw.c.c
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "getopt.h"#include "lzw.h"char *RemovePath(char *fullPath);int main(int argc, char *argv[]){ int opt; char *inFile, *outFile; char encode; inFile = NULL; outFile = NULL; encode = TRUE; while ((opt = getopt(argc, argv, "cdi:o:h?")) != -1) { switch(opt) { case 'c': encode = TRUE; break; case 'd': encode = FALSE; break; case 'i': if (inFile != NULL) { fprintf(stderr, "Multiple input files not allowed.\n"); free(inFile); if (outFile != NULL) { free(outFile); } exit(EXIT_FAILURE); } else if ((inFile = (char *)malloc(strlen(optarg) + 1)) == NULL) { perror("Memory allocation"); if (outFile != NULL) { free(outFile); } exit(EXIT_FAILURE); } strcpy(inFile, optarg); break; case 'o': if (outFile != NULL) { fprintf(stderr, "Multiple output files not allowed.\n"); free(outFile); if (inFile != NULL) { free(inFile); } exit(EXIT_FAILURE); } else if ((outFile = (char *)malloc(strlen(optarg) + 1)) == NULL) { perror("Memory allocation"); if (inFile != NULL) { free(inFile); } exit(EXIT_FAILURE); } strcpy(outFile, optarg); break; case 'h': case '?': printf("Usage: %s <options>\n\n", RemovePath(argv[0])); printf("options:\n"); printf(" -c : Encode input file to output file.\n"); printf(" -d : Decode input file to output file.\n"); printf(" -i <filename> : Name of input file.\n"); printf(" -o <filename> : Name of output file.\n"); printf(" -h | ? : Print out command line options.\n\n"); printf("Default: %s -c\n", RemovePath(argv[0])); return(EXIT_SUCCESS); } } if (inFile == NULL) { fprintf(stderr, "Input file must be provided\n"); fprintf(stderr, "Enter \"%s -?\" for help.\n", RemovePath(argv[0])); if (outFile != NULL) { free(outFile); } exit (EXIT_FAILURE); } else if (outFile == NULL) { fprintf(stderr, "Output file must be provided\n"); fprintf(stderr, "Enter \"%s -?\" for help.\n", RemovePath(argv[0])); if (inFile != NULL) { free(inFile); } exit (EXIT_FAILURE); } if (encode) { LZWEncodeFile(inFile, outFile); } else { LZWDecodeFile(inFile, outFile); } free(inFile); free(outFile); return EXIT_SUCCESS;}char *RemovePath(char *fullPath){ int i; char *start, *tmp; const char delim[3] = {'\\', '/', ':'}; start = fullPath; for (i = 0; i < 3; i++) { tmp = strrchr(start, delim[i]); if (tmp != NULL) { start = tmp + 1; } } return start;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -