86 lines
3.1 KiB
C
86 lines
3.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "compressor.h" // Підключаємо тільки заголовок
|
|
|
|
void display_help();
|
|
|
|
int main(int argc, char *argv[]) {
|
|
if (argc < 4) {
|
|
display_help();
|
|
return -1;
|
|
}
|
|
|
|
char *input_filename = argv[2];
|
|
char *output_filename = argv[3];
|
|
|
|
// Компресія
|
|
if (argc == 4 && strcmp(argv[1], "-c") == 0) {
|
|
if (strstr(output_filename, ".rle") != NULL) {
|
|
printf("Compressing using RLE...\n");
|
|
int result = rle_compress(input_filename, output_filename);
|
|
if (result >= 0) {
|
|
printf("Compression successful. Compressed size: %d bytes\n", result);
|
|
} else {
|
|
printf("Compression failed with error code %d\n", result);
|
|
}
|
|
} else if (strstr(output_filename, ".lz77") != NULL) {
|
|
printf("Compressing using LZ77...\n");
|
|
int result = lz77_compress(input_filename, output_filename);
|
|
if (result >= 0) {
|
|
printf("Compression successful. Compressed size: %d bytes\n", result);
|
|
} else {
|
|
printf("Compression failed with error code %d\n", result);
|
|
}
|
|
} else {
|
|
printf("Unsupported compression format. Use .rle or .lz77.\n");
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
// Декомпресія
|
|
else if (argc == 4 && strcmp(argv[1], "-d") == 0) {
|
|
if (strstr(input_filename, ".rle") != NULL) {
|
|
printf("Decompressing RLE...\n");
|
|
int result = rle_decompress(input_filename, output_filename);
|
|
if (result >= 0) {
|
|
printf("Decompression successful. Decompressed size: %d bytes\n", result);
|
|
} else {
|
|
printf("Decompression failed with error code %d\n", result);
|
|
}
|
|
} else if (strstr(input_filename, ".lz77") != NULL) {
|
|
printf("Decompressing LZ77...\n");
|
|
int result = lz77_decompress(input_filename, output_filename);
|
|
if (result >= 0) {
|
|
printf("Decompression successful. Decompressed size: %d bytes\n", result);
|
|
} else {
|
|
printf("Decompression failed with error code %d\n", result);
|
|
}
|
|
} else {
|
|
printf("Unsupported decompression format. Use .rle or .lz77.\n");
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
// Показати довідку
|
|
else if (argc == 3 && strcmp(argv[1], "-h") == 0) {
|
|
display_help();
|
|
} else {
|
|
printf("Invalid arguments\n");
|
|
display_help();
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void display_help() {
|
|
printf("Usage:\n");
|
|
printf(" -c infile outfile : Compress infile to outfile. Supported formats: .rle, .lz77\n");
|
|
printf(" -d compressed outfile : Decompress compressed to outfile. Supported formats: .rle, .lz77\n");
|
|
printf(" -h : Display this help message.\n");
|
|
printf("Examples:\n");
|
|
printf(" compression_tool -c input.txt output.rle\n");
|
|
printf(" compression_tool -d output.rle decompressed.txt\n");
|
|
}
|