Update sk1/main.c
This commit is contained in:
parent
b673ec84b1
commit
ba358765d3
127
sk1/main.c
127
sk1/main.c
@ -1,89 +1,68 @@
|
|||||||
#include <assert.h>
|
#include "compressor.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
|
||||||
#include "compressor.h"
|
|
||||||
|
|
||||||
#define BUFSIZE 1024
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
void print_help() {
|
char* action = argv[1]; // -c, -d, -c2, -d2
|
||||||
printf("Available Options:\n");
|
char* infile = argv[2]; // Вхідний файл
|
||||||
printf("1 - Compress using algorithm 1 (Huffman)\n");
|
char* outfile = argv[3]; // Вихідний файл
|
||||||
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 choice;
|
FILE* inf = fopen(infile, "rb"); // Відкриваємо вхідний файл для читання
|
||||||
char infile[256], outfile[256];
|
FILE* outf = fopen(outfile, "wb"); // Відкриваємо вихідний файл для запису
|
||||||
|
|
||||||
// Покажемо користувачу доступні варіанти
|
if (!inf || !outf) {
|
||||||
print_help();
|
printf("Error opening files.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
while (1) {
|
// Виконання операцій в залежності від дії
|
||||||
// Запитуємо користувача про вибір
|
if (action[1] == 'c') {
|
||||||
printf("Enter your choice (0 to exit): ");
|
// Компресія за допомогою алгоритму 1 (Huffman)
|
||||||
if (scanf("%d", &choice) != 1) {
|
if (compress_1(infile, outfile) > 0) {
|
||||||
// Якщо введення не є числом, виводимо повідомлення про помилку
|
printf("File successfully compressed using algorithm 1 (Huffman).\n");
|
||||||
fprintf(stderr, "Invalid input. Please enter a valid number (0-4).\n");
|
} else {
|
||||||
// Очищаємо буфер введення від непотрібних символів
|
printf("Error compressing file with algorithm 1 (Huffman).\n");
|
||||||
while(getchar() != '\n');
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (choice == 0) {
|
else if (action[1] == 'd') {
|
||||||
printf("Exiting program.\n");
|
// Декомпресія за допомогою алгоритму 1 (Huffman)
|
||||||
break;
|
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");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (choice < 0 || choice > 4) {
|
else if (action[1] == '2' && action[2] == '2') {
|
||||||
// Перевіряємо, чи вибір знаходиться в допустимому діапазоні
|
// Компресія за допомогою алгоритму 2 (RLE)
|
||||||
printf("Invalid choice. Please select a valid option (1-4).\n");
|
if (compress_2(infile, outfile) > 0) {
|
||||||
continue;
|
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') {
|
||||||
printf("Enter input file name: ");
|
// Декомпресія за допомогою алгоритму 2 (RLE)
|
||||||
scanf("%s", infile);
|
if (decompress_2(infile, outfile) > 0) {
|
||||||
printf("Enter output file name: ");
|
printf("File successfully decompressed using algorithm 2 (RLE).\n");
|
||||||
scanf("%s", outfile);
|
} else {
|
||||||
|
printf("Error decompressing file with algorithm 2 (RLE).\n");
|
||||||
// Перевіряємо, чи існує вхідний файл, і чи доступний вихідний файл для запису
|
|
||||||
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user