Bump for 3.6-28
[LibreOffice.git] / oovbaapi / genconstidl / api-to-idl.pl
blobd04cca73de95866eb7ce70d0c5eaf5d387c7b9bc
2 eval 'exec perl -S $0 ${1+"$@"}'
3 if 0;
4 #*************************************************************************
6 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 # Copyright 2000, 2010 Oracle and/or its affiliates.
10 # OpenOffice.org - a multi-platform office productivity suite
12 # This file is part of OpenOffice.org.
14 # OpenOffice.org is free software: you can redistribute it and/or modify
15 # it under the terms of the GNU Lesser General Public License version 3
16 # only, as published by the Free Software Foundation.
18 # OpenOffice.org is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU Lesser General Public License version 3 for more details
22 # (a copy is included in the LICENSE file that accompanied this code).
24 # You should have received a copy of the GNU Lesser General Public License
25 # version 3 along with OpenOffice.org. If not, see
26 # <http://www.openoffice.org/license.html>
27 # for a copy of the LGPLv3 License.
29 #*************************************************************************
31 sub usage() {
32 print "Usage: api-to-idl.pl source.api destination_path\n";
33 print;
34 print "This tool converts oovbaapi *.api files into *.idl's.\n";
35 exit 1;
38 my $src = shift;
39 my $dest = shift;
41 if ( !defined( $src ) || !defined( $dest ) || $src eq "-h" || $src eq "--help" ) {
42 usage();
45 # Parsing functions
46 my $state = "";
47 my $source = "";
48 my $name = "";
49 my $value = "";
51 my %result;
53 # Process element start event
54 sub start_element($) {
55 my ($el) = @_;
57 @element_attr = split( /\s+/, $el );
58 my $element = $element_attr[0];
60 if ( $element eq "element" ) {
61 if ( $element_attr[1] =~ /type="?([^"]*)"?/ && $1 eq "constant" ) {
62 $state = "constant";
63 $source = "";
64 $name = "";
65 $value = "";
68 elsif ( $state eq "constant" && $element eq "source" ) {
69 $state = "source";
70 if ( $element_attr[1] =~ /id="?([^"]*)"?/ ) {
71 chomp( $source = $1 );
74 elsif ( $state eq "source" && $element eq "name" ) {
75 $state = "name";
77 elsif ( $state eq "source" && $element eq "value" ) {
78 $state = "value";
82 # Process element end event
83 sub end_element($) {
84 my ($element) = @_;
86 if ( $state eq "name" && $element eq "name" ) {
87 $state = "source";
89 elsif ( $state eq "value" && $element eq "value" ) {
90 $state = "source";
92 elsif ( $state ne "" && $element eq "element" ) {
93 $state = "";
95 my @destination = split( /\./, $source );
96 my $module = shift( @destination );
97 my $type = shift( @destination );
99 $module =~ tr/[A-Z]/[a-z]/;
101 $result{$module} = {} unless exists $result{$module};
102 $result{$module}{$type} = [] unless exists $result{$module}{$type};
104 push( @{$result{$module}{$type}},
105 { "name" => $name, "value" => $value } );
109 # Process characters
110 sub characters($) {
111 my ($data) = @_;
113 if ( $state eq "name" ) {
114 chomp( $name = $data );
116 elsif ( $state eq "value" ) {
117 chomp( $value = $data );
121 # Create idls from the parsed data
122 sub generate_idls($) {
123 my ($path) = @_;
125 foreach $module ( keys %result ) {
126 foreach $type ( keys %{$result{$module}} ) {
127 my $fname = $path . "/" . $type . ".idl";
128 if ( uc($module) eq "ADODB" || uc($module) eq "DAO" ) {
129 $fname = $path . "/" . uc($module) . "_" . $type . ".idl";
131 open( IDL, ">$fname" ) || die "Cannot write $fname.";
133 if( $module eq "vba" ) {
134 print IDL "module ooo { module $module {\n";
136 else {
137 print IDL "module ooo { module vba { module $module {\n";
140 print IDL " constants $type {\n";
141 foreach $constant ( @{$result{$module}{$type}} ) {
142 print IDL " const long $constant->{'name'} = $constant->{'value'};\n";
144 if( $module eq "vba" ) {
145 print IDL " };\n}; };\n";
147 else {
148 print IDL " };\n}; }; };\n";
151 close( IDL );
156 # Parse the input
157 open( IN, "<$src" ) || die "Cannot open $src.";
159 my $in_comment = 0;
160 my $line = "";
161 while (<IN>) {
162 # ignore comments
163 s/<!--[^>]*-->//g;
164 if ( /<!--/ ) {
165 $in_comment = 1;
166 s/<!--.*//;
168 elsif ( /-->/ && $in_comment ) {
169 $in_comment = 0;
170 s/.*-->//;
172 elsif ( $in_comment ) {
173 next;
175 # ignore empty lines
176 chomp;
177 s/^\s*//;
178 s/\s*$//;
179 next if ( $_ eq "" );
181 # take care of lines where element continues
182 if ( $line ne "" ) {
183 $line .= " " . $_;
185 else {
186 $line = $_;
188 next if ( !/>$/ );
190 # the actual parsing
191 my @starts = split( /</, $line );
192 $line = "";
193 foreach $start ( @starts ) {
194 next if ( $start eq "" );
196 @ends = split( />/, $start );
197 my $element = $ends[0];
198 my $data = $ends[1];
200 # start or end element
201 if ( $element =~ /^\/(.*)/ ) {
202 end_element( $1 );
204 else {
205 start_element( $element );
208 # the data
209 characters( $data );
212 close( IN );
214 # Generate the output
215 generate_idls($dest);