This commit is contained in:
Bohdan Kapliuk 2025-01-12 17:01:23 +02:00
parent 629d0194ea
commit 6e906c76fc
2 changed files with 28 additions and 49 deletions

View File

@ -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

View File

@ -1,66 +1,45 @@
#include "compressor.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}