Generate built-in OIDs from oid.csv.
[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.1 2007-04-16 21:53:09 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 oid_to_c {input cfile hfile} {
38     set oids [readoids $input]
39
40     set cfile [open $cfile w]
41     set hfile [open $hfile w]
42
43     puts $cfile "\#include <yaz/oid_db.h>"
44     puts $cfile ""
45     foreach oid $oids {
46         set lname [string tolower [lindex $oid 2]]
47         set lname [string map {- _ . _ { } _ ( {} ) {}} $lname]
48         set prefix [string tolower [lindex $oid 0]]
49         
50         puts -nonewline $cfile "const int yaz_oid_${prefix}_${lname}\[\] = \{"
51         puts -nonewline $cfile [string map {. ,} [lindex $oid 1]]
52         puts $cfile ",-1\};"
53
54         puts $hfile "extern const int yaz_oid_${prefix}_${lname}\[\];"
55     }
56
57     puts $cfile "struct yaz_oid_entry yaz_oid_standard_entries\[\] ="
58     puts $cfile "\{"
59     foreach oid $oids {
60         set lname [string tolower [lindex $oid 2]]
61         set lname [string map {- _ . _ { } _ ( {} ) {}} $lname]
62         set prefix [string tolower [lindex $oid 0]]
63         
64         puts -nonewline $cfile "\t\{CLASS_[lindex $oid 0], "
65         puts -nonewline $cfile "yaz_oid_${prefix}_${lname}, "
66         puts -nonewline $cfile \"[lindex $oid 2]\"
67         puts $cfile "\},"
68     }
69
70     puts $cfile "\t\{CLASS_NOP, 0, 0\}"
71     puts $cfile "\};"
72
73     puts $hfile "extern struct yaz_oid_entry yaz_oid_standard_entries\[\];"
74     close $cfile
75     close $hfile
76 }
77
78 if {[llength $argv] != 3} {
79     puts "oidtoc.tcl csv cfile hfile"
80     exit 1
81 }
82 oid_to_c [lindex $argv 0] [lindex $argv 1] [lindex $argv 2]