#! /usr/bin/perl # any2c.pl -- converts text/binary to C string/data # $Id$ # Carlos Duarte , 000123 use strict; sub usage { print <) { if (!$o{'mode'}) { $o{'mode'} = detect(substr($_, 0, 100)); } if (!$inited) { die "invalid mode: $o{'mode'}" if ($o{'mode'} ne "text" && $o{'mode'} ne "bin"); my @p = $o{'mode'} eq "text" ? ("*", "", "\"\\") : ("", "[]", "{"); ## } for vi print {$fh} "unsigned char $p[0]$o{'name'}_buf$p[1] = $p[2]\n"; $inited++; } $len += length; if ($o{'mode'} eq "text") { s/[\000-\037\177-\377\\\"]/do { local $_ = $&; if ($_ eq "\n") { "\\n\\\n"; } elsif ($_ eq "\t") { "\\t"; } elsif ($_ eq "\\") { "\\\\"; } elsif ($_ eq "\"") { "\\\""; } else { sprintf "\\%03o", ord($_); } }/seg; } else { s/./do { my $b; if (++$bpos == 1) { $b .= "\t"; } else { $b .= ", "; # 8 is the nr of bytes per line # can be any other value $bpos%8==1 and $b .= "\n\t"; } $b .= sprintf "0x%02X", ord($&); $b; }/seg; } print {$fh} $_; } print {$fh} $o{'mode'} eq "text" ? "\";\n" : "\n};\n"; print {$fh} "unsigned int $o{'name'}_len = $len; \n"; $fh ne \*STDOUT and close $fh; } exit; ### sub openit { my $name = shift; local *FH; return open (FH, $name) ? *FH : undef; } sub detect { local $_ = shift; my $tot = length; y/ -~\n\t//d; # delete ascii my $n = length; ## less than 5% bin chars means text return $n<$tot/20 ? "text" : "bin"; }