Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Gerwitz <gerwitm@lovullo.com>2016-08-24 09:43:05 -0400
committerMike Gerwitz <gerwitm@lovullo.com>2016-08-24 12:38:00 -0400
commitff01f39c1e8c9b9549d884a0db1f9a74799cf37e (patch)
tree35978db88a8d385250b1b47ad05966e19516373d /src/current/tools/csv2xml
parent6c0aa54bd1b7b49d736f0db3a8f48b7aa90b3b65 (diff)
downloadtame-ff01f39c1e8c9b9549d884a0db1f9a74799cf37e.tar.gz
tame-ff01f39c1e8c9b9549d884a0db1f9a74799cf37e.tar.bz2
tame-ff01f39c1e8c9b9549d884a0db1f9a74799cf37e.zip
Liberate current implementation of "Calc DSL"
(Copyright headers will be added in the next commit; these are the original files, unaltered in any way.) The internal project name at LoVullo is simply "Calc DSL". This liberates the entire thing. If anything was missed, I'll be added later. To continue building at LoVullo with this move, symlinks are used for the transition; this is the exact code that is used in production. There is a lot here---over 25,000 lines. Much of it is in disarray from the environment surrounding its development, but it does work well for what it was intended to do. (LoVullo folks: fork point is 65723a0 in calcdsl.git.)
Diffstat (limited to 'src/current/tools/csv2xml')
-rwxr-xr-xsrc/current/tools/csv2xml110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/current/tools/csv2xml b/src/current/tools/csv2xml
new file mode 100755
index 0000000..3a5d1b8
--- /dev/null
+++ b/src/current/tools/csv2xml
@@ -0,0 +1,110 @@
+#!/usr/bin/awk -f
+#
+# Compiles the given CSV into a table definition
+
+
+function columngen( header )
+{
+ # output a field constant for each field in the header
+ i = 0
+ while ( field = header[ ++i ] )
+ {
+ printf " <t:table-column name=\"%s\" " \
+ "index=\"%d\" seq=\"%s\" />\n",
+ field,
+ ( i - 1 ),
+ ( seq[ i ] ) ? "true" : "false"
+ }
+}
+
+
+function seqchk( last )
+{
+ # if there's no last row, then do not bother
+ i = 0
+ while ( i++ < NF )
+ {
+ if ( seq[ i ] == "" ) seq[ i ] = 1
+
+ # this field is sequential if it is greater than or equal to the last field
+ # (we don't check for descending [yet]); note that on the first check, last
+ # will be empty and therefore this check will succeed (properly
+ # initializing seq[i] to 1)
+ seq[ i ] = seq[ i ] && ( $(i) >= last[ i ] )
+ }
+}
+
+
+# header
+BEGIN {
+ rootpath = "../../../"
+ file = ARGV[1]
+
+ # grab only the filename (remove all preceding directories and the file ext)
+ name = gensub( /^.*\/|\.[^.]+$/, "", "g", file )
+
+
+ # output package header
+ printf \
+ "<?xml-stylesheet type=\"text/xsl\" href=\"%1$srater/summary.xsl\"?>\n" \
+ "<package\n" \
+ " xmlns=\"http://www.lovullo.com/rater\"\n" \
+ " xmlns:c=\"http://www.lovullo.com/calc\"\n" \
+ " xmlns:t=\"http://www.lovullo.com/rater/apply-template\"\n" \
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" \
+ " xsi:schemaLocation=\"http://www.lovullo.com/rater %1$srater/rater.xsd\"\n\n" \
+ " name=\"suppliers/rates/tables/%2$s\"\n" \
+ " desc=\"%2$s rate table\">\n\n" \
+ " <!--\n" \
+ " WARNING: This file was generated by csv2xml; do not modify!\n" \
+ " -->\n\n" \
+ " <import package=\"/rater/core\" />\n" \
+ " <import package=\"/rater/core/vector/table\" />\n\n", \
+ rootpath, name
+
+ # the first row of the CSV is the header representing the column identifiers
+ getline
+ split( $0, header, /,/ )
+
+ # table constant identifier
+ tconst = toupper( gensub( /-/, "_", "g", name ) ) "_RATE_TABLE"
+
+ # generate the header for the table constant
+ printf " <t:create-table name=\"%s\">\n", name
+
+ printf "%s", " <t:table-rows data=\"\n"
+
+ # delimit fields by commas (the field separator for CSVs); note that this
+ # won't work properly if strings contain commas
+ FS = ","
+}
+
+
+# each row of the CSV
+{
+ # generate value string for each field
+ i = 0
+ while ( i++ < NF )
+ {
+ printf "%s", ( ( i > 1 ) ? "," : "" ) $(i)
+ }
+
+ print ";"
+
+ seqchk( last )
+ split( $0, last )
+}
+
+
+# footer
+END {
+ # end of table-rows node
+ print "\" />"
+
+ # columns can't be generated until after we know which ones represent
+ # sequential data
+ columngen( header )
+
+ print " </t:create-table>"
+ print "</package>"
+}