Rename internal func yaz_use_attribute_create
[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 #
6 # Converts a CSV file with Object identifiers to C
7
8 proc readoids {input} {
9     set csv [open $input r]
10     set lineno 0 
11
12     while {1} {
13         incr lineno
14         set cnt [gets $csv line]
15         if {$cnt < 0} {
16             break
17         }
18         if {![string compare [string index $line 0] \"]} {
19             continue
20         }
21         set tokens [string map {, { }} $line]
22         if {[llength $tokens] != 3} {
23             puts "$input:$lineno: Bad line '$line'"
24             exit 1
25         }
26         lappend oids $tokens
27     }
28     close $csv
29     if {![info exists oids]} {
30         puts "$input:0 No OIDS"
31         exit 1
32     }
33     return $oids
34 }
35
36 proc constant_var {oid} {
37     set lname [string tolower [lindex $oid 2]]
38     set lname [string map {- _ . _ { } _ ( {} ) {}} $lname]
39     set prefix [string tolower [lindex $oid 0]]
40         
41     return yaz_oid_${prefix}_${lname}
42 }
43
44 proc oid_to_xml {srcdir input xname} {
45     set oids [readoids "${input}"]
46     set xfile [open "${xname}" w]
47
48     puts $xfile "<!-- Generated by oidtoc.tcl from $input -->"
49     puts $xfile {<informaltable id="standard-oids">}
50     puts $xfile {<tgroup cols="3">}
51     puts $xfile {<colspec colwidth="3*" colname="name"></colspec>}
52     puts $xfile {<colspec colwidth="2*" colname="class"></colspec>}
53     puts $xfile {<colspec colwidth="4*" colname="oid"></colspec>}
54     puts $xfile {<thead>}
55     puts $xfile {<row>}
56     puts $xfile {<entry>Name</entry>}
57     puts $xfile {<entry>Class</entry>}
58     puts $xfile {<entry>Constant / OID</entry>}
59     puts $xfile {</row>}
60     puts $xfile {</thead>}
61     puts $xfile {<tbody>}
62
63     foreach oid $oids {
64         puts $xfile {<row>}
65
66         puts $xfile {<entry morerows="1">}
67         puts $xfile [lindex $oid 2]
68         puts $xfile {</entry>}
69
70
71         puts $xfile {<entry morerows="1">}
72         puts $xfile [lindex $oid 0]
73         puts $xfile {</entry>}
74
75         puts $xfile {<entry><literal>}
76         set v [constant_var $oid]
77         puts $xfile $v
78         puts $xfile {</literal></entry>}
79
80
81         puts $xfile {</row>}
82         puts $xfile {<row>}
83
84         puts $xfile {<entry namest="oid">}
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 {</informaltable>}
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 "\#if HAVE_CONFIG_H"
113     puts $cfile "\#include <config.h>"
114     puts $cfile "\#endif"
115
116     puts $cfile "\#include <yaz/oid_db.h>"
117     puts $cfile ""
118     # To avoid LNK4049
119     puts $hfile "\#ifdef YAZ_DLL"
120     puts $hfile "\#define OID_EXPORT YAZ_EXPORT"
121     puts $hfile "\#else"
122     puts $hfile "\#define OID_EXPORT YAZ_IMPORT"
123     puts $hfile "\#endif"
124
125     puts $hfile "YAZ_BEGIN_CDECL"
126     foreach oid $oids {
127
128         set v [constant_var $oid]
129         
130         puts -nonewline $cfile "YAZ_EXPORT const Odr_oid $v\[\] = \{"
131         puts -nonewline $cfile [string map {. ,} [lindex $oid 1]]
132         puts $cfile ",-1\};"
133
134         puts $hfile "OID_EXPORT extern const Odr_oid $v\[\];"
135     }
136
137     puts $cfile "YAZ_EXPORT struct yaz_oid_entry yaz_oid_standard_entries\[\] ="
138     puts $cfile "\{"
139     foreach oid $oids {
140         set v [constant_var $oid]
141         
142         puts -nonewline $cfile "\t\{CLASS_[lindex $oid 0], "
143         puts -nonewline $cfile "$v, "
144         puts -nonewline $cfile \"[lindex $oid 2]\"
145         puts $cfile "\},"
146     }
147
148     puts $cfile "\t\{CLASS_NOP, 0, 0\}"
149     puts $cfile "\};"
150
151     puts $hfile "OID_EXPORT extern struct yaz_oid_entry yaz_oid_standard_entries\[\];"
152     puts $hfile "YAZ_END_CDECL"
153     puts $hfile "\#endif"
154     close $cfile
155     close $hfile
156 }
157
158 if {[llength $argv] == 4} {
159     oid_to_c [lindex $argv 0] [lindex $argv 1] [lindex $argv 2] [lindex $argv 3]
160 } elseif {[llength $argv] == 3} {
161     oid_to_xml [lindex $argv 0] [lindex $argv 1] [lindex $argv 2]
162 } else {
163     puts "oidtoc.tcl srcdir csv cfile hfile"
164     exit 1
165 }