#!/usr/contrib/bin/perl -w

#---------------------------------
sub operateStruct
#---------------------------------
{
	# position of first "{"
	$i = index ($file, "{");
	$name = substr ($file, 6, $i-6);
	$name =~ s/\W//g;
	print "ostream & operator << ( ostream & o, const $name & d )\n\n";
    print "{\n";
	print "    o << \"  $name :{  \"\n";
	$i1 = index ($file, ";", $i+1);
	$iend = index ($file, "}");
	while ($i1 != -1 && $i1 < $iend)
	{
		$name = substr ($file, $i+1, $i1-$i);
#		print "--$name--\n";
		# get rid of comments
		$name =~ s/^\W+\/\/.*\n//;
#		print "--$name--\n";
		# get rid of CR, TAB ...
		$name =~ s/^\W+//;
#		print "--$name--\n";
        # get rid of member type
		$name =~ s/^.+\s+//;
#		print "--$name--\n";
		$name =~ s/;$//;
		print "      << \" $name: \"              << d.$name\n";
		$i = $i1;
		$i1 = index ($file, ";", $i+1);
	}
	print "      << \"}\"\n";
	print "      << endl;\n";
	print "    return o;\n";
	print "}\n\n";
	$file = substr ($file, $i);
}
	


#---------------------------------
sub operateEnum
#---------------------------------
{
	# position of first "{"
	$i = index ($file, "{");
	$name = substr ($file, 4, $i-4);
	$name =~ s/\W//g;
	print "ostream & operator << ( ostream & o, $name d )\n";
    print "{\n";
    print "    vector<string> sMap;\n";
	$i1 = index ($file, ",", $i+1);
	$iend = index ($file, "}");
	while ($i1 != -1 && $i1 < $iend)
	{
		$name = substr ($file, $i+1, $i1-$i);
		# get rid of comments
		$name =~ s/^\W+\/\/.*\n//;
		$name =~ s/,$//;
		$name =~ s/\W//g;
		print "    sMap.push_back (\"$name\");\n";
		$i = $i1;
		$i1 = index ($file, ",", $i+1);
	}
	
	# take last element
	$name = substr ($file, $i+1, $iend-$i);
	# get rid of leading comments
	$name =~ s/\W+\/\/.*\n//;
	# get rid of tailing comments
#	$name =~ s/\/\/.*\n//;
	$name =~ s/[\W}]//g;
	print "    sMap.push_back (\"$name\");\n";
	
	# finish output
	print "    ASSERT( d < sMap.size() );\n";
	print "    o << sMap [d];\n";
	print "    return o;\n";
	print "}\n\n";
	
	# remove enum from file string
	$file = substr ($file, $i);
}


#---------------------------------
# Hauptprogramm
#---------------------------------

$file = "";
while ($line = <STDIN>)
{
	$file = $file . $line;
}

#print "--" . substr ($file, 0, 10) . "--\n";

# parse file
while ($file)
{
	$file =~ s/^\s+//;
#	print "--" . substr ($file, 0, 10) . "--\n";
	
	if ($file =~ /^struct/)
	{
		&operateStruct ();
	}
	elsif ($file =~ /^enum/)
	{
		&operateEnum ();
	}
	else
	{
		$file =~ s/.+\s//;
	}
}
