-
Notifications
You must be signed in to change notification settings - Fork 0
/
newclass.pl
executable file
·133 lines (105 loc) · 3.16 KB
/
newclass.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/perl
# Perl script by DWK to create a new .cpp/.h combination template
# Updated by ethereal to create _fwd.h file
my $project = 'Kriti';
if(scalar(@ARGV) < 1) {
die "Usage: $0 <C++-name-of-class>\n" .
" e.g. $0 Socket::UDPSocket\n";
}
process_class($ARGV[0]);
sub process_class {
my $class = shift;
my $prefix = "src/kriti/";
while($class =~ /([^:]+)::/g) {
$prefix .= lc($1) . '/';
}
$class =~ /([^:]+)$/;
$prefix .= $1;
print "Class prefix: \"$prefix\"\n";
create_cpp_file($class, $prefix . ".cpp");
create_h_file($class, $prefix . ".h");
create_fwd_file($class, $prefix . ".fwd");
}
sub create_cpp_file {
my $class = shift;
my $file = shift;
open(FILE, ">$file") or die "Cannot open file";
my @namespaces = generate_namespaces($class);
$class =~ /([^:]+)$/;
print FILE "#include \"$1.h\"\n";
print FILE "\n";
print FILE open_namespaces(@namespaces);
print FILE "\n";
print FILE close_namespaces(@namespaces);
close FILE;
}
sub create_h_file {
my $class = shift;
my $file = shift;
open(FILE, ">$file") or die;
my $guard = $class;
$guard =~ s/([A-Z]+)/substr($1, 0, 1) . lc(substr($1, 1))/eg;
while($guard =~ s/([A-Z])([^:]*)$/'_' . lc($1) . $2/eg) {}
$guard =~ s/([^_])::([^:]+)$/$1_$2/; # put two underscores at last ::
$guard =~ s/([^_])::([^_])/$1_$2/g;
$guard =~ s/:://g;
$guard = uc($project) . "_" . uc($guard) . "_H";
my @namespaces = generate_namespaces($class);
print FILE "#ifndef $guard\n";
print FILE "#define $guard\n";
print FILE "\n";
print FILE open_namespaces(@namespaces);
print FILE "\n";
$class =~ /([^:]+)$/;
print FILE "class $1 {\n";
print FILE "public:\n";
print FILE "};\n";
print FILE "\n";
print FILE close_namespaces(@namespaces);
print FILE "\n";
print FILE "#endif\n";
close FILE;
}
sub create_fwd_file {
my $class = shift;
my $file = shift;
open(FILE, ">$file") or die;
my $guard = $class;
$guard =~ s/([A-Z]+)/substr($1, 0, 1) . lc(substr($1, 1))/eg;
while($guard =~ s/([A-Z])([^:]*)$/'_' . lc($1) . $2/eg) {}
$guard =~ s/([^_])::([^:]+)$/$1_$2/; # put two underscores at last ::
$guard =~ s/([^_])::([^_])/$1_$2/g;
$guard =~ s/:://g;
$guard = uc($project) . "_" . uc($guard) . "_H";
my @namespaces = generate_namespaces($class);
print FILE open_namespaces(@namespaces);
print FILE "\n";
$class =~ /([^:]+)$/;
print FILE "class $1;\n";
print FILE "\n";
print FILE close_namespaces(@namespaces);
print FILE "\n";
close FILE;
}
sub generate_namespaces {
my $class = shift;
my @spaces = ($project);
while($class =~ /([^:]+)::/g) {
push(@spaces, $1);
}
return @spaces;
}
sub open_namespaces {
my $return = '';
for my $namespace (@_) {
$return .= "namespace $namespace {\n";
}
return $return;
}
sub close_namespaces {
my $return = '';
for my $namespace (reverse @_) {
$return .= "} // namespace $namespace\n";
}
return $return;
}