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