summaryrefslogtreecommitdiffstats
path: root/abctext.c
blob: 09b16f14b241b144d080a4f5407de427676cf919 (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 <stdio.h>
#include <string.h>

/*
 * Convert an ABC text file in binary form to more normal
 * form.  This does not do character set conversions.
 */

int main(int argc, char *argv[])
{
  FILE *f;
  char buf[253], *p;
  int n, i;
  int was_tab;

  if (argc > 1)
    f = fopen(argv[1], "rb");
  else
    f = stdin;

  was_tab = 0;

  while ((n = fread(buf, 1, 253, f)) > 0) {
    if (!memcmp(buf, "\0\0\0\0\0\0", 6))
      break;			/* End of file */

    p = buf;
    while (n--) {
      if (was_tab) {
	/* TAB + count is used for compressed spaces */
	i = *p;
	while (i--)
	  putchar(' ');
	was_tab = 0;
      } else {
	switch (*p) {
	case '\t':
	  was_tab = 1;
	  break;
	case '\3':
	  n = 0;		/* ETX */
	  break;
	case '\r':
	  putchar('\n');
	  break;
	default:
	  putchar(*p);
	  break;
	}
      }
      p++;
    }
  }

  if (f != stdin)
    fclose(f);

  return 0;
}