diff options
| author | 2026-11-07 02:33:00 +0000 | |
|---|---|---|
| committer | 2026-11-07 02:38:20 +0000 | |
| commit | 795a8ecaf5656d299139e2c41fab19573bdac0c6 (patch) | |
| tree | 9d0529ad1abd7959256b139b2c9c6b7fa3e72e07 /src/rle-enc.c | |
| download | lab-00-795a8ecaf5656d299139e2c41fab19573bdac0c6.tar.gz | |
Diffstat (limited to 'src/rle-enc.c')
| -rw-r--r-- | src/rle-enc.c | 57 |
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 | |||
| 9 | int | ||
| 10 | main (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 | } | ||
