aboutsummaryrefslogtreecommitdiff
path: root/sc2csv
blob: 2f2fae5a73e3d2ac87c57453a610b7abe0912d1d (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/gawk -f
#
# sc2csv ver. 0.1  (2001/12/4)
# Copyright (C) 2001 SIGEHUZI Tomoo (tomoo@s.email.ne.jp)

function pos(s,        n, a) {
    sub(/[0-9]*$/, " &", s);
    n = split(s, a);
    if (n != 2) return 1;
    J = a[2];
    s = a[1];
    n = split(s, a, "");
    if (n < 1) return 1;
    I = 0;
    for (i = 1; i <= n; i++) {
	if (a[i] !~ /^[A-Z]$/) return 1;
	I = I * 26 + v[a[i]];
    }
    if (I > N[J]) N[J] = I;
    return 0;
}


function invalid_line() {
    print "Invalid line:", $0 > "/dev/stderr";
    exit(1);
}


BEGIN {
    nap = split("ABCDEFGHIJKLMNOPQRSTUVWXYZ", ap, "");
    for (i = 1; i <= nap; i++) v[ap[i]] = i - 1;
}


/^\#/ {next;}


/^goto / {next;}


{
    if (NF == 0) next;
    if ($1 ~ /^fmt$/) next;
    if (pos($2) || $3 != "=") invalid_line();
    val = $0;
    sub(/^[^=]*= /, "", val);
    if ($1 == "let") {
      if (val ~ /^@dts\([0-9]*,[0-9]*,[0-9]*\)$/) {
	split(substr(val, 6, length(val) - 6), date, ",");
	a[I,J] = sprintf("%d/%d/%d", date[1], date[2], date[3]);
      } 
      else a[I,J] = val;
    }
    else if ($1 ~ /^(left|right)string$/) {
        if (val ~ /^".*"$/) {
	    s = substr(val, 2, length(val) - 2);
	    gsub(/\\t/, "\t", s);
	    gsub(/\\n/, "\n", s);
	    gsub(/\\"/, "\"\"", s);
	    a[I,J] = (s ~ /[,"\n\t]/) ? sprintf("\"%s\"", s) : s;
	}
	else a[I,J] = val;  # Much left to be done.
    }
    else invalid_line();
}

END {
    for (j = 0; j <= J; j++) {
    	for (i = 0; i < N[j]; i++) printf("%s,", a[i,j]);
	print a[i,j];
    }
}