Fixed bug #2068: pkg-config trouble.
[yaz-moved-to-github.git] / src / oidtoc.tcl
1 # This file is part of the YAZ toolkit
2 # Copyright (c) Index Data 2006-2007
3 # See the file LICENSE for details.
4 #
5 # $Id: oidtoc.tcl,v 1.8 2007-05-31 12:14:02 adam Exp $
6 #
7 # Converts a CSV file with Object identifiers to C
8
9 proc readoids {input} {
10     set csv [open $input r]
11     set lineno 0 
12
13     while {1} {
14         incr lineno
15         set cnt [gets $csv line]
16         if {$cnt < 0} {
17             break
18         }
19         if {![string compare [string index $line 0] \"]} {
20             continue
21         }
22         set tokens [string map {, { }} $line]
23         if {[llength $tokens] != 3} {
24             puts "$input:$lineno: Bad line '$line'"
25             exit 1
26         }
27         lappend oids $tokens
28     }
29     close $csv
30     if {![info exists oids]} {
31         puts "$input:0 No OIDS"
32         exit 1
33     }
34     return $oids
35 }
36
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]]
41         
42     return yaz_oid_${prefix}_${lname}
43 }
44
45 proc oid_to_xml {srcdir input xname} {
46     set oids [readoids "${input}"]
47     set xfile [open "${xname}" w]
48
49     puts $xfile "<!-- Generated by oidtoc.tcl from $input -->"
50     puts $xfile {<informaltable id="standard-oids">}
51     puts $xfile {<tgroup cols="3">}
52     puts $xfile {<colspec colwidth="3*" colname="name"></colspec>}
53     puts $xfile {<colspec colwidth="2*" colname="class"></colspec>}
54     puts $xfile {<colspec colwidth="4*" colname="oid"></colspec>}
55     puts $xfile {<thead>}
56     puts $xfile {<row>}
57     puts $xfile {<entry>Name</entry>}
58     puts $xfile {<entry>Class</entry>}
59     puts $xfile {<entry>Constant / OID</entry>}
60     puts $xfile {</row>}
61     puts $xfile {</thead>}
62     puts $xfile {<tbody>}
63
64     foreach oid $oids {
65         puts $xfile {<row>}
66
67         puts $xfile {<entry morerows="1">}
68         puts $xfile [lindex $oid 2]
69         puts $xfile {</entry>}
70
71
72         puts $xfile {<entry morerows="1">}
73         puts $xfile [lindex $oid 0]
74         puts $xfile {</entry>}
75
76         puts $xfile {<entry><literal>}
77         set v [constant_var $oid]
78         puts $xfile $v
79         puts $xfile {</literal></entry>}
80
81
82         puts $xfile {</row>}
83         puts $xfile {<row>}
84
85         puts $xfile {<entry namest="oid">}
86         puts $xfile [lindex $oid 1]
87         puts $xfile {</entry>}
88
89         puts $xfile {</row>}
90     }
91
92     puts $xfile {</tbody>}
93     puts $xfile {</tgroup>}
94
95     puts $xfile {</informaltable>}
96     close $xfile
97 }
98
99 proc oid_to_c {srcdir input cname hname} {
100     set oids [readoids "${input}"]
101
102     set cfile [open "${srcdir}/${cname}" w]
103     set hfile [open "${srcdir}/../include/yaz/${hname}" w]
104
105     puts $cfile "/** \\file $cname"
106     puts $hfile "/** \\file $hname"
107     set preamble "    \\brief Standard Object Identifiers: Generated from $input */"
108     puts $cfile $preamble
109     puts $hfile $preamble
110     puts $hfile "\#ifndef OID_STD_H"
111     puts $hfile "\#define OID_STD_H"
112
113     puts $cfile "\#include <yaz/oid_db.h>"
114     puts $cfile ""
115     # To avoid LNK4049
116     puts $hfile "\#ifdef YAZ_DLL"
117     puts $hfile "\#define OID_EXPORT YAZ_EXPORT"
118     puts $hfile "\#else"
119     puts $hfile "\#define OID_EXPORT YAZ_IMPORT"
120     puts $hfile "\#endif"
121
122     puts $hfile "YAZ_BEGIN_CDECL"
123     foreach oid $oids {
124
125         set v [constant_var $oid]
126         
127         puts -nonewline $cfile "YAZ_EXPORT const Odr_oid $v\[\] = \{"
128         puts -nonewline $cfile [string map {. ,} [lindex $oid 1]]
129         puts $cfile ",-1\};"
130
131         puts $hfile "OID_EXPORT extern const Odr_oid $v\[\];"
132     }
133
134     puts $cfile "YAZ_EXPORT struct yaz_oid_entry yaz_oid_standard_entries\[\] ="
135     puts $cfile "\{"
136     foreach oid $oids {
137         set v [constant_var $oid]
138         
139         puts -nonewline $cfile "\t\{CLASS_[lindex $oid 0], "
140         puts -nonewline $cfile "$v, "
141         puts -nonewline $cfile \"[lindex $oid 2]\"
142         puts $cfile "\},"
143     }
144
145     puts $cfile "\t\{CLASS_NOP, 0, 0\}"
146     puts $cfile "\};"
147
148     puts $hfile "OID_EXPORT extern struct yaz_oid_entry yaz_oid_standard_entries\[\];"
149     puts $hfile "YAZ_END_CDECL"
150     puts $hfile "\#endif"
151     close $cfile
152     close $hfile
153 }
154
155 if {[llength $argv] == 4} {
156     oid_to_c [lindex $argv 0] [lindex $argv 1] [lindex $argv 2] [lindex $argv 3]
157 } elseif {[llength $argv] == 3} {
158     oid_to_xml [lindex $argv 0] [lindex $argv 1] [lindex $argv 2]
159 } else {
160     puts "oidtoc.tcl srcdir csv cfile hfile"
161     exit 1
162 }