-
Notifications
You must be signed in to change notification settings - Fork 0
/
rle_decode.c
50 lines (34 loc) · 946 Bytes
/
rle_decode.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MAX_STRING_LENGTH 256
int main(int argc, char **argv) {
assert(argc == 2);
FILE *fpIn = NULL;
FILE *fpOut = NULL;
// Verify File is of Correct Type
int strLen = strlen(argv[1]);
char rleFileTag[MAX_STRING_LENGTH];
strcpy(rleFileTag, argv[1] + strLen - 4);
if(strcmp(rleFileTag, ".rle") != 0) {
printf("Invalid File Type. Must be '.rle'\n");
exit(1);
}
char outputFile[MAX_STRING_LENGTH];
strncpy(outputFile, argv[1], strLen - 4);
fpIn = fopen(argv[1], "rb");
fpOut = fopen(outputFile, "wb");
assert(fpIn != NULL);
assert(fpOut != NULL);
unsigned char current = 0;
unsigned char counter = 0;
while(fread(&counter, sizeof(counter), 1, fpIn)) {
fread(¤t, sizeof(current), 1, fpIn);
for(int i = 0; i <= counter; i++)
fwrite(¤t, sizeof(current), 1, fpOut);
}
fclose(fpIn);
fclose(fpOut);
return 0;
}