aboutsummaryrefslogtreecommitdiff
path: root/src/rle-enc.c
blob: 6620a810ca1df02e57f0bb988a0991ea6082bed1 (plain)
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
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
#include <libgen.h>

#include "config.h"

int
main (int argc, char *argv[])
{
  setlocale (LC_ALL, "");

  if (argc != 3) {
    fprintf (stderr, "Usage: %s {infile} {outfile}\n", basename (argv[0]));
    return EXIT_FAILURE;
  }

  FILE *istream = fopen (argv[1], "r,ccs="CCS);
  if (istream == NULL) {
    perror (NULL);
    return EXIT_FAILURE;
  }

  FILE *ostream = fopen (argv[2], "w,ccs="CCS);
  if (ostream == NULL) {
    perror (NULL);
    return EXIT_FAILURE;
  }

  wint_t current = fgetwc (istream);
  while (current != WEOF) {
    size_t count = 1;
    wint_t next;
    while ((next = fgetwc (istream)) == current)
      ++count;

    if (count == 1)
      fwprintf (ostream, L"%lc", current);
    else
      fwprintf (ostream, L"%lc%lc%llu ", current, current, count);

    current = next;
  }

  if (fclose (ostream) == EOF) {
    perror (NULL);
    return EXIT_FAILURE;
  }

  if (fclose (istream) == EOF) {
    perror (NULL);
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}