GFS: scan handler gets extra_args from request
[yaz-moved-to-github.git] / src / csvtodiag.tcl
1 # This file is part of the YAZ toolkit
2 # Copyright (c) Index Data 1996-2013
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