Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: 60bd7451e589ea0d8df2fafe5d05c859bee0ad07 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/awk -f
#
# Compiles the given CSV into a table definition
#
#   Copyright (C) 2016 LoVullo Associates, Inc.
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
##


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>"
}