-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhonkpack_template.c
218 lines (180 loc) · 3.51 KB
/
honkpack_template.c
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int read_byte(FILE *input, uint8_t *b)
{
int result = fgetc(input);
if (result == EOF)
{
if (!feof(input))
{
fprintf(stderr, "Failed to read from input.");
exit(EXIT_FAILURE);
}
return 0;
}
*b = (uint8_t)result;
return 1;
}
static void write_byte(FILE *output, uint8_t b)
{
if (fputc(b, output) == EOF)
{
fprintf(stderr, "Failed to write to output.");
exit(EXIT_FAILURE);
}
}
void loding_bar(long int anzahl, long int ges)
{
float pr = (float)anzahl / (float)ges * 100;
fprintf(stderr, "\r");
fprintf(stderr, "%.0f%%", pr);
fflush(stderr);
}
typedef enum __state_t__
{
STATE_IND,
STATE_HOM,
STATE_HET,
} state_t;
int main(int argc, char **argv)
{
FILE *input = stdin;
FILE *output = stdout;
if (input == NULL || output == NULL)
{
fprintf(stderr, "Can not open the file!\n");
exit(EXIT_FAILURE);
}
if (argc > 2)
{
fprintf(stderr, "Too many arguments!\n usage: honkpack -c|-u <input >output\n");
return 1;
}
if (argc < 2)
{
fprintf(stderr, "Too few arguments!\n usage: honkpack -c|-u <input >output\n ");
return 1;
}
fseek(input, 0L, SEEK_END);
// calculating the size of the file
long int ges = ftell(input);
fseek(input, 0L, SEEK_SET);
//State machine:
uint8_t b = 0x00;
size_t count = 0;
state_t state = STATE_IND;
long int byte_count = 0;
if (strcmp(argv[1], "-u") == 0)
{
while (read_byte(input, &b))
{
byte_count++;
loding_bar(byte_count, ges);
switch (state)
{
case STATE_IND:
count = (size_t)(b & 0x7F); //Binary: 0b01111111
state = (b >> 7) ? STATE_HOM : (count > 0) ? STATE_HET : STATE_IND;
break;
case STATE_HOM:
for (size_t i = 0; i < count; i++)
{
write_byte(output, b);
}
state = STATE_IND;
break;
case STATE_HET:
write_byte(output, b);
if (--count == 0)
{
state = STATE_IND;
}
break;
}
}
}
else if (strcmp(argv[1], "-c") == 0)
{
uint8_t a_byte[127];
uint8_t honk_byte = 0x00;
while (read_byte(input, &b))
{
byte_count++;
loding_bar(byte_count, ges);
switch (state)
{
case STATE_IND:
if (count > 0)
{
if (a_byte[count - 1] == b)
state = STATE_HOM;
else
state = STATE_HET;
}
count++;
a_byte[count - 1] = b;
break;
case STATE_HOM:
if (b != a_byte[0] || count == 127)
{
state = STATE_IND;
honk_byte = 0x80 | (uint8_t)count;
write_byte(output, honk_byte);
write_byte(output, a_byte[0]);
count = 1;
a_byte[0] = b;
}
else
{
count++;
}
break;
case STATE_HET:
if (b != a_byte[count - 1] && count < 127)
{
a_byte[count] = b;
count++;
}
else
{
honk_byte = 0x00 | (uint8_t)count;
write_byte(output, honk_byte);
for (int i = 0; i < count; i++)
write_byte(output, a_byte[i]);
count = 1;
a_byte[count - 1] = b;
state = STATE_IND;
}
break;
}
}
switch (state)
{
case STATE_IND:
break;
case STATE_HOM:
honk_byte = 0x80 | (uint8_t)count;
write_byte(output, honk_byte);
write_byte(output, a_byte[count - 1]);
break;
case STATE_HET:
honk_byte = (uint8_t)count;
write_byte(output, honk_byte);
for (int i = 0; i < count; i++)
write_byte(output, a_byte[i]);
count = 1;
break;
}
}
else
{
fprintf(stderr, "wrong argument!\n honkpack -c|-u <input >output\n");
return 1;
}
//Close the files:
fprintf(stderr, "\n");
fclose(input);
fclose(output);
}