#!/usr/bin/perl -w use English; if (@ARGV < 2) { printf "$PROGRAM_NAME fmt_type filename data1 data2 ... dataN\n"; printf "Create binary file for Micro I lab. \n"; printf "File formats types:\n"; printf "\n"; printf "format 0: byte data (1 record = 1 byte)\n"; printf "format 1: 16 bit integers, little endian (1 record = 2 bytes)\n"; printf "format 2: 16 bit integers, big endian (1 record = 2 bytes)\n"; printf "format 3: 32 bit integers, little endian (1 record = 4 bytes)\n"; printf "format 4: 32 bit integers, big endian (1 record = 4 bytes)\n"; printf "\n"; printf "For format #0, only one 'data' is allowed, and is a quoted string\n"; printf "\n"; printf "File format:\n"; printf "\n"; printf "byte 0: format number that determines file format\n"; printf "bytes 1,2: # of records (16-bit number , little endian)\n"; printf "bytes: 3 to end -- records\n"; printf "\n"; exit(0); } $fmt_type = $ARGV[0]; $fname = $ARGV[1]; printf "Format type: $fmt_type, Fname: $fname \n"; open(OUTPUT,">$fname") || die "Can't open output file $fname"; binmode(OUTPUT); if ($fmt_type == 0) { $dstring = $ARGV[2]; $num_records = length($dstring); printf "Format 0, number of records of data is $num_records\n"; $temp = "Cva". length($dstring); $buf = pack $temp,$fmt_type,$num_records,$ARGV[2]; print OUTPUT $buf; }elsif ($fmt_type == 1) { $num_records = @ARGV; $num_records = $num_records - 2; printf "Format 1, number of records of data is $num_records\n"; $buf = pack "Cv",$fmt_type,$num_records; print OUTPUT $buf; for ($i=2;$i<@ARGV;$i++) { $j = $ARGV[$i]; $buf = pack "v",$j; print OUTPUT $buf; } }elsif ($fmt_type == 2) { $num_records = @ARGV; $num_records = $num_records - 2; printf "Format 2, number of records of data is $num_records\n"; $buf = pack "Cv",$fmt_type,$num_records; print OUTPUT $buf; for ($i=2;$i<@ARGV;$i++) { $j = $ARGV[$i]; $buf = pack "n",$j; print OUTPUT $buf; } }elsif ($fmt_type == 3) { $num_records = @ARGV; $num_records = $num_records - 2; printf "Format 3, number of records of data is $num_records\n"; $buf = pack "Cv",$fmt_type,$num_records; print OUTPUT $buf; for ($i=2;$i<@ARGV;$i++) { $j = $ARGV[$i]; $buf = pack "V",$j; print OUTPUT $buf; } }elsif ($fmt_type == 4) { $num_records = @ARGV; $num_records = $num_records - 2; printf "Format 3, number of records of data is $num_records\n"; $buf = pack "Cv",$fmt_type,$num_records; print OUTPUT $buf; for ($i=2;$i<@ARGV;$i++) { $j = $ARGV[$i]; $buf = pack "N",$j; print OUTPUT $buf; } } else { printf "Unsupported format type: $fmt_type, exiting....\n"; exit(0); } close(OUTPUT);