00001 00002 00003 #include <ctype.h> 00004 #include <stdio.h> 00005 #include <fcntl.h> 00006 #include <errno.h> 00007 00008 #ifndef __GNUC__ 00009 #include <io.h> 00010 #else 00011 #include <unistd.h> 00012 #endif 00013 00014 #include <swcomprs.h> 00015 00016 class FileCompress: public SWCompress { 00017 int ifd; 00018 int ofd; 00019 int ufd; 00020 int zfd; 00021 public: 00022 FileCompress(char *); 00023 ~FileCompress(); 00024 int GetChars(char *, int len); 00025 int SendChars(char *, int len); 00026 void Encode(); 00027 void Decode(); 00028 }; 00029 00030 00031 FileCompress::FileCompress(char *fname) 00032 { 00033 char buf[256]; 00034 00035 #ifndef O_BINARY 00036 #define O_BINARY 0 00037 #endif 00038 00039 ufd = open(fname, O_RDWR|O_CREAT|O_BINARY); 00040 00041 sprintf(buf, "%s.zzz", fname); 00042 zfd = open(buf, O_RDWR|O_CREAT|O_BINARY); 00043 } 00044 00045 00046 FileCompress::~FileCompress(char *fname) 00047 { 00048 close(ufd); 00049 close(zfd); 00050 } 00051 00052 00053 int FileCompress::GetChars(char *buf, int len) 00054 { 00055 return read(ifd, buf, len); 00056 } 00057 00058 00059 int FileCompress::SendChars(char *buf, int len) 00060 { 00061 return write(ofd, buf, len); 00062 } 00063 00064 00065 void FileCompress::Encode() 00066 { 00067 ifd = ufd; 00068 ofd = zfd; 00069 00070 SWCompress::Encode(); 00071 } 00072 00073 00074 void FileCompress::Decode() 00075 { 00076 ifd = zfd; 00077 ofd = ufd; 00078 00079 SWCompress::Decode(); 00080 } 00081 00082 00083 main(int argc, char **argv) 00084 { 00085 int decomp = 0; 00086 SWCompress *fobj; 00087 00088 if (argc != 2) { 00089 fprintf(stderr, "usage: %s <filename|filename.zzz>\n", argv[0]); 00090 exit(1); 00091 } 00092 00093 if (strlen(argv[1]) > 4) { 00094 if (!strcmp(&argv[1][strlen(argv[1])-4], ".zzz")) { 00095 argv[1][strlen(argv[1])-4] = 0; 00096 decomp = 1; 00097 } 00098 } 00099 00100 fobj = new FileCompress(argv[1]); 00101 00102 if (decomp) 00103 fobj->Decode(); 00104 else fobj->Encode(); 00105 00106 delete fobj; 00107 }