From f64b7a86773f697754dda624d5fcc28196a125aa Mon Sep 17 00:00:00 2001 From: Bohdan Kapliuk Date: Sun, 12 Jan 2025 17:02:25 +0200 Subject: [PATCH] sk1 --- sk1/main.c | 75 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 27 deletions(-) diff --git a/sk1/main.c b/sk1/main.c index c29f2d0..2e85d72 100644 --- a/sk1/main.c +++ b/sk1/main.c @@ -1,45 +1,66 @@ -#include "compressor.h" #include #include #include +#include "compressor.h" -void print_usage() { - printf("Usage:\n"); - printf("./compressor -c infile outfile\n"); - printf("./compressor -d infile outfile\n"); - printf("./compressor -h\n"); +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"); } int main(int argc, char* argv[]) { - if (argc < 4) { - print_usage(); - return 1; + 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; } - const char* operation = argv[1]; const char* input_file = argv[2]; const char* output_file = argv[3]; + int result = 0; - 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; + if (strcmp(argv[1], "-c") == 0) { + result = compress_1(input_file, output_file); + if (result < 0) { + fprintf(stderr, "Chyba: Kompresia 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(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, "-h") == 0) { - print_usage(); - result = 0; } else { - print_usage(); + fprintf(stderr, "Chyba: Neznámy argument. Použite -h pre zobrazenie pomoci.\n"); + return -1; } - return result; + return 0; } \ No newline at end of file