home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / lang / mpw_util.hqx / hexdump.pit / hexdump.p < prev    next >
Encoding:
Text File  |  1986-10-02  |  2.7 KB  |  116 lines

  1. program hexdump;
  2.  
  3. uses     {$LOAD hexdump.dump}
  4.             {$U PasLibIntf.p}                PasLibIntf,
  5.             {$U IntEnv.p    }                    IntEnv;
  6. var
  7.     i,j,k,l: integer;                                {a bunch of integers I might need 
  8.                                                                                         (let's here it for FORTRAN)}
  9.     marker : integer;                             {to mark eof in asciistring}
  10.     fileEnd,rezOut:boolean;                    {my way to mark end of file, and -r option}
  11.     
  12. function GetChar: char;
  13. var
  14.     theChar:char;
  15.     wasEoln: boolean;
  16. begin
  17.         {check to see if the next character is an eoln char,
  18.             because Pascal is will return an eoln as a space }
  19.     if eoln then wasEoln := true else wasEoln := false;
  20.     read(input, theChar);
  21.     if wasEoln and not eof then        {this is because eoln is true at the end of the file}
  22.         GetChar := chr(13)
  23.     else if eof then
  24.         GetChar := ' '                            {we're liable to be called even if already at eof...}
  25.     else
  26.         GetChar := theChar;
  27. end;
  28.  
  29. function Get16Chars: string;
  30. var 
  31.     i:integer;
  32. begin
  33.     Get16Chars := '';
  34.     marker := 16;                    {the marker is in case we reach eof, so we know how
  35.                                                     many of the 16 are actually good characters.}
  36.     for i:=1 to 16 do
  37.     begin
  38.         Get16Chars[i] := GetChar;
  39.         if eof and (marker=16) then marker := i-1;
  40.     end;
  41. end;
  42.  
  43. function HexByte(theChar:char):string;
  44. var 
  45.     i,hiNybble,loNybble: integer;
  46.     
  47. begin        {turn a byte into a 2-digit hex string}
  48.     hiNybble:= ord(theChar) div 16;
  49.     loNybble:= ord(theChar) mod 16;
  50.     HexByte[0] := chr(2);
  51.     if fileEnd then                    {if we're past eof on a line of 16 chars, make it spaces}
  52.         HexByte:= '  '
  53.     else
  54.         begin
  55.             if hiNybble <= 9 then 
  56.                 HexByte[1] := chr(hiNybble + ord('0'))
  57.             else
  58.                 HexByte[1] := chr(hiNybble - 10 + ord('A'));
  59.             if loNybble <= 9 then 
  60.                 HexByte[2] := chr(loNybble + ord('0'))
  61.             else
  62.                 HexByte[2] := chr(loNybble - 10 + ord('A'));
  63.         end;
  64. end;
  65.  
  66. procedure ProcessTheFile;
  67. var 
  68.     i,j:integer;
  69.     asciiString:string;
  70. begin
  71.     while not eof do
  72.     begin
  73.         asciiString := Get16Chars;
  74.         if rezOut then write(output, '"$');
  75.         i:=1;
  76.         repeat
  77.             for j:=0 to 1 do        {this is to print bytes in pairs, but not print
  78.                                                         the second one if the first one was the last char in the file}
  79.             begin
  80.                 if not eof or (i+j<=marker) then
  81.                     fileEnd := false
  82.                 else
  83.                     fileEnd := true;
  84.                 write(output, HexByte(asciiString[i+j]));
  85.             end;
  86.             write(output, ' ');
  87.             i:=i+2;
  88.         until i>15;
  89.         if rezOut then
  90.             write(output,'"     /* |')
  91.         else
  92.             write(output,'    |');
  93.         for i:=1 to 16 do
  94.             if (ord(asciiString[i])<32) or (ord(asciiString[i])=127) then
  95.                 write(output,chr(255))
  96.             else
  97.                 write(output,asciiString[i]);
  98.         if rezOut then
  99.             writeln(output,'| */')
  100.         else
  101.             writeln(output,'|');
  102.     end;
  103. end;
  104.  
  105. procedure OpenTheFile;
  106. begin
  107.     if argC>1 then        {check to see if they want Rez-type output}
  108.         if argV^[1]^ = '-r' then rezOut:=true else rezOut:=false;
  109. end;
  110.  
  111. {MAIN}
  112. begin
  113.     OpenTheFile;
  114.     ProcessTheFile;
  115.     IEExit(0);
  116. end.