bump product version to 6.3.0.0.beta1
[LibreOffice.git] / oovbaapi / genconstidl / api-to-idl.pl
blob933025cef2c365936ef239f0032354e4acf1bd54
2 eval 'exec perl -S $0 ${1+"$@"}'
3 if 0;
5 # This file is part of the LibreOffice project.
7 # This Source Code Form is subject to the terms of the Mozilla Public
8 # License, v. 2.0. If a copy of the MPL was not distributed with this
9 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 # This file incorporates work covered by the following license notice:
13 # Licensed to the Apache Software Foundation (ASF) under one or more
14 # contributor license agreements. See the NOTICE file distributed
15 # with this work for additional information regarding copyright
16 # ownership. The ASF licenses this file to you under the Apache
17 # License, Version 2.0 (the "License"); you may not use this file
18 # except in compliance with the License. You may obtain a copy of
19 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 sub usage() {
23 print "Usage: api-to-idl.pl source.api destination_path\n";
24 print;
25 print "This tool converts oovbaapi *.api files into *.idl's.\n";
26 exit 1;
29 my $src = shift;
30 my $dest = shift;
32 if ( !defined( $src ) || !defined( $dest ) || $src eq "-h" || $src eq "--help" ) {
33 usage();
36 # Parsing functions
37 my $state = "";
38 my $source = "";
39 my $name = "";
40 my $value = "";
42 my %result;
44 # Process element start event
45 sub start_element($) {
46 my ($el) = @_;
48 @element_attr = split( /\s+/, $el );
49 my $element = $element_attr[0];
51 if ( $element eq "element" ) {
52 if ( $element_attr[1] =~ /type="?([^"]*)"?/ && $1 eq "constant" ) {
53 $state = "constant";
54 $source = "";
55 $name = "";
56 $value = "";
59 elsif ( $state eq "constant" && $element eq "source" ) {
60 $state = "source";
61 if ( $element_attr[1] =~ /id="?([^"]*)"?/ ) {
62 chomp( $source = $1 );
65 elsif ( $state eq "source" && $element eq "name" ) {
66 $state = "name";
68 elsif ( $state eq "source" && $element eq "value" ) {
69 $state = "value";
73 # Process element end event
74 sub end_element($) {
75 my ($element) = @_;
77 if ( $state eq "name" && $element eq "name" ) {
78 $state = "source";
80 elsif ( $state eq "value" && $element eq "value" ) {
81 $state = "source";
83 elsif ( $state ne "" && $element eq "element" ) {
84 $state = "";
86 my @destination = split( /\./, $source );
87 my $module = shift( @destination );
88 my $type = shift( @destination );
90 $module =~ tr/[A-Z]/[a-z]/;
92 $result{$module} = {} unless exists $result{$module};
93 $result{$module}{$type} = [] unless exists $result{$module}{$type};
95 push( @{$result{$module}{$type}},
96 { "name" => $name, "value" => $value } );
100 # Process characters
101 sub characters($) {
102 my ($data) = @_;
104 if ( $state eq "name" ) {
105 chomp( $name = $data );
107 elsif ( $state eq "value" ) {
108 chomp( $value = $data );
112 # Create idls from the parsed data
113 sub generate_idls($) {
114 my ($path) = @_;
116 foreach $module ( keys %result ) {
117 foreach $type ( keys %{$result{$module}} ) {
118 my $fname = $path . "/" . $type . ".idl";
119 open( IDL, ">$fname" ) || die "Cannot write $fname.";
121 if( $module eq "vba" ) {
122 print IDL "module ooo { module $module {\n";
124 else {
125 print IDL "module ooo { module vba { module $module {\n";
128 print IDL " constants $type {\n";
129 foreach $constant ( @{$result{$module}{$type}} ) {
130 print IDL " const long $constant->{'name'} = $constant->{'value'};\n";
132 if( $module eq "vba" ) {
133 print IDL " };\n}; };\n";
135 else {
136 print IDL " };\n}; }; };\n";
139 close( IDL );
144 # Parse the input
145 open( IN, "<$src" ) || die "Cannot open $src.";
147 my $in_comment = 0;
148 my $line = "";
149 while (<IN>) {
150 # ignore comments
151 s/<!--[^>]*-->//g;
152 if ( /<!--/ ) {
153 $in_comment = 1;
154 s/<!--.*//;
156 elsif ( /-->/ && $in_comment ) {
157 $in_comment = 0;
158 s/.*-->//;
160 elsif ( $in_comment ) {
161 next;
163 # ignore empty lines
164 chomp;
165 s/^\s*//;
166 s/\s*$//;
167 next if ( $_ eq "" );
169 # take care of lines where element continues
170 if ( $line ne "" ) {
171 $line .= " " . $_;
173 else {
174 $line = $_;
176 next if ( !/>$/ );
178 # the actual parsing
179 my @starts = split( /</, $line );
180 $line = "";
181 foreach $start ( @starts ) {
182 next if ( $start eq "" );
184 @ends = split( />/, $start );
185 my $element = $ends[0];
186 my $data = $ends[1];
188 # start or end element
189 if ( $element =~ /^\/(.*)/ ) {
190 end_element( $1 );
192 else {
193 start_element( $element );
196 # the data
197 characters( $data );
200 close( IN );
202 # Generate the output
203 generate_idls($dest);