Update sk1/main.c

This commit is contained in:
Yurii Chechur 2024-12-26 17:06:08 +00:00
parent f0122c07a0
commit c3a3eb374b

View File

@ -7,16 +7,20 @@
void display_help(); void display_help();
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc < 3) { // Перевірка кількості аргументів
if (argc < 4) {
display_help(); display_help();
return -1; return -1;
} }
// Імена вхідного та вихідного файлів
char *input_filename = argv[2]; char *input_filename = argv[2];
char *output_filename = argv[3]; char *output_filename = argv[3];
char *algorithm = argc >= 5 ? argv[4] : "rle"; // Алгоритм, за замовчуванням rle
if (argc == 4 && strcmp(argv[1], "-c") == 0) { // Обробка компресії
if (strstr(output_filename, ".rle") != NULL) { if (argc >= 4 && strcmp(argv[1], "-c") == 0) {
if (strcmp(algorithm, "rle") == 0) {
printf("Compressing using RLE...\n"); printf("Compressing using RLE...\n");
int result = rle_compress(input_filename, output_filename); int result = rle_compress(input_filename, output_filename);
if (result >= 0) { if (result >= 0) {
@ -24,7 +28,7 @@ int main(int argc, char *argv[]) {
} else { } else {
printf("Compression failed with error code %d\n", result); printf("Compression failed with error code %d\n", result);
} }
} else if (strstr(output_filename, ".lz77") != NULL) { } else if (strcmp(algorithm, "lz77") == 0) {
printf("Compressing using LZ77...\n"); printf("Compressing using LZ77...\n");
int result = lz77_compress(input_filename, output_filename); int result = lz77_compress(input_filename, output_filename);
if (result >= 0) { if (result >= 0) {
@ -33,11 +37,13 @@ int main(int argc, char *argv[]) {
printf("Compression failed with error code %d\n", result); printf("Compression failed with error code %d\n", result);
} }
} else { } else {
printf("Unsupported compression format.\n"); printf("Unsupported compression algorithm. Use 'rle' or 'lz77'.\n");
return -1; return -1;
} }
} }
else if (argc == 4 && strcmp(argv[1], "-d") == 0) {
// Обробка декомпресії
else if (argc >= 4 && strcmp(argv[1], "-d") == 0) {
if (strstr(input_filename, ".rle") != NULL) { if (strstr(input_filename, ".rle") != NULL) {
printf("Decompressing RLE...\n"); printf("Decompressing RLE...\n");
int result = rle_decompress(input_filename, output_filename); int result = rle_decompress(input_filename, output_filename);
@ -47,7 +53,7 @@ int main(int argc, char *argv[]) {
printf("Decompression failed with error code %d\n", result); printf("Decompression failed with error code %d\n", result);
} }
} else if (strstr(input_filename, ".lz77") != NULL) { } else if (strstr(input_filename, ".lz77") != NULL) {
printf("Decompressing using LZ77...\n"); printf("Decompressing LZ77...\n");
int result = lz77_decompress(input_filename, output_filename); int result = lz77_decompress(input_filename, output_filename);
if (result >= 0) { if (result >= 0) {
printf("Decompression successful. Decompressed size: %d bytes\n", result); printf("Decompression successful. Decompressed size: %d bytes\n", result);
@ -59,6 +65,8 @@ int main(int argc, char *argv[]) {
return -1; return -1;
} }
} }
// Виведення довідки
else if (argc == 3 && strcmp(argv[1], "-h") == 0) { else if (argc == 3 && strcmp(argv[1], "-h") == 0) {
display_help(); display_help();
} else { } else {
@ -70,14 +78,14 @@ int main(int argc, char *argv[]) {
return 0; return 0;
} }
// Функція для виведення довідки
void display_help() { void display_help() {
printf("Usage:\n"); printf("Usage:\n");
printf(" -c infile outfile : Compress infile to outfile (.rle or .lz77).\n"); printf(" -c infile outfile [algorithm] : Compress infile to outfile. Algorithm can be 'rle' or 'lz77'. Default is 'rle'.\n");
printf(" -d infile outfile : Decompress infile to outfile (.rle or .lz77).\n"); printf(" -d compressed outfile : Decompress compressed to outfile.\n");
printf(" -h : Display this help message.\n"); printf(" -h : Display this help message.\n");
printf("Example:\n"); printf("Examples:\n");
printf(" compression_tool -c input.txt output.rle\n"); printf(" compression_tool -c input.txt output.rle rle\n");
printf(" compression_tool -c input.txt output.lz77 lz77\n");
printf(" compression_tool -d output.rle decompressed.txt\n"); printf(" compression_tool -d output.rle decompressed.txt\n");
printf(" compression_tool -c input.txt output.lz77\n");
printf(" compression_tool -d output.lz77 decompressed.txt\n");
} }