home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1999 November / VPR9911B.ISO / misc / src / trees / syslinux-1.42 / bin2c.pl next >
Perl Script  |  1998-12-05  |  1KB  |  51 lines

  1. #!/usr/bin/perl
  2. # $Id: bin2c.pl,v 1.2 1998/02/04 06:23:54 hpa Exp $
  3. # -----------------------------------------------------------------------
  4. #   
  5. #   Copyright 1998 H. Peter Anvin - All Rights Reserved
  6. #
  7. #   This program is free software; you can redistribute it and/or modify
  8. #   it under the terms of the GNU General Public License as published by
  9. #   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
  10. #   USA; either version 2 of the License, or (at your option) any later
  11. #   version; incorporated herein by reference.
  12. #
  13. # -----------------------------------------------------------------------
  14. #
  15. # bin2c.pl: binary file to C source converter
  16. #
  17.  
  18. if ( $#ARGV != 0 ) {
  19.     print STDERR "Usage: $0 table_name < input_file > output_file\n";
  20.     exit 1;
  21. }
  22.  
  23. ($table_name) = @ARGV;
  24.  
  25. printf "unsigned char %s[] = {\n", $table_name;
  26.  
  27. $pos = 0;
  28. $linelen = 8;
  29.  
  30. $total_len = 0;
  31.  
  32. while ( ($n = read(STDIN, $data, 4096)) > 0 ) {
  33.     $total_len += $n;
  34.     for ( $i = 0 ; $i < $n ; $i++ ) {
  35.     $byte = substr($data, $i, 1);
  36.     if ( $pos >= $linelen ) {
  37.         print ",\n\t";
  38.         $pos = 0;
  39.     } elsif ( $pos > 0 ) {
  40.         print ", ";
  41.     } else {
  42.         print "\t";
  43.     }
  44.     printf("0x%02x", unpack("C", $byte));
  45.     $pos++;
  46.     }
  47. }
  48.  
  49. printf "\n};\n\nunsigned int %s_len = %u;\n", $table_name, $total_len;
  50.  
  51.