c57a56e32db9ea574546c4f97628bd9036c991e0
[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.6 2007-05-07 13:18:32 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 {<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>}
57     puts $xfile {<thead>}
58     puts $xfile {<row>}
59     puts $xfile {<entry>Name</entry>}
60     puts $xfile {<entry>Class</entry>}
61     puts $xfile {<entry>Constant</entry>}
62     puts $xfile {<entry>OID</entry>}
63     puts $xfile {</row>}
64     puts $xfile {</thead>}
65     puts $xfile {<tbody>}
66
67     foreach oid $oids {
68         puts $xfile {<row>}
69
70         puts $xfile {<entry>}
71         puts $xfile [lindex $oid 2]
72         puts $xfile {</entry>}
73
74
75         puts $xfile {<entry>}
76         puts $xfile [lindex $oid 0]
77         puts $xfile {</entry>}
78
79         puts $xfile {<entry><literal>}
80         set v [constant_var $oid]
81         puts $xfile $v
82         puts $xfile {</literal></entry>}
83
84         puts $xfile {<entry>}
85         puts $xfile [lindex $oid 1]
86         puts $xfile {</entry>}
87
88         puts $xfile {</row>}
89     }
90
91     puts $xfile {</tbody>}
92     puts $xfile {</tgroup>}
93
94     puts $xfile {</table>}
95     close $xfile
96 }
97
98 proc oid_to_c {srcdir input cname hname} {
99     set oids [readoids "${input}"]
100
101     set cfile [open "${srcdir}/${cname}" w]
102     set hfile [open "${srcdir}/../include/yaz/${hname}" w]
103
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"
111
112     puts $cfile "\#include <yaz/oid_db.h>"
113     puts $cfile ""
114     # To avoid LNK4049
115     puts $hfile "\#ifdef YAZ_DLL"
116     puts $hfile "\#define OID_EXPORT YAZ_EXPORT"
117     puts $hfile "\#else"
118     puts $hfile "\#define OID_EXPORT YAZ_IMPORT"
119     puts $hfile "\#endif"
120
121     puts $hfile "YAZ_BEGIN_CDECL"
122     foreach oid $oids {
123
124         set v [constant_var $oid]
125         
126         puts -nonewline $cfile "YAZ_EXPORT const int $v\[\] = \{"
127         puts -nonewline $cfile [string map {. ,} [lindex $oid 1]]
128         puts $cfile ",-1\};"
129
130         puts $hfile "OID_EXPORT extern const int $v\[\];"
131     }
132
133     puts $cfile "YAZ_EXPORT struct yaz_oid_entry yaz_oid_standard_entries\[\] ="
134     puts $cfile "\{"
135     foreach oid $oids {
136         set v [constant_var $oid]
137         
138         puts -nonewline $cfile "\t\{CLASS_[lindex $oid 0], "
139         puts -nonewline $cfile "$v, "
140         puts -nonewline $cfile \"[lindex $oid 2]\"
141         puts $cfile "\},"
142     }
143
144     puts $cfile "\t\{CLASS_NOP, 0, 0\}"
145     puts $cfile "\};"
146
147     puts $hfile "OID_EXPORT extern struct yaz_oid_entry yaz_oid_standard_entries\[\];"
148     puts $hfile "YAZ_END_CDECL"
149     puts $hfile "\#endif"
150     close $cfile
151     close $hfile
152 }
153
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]
158 } else {
159     puts "oidtoc.tcl srcdir csv cfile hfile"
160     exit 1
161 }