-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.c
More file actions
145 lines (132 loc) · 3.54 KB
/
decode.c
File metadata and controls
145 lines (132 loc) · 3.54 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include "A3header.h"
/*
* Program name: decode.c
* Author: Rehan Nagoor Mohideen
* Student ID: 1100592
* Purpose: main function that takes in text and print out the decoded text.
*/
int main(int argc, char *argv[]){
int opt;
char InputFile[100], OutputFile[100];
int FCheck = 0, OCheck = 0, nCheck = 0, sCheck = 0, SCheck = 0, tCheck = 0, xCheck = 0;
int encodeShift, decodeShift;
char line;
int noChar = 1, i;
FILE *fp;
char *string = malloc(sizeof(char));
if (string == NULL) { /*For error handling*/
fprintf(stderr, "error: not enough space for malloc\n");
exit(1);
}
while ((opt = getopt(argc, argv, "F:O:nsStx")) != -1) {
switch (opt) {
case 'F':
strcpy(InputFile, optarg);
FCheck = 1;
break;
case 'O':
strcpy(OutputFile, optarg);
OCheck = 1;
break;
case 'n':
nCheck = 1;
break;
case 's':
sCheck = 1;
break;
case 'S':
SCheck = 1;
break;
case 't':
tCheck = 1;
break;
case 'x':
xCheck = 1;
break;
case '?':
printf("unknown option: %c\n", optopt);
break;
}
}
/*To get input and take information either from file or stdin*/
if (FCheck == 1) {
if((fp = fopen(InputFile, "r")) != NULL ){
if ( ((line =fgetc ( fp ))) != EOF ) {
string[0]=line;
}
while ( (line =fgetc ( fp )) != EOF ) {
string = realloc(string, sizeof(char) * ++noChar);
if (string == NULL) { /*For error handling*/
fprintf(stderr, "error: not enough space for realloc\n");
exit(1);
}
string[noChar-1]=line;
}
}
fclose(fp);
}else {
if ( (line =fgetc( stdin )) != EOF ) {
string[0]=line;
}
while ( ((line =fgetc ( stdin ))) != EOF ) {
string = realloc(string, sizeof(char) * ++noChar);
if (string == NULL) { /*For error handling*/
fprintf(stderr, "error: not enough space for realloc\n");
exit(1);
}
string[noChar-1]=line;
}
}
encodeShift = encode_shift(string);
decodeShift = to_decode(encodeShift);
int totalLetters = letter_count(string);
char *decodedString = malloc(sizeof(char) * noChar);
if (decodedString == NULL) { /*For error handling*/
fprintf(stderr, "error: not enough space for malloc\n");
exit(1);
}
for (i = 0; i < noChar; i++) {
decodedString[i] = encode(string[i], decodeShift);
}
if (sCheck == 1) {
printf("decode shift = %d\n\n", decodeShift);
}
if (SCheck == 1) {
printf("encode shift = %d\n\n", encodeShift);
}
if (tCheck == 1) {
int *freqTable = frequency_table(string);
printf("Total no. of letter: %d, Total no. of characters: %ld\n", totalLetters, strlen(string));
printf("Letter\tTotal no.\n");
for (i = 0; i < 26; i++) {
printf("%c:\t%d\n", 'A'+i, freqTable[i]);
}
printf("\n");
free(freqTable);
}
if (xCheck == 1) {
int *freqTable1 = frequency_table(string);
printf("Shift\tChi_square(shift)\n");
for (i = 0; i < 26; i++) {
printf("%d:\t%lf\n", i, chi_sq(i, freqTable1));
}
printf("\n");
free(freqTable1);
}
/*To output information to a file or stdout if needed*/
if (OCheck == 1) {
if((fp = fopen(OutputFile, "w")) != NULL ){
fprintf(fp, "%s", decodedString);
}
fclose(fp);
}else if(nCheck == 0){
printf("%s\n", decodedString);
}
free(string);
free(decodedString);
return 0;
}