aboutsummaryrefslogtreecommitdiff
path: root/csv2sc
blob: 8efb54b41d4535807577961a5057397751513cc1 (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
74
75
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 = "";
}