Don't print directly from the code generators, instead append to an output string for printing at a later time.

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@5374 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2008-07-03 21:51:23 +00:00
parent bf5230b419
commit 6e853b04d1
4 changed files with 137 additions and 128 deletions

View File

@ -8,16 +8,16 @@ use strict;
sub printStructFieldType
{
my $field = $_[0];
my ($output, $field) = @_;
$_ = ${$field}{"type"};
if (/count/) { print "unsigned int "; }
elsif (/string/) { print "const char* "; }
elsif (/real/) { print "float "; }
elsif (/bool/) { print "bool "; }
elsif (/set/) { print "bool "; }
elsif (/enum/) { print "${$field}{\"enum\"} "; }
if (/count/) { $$output .= "unsigned int "; }
elsif (/string/) { $$output .= "const char* "; }
elsif (/real/) { $$output .= "float "; }
elsif (/bool/) { $$output .= "bool "; }
elsif (/set/) { $$output .= "bool "; }
elsif (/enum/) { $$output .= "${$field}{\"enum\"} "; }
else { die "UKNOWN TYPE: $_"; }
}
@ -49,7 +49,7 @@ sub preProcessField
sub postProcessField
{
my ($field, $enumMap) = @_;
my ($output, $field, $enumMap) = @_;
$_ = ${$field}{"type"};
$_ = "" unless $_;
@ -59,57 +59,58 @@ sub postProcessField
my $enum = ${$enumMap}{$enumName};
my $enumSize = @{${$enum}{"values"}};
print "\[${enumSize}]" if (/set/);
$$output .= "\[${enumSize}]" if (/set/);
}
}
sub printComments
{
my ($comments, $indent) = @_;
my ($output, $comments, $indent) = @_;
return unless @{$comments};
print "\t" if $indent;
print "/**\n";
$$output .= "\t" if $indent;
$$output .= "/**\n";
foreach my $comment (@{$comments})
{
print "\t" if $indent;
print " *${comment}\n";
$$output .= "\t" if $indent;
$$output .= " *${comment}\n";
}
print "\t" if $indent;
print " */\n";
$$output .= "\t" if $indent;
$$output .= " */\n";
}
sub printStructFields
{
my @fields = @{${$_[0]}{"fields"}};
my $enumMap = $_[1];
my $output = $_[0];
my @fields = @{${$_[1]}{"fields"}};
my $enumMap = $_[2];
while (@fields)
{
my $field = shift(@fields);
my @comments = @{${$field}{"comment"}};
preProcessField $field, \@comments;
preProcessField($field, \@comments);
printComments \@comments, 1;
printComments($output, \@comments, 1);
print "\t";
printStructFieldType $field;
print ${$field}{"name"};
$$output .= "\t";
printStructFieldType($output, $field);
$$output .= ${$field}{"name"};
postProcessField $field, $enumMap;
print ";\n";
postProcessField($output, $field, $enumMap);
$$output .= ";\n";
print "\n" if @fields;
$$output .= "\n" if @fields;
}
}
sub printStructContent
{
my ($struct, $name, $prefix, $structMap, $enumMap) = @_;
my ($output, $struct, $name, $prefix, $structMap, $enumMap) = @_;
foreach (keys %{${$struct}{"qualifiers"}})
{
@ -120,24 +121,25 @@ sub printStructContent
my $inheritName = ${${$struct}{"qualifiers"}}{"inherit"};
my $inheritStruct = ${$structMap}{$inheritName};
print "\t/* BEGIN of inherited \"$inheritName\" definition */\n";
printStructContent($inheritStruct, $name, $prefix, $structMap, $enumMap);
print "\t/* END of inherited \"$inheritName\" definition */\n\n";
$$output .= "\t/* BEGIN of inherited \"$inheritName\" definition */\n";
printStructContent($output, $inheritStruct, $name, $prefix, $structMap, $enumMap);
$$output .= "\t/* END of inherited \"$inheritName\" definition */\n";
}
}
$$name = ${$struct}{"name"};
printStructFields($struct, $enumMap);
printStructFields($output, $struct, $enumMap);
}
sub printEnum()
{
my ($enum) = @_;
my ($output, $enum) = @_;
printComments ${$enum}{"comment"}, 0;
printComments($output, ${$enum}{"comment"}, 0);
print "typedef enum\n{\n";
# Start printing the enum
$$output .= "typedef enum\n{\n";
my @values = @{${$enum}{"values"}};
@ -146,66 +148,67 @@ sub printEnum()
my $value = shift(@values);
my $name = ${$value}{"name"};
printComments ${$value}{"comment"}, 1;
printComments($output, ${$value}{"comment"}, 1);
print "\t${$enum}{\"name\"}_${name},\n";
$$output .= "\t${$enum}{\"name\"}_${name},\n";
print "\n" if @values;
$$output .= "\n" if @values;
}
print "} ${$enum}{\"name\"};\n\n";
# Finish printing the enum
$$output .= "} ${$enum}{\"name\"};\n\n";
}
sub printStruct()
{
my ($struct, $structMap, $enumMap) = @_;
my ($output, $struct, $structMap, $enumMap) = @_;
my $name;
my $prefix = "";
printComments ${$struct}{"comment"}, 0;
printComments($output, ${$struct}{"comment"}, 0);
# Start printing the structure
print "typedef struct\n{\n";
$$output .= "typedef struct\n{\n";
printStructContent($struct, \$name, \$prefix, $structMap, $enumMap);
printStructContent($output, $struct, \$name, \$prefix, $structMap, $enumMap);
# Finish printing the structure
print "} ${prefix}${name};\n\n";
$$output .= "} ${prefix}${name};\n\n";
}
sub printHdrGuard
{
my ($name) = @_;
my ($output, $name) = @_;
$name =~ s/\./_/g;
$name = uc($name);
print "__INCLUDED_DB_TEMPLATE_SCHEMA_STRUCTDEF_${name}_H__";
$$output .= "__INCLUDED_DB_TEMPLATE_SCHEMA_STRUCTDEF_${name}_H__";
}
sub startFile()
{
my ($name) = @_;
my ($output, $name) = @_;
print "/* This file is generated automatically, do not edit, change the source ($name) instead. */\n\n";
$$output .= "/* This file is generated automatically, do not edit, change the source ($name) instead. */\n\n";
print "#ifndef ";
printHdrGuard($name);
print "\n";
$$output .= "#ifndef ";
printHdrGuard($output, $name);
$$output .= "\n";
print "#define ";
printHdrGuard($name);
print "\n\n";
$$output .= "#define ";
printHdrGuard($output, $name);
$$output .= "\n\n";
}
sub endFile()
{
my ($name) = @_;
my ($output, $name) = @_;
print "#endif // ";
printHdrGuard($name);
print "\n";
$$output .= "#endif // ";
printHdrGuard($output, $name);
$$output .= "\n";
}
1;

