summaryrefslogtreecommitdiffstats
path: root/tcllib/examples/csv/csv2html
blob: fb2a4ba8287ce62c33971827873004602611c4a5 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env tclsh
## -*- tcl -*-
# Generate HTML table from CSV data

package require csv
package require cmdline
package require report
package require struct

# ----------------------------------------------------
# csv2html ?-sep sepchar? file...
#
# Argument processing and checks.

set sepChar ,
set title   "Title"
set usage "Usage: $argv0 ?-sep sepchar? ?-title string? file..."

while {[set ok [cmdline::getopt argv {sep.arg title.arg} opt val]] > 0} {
    #puts stderr "= $opt $val"
    switch -exact -- $opt {
	sep   {set sepChar $val}
	title {set title   $val}
    }
}
if {($ok < 0) || ([llength $argv] < 1)} {
    #puts stderr "A >>$ok<< >>[llength $argv]<<"
    puts stderr $usage
    exit -1
}

set files $argv

if {[llength $files] == 0} {
    set files -
}

# ----------------------------------------------------
# Actual processing, uses the following information from the
# commandline:
#
# files   - name of the files to read
# indices - preprocessed indices
# sepChar - separator character

::report::defstyle html {} {
    set c  [columns]
    set cl $c ; incr cl -1
    data set "<tr> [split [string repeat " " $cl] ""] </tr>"
    for {set col 0} {$col < $c} {incr col} {
	pad $col left  "<td>"
	pad $col right "</td>"
    }
    return
}

set stdin 1
set first 1

struct::matrix::matrix m

foreach f $files {
    if {![string compare $f -]} {
	if {!$stdin} {
	    puts stderr "Cannot use - (stdin) more than once"
	    exit -1
	}
	set in stdin
	set stdin 0
    } else {
	set in [open $f r]
    }

    if {$first} {
	set first 0
	if {[gets $in line] < 0} {
	    continue
	}
	set data [::csv::split $line $sepChar]

	m add columns [llength $data]
	m add row $data
    }

    csv::read2matrix $in m $sepChar

    if {[string compare $f -]} {
	close $in
    }
}

# And writing the accumulated results

report::report r [m columns] style html

puts stdout "<html><head><title>$title</title></head><body>"
puts stdout "<h1>$title</h1>"
puts stdout "<p><table border=1>"
r printmatrix2channel m stdout
#m format 2chan r stdout
puts stdout "</table></p></body></html>"
r destroy

exit