Remove the extensive logging
[yaz-moved-to-github.git] / src / csvtodiag.tcl
1 # This file is part of the YAZ toolkit
2 # Copyright (c) Index Data 1996-2007
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 "
24 #include \"diag-entry.h\"
25 \#include \"[lindex $ifiles 2]\"
26 struct yaz_diag_entry yaz_diag_${name}_tab\[\] = \{
27 "
28     puts $hfile $preamble
29     puts $hfile "
30 \#include <yaz/yconfig.h>
31
32 \#ifndef YAZ_DIAG_${name}_H
33 \#define YAZ_DIAG_${name}_H
34 YAZ_BEGIN_CDECL
35 YAZ_EXPORT $funcproto;"
36
37     if {[info exists funcalias]} {
38          puts $hfile "YAZ_EXPORT $funcalias;"
39 }
40     while {1} {
41         incr lineno
42         set cnt [gets $csv line]
43         if {$cnt < 0} {
44             break
45         }
46         if {[regexp {([0-9]+)[^\"]*"([^\"]*)"} $line s code msg]} {
47             puts $cfile "\{$code, \"$msg\"\},"
48
49             set m [string toupper $msg]
50             regsub -all {DUPLICATE} $m {DUP} m
51             regsub -all {SECURITY CHALLENGE} $m {SEC_CHAL} m
52             regsub -all {COULD NOT} $m {COULDNT} m
53             regsub -all {COULD NOT} $m {COULDNT} m
54             regsub -all {NOT SUPPORTED} $m {UNSUPP} m
55             regsub -all {UNSUPPORTED} $m {UNSUPP} m
56             regsub -all {COMBINATION} $m {COMBI} m
57             regsub -all {PROXIMITY} $m {PROX} m
58             regsub -all {CHARACTERS} $m {CHARS} m
59             regsub -all {CHARACTER} $m {CHAR} m
60             regsub -all {[-/,:;."' \{\}()]} $m _ m
61             set m [string map {___ _ __ _} $m]
62             if {[string length $m] > 55} {
63                 set m [string range $m 0 55]
64                 set p [string last _ $m]
65                 if {$p > 30} {
66                     set m [string range $m 0 $p]
67                 }
68             }
69             puts $hfile "\#define YAZ_${uname}_${m} $code"
70         }
71     }
72     puts $cfile "\{0, 0\}\}\;"
73     puts $cfile $funcproto
74     puts $cfile "\{"
75     puts $cfile "    return yaz_diag_to_str(yaz_diag_${name}_tab, code);"
76     puts $cfile "\}"
77
78     if {[info exists funcalias]} {
79         puts $cfile $funcalias
80         puts $cfile "\{"
81         puts $cfile "    return yaz_diag_to_str(yaz_diag_${name}_tab, code);"
82         puts $cfile "\}"
83     }
84     puts $hfile "
85 YAZ_END_CDECL
86 \#endif
87 "
88     close $csv
89     close $cfile
90     close $hfile
91 }
92