View File

@ -134,18 +134,23 @@ while (<>)
else { die "Unmatched line: $_\n"; }
}
CG::startFile($name);
my $output = "";
CG::startFile(\$output, $name);
# Print all enums
foreach my $enum (@enumList)
{
CG::printEnum($enum);
CG::printEnum(\$output, $enum);
}
# Print all structs
foreach my $struct (@structList)
{
CG::printStruct($struct, \%structMap, \%enumMap);
CG::printStruct(\$output, $struct, \%structMap, \%enumMap);
}
CG::endFile($name);
CG::endFile(\$output, $name);
# Actually print out the output
print $output;

View File

@ -8,20 +8,20 @@ use strict;
sub printComments
{
my ($comments, $indent) = @_;
my ($output, $comments, $indent) = @_;
return unless @{$comments};
foreach my $comment (@{$comments})
{
print "\t" if $indent;
print "--${comment}\n";
$$output .= "\t" if $indent;
$$output .= "--${comment}\n";
}
}
sub printStructFields
{
my ($struct, $enumMap) = @_;
my ($output, $struct, $enumMap) = @_;
my @fields = @{${$struct}{"fields"}};
my $structName = ${$struct}{"name"};
@ -29,7 +29,7 @@ sub printStructFields
{
my $field = shift(@fields);
printComments ${$field}{"comment"}, 1;
printComments($output, ${$field}{"comment"}, 1);
if (${$field}{"type"} and ${$field}{"type"} =~ /set/)
{
@ -41,22 +41,22 @@ sub printStructFields
{
my $value = shift(@values);
print "\t`$structName`.`${$field}{\"name\"}_${$value}{\"name\"}`";
print ",\n" if @values;
$$output .= "\t`$structName`.`${$field}{\"name\"}_${$value}{\"name\"}`";
$$output .= ",\n" if @values;
}
}
else
{
print "\t`$structName`.`${$field}{\"name\"}`";
$$output .= "\t`$structName`.`${$field}{\"name\"}`";
}
print ",\n\n" if @fields;
$$output .= ",\n\n" if @fields;
}
}
sub printStructContent
{
my ($struct, $structMap, $enumMap, $first) = @_;
my ($output, $struct, $structMap, $enumMap, $first) = @_;
foreach (keys %{${$struct}{"qualifiers"}})
{
@ -65,18 +65,18 @@ sub printStructContent
my $inheritName = ${${$struct}{"qualifiers"}}{"inherit"};
my $inheritStruct = ${$structMap}{$inheritName};
printStructContent($inheritStruct, $structMap, $enumMap, 0);
printStructContent($output, $inheritStruct, $structMap, $enumMap, 0);
}
}
printStructFields($struct, $enumMap);
print ",\n" unless $first;
print "\n";
printStructFields($output, $struct, $enumMap);
$$output .= ",\n" unless $first;
$$output .= "\n";
}
sub printBaseStruct
{
my ($struct, $structMap) = @_;
my ($outstr, $struct, $structMap) = @_;
my $is_base = 1;
foreach (keys %{${$struct}{"qualifiers"}})
@ -86,22 +86,22 @@ sub printBaseStruct
my $inheritName = ${${$struct}{"qualifiers"}}{"inherit"};
my $inheritStruct = ${$structMap}{$inheritName};
printBaseStruct($inheritStruct, $structMap);
printBaseStruct($outstr, $inheritStruct, $structMap);
$is_base = 0;
}
elsif (/abstract/)
{
print "`${$struct}{\"name\"}`";
$$outstr .= "`${$struct}{\"name\"}`";
$is_base = 0;
}
}
print "`${$struct}{\"name\"}`" if $is_base;
$$outstr .= "`${$struct}{\"name\"}`" if $is_base;
}
sub printStructJoins
{
my ($struct, $structMap) = @_;
my ($outstr, $struct, $structMap) = @_;
foreach (keys %{${$struct}{"qualifiers"}})
{
@ -110,8 +110,8 @@ sub printStructJoins
my $inheritName = ${${$struct}{"qualifiers"}}{"inherit"};
my $inheritStruct = ${$structMap}{$inheritName};
printStructJoins($inheritStruct, $structMap);
print " INNER JOIN `${$struct}{\"name\"}` ON `${inheritName}`.`unique_inheritance_id` = `${$struct}{\"name\"}`.`unique_inheritance_id`";
printStructJoins($outstr, $inheritStruct, $structMap);
$$outstr .= " INNER JOIN `${$struct}{\"name\"}` ON `${inheritName}`.`unique_inheritance_id` = `${$struct}{\"name\"}`.`unique_inheritance_id`";
}
}
}
@ -122,27 +122,27 @@ sub printEnum()
sub printStruct()
{
my ($struct, $structMap, $enumMap) = @_;
my ($output, $struct, $structMap, $enumMap) = @_;
printComments(${$struct}{"comment"}, 0);
printComments($output, ${$struct}{"comment"}, 0);
# Start printing the select statement
print "SELECT\n";
$$output .= "SELECT\n";
printStructContent($struct, $structMap, $enumMap, 1);
print "FROM ";
printStructContent($output, $struct, $structMap, $enumMap, 1);
$$output .= "FROM ";
printBaseStruct($struct, $structMap);
printStructJoins($struct, $structMap);
printBaseStruct($output, $struct, $structMap);
printStructJoins($output, $struct, $structMap);
print ";\n\n";
$$output .= ";\n\n";
}
sub startFile()
{
my ($name) = @_;
my ($output, $name) = @_;
print "-- This file is generated automatically, do not edit, change the source ($name) instead.\n\n";
$$output .= "-- This file is generated automatically, do not edit, change the source ($name) instead.\n\n";
}
sub endFile()

