Merge branch 'master' of ssh://git.indexdata.com/home/git/pub/yaz
[yaz-moved-to-github.git] / src / csvtodiag.tcl
1 # This file is part of the YAZ toolkit
2 # Copyright (C) Index Data
3 # See the file LICENSE for details.
4 #
5 #
6 # Converts a CSV file with diagnostics to C+H file for easy
7 # maintenance
8
9 proc csvtodiag {ifiles name alias} {
10     set uname [string toupper $name]
11     set funcproto "const char *yaz_diag_${name}_str(int code)"
12     if {[string length $alias]} {
13         set funcalias "const char *${alias}(int code)"
14     }
15     set csv [open [lindex $ifiles 0] r]
16     set cfile [open [lindex $ifiles 1] w]
17     set hfile [open [lindex $ifiles 2] w]
18     set lineno 0
19     puts $cfile "/** \\file [lindex $ifiles 1]"
20     puts $hfile "/** \\file [lindex $ifiles 2]"
21     set preamble "    \\brief Diagnostics: Generated by csvtodiag.tcl from [lindex $ifiles 0] */"
22     puts $cfile $preamble
23     puts $cfile "\#ifdef HAVE_CONFIG_H"
24     puts $cfile "\#include <config.h>"
25     puts $cfile "\#endif"
26
27     puts $cfile "
28 #include \"diag-entry.h\"
29 \#include \"[lindex $ifiles 2]\"
30 struct yaz_diag_entry yaz_diag_${name}_tab\[\] = \{
31 "
32     puts $hfile $preamble
33     puts $hfile "
34 \#include <yaz/yconfig.h>
35
36 \#ifndef YAZ_DIAG_${name}_H
37 \#define YAZ_DIAG_${name}_H
38 YAZ_BEGIN_CDECL
39 YAZ_EXPORT $funcproto;"
40
41     if {[info exists funcalias]} {
42          puts $hfile "YAZ_EXPORT $funcalias;"
43 }
44     while {1} {
45         incr lineno
46         set cnt [gets $csv line]
47         if {$cnt < 0} {
48             break
49         }
50         if {[regexp {([0-9]+)[^\"]*"([^\"]*)"} $line s code msg]} {
51             puts $cfile "\{$code, \"$msg\"\},"
52
53             set m [string toupper $msg]
54             regsub -all {DUPLICATE} $m {DUP} m
55             regsub -all {SECURITY CHALLENGE} $m {SEC_CHAL} m
56             regsub -all {COULD NOT} $m {COULDNT} m
57             regsub -all {COULD NOT} $m {COULDNT} m
58             regsub -all {NOT SUPPORTED} $m {UNSUPP} m
59             regsub -all {UNSUPPORTED} $m {UNSUPP} m
60             regsub -all {COMBINATION} $m {COMBI} m
61             regsub -all {PROXIMITY} $m {PROX} m
62             regsub -all {CHARACTERS} $m {CHARS} m
63             regsub -all {CHARACTER} $m {CHAR} m
64             regsub -all {[-/,:;."' \{\}()]} $m _ m
65             set m [string map {___ _ __ _} $m]
66             if {[string length $m] > 55} {
67                 set m [string range $m 0 55]
68                 set p [string last _ $m]
69                 if {$p > 30} {
70                     set m [string range $m 0 $p]
71                 }
72             }
73             puts $hfile "\#define YAZ_${uname}_${m} $code"
74         }
75     }
76     puts $cfile "\{0, 0\}\}\;"
77     puts $cfile $funcproto
78     puts $cfile "\{"
79     puts $cfile "    return yaz_diag_to_str(yaz_diag_${name}_tab, code);"
80     puts $cfile "\}"
81
82     if {[info exists funcalias]} {
83         puts $cfile $funcalias
84         puts $cfile "\{"
85         puts $cfile "    return yaz_diag_to_str(yaz_diag_${name}_tab, code);"
86         puts $cfile "\}"
87     }
88     puts $hfile "
89 YAZ_END_CDECL
90 \#endif
91 "
92     close $csv
93     close $cfile
94     close $hfile
95 }
96
97 proc csvto_xml {csvfname xmlfname id} {
98     set xfile [open $xmlfname w]
99     set csv [open $csvfname r]
100
101     puts $xfile "<!-- Generated by csvtodiag.tcl from $csvfname -->"
102     puts $xfile "<informaltable id=\"${id}\">"
103     puts $xfile {<tgroup cols="2">}
104     puts $xfile {<colspec colwidth="1*" colname="code"></colspec>}
105     puts $xfile {<colspec colwidth="4*" colname="text"></colspec>}
106     puts $xfile {<thead>}
107     puts $xfile {<row>}
108     puts $xfile {<entry>Code</entry>}
109     puts $xfile {<entry>Text</entry>}
110     puts $xfile {</row>}
111     puts $xfile {</thead>}
112     puts $xfile {<tbody>}
113
114     set lineno 0
115     while {1} {
116         incr lineno
117         set cnt [gets $csv line]
118         if {$cnt < 0} {
119             break
120         }
121         if {[regexp {([0-9]+)[^\"]*"([^\"]*)"} $line s code msg]} {
122             puts $xfile {<row>}
123             puts $xfile {<entry>}
124             puts $xfile $code
125             puts $xfile {</entry><entry>}
126             puts $xfile $msg
127             puts $xfile {</entry>}
128             puts $xfile {</row>}
129         }
130     }
131     puts $xfile {</tbody>}
132     puts $xfile {</tgroup>}
133     puts $xfile {</informaltable>}
134
135     close $xfile
136     close $csv
137 }
138
139 if {[llength $argv] >= 4} {
140     set alias {}
141     if {[llength $argv] >= 5} {
142         set alias [lindex $argv 4]
143     }
144     csvtodiag [list [lindex $argv 0] [lindex $argv 1] [lindex $argv 2]] \
145         [lindex $argv 3] $alias
146 } elseif {[llength $argv] == 3} {
147     csvto_xml [lindex $argv 0] [lindex $argv 1] [lindex $argv 2]
148 }