0066a3f9e4e267738f323046f0c8424d9875ab02
[yaz-moved-to-github.git] / src / csvtodiag.tcl
1 # This file is part of the YAZ toolkit
2 # Copyright (c) Index Data 1996-2005
3 # See the file LICENSE for details.
4 #
5 # $Id: csvtodiag.tcl,v 1.2 2005-04-22 14:51:11 adam Exp $
6 #
7 # Converts a CSV file with diagnostics to C+H file for easy
8 # maintenance
9
10 proc csvtodiag {ifiles name alias} {
11     set uname [string toupper $name]
12     set funcproto "const char *yaz_diag_${name}_str(int code)"
13     if {[string length $alias]} {
14         set funcalias "const char *${alias}(int code)"
15     }
16     set csv [open [lindex $ifiles 0] r]
17     set cfile [open [lindex $ifiles 1] w]
18     set hfile [open [lindex $ifiles 2] w]
19     set lineno 0
20     set preamble "/* Generated automatically by csvtodiag.tcl from [lindex $ifiles 0] */"
21     puts $cfile $preamble
22     puts $cfile "
23 #include \"diag-entry.h\"
24 \#include \"[lindex $ifiles 2]\"
25 struct yaz_diag_entry yaz_diag_${name}_tab\[\] = \{
26 "
27     puts $hfile $preamble
28     puts $hfile "
29 \#include <yaz/yconfig.h>
30
31 \#ifndef YAZ_DIAG_${name}_H
32 \#define YAZ_DIAG_${name}_H
33 YAZ_BEGIN_CDECL
34 YAZ_EXPORT $funcproto;"
35
36     if {[info exists funcalias]} {
37          puts $hfile "YAZ_EXPORT $funcalias;"
38 }
39     while {1} {
40         incr lineno
41         set cnt [gets $csv line]
42         if {$cnt < 0} {
43             break
44         }
45         if {[regexp {([0-9]+)[^\"]*"([^\"]*)"} $line s code msg]} {
46             puts $cfile "\{$code, \"$msg\"\},"
47
48             set m [string toupper $msg]
49             regsub -all {DUPLICATE} $m {DUP} m
50             regsub -all {SECURITY CHALLENGE} $m {SEC_CHAL} m
51             regsub -all {COULD NOT} $m {COULDNT} m
52             regsub -all {COULD NOT} $m {COULDNT} m
53             regsub -all {NOT SUPPORTED} $m {UNSUPP} m
54             regsub -all {UNSUPPORTED} $m {UNSUPP} m
55             regsub -all {COMBINATION} $m {COMBI} m
56             regsub -all {PROXIMITY} $m {PROX} m
57             regsub -all {CHARACTERS} $m {CHARS} m
58             regsub -all {CHARACTER} $m {CHAR} m
59             regsub -all {[-/,:;."' \{\}()]} $m _ m
60             set m [string map {___ _ __ _} $m]
61             if {[string length $m] > 55} {
62                 set m [string range $m 0 55]
63                 set p [string last _ $m]
64                 if {$p > 30} {
65                     set m [string range $m 0 $p]
66                 }
67             }
68             puts $hfile "\#define YAZ_${uname}_${m} $code"
69         }
70     }
71     puts $cfile "\{0, 0\}\}\;"
72     puts $cfile $funcproto
73     puts $cfile "\{"
74     puts $cfile "    return yaz_diag_to_str(yaz_diag_${name}_tab, code);"
75     puts $cfile "\}"
76
77     if {[info exists funcalias]} {
78         puts $cfile $funcalias
79         puts $cfile "\{"
80         puts $cfile "    return yaz_diag_to_str(yaz_diag_${name}_tab, code);"
81         puts $cfile "\}"
82     }
83     puts $hfile "
84 YAZ_END_CDECL
85 \#endif
86 "
87     close $csv
88     close $cfile
89     close $hfile
90 }
91