View File

@ -8,34 +8,34 @@ use strict;
sub printStructFieldType
{
my ($field) = @_;
my ($output, $field) = @_;
$_ = ${$field}{"type"};
if (/count/) { print "INTEGER NOT NULL"; }
elsif (/string/) { print "TEXT NOT NULL"; }
elsif (/real/) { print "NUMERIC NOT NULL"; }
elsif (/bool/) { print "INTEGER NOT NULL"; }
elsif (/enum/) { print "INTEGER NOT NULL"; }
if (/count/) { $$output .= "INTEGER NOT NULL"; }
elsif (/string/) { $$output .= "TEXT NOT NULL"; }
elsif (/real/) { $$output .= "NUMERIC NOT NULL"; }
elsif (/bool/) { $$output .= "INTEGER NOT NULL"; }
elsif (/enum/) { $$output .= "INTEGER NOT NULL"; }
else { die "UKNOWN TYPE: $_"; }
}
sub printComments
{
my ($comments, $indent) = @_;
my ($output, $comments, $indent) = @_;
return unless @{$comments};
foreach my $comment (@{$comments})
{
print "\t" if $indent;
print "--${comment}\n";
$$output .= "\t" if $indent;
$$output .= "--${comment}\n";
}
}
sub printStructFields
{
my ($struct, $enumMap) = @_;
my ($output, $struct, $enumMap) = @_;
my @fields = @{${$struct}{"fields"}};
my @constraints = ();
@ -43,7 +43,7 @@ sub printStructFields
{
my $field = shift(@fields);
printComments ${$field}{"comment"}, 1;
printComments($output, ${$field}{"comment"}, 1);
if (${$field}{"type"} and ${$field}{"type"} =~ /set/)
{
@ -56,9 +56,9 @@ sub printStructFields
{
my $value = shift(@values);
print "\t${$field}{\"name\"}_${$value}{\"name\"} INTEGER NOT NULL";
print "," if @values or @fields or @constraints or (${$field}{"qualifier"} and ${$field}{"qualifier"} =~ /unique/);
print "\n";
$$output .= "\t${$field}{\"name\"}_${$value}{\"name\"} INTEGER NOT NULL";
$$output .= "," if @values or @fields or @constraints or (${$field}{"qualifier"} and ${$field}{"qualifier"} =~ /unique/);
$$output .= "\n";
$unique_string .= "${$field}{\"name\"}_${$value}{\"name\"}";
$unique_string .= ", " if @values;
}
@ -68,28 +68,29 @@ sub printStructFields
}
else
{
print "\t${$field}{\"name\"} ";
printStructFieldType($field);
print " UNIQUE" if ${$field}{"qualifier"} and ${$field}{"qualifier"} =~ /unique/;
print ",\n" if @fields or @constraints;
$$output .= "\t${$field}{\"name\"} ";
printStructFieldType($output, $field);
$$output .= " UNIQUE" if ${$field}{"qualifier"} and ${$field}{"qualifier"} =~ /unique/;
$$output .= ",\n" if @fields or @constraints;
}
print "\n";
$$output .= "\n";
}
while (@constraints)
{
my $constraint = shift(@constraints);
print "\t${constraint}";
print ",\n" if @constraints;
print "\n";
$$output .= "\t${constraint}";
$$output .= ",\n" if @constraints;
$$output .= "\n";
}
}
sub printStructContent
{
my ($struct, $name, $structMap, $enumMap, $printFields) = @_;
my ($output, $struct, $name, $structMap, $enumMap, $printFields) = @_;
foreach (keys %{${$struct}{"qualifiers"}})
{
@ -98,20 +99,20 @@ sub printStructContent
my $inheritName = ${${$struct}{"qualifiers"}}{"inherit"};
my $inheritStruct = ${$structMap}{$inheritName};
printStructContent($inheritStruct, $name, $structMap, $enumMap, 0);
printStructContent($output, $inheritStruct, $name, $structMap, $enumMap, 0);
}
elsif (/abstract/)
{
print "\t-- Automatically generated ID to link the inheritance hierarchy.\n"
."\tunique_inheritance_id INTEGER PRIMARY KEY ";
print "AUTOINCREMENT " if $printFields;
print "NOT NULL,\n\n";
$$output .= "\t-- Automatically generated ID to link the inheritance hierarchy.\n";
. "\tunique_inheritance_id INTEGER PRIMARY KEY ";
$$output .= "AUTOINCREMENT " if $printFields;
$$output .= "NOT NULL,\n\n";
}
}
$$name = ${$struct}{"name"};
printStructFields($struct, $enumMap) if $printFields;
printStructFields($output, $struct, $enumMap) if $printFields;
}
sub printEnum()
@ -120,25 +121,25 @@ sub printEnum()
sub printStruct()
{
my ($struct, $structMap, $enumMap) = @_;
my ($output, $struct, $structMap, $enumMap) = @_;
my $name = ${$struct}{"name"};
printComments(${$struct}{"comment"}, 0);
printComments($output, ${$struct}{"comment"}, 0);
# Start printing the structure
print "CREATE TABLE `${name}` (\n";
$$output .= "CREATE TABLE `${name}` (\n";
printStructContent($struct, \$name, $structMap, $enumMap, 1);
printStructContent($output, $struct, \$name, $structMap, $enumMap, 1);
# Finish printing the structure
print ");\n\n";
$$output .= ");\n\n";
}
sub startFile()
{
my ($name) = @_;
my ($output, $name) = @_;
print "-- This file is generated automatically, do not edit, change the source ($name) instead.\n\n";
$$output .= "-- This file is generated automatically, do not edit, change the source ($name) instead.\n\n";
}
sub endFile()