1 # This file is part of the YAZ toolkit
2 # Copyright (c) Index Data 1996-2005
3 # See the file LICENSE for details.
5 # $Id: csvtodiag.tcl,v 1.2 2005-04-22 14:51:11 adam Exp $
7 # Converts a CSV file with diagnostics to C+H file for easy
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)"
16 set csv [open [lindex $ifiles 0] r]
17 set cfile [open [lindex $ifiles 1] w]
18 set hfile [open [lindex $ifiles 2] w]
20 set preamble "/* Generated automatically by csvtodiag.tcl from [lindex $ifiles 0] */"
23 #include \"diag-entry.h\"
24 \#include \"[lindex $ifiles 2]\"
25 struct yaz_diag_entry yaz_diag_${name}_tab\[\] = \{
29 \#include <yaz/yconfig.h>
31 \#ifndef YAZ_DIAG_${name}_H
32 \#define YAZ_DIAG_${name}_H
34 YAZ_EXPORT $funcproto;"
36 if {[info exists funcalias]} {
37 puts $hfile "YAZ_EXPORT $funcalias;"
41 set cnt [gets $csv line]
45 if {[regexp {([0-9]+)[^\"]*"([^\"]*)"} $line s code msg]} {
46 puts $cfile "\{$code, \"$msg\"\},"
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]
65 set m [string range $m 0 $p]
68 puts $hfile "\#define YAZ_${uname}_${m} $code"
71 puts $cfile "\{0, 0\}\}\;"
72 puts $cfile $funcproto
74 puts $cfile " return yaz_diag_to_str(yaz_diag_${name}_tab, code);"
77 if {[info exists funcalias]} {
78 puts $cfile $funcalias
80 puts $cfile " return yaz_diag_to_str(yaz_diag_${name}_tab, code);"