Ticket #21: geninttypes.pl

File geninttypes.pl, 2.8 KB (added by debrouxl, 3 years ago)

Simple generator for inttypes.h documentation

Line 
1#!/usr/bin/perl
2use strict;
3
4package main;
5    my $line;
6    my $name;
7    my $size;
8    my $description;
9
10    (open INFILE, "<inttypes.h") or die "Cannot open inttypes.h: $!\n";
11
12    while ($line = <INFILE>) {
13       
14        if ($line =~ m@^#\s*define (\S+) (\C*?)$@) {
15            $name = $1;
16            my $name2;
17            my $description2;
18            my $scanf = '';
19            my $realname; # Actual name of the .hsf
20           
21            # This file contains only macros without arguments
22            warn "$name\n" if ($name !~ m@(PRI|SCN)(d|i|u|x|X)(\d{1,2}|LEAST\d{1,2}|FAST\d{1,2}|PTR)@);
23       
24            if ($1 eq 'PRI') {
25                $description = 'Format specifier for printing an integer of type ';
26            }
27            else {
28                $description = 'Format specifier for scanning an integer of type ';
29                $scanf = ' written';
30            }
31
32            $name2 = lc($3);
33            if ((index($3, 'LEAST') != -1) || (index($3, 'FAST') != -1)) {
34                $name2 = '_' . $name2;
35            }
36            $name2 = 'int' . $name2 . '_t';
37
38            $description = $description . $name2 . ' or u' . $name2;
39
40            $realname = $name; # not necessarily equal to $name, due to name clash between uppercase and lowercase on case-insensitive filesystems.
41            if (($2 eq 'd') || ($2 eq 'i')) {
42                $description = $description . $scanf . ' as a signed decimal integer'
43            }
44            elsif ($2 eq 'u') {
45                $description = $description . $scanf . ' as an unsigned decimal integer'
46            }
47            elsif ($2 eq 'x') {
48                $description = $description . $scanf . ' as an unsigned hexadecimal (lowercase) integer'
49            }
50            elsif ($2 eq 'X') {
51                $description = $description . $scanf . ' as an unsigned hexadecimal (uppercase) integer';
52                $realname =~ y@X@Z@;
53            }
54
55            $description2 = '';
56
57            $name2 = $name;
58            if ($2 eq 'd') {
59                $name2 =~ y@d@i@;
60                $description2 = 'This definition is the same as <A HREF="$$LINK(inttypes.h/' . $name2 . ')">' . $name2 . "</A>.";
61            }
62            elsif ($2 eq 'i') {
63                $name2 =~ y@i@d@;
64                $description2 = 'This definition is the same as <A HREF="$$LINK(inttypes.h/' . $name2 . ')">' . $name2 . "</A>.";
65            }
66
67            $description2 = $description2 . "\r\nDefinition mandated by the C99 standard.";
68
69            $line =~ s/\n//g; # Swallow newline.
70            open OUTFILE, "+>trunk/tigcc/doc/System/Include/inttypes.h/$realname.hsf";
71            print OUTFILE <<EOF
72[Main]\r
73Name=$name\r
74Type=Constant\r
75Header Files=inttypes.h\r
76Definition=$line\r
77\r
78[Description]\r
79$description.\r
80\r
81[Explanation]\r
82$description2\r
83EOF
84;
85            close OUTFILE;
86            }
87    }