aboutsummaryrefslogtreecommitdiff
path: root/src/rle-dec.c
blob: 02a006c324a2e4638f9e140d0977aca3f9307548 (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
58
59
#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);
  // FILE *ostream = stdout;
  if (ostream == NULL) {
    perror (NULL);
    return EXIT_FAILURE;
  }

  wint_t current = fgetwc (istream);
  while (current != WEOF) {
    wint_t next = fgetwc (istream);
    if (next == current) {
      size_t count;
      fwscanf (istream, L"%llu", &count);
      fgetwc (istream);
      while (count--)
        fputwc (current, ostream);
      current = fgetwc (istream);
    } else {
      fputwc (current, ostream);
      current = next;
    }
  }

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

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

  return EXIT_SUCCESS;
}