Changed Block to use a std::vector again, hopefully for the last time.
[aesalon.git] / newclass.pl
blob766780c8e0498b8287f2626cefefd7d5619e8b20
1 #!/usr/bin/perl
2 # Perl script by DWK to create a new .cpp/.h combination template
4 if(scalar(@ARGV) < 1) {
5 die "Usage: $0 <C++-name-of-class>\n" .
6 "\te.g. $0 Engine::TerrainCoordinate\n";
9 process_class($ARGV[0]);
11 sub process_class {
12 my $class = shift;
13 my $prefix = "src/";
15 while($class =~ /([^:]+)::/g) {
16 $prefix .= lc($1) . '/';
19 $class =~ /([^:]+)$/;
20 $prefix .= $1;
22 print "Class prefix: \"$prefix\"\n";
23 create_cpp_file($class, $prefix . ".cpp");
24 create_h_file($class, $prefix . ".h");
27 sub create_cpp_file {
28 my $class = shift;
29 my $file = shift;
30 open(FILE, ">$file") or die;
32 $class =~ /([^:]+)$/;
33 print FILE "#include \"$1.h\"\n";
34 print FILE "\n";
35 print FILE open_namespaces(generate_namespaces($class));
36 print FILE "\n";
37 print FILE close_namespaces(generate_namespaces($class));
39 close FILE;
42 sub create_h_file {
43 my $class = shift;
44 my $file = shift;
45 open(FILE, ">$file") or die;
47 my $guard = $class;
48 $guard =~ s/([A-Z]+)/substr($1, 0, 1) . lc(substr($1, 1))/eg;
49 $guard =~ s/:://g;
50 $guard =~ s/([A-Z])/'_' . lc($1)/eg;
51 $guard = uc($guard);
52 print FILE "#ifndef AESALON$guard" . "_H\n";
53 print FILE "#define AESALON$guard" . "_H\n";
54 print FILE "\n";
55 print FILE open_namespaces(generate_namespaces($class));
56 print FILE "\n";
57 $class =~ /([^:]+)$/;
58 print FILE "class $1 {\n";
59 print FILE "public:\n";
60 print FILE "};\n";
61 print FILE "\n";
62 print FILE close_namespaces(generate_namespaces($class));
63 print FILE "\n";
64 print FILE "#endif\n";
66 close FILE;
69 sub generate_namespaces {
70 my $class = shift;
71 my @spaces = ('Aesalon');
73 while($class =~ /([^:]+)::/g) {
74 push(@spaces, $1);
77 return @spaces;
80 sub open_namespaces {
81 my $return = '';
82 for my $namespace (@_) {
83 $return .= "namespace $namespace {\n";
86 return $return;
89 sub close_namespaces {
90 my $return = '';
91 for my $namespace (reverse @_) {
92 $return .= "} // namespace $namespace\n";
95 return $return;