usaa24/sk1/main.c
2024-12-26 17:33:31 +00:00

84 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) { // Мінімум 4 аргументи
display_help();
return -1;
}
char *action = argv[1]; // -c, -d, -c2, -d2
char *input_filename = argv[2]; // Вхідний файл
char *output_filename = argv[3]; // Вихідний файл
// Для першого алгоритму компресії (наприклад, RLE)
if (strcmp(action, "-c") == 0) {
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 (strcmp(action, "-d") == 0) {
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);
}
}
// Для другого алгоритму компресії (наприклад, LZ77)
else if (strcmp(action, "-c2") == 0) {
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 if (strcmp(action, "-d2") == 0) {
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 if (strcmp(action, "-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 using RLE algorithm.\n");
printf(" -d compressed uncompressed : Decompress compressed file using RLE.\n");
printf(" -c2 infile outfile : Compress infile using LZ77 algorithm.\n");
printf(" -d2 compressed uncompressed: Decompress compressed file using LZ77.\n");
printf(" -h : Display this help message.\n");
printf("Examples:\n");
printf(" ./compressor -c input.txt output.rle\n");
printf(" ./compressor -d output.rle decompressed.txt\n");
printf(" ./compressor -c2 input.txt output.lz77\n");
printf(" ./compressor -d2 output.lz77 decompressed.txt\n");
}