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