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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
use IO::File;
use Getopt::Long;
my $perl_module = 0;
my $c_header = 0;
GetOptions("-perl" => \$perl_module,
"-header" => \$c_header);
my $types = &read_input();
if($c_header)
{
&write_c_header($types);
}
if($perl_module)
{
&write_perl_modules($types);
}
# Write a perl module for each enumerated type defined in the $type hash
sub write_perl_modules
{
my $types = shift;
my $module_name;
my $module;
my $basename;
foreach my $type (sort keys %{$types})
{
$module_name = $type;
$module_name =~ s/globus_gram_protocol_//;
$module_name =~ s/_t$//;
$module_name = join("", map {ucfirst} (split(/_/,$module_name)));
$basename = $type;
$basename =~ s/_t$//;
$basename = uc $basename;
$module = new IO::File(">$module_name.pm");
$module->print(<<EOF);
package Globus::GRAM::$module_name;
=head1 NAME
Globus::GRAM::$module_name - GRAM Protocol $module_name Constants
=head1 DESCRIPTION
The Globus::GRAM::$module_name module defines symbolic names for the
$module_name constants in the GRAM Protocol.
EOF
# Errors are typed; other enumerations are just constant values
if($module_name eq 'Error')
{
$module->print(<<EOF);
=pod
The Globus::GRAM::Error module methods return an object consisting
of an integer error code, and (optionally) a string explaining the
error.
=head2 Methods
=over 4
=item \$error = new Globus::GRAM::Error(\$number, \$string);
Create a new error object with the given error number and string
description. This is called by the error-specific factory methods described
below.
=cut
sub new
{
my \$proto = shift;
my \$class = ref(\$proto) || \$proto;
my \$self = {};
my \$value = shift;
my \$string = shift;
\$self->{value} = \$value if defined(\$value);
\$self->{string} = \$string if defined(\$string);
bless \$self, \$class;
return \$self;
}
=item \$error->string()
Return the error string associated with a Globus::GRAM::Error object.
=cut
sub string
{
my \$self = shift;
return \$self->{string};
}
=item \$error->value()
Return the integer error code associated with a Globus::GRAM::Error object.
=cut
sub value
{
my \$self = shift;
return \$self->{value};
}
EOF
}
else
{
$module->print(<<EOF);
=head2 Methods
=over 4
EOF
}
foreach (@{$types->{$type}->{enumeration}})
{
$scopedname = $_->{name};
$scopedname =~ s/${basename}_//;
if($module_name eq 'Error')
{
$module->print(<<EOF);
=item \$error = Globus::GRAM::Error::$scopedname()
Create a new $scopedname GRAM error.
=cut
sub $scopedname
{
return new Globus::GRAM::Error($_->{value});
}
EOF
}
else
{
$module->print(<<EOF);
=item \$value = Globus::GRAM::$module_name::$scopedname()
Return the value of the $scopedname constant.
=cut
sub $scopedname
{
return $_->{value};
}
EOF
}
}
$module->print(<<EOF);
=back
=cut
1;
EOF
$module->close();
}
}
sub write_c_header
{
my $types = shift;
my $output = new IO::File(">globus_gram_protocol_constants.h");
my $basename;
$output->print(<<EOF);
/* This file is automatically generated by
* $0. Do not modify.
*/
/**
* \@file globus_gram_protocol_constants.h
* \@brief Protocol Constants
*/
#ifndef GLOBUS_GRAM_PROTOCOL_CONSTANTS_H
#define GLOBUS_GRAM_PROTOCOL_CONSTANTS_H
EOF
foreach my $type (sort keys %{$types})
{
$break = "\n";
$basename = $type;
$basename =~ s/_t//;
$output->print(<<EOF);
/**
* \@defgroup $basename $types->{$type}->{brief}
* \@ingroup globus_gram_protocol
* \@brief $types->{$type}->{brief}
* \@details
* $types->{$type}->{documentation}
* \@anchor $type
*/
/**
* $types->{$type}->{brief}
* \@ingroup $basename
*/
typedef enum
{
EOF
foreach (@{$types->{$type}->{enumeration}})
{
$output->print("$break "
. $_->{name} . "=" . $_->{value}
. "/**< $_->{doc} */");
$break = ",\n";
}
$output->print("\n}\n$type;\n\n");
}
$output->print("#endif\n");
$output->close();
}
sub read_input
{
my $types = {};
my $type_name = "";
my $documentation = "";
my $enumeration = undef;
while(<>)
{
next if(/^#[^#]/);
if($_ eq "\n")
{
# end of type
&insert_type($types, $type_name, $brief_documentation, $documentation, $enumeration);
$type_name = "";
$brief_documentation = "";
$documentation = "";
$enumeration = undef;
}
elsif(m/^##[^#]/)
{
# type name and/or documentation
#
chomp;
s/^##\s*//;
if($type_name eq "")
{
($type_name, $brief_documentation) = split(/\s+/, $_, 2);
}
else
{
$documentation .= " $_";
}
}
elsif(m/^###/)
{
s/^###\s+//;
$enumerated_doc .= $_;
}
else
{
chomp;
($enumerated_name, $enumerated_value) = split(/=/, $_, 2);
push(@{$enumeration},
{
name => $enumerated_name,
value => $enumerated_value,
doc => $enumerated_doc
});
$enumerated_name = $enumerated_value = $enumerated_doc = "";
}
}
&insert_type($types, $type_name, $brief_documentation, $documentation, $enumeration);
return $types;
}
sub insert_type
{
my $types = shift;
my $type_name = shift;
my $brief = shift;
my $documentation = shift;
my $enumeration = shift;
if($type_name eq "")
{
return $types;
}
$types->{$type_name}{brief} = $brief;
$types->{$type_name}{documentation} = $documentation;
$types->{$type_name}{enumeration} = $enumeration;
return $types;
}
|