📄 best.cpp
字号:
// Created:10-13-98
// By Jeff Connelly
// Compresses a file and tells best ratio
#include "comprlib.h"
typedef struct
{
char* ext; // Extension for compressed file
char* name; // Name of compression method
void (*encode)(); // Encoding function
} METHOD;
// These are the methods in ComprLib that work
METHOD methods[] =
{
{ ".lzs", "LZSS", lzss_encode },
{ ".lzw", "LZW", lzw_encode },
{ ".rl1", "RLE1", rle1_encode },
{ ".rl2", "RLE2", rle2_encode },
{ ".inc", "INCR", incr_encode },
{ NULL, NULL, NULL },
};
int file_size(FILE* fp)
{
int pos = ftell(fp);
int size;
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, pos, SEEK_SET);
return size;
}
int main(int argc, char* argv[])
{
int i;
char* ext;
char* out;
if (argc == 1)
{
printf ("Compresses a file with various methods and reports best\n"
"\tBEST filename\n"
"Compressed files have unique extensions\n"
"\n");
exit (0);
}
// Use file I/O
ComprLibFileIO::Set();
ComprLibFileIO::source_file = fopen(argv[1], "r+b");
if (!ComprLibFileIO::source_file)
{
printf ("Cannot open %s\n", argv[1]);
}
for (i = 0; methods[i].encode; ++i)
{
// Use the extension that is unique to the method
out = xmalloc(strlen(argv[1]));
strcpy (out, argv[1]);
ext = strchr(out, '.');
strcpy (ext, methods[i].ext);
// Open the destination file
ComprLibFileIO::dest_file = fopen(out, "w+b");
if (!ComprLibFileIO::dest_file)
{
printf ("Cannot open %s\n", out);
}
printf ("\n%s: %d%%", methods[i].name,
((double)file_size(ComprLibFileIO::dest_file) /
(double)file_size(ComprLibFileIO::source_file))
* 100);
methods[i].encode();
xfree(out);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -