1 # This file is part of the YAZ toolkit
2 # Copyright (c) Index Data 2006-2007
3 # See the file LICENSE for details.
5 # $Id: oidtoc.tcl,v 1.7 2007-05-08 08:22:36 adam Exp $
7 # Converts a CSV file with Object identifiers to C
9 proc readoids {input} {
10 set csv [open $input r]
15 set cnt [gets $csv line]
19 if {![string compare [string index $line 0] \"]} {
22 set tokens [string map {, { }} $line]
23 if {[llength $tokens] != 3} {
24 puts "$input:$lineno: Bad line '$line'"
30 if {![info exists oids]} {
31 puts "$input:0 No OIDS"
37 proc constant_var {oid} {
38 set lname [string tolower [lindex $oid 2]]
39 set lname [string map {- _ . _ { } _ ( {} ) {}} $lname]
40 set prefix [string tolower [lindex $oid 0]]
42 return yaz_oid_${prefix}_${lname}
45 proc oid_to_xml {srcdir input xname} {
46 set oids [readoids "${input}"]
47 set xfile [open "${xname}" w]
49 puts $xfile "<!-- Generated by oidtoc.tcl from $input -->"
50 puts $xfile {<table id="standard-oids">}
51 puts $xfile {<title>Standard Object Identifiers</title>}
52 puts $xfile {<tgroup cols="4">}
53 puts $xfile {<colspec colwidth="3*" colname="name"></colspec>}
54 puts $xfile {<colspec colwidth="2*" colname="class"></colspec>}
55 puts $xfile {<colspec colwidth="4*" colspec="constant"></colspec>}
56 puts $xfile {<colspec colwidth="5*" colname="oid"></colspec>}
59 puts $xfile {<entry>Name</entry>}
60 puts $xfile {<entry>Class</entry>}
61 puts $xfile {<entry>Constant</entry>}
62 puts $xfile {<entry>OID</entry>}
64 puts $xfile {</thead>}
71 puts $xfile [lindex $oid 2]
72 puts $xfile {</entry>}
76 puts $xfile [lindex $oid 0]
77 puts $xfile {</entry>}
79 puts $xfile {<entry><literal>}
80 set v [constant_var $oid]
82 puts $xfile {</literal></entry>}
85 puts $xfile [lindex $oid 1]
86 puts $xfile {</entry>}
91 puts $xfile {</tbody>}
92 puts $xfile {</tgroup>}
94 puts $xfile {</table>}
98 proc oid_to_c {srcdir input cname hname} {
99 set oids [readoids "${input}"]
101 set cfile [open "${srcdir}/${cname}" w]
102 set hfile [open "${srcdir}/../include/yaz/${hname}" w]
104 puts $cfile "/** \\file $cname"
105 puts $hfile "/** \\file $hname"
106 set preamble " \\brief Standard Object Identifiers: Generated from $input */"
107 puts $cfile $preamble
108 puts $hfile $preamble
109 puts $hfile "\#ifndef OID_STD_H"
110 puts $hfile "\#define OID_STD_H"
112 puts $cfile "\#include <yaz/oid_db.h>"
115 puts $hfile "\#ifdef YAZ_DLL"
116 puts $hfile "\#define OID_EXPORT YAZ_EXPORT"
118 puts $hfile "\#define OID_EXPORT YAZ_IMPORT"
119 puts $hfile "\#endif"
121 puts $hfile "YAZ_BEGIN_CDECL"
124 set v [constant_var $oid]
126 puts -nonewline $cfile "YAZ_EXPORT const Odr_oid $v\[\] = \{"
127 puts -nonewline $cfile [string map {. ,} [lindex $oid 1]]
130 puts $hfile "OID_EXPORT extern const Odr_oid $v\[\];"
133 puts $cfile "YAZ_EXPORT struct yaz_oid_entry yaz_oid_standard_entries\[\] ="
136 set v [constant_var $oid]
138 puts -nonewline $cfile "\t\{CLASS_[lindex $oid 0], "
139 puts -nonewline $cfile "$v, "
140 puts -nonewline $cfile \"[lindex $oid 2]\"
144 puts $cfile "\t\{CLASS_NOP, 0, 0\}"
147 puts $hfile "OID_EXPORT extern struct yaz_oid_entry yaz_oid_standard_entries\[\];"
148 puts $hfile "YAZ_END_CDECL"
149 puts $hfile "\#endif"
154 if {[llength $argv] == 4} {
155 oid_to_c [lindex $argv 0] [lindex $argv 1] [lindex $argv 2] [lindex $argv 3]
156 } elseif {[llength $argv] == 3} {
157 oid_to_xml [lindex $argv 0] [lindex $argv 1] [lindex $argv 2]
159 puts "oidtoc.tcl srcdir csv cfile hfile"