$infile = "switches.def"; $outfile = "switches.cpp"; if ( @ARGV ) { $infile = shift @ARGV; } if ( @ARGV ) { $outfile = shift @ARGV; } ############################################################################ ## Script for generating switches.cpp ## ############################################################################ open INFILE, $infile or die; @inf = ; close INFILE; unshift @inf, "="; unshift @inf, "=bRet = SWFN_HELP( USAGE );"; unshift @inf, "this help"; unshift @inf, "help, ?"; push @out, < $longest ); } $longest += 2 + 9; foreach $line ( @help ) { my ( $switch, $desc, $par ) = split( /\|/, $line, 3 ); my $len = length( $switch ) + length( $par ); my $l = "printf( \"-$switch"; if ( $par ) { $l .= " $par"; } $l .= ' ' x ( $longest - length( $l ) ); $l .= " -$desc\\n\" );\n"; push @helpf, $l; } unshift @out, @helpf; unshift @out, <$outfile"; print OUTFILE "//THIS IS A GENERATED FILE. DO NOT EDIT!!!\n"; print OUTFILE "//EDIT $infile INSTEAD!\n\n"; $indent = 0; $bigline = join( '', @out ); @out = split( /\n/, $bigline ); foreach $line ( @out ) { $indent-- if ( $line =~ /}/ ); print OUTFILE "\t" x $indent, $line, "\n"; $indent++ if ( $line =~ /{/ ); } close OUTFILE; sub switch() { my $switch_desc = ""; my $switches_names = shift @inf; my $switches_desc = shift @inf; chomp $switches_desc; my @switches = split( /,/, $switches_names ); foreach $switch ( @switches ) { $switch =~ s/\s+$//; $switch =~ s/^\s+//; if ( defined ( $SWITCHES{ $switch } ) ) { die "Duplicate switch $switch"; } $SWITCHES{ $switch } = 1; } $switches_count = @switches; $switch_desc = $switches[ 0 ] . "|" . $switches_desc . "|"; push @out, "if ("; my @asc; foreach $switch (@switches ) { push @asc, "!strcmp( m_szSwitch, \"$switch\" )"; } push @out, join( " || ", @asc ); push @out, ")"; push @out, "\n{\n"; my @switch_pars = (); for(;;) { my $par_desc = shift @inf; my ( $par, $code ) = split( /=/, $par_desc, 2 ); $par =~ s/\s+$//; $par =~ s/^\s+//; $code =~ s/\s+$//; $code =~ s/^\s+//; last if ( !$par && !$code ); if ( $code =~ /(SWFN_\w+)/ ) { $CODE_INCLUDE{ $1 } = 1; } if ( $par ) { push @switch_pars, $par; } push @out, $code . "\n"; } push @out, "continue;\n"; $switch_desc .= join( ' ', @switch_pars ); push @help, $switch_desc; push @out, "}\n"; }