aboutsummaryrefslogtreecommitdiff
path: root/src/rle-dec.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rle-dec.c')
-rw-r--r--src/rle-dec.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/rle-dec.c b/src/rle-dec.c
new file mode 100644
index 0000000..02a006c
--- /dev/null
+++ b/src/rle-dec.c
@@ -0,0 +1,59 @@
1#include <stdlib.h>
2#include <stdio.h>
3#include <wchar.h>
4#include <locale.h>
5#include <libgen.h>
6
7#include "config.h"
8
9int
10main (int argc, char *argv[])
11{
12 setlocale (LC_ALL, "");
13
14 if (argc != 3) {
15 fprintf (stderr, "Usage: %s {infile} {outfile}\n", basename (argv[0]));
16 return EXIT_FAILURE;
17 }
18
19 FILE *istream = fopen (argv[1], "r,ccs="CCS);
20 if (istream == NULL) {
21 perror (NULL);
22 return EXIT_FAILURE;
23 }
24
25 FILE *ostream = fopen (argv[2], "w,ccs="CCS);
26 // FILE *ostream = stdout;
27 if (ostream == NULL) {
28 perror (NULL);
29 return EXIT_FAILURE;
30 }
31
32 wint_t current = fgetwc (istream);
33 while (current != WEOF) {
34 wint_t next = fgetwc (istream);
35 if (next == current) {
36 size_t count;
37 fwscanf (istream, L"%llu", &count);
38 fgetwc (istream);
39 while (count--)
40 fputwc (current, ostream);
41 current = fgetwc (istream);
42 } else {
43 fputwc (current, ostream);
44 current = next;
45 }
46 }
47
48 if (fclose (ostream) == EOF) {
49 perror (NULL);
50 return EXIT_FAILURE;
51 }
52
53 if (fclose (istream) == EOF) {
54 perror (NULL);
55 return EXIT_FAILURE;
56 }
57
58 return EXIT_SUCCESS;
59}