-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftstore.cpp
162 lines (106 loc) · 2.98 KB
/
ftstore.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "F53Encoder.h"
#include "F53Decoder.h"
#include "F53File.h"
using namespace std;
void createData(bool fromFile, int argc, char **argv) {
int hasOut = -1;
for (int i = 0; i < argc; i++) {
if (strlen(argv[i]) == 2 && !strcmp(argv[i], "-o")) {
hasOut = i;
break;
}
}
//Get output
char *input;
if (fromFile) {
struct stat st;
if (stat(argv[2], &st) != 0) {
cout << "Unable to open file " << argv[2] << endl;
exit(-1);
}
//check its there
FILE *pFile = fopen(argv[2], "r");
input = (char *)calloc(st.st_size+1,sizeof(char));
fread(input, 1, st.st_size, pFile);
fclose(pFile);
} else {
input = (char *)calloc(strlen(argv[2])+1,sizeof(char));
strcpy(input, argv[2]);
}
F53Encoder encoder;
int length = 0;
uint6_t *digits = encoder.encodeStatement(input, &length);
if (hasOut == -1) {
//print to the console or w.e
for (int i = 0; i < length; i++) {
cout << digits[i].b1 << digits[i].b2 << digits[i].b3 << digits[i].b4 << digits[i].b5 << digits[i].b6;
}
cout << endl;
} else {
FILE *pFile = fopen(argv[hasOut+1], "w+");
fclose(pFile);
F53File file((char *)argv[hasOut+1]);
file.writeData(digits, length);
}
delete digits;
delete input;
}
void extractData(bool afile, int argc, char **argv) {
if (!afile) {
cout << "Does not support decoding through input from cmdline"<<endl<<"Try doing echo \""<<argv[2]<<"\" > poop.bin && "<<argv[0]<<" "<<argv[1]<<" poop.bin"<<endl;
exit(-1);
}
int hasOut = -1;
for (int i = 0; i < argc; i++) {
if (strlen(argv[i]) == 2 && !strcmp(argv[i], "-o")) {
hasOut = i;
break;
}
}
//Get output
F53File reader((char *)argv[2]);
int length = 0;
uint6_t *adigits = reader.getData(&length);
F53Decoder decoder;
char *aresult = decoder.decodeStatement(adigits, length);
if (hasOut == -1) {
cout << aresult << endl;
} else {
FILE *pFile = fopen(argv[hasOut+1], "w+");
fwrite(aresult, 1, strlen(aresult)+1, pFile);
fclose(pFile);
}
}
void printHelp(int argc, char **argv) {
cout <<endl;
cout << '\t' << argv[0] << " -c[f] <data> [-o output]"<< endl << "\t\t" <<" data is a file name or a plain string"<<endl<<endl;
cout << '\t' << argv[0] << " -x <file> [-o output]"<<endl<<endl;
cout << "6 bit 53store ftw"<<endl<<endl;
}
int main(int argc, char **argv) {
int args = argc - 1; //just easier to think about when coding
if (args <= 0) {
printHelp(argc,argv);
exit(-1);
}
string firstFlag = argv[1];
if (argv[1][0] == '-')
firstFlag = firstFlag.substr(1,firstFlag.length()-1);
switch (firstFlag.c_str()[0]) {
case 'c':
createData(firstFlag.c_str()[1] == 'f', argc, argv);
break;
case 'x':
extractData(firstFlag.c_str()[1] == 'f', argc, argv);
break;
default:
printHelp(argc,argv);
exit(-1);
break;
}
return 0;
}