diff options
-rw-r--r-- | README.md | 0 | ||||
-rw-r--r-- | TODO.md | 0 | ||||
-rwxr-xr-x | csv2sc | 76 | ||||
-rwxr-xr-x | sc2csv | 73 |
4 files changed, 149 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/README.md @@ -0,0 +1,76 @@ +#!/usr/bin/gawk -f +# +# csv2sc ver. 0.1 (2001/12/4) +# Copyright (C) 2001 SIGEHUZI Tomoo (tomoo@s.email.ne.jp) + +function is_date(s) { + if (split(s, date, "/") != 3) return 0; + if (date[1] !~ /(19|20)?[0-9][0-9]/) return 0; + if (date[2] !~ /(0?[1-9]|1[012])/) return 0; + if (date[2] !~ /(0?[1-9]|[12][0-9]|3[01])/) return 0; + return 1; +} + +function i_index(n, i, s) { + s = ""; + for (s = ""; ; n = (n - i) / nix - 1) { + i = n % nix; + s = sprintf("%s%s", ix[i], s); + if (n < 1) break; + } + return s; +} + +BEGIN { + nix = split("BCDEFGHIJKLMNOPQRSTUVWXYZ", ix, "") + 1; + ix[0] = "A"; +} + +{ + s = sprintf("%s%s", s, $0); + np += gsub(/"/, "\a"); + if (np % 2) { + s = sprintf("%s\n", s); + next; + } + m = n = split(s, a, ","); + + np = 0; + s = ""; + for (i = j = 1; i <= n; i++) { + s = sprintf("%s%s", s, a[i]); + np += gsub(/"/, "\a", a[i]); + if (np % 2) { + s = sprintf("%s,", s); + continue; + } + np = 0; + a[j++] = s; + s = ""; + } + n = j - 1; + for (i = 1; i <= n; i++) { + s = a[i]; + sub(/^"/, "", s); + sub(/"$/, "", s); + gsub(/\\/, "\\\\", s); + gsub(/""/, "\\\"", s); + gsub(/\n/, "\\n", s); + gsub(/\t/, "\\t", s); + a[i] = s; + } + + for (i = 1; i <= n; i++) { + s = a[i]; + I = i_index(i - 1); + if (s ~ /^[0-9.]+$/) printf("let %s%d = %s\n", I, J, s); + else if (is_date(s)) + printf("let %s%d = @dts(%d,%d,%d)\nfmt %s%d \"\004%%Y/%%m/%%d\"\n", + I, J, date[1], date[2], date[3], I, J); + else printf("leftstring %s%d = \"%s\"\n", I, J, s); + } + + J++; + np = 0; + s = ""; +} @@ -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]; + } +} |