7ef102c03ef266526d6af68d26d62c0b14e8a823
[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.3 2007-04-24 12:55:15 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 {srcdir input cname hname} {
38     set oids [readoids "${srcdir}/${input}"]
39
40     set cfile [open "${srcdir}/${cname}" w]
41     set hfile [open "${srcdir}/../include/yaz/${hname}" w]
42
43     puts $cfile "/** \\file $cname"
44     puts $hfile "/** \\file $hname"
45     set preamble "    \\brief Standard Object Identifiers: Generated from $input */"
46     puts $cfile $preamble
47     puts $hfile $preamble
48     puts $hfile "\#ifndef OID_STD_H"
49     puts $hfile "\#define OID_STD_H"
50
51     # Define this. So that we don't get duplicate declartions with MSVC
52     puts $cfile "\#define OID_STD_H"
53     puts $cfile "\#include <yaz/oid_db.h>"
54     puts $cfile ""
55     # To avoid LNK4049
56     puts $hfile "\#ifdef YAZ_DLL"
57     puts $hfile "\#define OID_EXPORT YAZ_EXPORT"
58     puts $hfile "\#else"
59     puts $hfile "\#define OID_EXPORT YAZ_IMPORT"
60     puts $hfile "\#endif"
61
62     puts $hfile "YAZ_BEGIN_CDECL"
63     foreach oid $oids {
64         set lname [string tolower [lindex $oid 2]]
65         set lname [string map {- _ . _ { } _ ( {} ) {}} $lname]
66         set prefix [string tolower [lindex $oid 0]]
67         
68         puts -nonewline $cfile "YAZ_EXPORT const int yaz_oid_${prefix}_${lname}\[\] = \{"
69         puts -nonewline $cfile [string map {. ,} [lindex $oid 1]]
70         puts $cfile ",-1\};"
71
72         puts $hfile "OID_EXPORT extern const int yaz_oid_${prefix}_${lname}\[\];"
73     }
74
75     puts $cfile "YAZ_EXPORT struct yaz_oid_entry yaz_oid_standard_entries\[\] ="
76     puts $cfile "\{"
77     foreach oid $oids {
78         set lname [string tolower [lindex $oid 2]]
79         set lname [string map {- _ . _ { } _ ( {} ) {}} $lname]
80         set prefix [string tolower [lindex $oid 0]]
81         
82         puts -nonewline $cfile "\t\{CLASS_[lindex $oid 0], "
83         puts -nonewline $cfile "yaz_oid_${prefix}_${lname}, "
84         puts -nonewline $cfile \"[lindex $oid 2]\"
85         puts $cfile "\},"
86     }
87
88     puts $cfile "\t\{CLASS_NOP, 0, 0\}"
89     puts $cfile "\};"
90
91     puts $hfile "OID_EXPORT extern struct yaz_oid_entry yaz_oid_standard_entries\[\];"
92     puts $hfile "YAZ_END_CDECL"
93     puts $hfile "\#endif"
94     close $cfile
95     close $hfile
96 }
97
98 if {[llength $argv] != 4} {
99     puts "oidtoc.tcl srcdir csv cfile hfile"
100     exit 1
101 }
102 oid_to_c [lindex $argv 0] [lindex $argv 1] [lindex $argv 2] [lindex $argv 3]