usaa24/sk1/main.c

90 lines
3.3 KiB
C
Raw Normal View History

2024-12-24 15:01:40 +00:00
#include <stdio.h>
#include <stdlib.h>
2024-12-26 15:59:23 +00:00
#include <string.h>
2024-12-26 17:27:19 +00:00
#include "compressor.h"
2024-12-24 15:01:40 +00:00
2024-12-26 15:59:23 +00:00
void display_help();
2024-12-24 17:17:13 +00:00
2024-12-26 15:59:23 +00:00
int main(int argc, char *argv[]) {
2024-12-26 17:27:19 +00:00
if (argc < 5) { // Мінімум 4 аргументи (включаючи алгоритм та файл)
2024-12-26 15:59:23 +00:00
display_help();
return -1;
2024-12-24 18:44:48 +00:00
}
2024-12-24 17:17:13 +00:00
2024-12-26 17:27:19 +00:00
char *action = argv[1]; // -c або -d
char *algorithm = argv[2]; // rle або lz77
char *input_filename = argv[3]; // Вхідний файл
char *output_filename = argv[4]; // Вихідний файл
2024-12-26 15:59:23 +00:00
2024-12-26 17:16:15 +00:00
// Компресія
2024-12-26 17:27:19 +00:00
if (strcmp(action, "-c") == 0) {
if (strcmp(algorithm, "rle") == 0) {
2024-12-26 15:59:23 +00:00
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);
}
2024-12-26 17:27:19 +00:00
} else if (strcmp(algorithm, "lz77") == 0) {
2024-12-26 15:59:23 +00:00
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);
}
2024-12-24 18:44:48 +00:00
} else {
2024-12-26 17:27:19 +00:00
printf("Unsupported compression algorithm. Use 'rle' or 'lz77'.\n");
2024-12-26 15:59:23 +00:00
return -1;
2024-12-24 17:21:43 +00:00
}
2024-12-24 18:44:48 +00:00
}
2024-12-26 17:06:08 +00:00
2024-12-26 17:16:15 +00:00
// Декомпресія
2024-12-26 17:27:19 +00:00
else if (strcmp(action, "-d") == 0) {
if (strcmp(algorithm, "rle") == 0) {
2024-12-26 15:59:23 +00:00
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);
}
2024-12-26 17:27:19 +00:00
} else if (strcmp(algorithm, "lz77") == 0) {
2024-12-26 17:06:08 +00:00
printf("Decompressing LZ77...\n");
2024-12-26 15:59:23 +00:00
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);
}
2024-12-24 18:44:48 +00:00
} else {
2024-12-26 17:27:19 +00:00
printf("Unsupported decompression algorithm. Use 'rle' or 'lz77'.\n");
2024-12-26 15:59:23 +00:00
return -1;
2024-12-24 17:21:43 +00:00
}
2024-12-24 18:44:48 +00:00
}
2024-12-26 17:06:08 +00:00
2024-12-26 17:16:15 +00:00
// Показати довідку
2024-12-26 17:27:19 +00:00
else if (strcmp(action, "-h") == 0) {
2024-12-26 15:59:23 +00:00
display_help();
} else {
printf("Invalid arguments\n");
display_help();
return -1;
2024-12-24 15:01:40 +00:00
}
return 0;
}
2024-12-26 15:59:23 +00:00
void display_help() {
printf("Usage:\n");
2024-12-26 17:27:19 +00:00
printf(" -c algorithm infile outfile : Compress infile using specified algorithm (rle or lz77).\n");
printf(" -d algorithm infile outfile : Decompress infile using specified algorithm (rle or lz77).\n");
printf(" -h : Display this help message.\n");
2024-12-26 17:06:08 +00:00
printf("Examples:\n");
2024-12-26 17:27:19 +00:00
printf(" compression_tool -c rle input.txt output.rle\n");
printf(" compression_tool -c lz77 input.txt output.lz77\n");
printf(" compression_tool -d rle input.rle output.txt\n");
printf(" compression_tool -d lz77 input.lz77 output.txt\n");
2024-12-26 15:59:23 +00:00
}