# # $Id: Recs.pl,v 1.8 2001/03/06 13:44:39 ajk Exp $ # # Generate Recs.java for record types for file formats. # # Java Serializable is CLOSE but not quite close enough to what we need # (e.g. it would write out the "offset" field) # AND we want a standard binary format. $file = $ARGV[0]; $file = "Recs.pl" if ($file eq ""); open F, ">$file" or die "Can't open file $file"; $rcsid = '$Id: Recs.pl,v 1.8 2001/03/06 13:44:39 ajk Exp $'; print F <[2]; my $len = join '+', " super.length() ", map { $_->[0] eq "long" and 8 or $_->[0] eq "int" and 4 or $_->[0] eq "short" and 2 or $_->[0] eq "boolean" and 1 or $_->[0] eq "String" and " (2 + $_->[1].getBytes(\"UTF8\").length) " or die "Invalid type $_->[0]" } @f; my $write = join '', map { $_->[0] eq "String" and "{ byte[] bt = $_->[1].getBytes(\"UTF8\"); s.writeShort(bt.length); s.write(bt, 0, bt.length); } " # or $_->[0] eq "bool" and "s.writeBoolean($_->[1]);" or "s.write".(ucfirst $_->[0])."($_->[1]);"; } @f; my $read = join '', map { $_->[0] eq "String" and "{ short l = s.readShort(); byte[] bt = new byte[l]; int n = s.read(bt); if(n!=l) throw new ZZError(\"NOT ENOUGH DATA\"); $_->[1] = new String(bt, \"UTF8\"); } " # or $_->[0] eq "bool" and "$_->[1] = s.readBoolean();" or "$_->[1] = s.read".(ucfirst $_->[0])."();"; } @f; my $tostring = join '', map { qq( + " $_->[1]: "+$_->[1]+"\\n" ) } @f; print F <[0] extends $_->[1] { $_->[2]; public int length() { try { return $len; } catch(Exception e) { ZZLogger.exc(e); throw new ZZError("ARGH"); } } public short getTypeNo() { return $_->[3]; } public void write_data(DataOutputStream s) throws IOException { super.write_data(s); $write } public void read_data(long offs, DataInputStream s) throws IOException { super.read_data(offs, s); $read } public String toString() { return "$_->[0]:\\n" $tostring + super.toString(); } } END } print F qq[ static public Record createRecord(int typ) { $rettype throw new ZZError("Invalid record type "+typ); } } ];