diff options
Diffstat (limited to 'sc2csv')
-rwxr-xr-x | sc2csv | 73 |
1 files changed, 73 insertions, 0 deletions
@@ -0,0 +1,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]; + } +} |