aboutsummaryrefslogtreecommitdiff
path: root/src/rle-enc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rle-enc.c')
-rw-r--r--src/rle-enc.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/rle-enc.c b/src/rle-enc.c
new file mode 100644
index 0000000..6620a81
--- /dev/null
+++ b/src/rle-enc.c
@@ -0,0 +1,57 @@
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 if (ostream == NULL) {
27 perror (NULL);
28 return EXIT_FAILURE;
29 }
30
31 wint_t current = fgetwc (istream);
32 while (current != WEOF) {
33 size_t count = 1;
34 wint_t next;
35 while ((next = fgetwc (istream)) == current)
36 ++count;
37
38 if (count == 1)
39 fwprintf (ostream, L"%lc", current);
40 else
41 fwprintf (ostream, L"%lc%lc%llu ", current, current, count);
42
43 current = next;
44 }
45
46 if (fclose (ostream) == EOF) {
47 perror (NULL);
48 return EXIT_FAILURE;
49 }
50
51 if (fclose (istream) == EOF) {
52 perror (NULL);
53 return EXIT_FAILURE;
54 }
55
56 return EXIT_SUCCESS;
57}