Update sk1/main.c

This commit is contained in:
Yurii Chechur 2024-12-24 18:44:48 +00:00
parent b673ec84b1
commit ba358765d3

View File

@ -1,89 +1,68 @@
#include <assert.h>
#include "compressor.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "compressor.h"
#define BUFSIZE 1024
void print_help() {
printf("Available Options:\n");
printf("1 - Compress using algorithm 1 (Huffman)\n");
printf("2 - Decompress using algorithm 1 (Huffman)\n");
printf("3 - Compress using algorithm 2 (RLE)\n");
printf("4 - Decompress using algorithm 2 (RLE)\n");
printf("0 - Exit\n");
int main(int argc, char** argv) {
// Перевірка правильності кількості аргументів
if (argc != 4 || (argv[1][1] != 'c' && argv[1][1] != 'd' && argv[1][1] != '2' && argv[1][2] != '2')) {
printf("Usage: \n");
printf(" Compress: ./compressor -c infile.txt outfile.compress\n");
printf(" Decompress: ./compressor -d infile.compress outfile.txt\n");
printf(" Compress using RLE: ./compressor -c2 infile.txt outfile.rle\n");
printf(" Decompress using RLE: ./compressor -d2 infile.rle outfile.txt\n");
return 1;
}
int main() {
int choice;
char infile[256], outfile[256];
char* action = argv[1]; // -c, -d, -c2, -d2
char* infile = argv[2]; // Вхідний файл
char* outfile = argv[3]; // Вихідний файл
// Покажемо користувачу доступні варіанти
print_help();
// Відкриваємо вхідний і вихідний файли
FILE* inf = fopen(infile, "rb"); // Відкриваємо вхідний файл для читання
FILE* outf = fopen(outfile, "wb"); // Відкриваємо вихідний файл для запису
while (1) {
// Запитуємо користувача про вибір
printf("Enter your choice (0 to exit): ");
if (scanf("%d", &choice) != 1) {
// Якщо введення не є числом, виводимо повідомлення про помилку
fprintf(stderr, "Invalid input. Please enter a valid number (0-4).\n");
// Очищаємо буфер введення від непотрібних символів
while(getchar() != '\n');
continue;
if (!inf || !outf) {
printf("Error opening files.\n");
return 1;
}
if (choice == 0) {
printf("Exiting program.\n");
break;
// Виконання операцій в залежності від дії
if (action[1] == 'c') {
// Компресія за допомогою алгоритму 1 (Huffman)
if (compress_1(infile, outfile) > 0) {
printf("File successfully compressed using algorithm 1 (Huffman).\n");
} else {
printf("Error compressing file with algorithm 1 (Huffman).\n");
}
}
else if (action[1] == 'd') {
// Декомпресія за допомогою алгоритму 1 (Huffman)
if (decompress_1(infile, outfile) > 0) {
printf("File successfully decompressed using algorithm 1 (Huffman).\n");
} else {
printf("Error decompressing file with algorithm 1 (Huffman).\n");
}
}
else if (action[1] == '2' && action[2] == '2') {
// Компресія за допомогою алгоритму 2 (RLE)
if (compress_2(infile, outfile) > 0) {
printf("File successfully compressed using algorithm 2 (RLE).\n");
} else {
printf("Error compressing file with algorithm 2 (RLE).\n");
}
}
else if (action[1] == 'd' && action[2] == '2') {
// Декомпресія за допомогою алгоритму 2 (RLE)
if (decompress_2(infile, outfile) > 0) {
printf("File successfully decompressed using algorithm 2 (RLE).\n");
} else {
printf("Error decompressing file with algorithm 2 (RLE).\n");
}
}
if (choice < 0 || choice > 4) {
// Перевіряємо, чи вибір знаходиться в допустимому діапазоні
printf("Invalid choice. Please select a valid option (1-4).\n");
continue;
}
// Вводимо імена файлів
printf("Enter input file name: ");
scanf("%s", infile);
printf("Enter output file name: ");
scanf("%s", outfile);
// Перевіряємо, чи існує вхідний файл, і чи доступний вихідний файл для запису
FILE *infile_ptr = fopen(infile, "rb");
if (!infile_ptr) {
printf("Error: Input file does not exist or cannot be opened.\n");
continue;
}
fclose(infile_ptr);
FILE *outfile_ptr = fopen(outfile, "wb");
if (!outfile_ptr) {
printf("Error: Output file cannot be created or opened.\n");
continue;
}
fclose(outfile_ptr);
// Виконання обраного алгоритму
switch (choice) {
case 1:
compress_1(infile, outfile);
break;
case 2:
decompress_1(infile, outfile);
break;
case 3:
compress_2(infile, outfile);
break;
case 4:
decompress_2(infile, outfile);
break;
default:
printf("Invalid choice. Please select again.\n");
break;
}
}
// Закриваємо файли після операцій
fclose(inf);
fclose(outf);
return 0;
}