-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTableParse.cpp
More file actions
167 lines (140 loc) · 3.89 KB
/
TableParse.cpp
File metadata and controls
167 lines (140 loc) · 3.89 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <cstdlib>
#include "PrintInfo.h"
int Tvalue[32];
int Tinput[32];
int Tsize[32];
int Telements {0};
// Very similar to pre_convert() in main.cpp
int FilterCharacters(int c) {
// ASCII codes: 32 - space, 10 - new line, 35 - #, 65-86 are A-V, 48-57 are 0-9
if(c >= 48 && c <= 57) {
return c - 48;
}
if(c >= 65 && c <= 86) {
return c - 55;
}
if(c == EOF) {
return EOF;
}
if(c == 35) {
return -3;
}
if(c == 10) {
return -4;
}
if(c == 32) {
return -5;
}
return -2;
}
// Reads the table file and stores the values in the appropriate array
int TRead(char *table_name) {
// File related variables
FILE *table_in;
// Check if file can be read
if ((table_in = fopen(table_name,"rb")) == NULL) {
// ERROR: Cannot read table
PrintError(6);
return 1;
}
// More variables for the rest of the code in this function
int a {0};
int count {0};
int part {0};
// Do some parsing code, uh, I dunno if this is the right way to do it but whatever
while(a != EOF) {
// Skip to new line if the character is a "#"
if(a == -3) {
while(a != -4 && a != EOF) {
a = fgetc(table_in);
a = FilterCharacters(a);
}
}
// Get a character
a = fgetc(table_in);
a = FilterCharacters(a);
// Put the first element from the table into Tinput[] if it doesn't already exist
if(a >= 0 && part == 0) {
count = 0;
while(count < Telements) {
if(a == Tinput[count]) {
// ERROR: Duplicate input character
PrintError(7);
return 1;
}
count ++;
}
Tinput[Telements] = a;
Tsize[Telements] = 0;
Tvalue[Telements] = 0;
a = fgetc(table_in);
a = FilterCharacters(a);
part = 1;
}
// Make sure that there is at least 1 space before the next element or get another character
if(a == -5 && part == 1) {
while(a < 0 && a != EOF) {
a = fgetc(table_in);
a = FilterCharacters(a);
}
part = 2;
} else if(part == 1) {
// ERROR: No space or invalid character
PrintError(8);
return 1;
}
// Put the second element from the table into Tvalue
if((a == 0 || a == 1) && part == 2) {
while(a == 0 || a == 1) {
Tvalue[Telements] = (Tvalue[Telements] << 1) | a ;
Tsize[Telements]++;
if(Tsize[Telements] > 8) {
// ERROR: More than 8 output bits
PrintError(9);
return 1;
}
a = fgetc(table_in);
a = FilterCharacters(a);
}
}
if((a == EOF || a == -4) && part == 2) {
part = 0;
Telements++;
} else if(part == 2 && a != -3) {
// ERROR: Invalid character
PrintError(10);
return 1;
}
}
// Print table elements
PrintInfoWithVariable(2, 0, &Telements);
count = 0;
// "Table input,output,size (dec):"
PrintInfo(3);
while(count < Telements) {
printf("%d", Tinput[count]);
printf(",%d", Tvalue[count]);
printf(",%d\n", Tsize[count]);
count++;
}
// Close file and exit
fclose(table_in);
return 0;
}
// Makes the default table
int DefTable(int n) {
unsigned char i {0b10000000};
int count {0};
int size {0};
while(((i >> size) | (n - 1)) > (n - 1)) size++;
size = 8 - size;
while(count < n) {
Tvalue[count] = count;
Tinput[count] = count;
Tsize[count] = size;
count++;
}
Telements = count;
return 0;
}