a780ac3adbaee542b2c493d62d417be684a5faf0
[free-sw/xcb/libxcb] / tools / api_conv.pl
1 #!/usr/bin/perl -plw
2 use strict;
3
4 BEGIN {
5         %::const = map { $_ => 1 } (
6                 "XCBNone",
7                 "XCBCopyFromParent",
8                 "XCBCurrentTime",
9                 "XCBNoSymbol",
10                 "XCBError",
11                 "XCBReply",
12         );
13         open(CONST, shift) or die "failed to open constants list: $!";
14         while(<CONST>)
15         {
16                 chomp;
17                 die "invalid constant name: \"$_\"" unless /^XCB[A-Za-z0-9_]*$/;
18                 $::const{$_} = 1;
19         }
20         close(CONST);
21 }
22
23 sub convert($$)
24 {
25         local $_ = shift;
26         my ($fun) = @_;
27
28         return "uint$1_t" if /^CARD(8|16|32)$/;
29         return "int$1_t" if /^INT(8|16|32)$/;
30         return "uint8_t" if $_ eq 'BOOL' or $_ eq 'BYTE';
31         return $_ if /_/ or !/^XCB(.+)/;
32         my $const = defined $::const{$_};
33         $_ = $1;
34
35         my %abbr = (
36                 "Iter" => "iterator",
37                 "Req" => "request",
38                 "Rep" => "reply",
39         );
40
41         s/[A-Z](?:[A-Z0-9]*|[a-z0-9]*)(?=[A-Z]|$)/"_" . ($abbr{$&} or lc($&))/eg;
42
43         return "XCB" . uc($_) if $const;
44
45         $_ .= "_t" unless $fun or /_id$/;
46
47         return "xcb" . $_;
48 }
49
50 s/([_A-Za-z][_A-Za-z0-9]*)([ \t]*\()?/convert($1, defined $2) . ($2 or "")/eg;