Created
January 18, 2023 19:08
-
-
Save pamoroso/ce69c2ba71ab9577533b76ed9648e119 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ Print sequences of printable characters in a file. | |
The program, which is similar to Unix strings(1), implements the following | |
state machine: | |
STATE INPUT NEXT STATE ACTIONS | |
---------------------------------------------------------- | |
Binary Unprint Binary | |
Binary Print MaybeStr Append input to Buffer | |
MaybeStr Unprint Binary | |
MaybeStr Print if Len >= Min Append input to Buffer | |
then Str | |
else MaybeStr | |
Str Unprint Binary Print Buffer | |
Reset Buffer | |
Str Print Str Append input to Buffer | |
Designed for Turbo Pascal 3 for CP/M. It has a bug, still under development } | |
program Strings; | |
const | |
MinLength = 4; { Minimum string length. } | |
type | |
String12 = string[12]; | |
String255 = string[255]; | |
BinFile = file of byte; | |
var | |
FileName : String12; | |
Infile : BinFile; | |
Buffer : String255; | |
function GetFilename: String12; | |
begin | |
if ParamCount <> 1 then | |
writeln('INVALID ARGUMENT') | |
else | |
GetFilename := ParamStr(1) | |
end; | |
procedure Open(var Stream: BinFile; var Name: String12); | |
begin | |
assign(Stream, Name); | |
reset(Stream) | |
end; | |
function IsPrintable(Code: byte): boolean; | |
begin | |
IsPrintable := (Code >= 32) and (code <= 127) | |
end; | |
procedure PrintStrings(var Stream: BinFile; MinLen: byte); | |
type | |
StateType = (Binary, MaybeStr, Str); | |
var | |
State : StateType; | |
Code : byte; | |
Buffer : String255; | |
begin | |
State := Binary; | |
Buffer := ''; | |
while not eof(Stream) do | |
begin | |
read(Stream, Code); | |
case State of | |
Binary : begin | |
if IsPrintable(Code) then | |
begin | |
Buffer := Buffer + chr(Code); | |
State := MaybeStr | |
end | |
end; | |
MaybeStr : begin | |
if IsPrintable(Code) then | |
begin | |
Buffer := Buffer + chr(Code); | |
if length(Buffer) >= MinLen then | |
State := Str | |
end | |
end; | |
Str : begin | |
if IsPrintable(Code) then | |
Buffer := Buffer + chr(Code) | |
else | |
begin | |
writeln(Buffer); | |
Buffer := ''; | |
State := Binary | |
end | |
end | |
end | |
end; | |
{ The state machine doesn't handle the case of a printable string | |
that ends at the end of the file, so check if this is the case. } | |
if length(Buffer) >= MinLength then | |
writeln(Buffer) | |
end; | |
BEGIN | |
FileName := GetFilename; | |
Open(Infile, FileName); | |
PrintStrings(Infile, MinLength); | |
close(Infile); | |
END. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment