Debian Codegraph @

common/is_valid_types_gen.pl

raw
1 #!/usr/bin/env perl
2 # is_valid_types_gen.pl
3
4 # Dynamically generate the list of valid types from object.h
5 # This is triggered to run on build, and the output is then included into the library as a source.
6 use strict;
7 # First step -- open object.h
8 open(my $file, "< ../include/object.h") or die "Failed to load object.h";
9 my $tmp;
10 my @arr;
11 my $max = 0;
12 my $search = 1;
13 # Look for a line with enum object_type
14 OUT: while(<$file>)
15 {
16 if (/enum object_type/)
17 {
18 # Go until we find a };
19 # Also move to the next line, since that is where the data starts.
20 while (<$file>)
21 {
22 # If not the end of the enum
23 last OUT if not (/^((?!.*};).*)$/);
24
25 $tmp = $1; # Store the captured text.
26 # Run it through a filter to get the data we want
27 if ($tmp =~ /^\W*(\w+)\s*\=\s*(\d+)\,?.*$/)
28 {
29 $arr[$2] = 1;
30 if ($1 == "OBJECT_TYPE_MAX")
31 {
32 $max = $2;
33 }
34 }
35 }
36 }
37 }
38 close($file);
39
40 # Now we build the output.
41 open(my $outfile, "> ./arch_types_valid.c") or die "Cannot write to file.";
42 print $outfile "/*****************************************\n";
43 print $outfile " * This file is automatically generated! *\n";
44 print $outfile " * Its contents will be overwritten on *\n";
45 print $outfile " * the next build. *\n";
46 print $outfile " * *\n";
47 print $outfile " * is_valid_types_gen.pl generates this. *\n";
48 print $outfile " *****************************************/\n";
49 print $outfile "\n/**\n";
50 print $outfile " * Checks if the specified type is a valid one for a Crossfire object.\n";
51 print $outfile " *\n";
52 print $outfile ' * @param type value to check.'."\n";
53 print $outfile ' * @return 1 if the type is valid, 0 else.'."\n";
54 print $outfile " */\n";
55 print $outfile "\#include <global.h>\n";
56 print $outfile "\#include <libproto.h>\n";
57 print $outfile "int is_type_valid(uint8_t type) {\n";
58 print $outfile " if (type >= OBJECT_TYPE_MAX)\n";
59 print $outfile " return 0;\n";
60 print $outfile " switch (type){\n";
61 # Now we print the invalid types.
62 for (my $i = 1; $i < $max; ++$i)
63 {
64 if ($arr[$i] != 1)
65 {
66 print $outfile " case $i:\n";
67 }
68 }
69 print $outfile " return 0;\n";
70 print $outfile " }\n";
71 print $outfile " return 1;\n";
72 print $outfile "}\n";
73 close($outfile);