Silly of me to overlook it: another obvious way you might like to
[sgt/charset] / sbcsgen.pl
index 02aaa0f..e2991b2 100644 (file)
@@ -7,6 +7,8 @@ $infile = "sbcs.dat";
 $infile = shift @ARGV if defined $ARGV[0];
 $outfile = "sbcsdat.c";
 $outfile = shift @ARGV if defined $ARGV[0];
+$outheader = "sbcsdat.h";
+$outheader = shift @ARGV if defined $ARGV[0];
 
 open FOO, $infile;
 open BAR, ">$outfile";
@@ -33,6 +35,7 @@ my @sortpriority = ();
 
 while (<FOO>) {
     chomp;
+    y/\r\n//; # robustness in the face of strange line endings
     if (/^charset (.*)$/) {
        $charsetname = $1;
        @vals = ();
@@ -65,11 +68,37 @@ foreach $i (@charsetnames) {
 print "\n";
 print "#endif /* ENUM_CHARSETS */\n";
 
+close BAR;
+
+open BAR, ">$outheader";
+select BAR;
+
+print "/*\n";
+print " * sbcsdat.h - header file for SBCS data structures.\n";
+print " *\n";
+print " * Generated by sbcsgen.pl from sbcs.dat.\n";
+print " * You should edit those files rather than editing this one.\n";
+print " */\n";
+print "\n";
+print "#ifndef charset_sbcsdat_h\n";
+print "#define charset_sbcsdat_h\n";
+print "\n";
+print "#include \"charset.h\"\n";
+print "#include \"internal.h\"\n";
+print "\n";
+foreach $i (@charsetnames) {
+    print "extern const sbcs_data sbcsdata_$i;\n";
+}
+print "\n";
+print "#endif /* charset_sbcsdat_h */\n";
+
+close BAR;
+
 sub outcharset($$$) {
     my ($name, $vals, $sortpriority) = @_;
     my ($prefix, $i, @sorted);
 
-    print "static const sbcs_data data_$name = {\n";
+    print "const sbcs_data sbcsdata_$name = {\n";
     print "    {\n";
     $prefix = "    ";
     @sorted = ();
@@ -88,9 +117,10 @@ sub outcharset($$$) {
        }
     }
     print "\n    },\n    {\n";
-    @sorted = sort { $a->[1] == $b->[1] ?
-                    $b->[2] <=> $a->[2] :
-                    $a->[1] <=> $b->[1] } @sorted;
+    @sorted = sort { ($a->[1] == $b->[1] ?
+                     $b->[2] <=> $a->[2] :
+                     $a->[1] <=> $b->[1]) ||
+                     $a->[0] <=> $b->[0] } @sorted;
     $prefix = "    ";
     $uval = -1;
     for ($i = $j = 0; $i < scalar @sorted; $i++) {
@@ -107,5 +137,5 @@ sub outcharset($$$) {
     printf "\n    },\n    %d\n", $j;
     print "};\n";
     print "const charset_spec charset_$name = {\n" .
-          "    $name, read_sbcs, write_sbcs, &data_$name\n};\n\n";
+          "    $name, read_sbcs, write_sbcs, &sbcsdata_$name\n};\n\n";
 }