Update sk1/main.c

This commit is contained in:
Yurii Chechur 2024-12-26 17:27:19 +00:00
parent fba872e93a
commit 699da1fb95

View File

@ -1,22 +1,24 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "compressor.h" // Підключаємо тільки заголовок
#include "compressor.h"
void display_help();
int main(int argc, char *argv[]) {
if (argc < 4) {
if (argc < 5) { // Мінімум 4 аргументи (включаючи алгоритм та файл)
display_help();
return -1;
}
char *input_filename = argv[2];
char *output_filename = argv[3];
char *action = argv[1]; // -c або -d
char *algorithm = argv[2]; // rle або lz77
char *input_filename = argv[3]; // Вхідний файл
char *output_filename = argv[4]; // Вихідний файл
// Компресія
if (argc == 4 && strcmp(argv[1], "-c") == 0) {
if (strstr(output_filename, ".rle") != NULL) {
if (strcmp(action, "-c") == 0) {
if (strcmp(algorithm, "rle") == 0) {
printf("Compressing using RLE...\n");
int result = rle_compress(input_filename, output_filename);
if (result >= 0) {
@ -24,7 +26,7 @@ int main(int argc, char *argv[]) {
} else {
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");
int result = lz77_compress(input_filename, output_filename);
if (result >= 0) {
@ -33,14 +35,14 @@ int main(int argc, char *argv[]) {
printf("Compression failed with error code %d\n", result);
}
} else {
printf("Unsupported compression format. Use .rle or .lz77.\n");
printf("Unsupported compression algorithm. Use 'rle' or 'lz77'.\n");
return -1;
}
}
// Декомпресія
else if (argc == 4 && strcmp(argv[1], "-d") == 0) {
if (strstr(input_filename, ".rle") != NULL) {
else if (strcmp(action, "-d") == 0) {
if (strcmp(algorithm, "rle") == 0) {
printf("Decompressing RLE...\n");
int result = rle_decompress(input_filename, output_filename);
if (result >= 0) {
@ -48,7 +50,7 @@ int main(int argc, char *argv[]) {
} else {
printf("Decompression failed with error code %d\n", result);
}
} else if (strstr(input_filename, ".lz77") != NULL) {
} else if (strcmp(algorithm, "lz77") == 0) {
printf("Decompressing LZ77...\n");
int result = lz77_decompress(input_filename, output_filename);
if (result >= 0) {
@ -57,13 +59,13 @@ int main(int argc, char *argv[]) {
printf("Decompression failed with error code %d\n", result);
}
} else {
printf("Unsupported decompression format. Use .rle or .lz77.\n");
printf("Unsupported decompression algorithm. Use 'rle' or 'lz77'.\n");
return -1;
}
}
// Показати довідку
else if (argc == 3 && strcmp(argv[1], "-h") == 0) {
else if (strcmp(action, "-h") == 0) {
display_help();
} else {
printf("Invalid arguments\n");
@ -76,10 +78,12 @@ int main(int argc, char *argv[]) {
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(" -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");
printf("Examples:\n");
printf(" compression_tool -c input.txt output.rle\n");
printf(" compression_tool -d output.rle decompressed.txt\n");
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");
}