From 6e906c76fccd6ada41083a3ed947a64665eb6b26 Mon Sep 17 00:00:00 2001 From: Bohdan Kapliuk Date: Sun, 12 Jan 2025 17:01:23 +0200 Subject: [PATCH] sk1 --- sk1/compressor.c | 2 +- sk1/main.c | 75 +++++++++++++++++------------------------------- 2 files changed, 28 insertions(+), 49 deletions(-) diff --git a/sk1/compressor.c b/sk1/compressor.c index 1751397..17d1ac9 100644 --- a/sk1/compressor.c +++ b/sk1/compressor.c @@ -100,7 +100,7 @@ int decompress_1(const char* input_file_name, const char* output_file_name) { free(data); free(decompressed); - return 0; + return result == 0 ? (int)out_size : -1; } // LZ78 diff --git a/sk1/main.c b/sk1/main.c index 2e85d72..c29f2d0 100644 --- a/sk1/main.c +++ b/sk1/main.c @@ -1,66 +1,45 @@ +#include "compressor.h" #include #include #include -#include "compressor.h" -void print_help() { - printf("Použitie:\n"); - printf(" ./compressor -c infile outfile # Kompresia pomocou algoritmu RLE\n"); - printf(" ./compressor -d infile outfile # Dekompresia pomocou algoritmu RLE\n"); - printf(" ./compressor -c2 infile outfile # Kompresia pomocou algoritmu LZ78\n"); - printf(" ./compressor -d2 infile outfile # Dekompresia pomocou algoritmu LZ78\n"); - printf(" ./compressor -h # Zobrazit pomoc\n"); +void print_usage() { + printf("Usage:\n"); + printf("./compressor -c infile outfile\n"); + printf("./compressor -d infile outfile\n"); + printf("./compressor -h\n"); } int main(int argc, char* argv[]) { - if (argc < 2) { - fprintf(stderr, "Chyba: Chybaju argumenty. Pouzite -h pre zobrazenie pomoci.\n"); - return -1; - } - - if (strcmp(argv[1], "-h") == 0) { - print_help(); - return -1; - } - - if ((strcmp(argv[1], "-c") == 0 || strcmp(argv[1], "-d") == 0 || - strcmp(argv[1], "-c2") == 0 || strcmp(argv[1], "-d2") == 0) && argc != 4) { - fprintf(stderr, "Chyba: Nespravny pocet argumentov. Pouzite -h pre zobrazenie pomoci.\n"); - return -1; + if (argc < 4) { + print_usage(); + return 1; } + const char* operation = argv[1]; const char* input_file = argv[2]; const char* output_file = argv[3]; - int result = 0; - if (strcmp(argv[1], "-c") == 0) { - result = compress_1(input_file, output_file); - if (result < 0) { - fprintf(stderr, "Chyba: Kompresia zlyhala.\n"); - return -1; + int result = 1; + + if (strcmp(operation, "-c") == 0) { + if (compress_1(input_file, output_file) == 0) { + result = 0; + } else if (compress_2(input_file, output_file) == 0) { + result = 0; } - } else if (strcmp(argv[1], "-d") == 0) { - result = decompress_1(input_file, output_file); - if (result < 0) { - fprintf(stderr, "Chyba: Dekompresia zlyhala.\n"); - return -1; - } - } else if (strcmp(argv[1], "-c2") == 0) { - result = compress_2(input_file, output_file); - if (result < 0) { - fprintf(stderr, "Chyba: Kompresia zlyhala.\n"); - return -1; - } - } else if (strcmp(argv[1], "-d2") == 0) { - result = decompress_2(input_file, output_file); - if (result < 0) { - fprintf(stderr, "Chyba: Dekompresia zlyhala.\n"); - return -1; + } else if (strcmp(operation, "-d") == 0) { + if (decompress_1(input_file, output_file) == 0) { + result = 0; + } else if (decompress_2(input_file, output_file) == 0) { + result = 0; } + } else if (strcmp(operation, "-h") == 0) { + print_usage(); + result = 0; } else { - fprintf(stderr, "Chyba: Neznámy argument. Použite -h pre zobrazenie pomoci.\n"); - return -1; + print_usage(); } - return 0; + return result; } \ No newline at end of file