-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
104 lines (87 loc) · 2.96 KB
/
main.c
File metadata and controls
104 lines (87 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freq.h"
#include "compress.h"
#include "decompress.h"
void usage_print(){
printf("\n");
printf("====================================================\n");
printf(" Welcome to the Geeks 5 File Compressor\n");
printf("====================================================\n\n");
printf("Usage:\n");
printf(" ./main <filename> --compress\n");
printf(" ./main <filename> --decompress\n\n");
printf("Examples:\n");
printf(" ./main sample.txt --compress\n");
printf(" ./main sample.g5 --decompress\n\n");
printf("Output:\n");
printf(" Compression creates a .g5 file\n");
printf(" Decompression creates a .txt file\n\n");
printf("Tip: You will be asked to enter the output file name after running the command.\n\n");
}
long get_file_size(const char *filename) {
FILE *file = fopen(filename, "rb");
long size;
if (!file) {
return -1;
}
fseek(file, 0, SEEK_END);
size = ftell(file);
fclose(file);
return size;
}
int main(int argc, char *argv[]){
if (argc != 3){
usage_print();
return 1;
}
else{
if(strcmp(argv[2],"--compress")==0){
FILE* file = fopen(argv[1], "r");
if (!file){
printf("Unable to Access the file\n");
return 1;
}
long freq[256];
count_frequency(file, freq);
fclose(file);
print_frequency(freq);
char output[50];
printf("Enter out compressed file name: ");
scanf("%s", output);
printf("Output file name: %s", output);
if (compress(argv[1], output) == 0) {
char compressed_file[60];
strcpy(compressed_file, output);
printf("Compressed: %s -> %s\n", argv[1], compressed_file);
printf("Original size: %ld bytes\n", get_file_size(argv[1]));
printf("Compressed size: %ld bytes\n", get_file_size(compressed_file));
}
else {
return 1;
}
}
else if (strcmp(argv[2], "--decompress") == 0) {
if (argc != 3) { usage_print(); return 1; }
char output[50];
printf("Enter output decompressed file name: ");
scanf("%s", output);
printf("Output file name: %s", output);
if (decompress(argv[1], output) == 0) {
char decompressed_file[60];
strcpy(decompressed_file, output);
printf("Decompressed: %s -> %s\n", argv[1], decompressed_file);
printf("Compressed size: %ld bytes\n", get_file_size(argv[1]));
printf("Decompressed size: %ld bytes\n", get_file_size(decompressed_file));
}
else {
return 1;
}
}
else{
usage_print();
}
}
return 0;
}