home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Overload
/
ShartewareOverload.cdr
/
progm
/
tptools.zip
/
ALLINST.ZIP
/
EDITOOLS.PAS
next >
Wrap
Pascal/Delphi Source File
|
1987-12-21
|
12KB
|
412 lines
{ EDITOOLS.INC
Editor Toolbox 4.0
Copyright (c) 1985, 87 by Borland International, Inc. }
{$I-}
{$S-}
{$R-}
unit EDItools;
interface
uses
Crt, {standard screen routines}
Dos, {dos calls - standard unit}
SInst, {fast screen routines}
EscSeq; {returns name for extended keystroke sequence}
const
KeyLength = 6; {maximum bytes in keystroke sequence}
Escape = #27;
type
VarString = string[DefNoCols];
KeyString = string[KeyLength];
KeyRec =
record
Modified,
Conflict : Boolean;
Keys : KeyString;
MinCol, MaxCol : Byte;
end;
var
BlankLine : VarString;
function FileExists(Name : VarString; var F : file) : Boolean;
{-return true and an open file if file exists}
procedure StUpcase(var S : VarString);
{-uppercase the string}
function GetCursorCommand : Char;
{-return cursor equivalent keys}
function Getkey(Prompt, Choices : VarString) : Char;
{-return a legal menu choice}
function YesNo(Prompt : VarString; Default : Char) : Boolean;
{-return True for yes, False for no}
procedure ClrEol(Col, Row, Attr : Integer);
{-clear to end of line}
procedure Center(Row, Attr : Byte; Prompt : varstring);
{-write the prompt centered on row with attribute attr}
function CenterPad(S : VarString; PadCh : Char; Width : Byte) : varstring;
{-pad s on both sides with padch to total size width}
function Pad(S : VarString; w : Byte) : VarString;
{-return a string right padded with blanks to width w}
function TextRepresentation(var K) : VarString;
{-return a text representation of the keys}
procedure HaltError(msg : varstring);
{-display an error message and halt}
function FindString(IdString : String; var Deft; Size : Word) : LongInt;
{-return the location of IdString in ProgramFile and read Size bytes into
Deft.}
function ModifyDefaults(FileOfst : LongInt; var B; Size : Word) : Boolean;
{-Write modified default settings back to disk, returning a success flag}
procedure Initialize(Name, Version : String);
{-Set up for installation}
procedure CleanUp;
{-Clean up at end of program}
{==========================================================================}
implementation
const
SBSize = 65518;
type
SearchBuffer = array[0..SBSize] of Char; {Used to search for ID strings}
var
Regs : Registers;
ProgramFile : file;
ProgramName : string[64];
BufPtr : ^SearchBuffer;
BytesRead : Word;
FilePtr : LongInt;
BufferFull : Boolean;
function FileExists(Name : VarString; var F : file) : Boolean;
{-return true and an open file if file exists}
begin
Assign(F, Name);
Reset(F, 1);
FileExists := (IOResult = 0);
end; {fileexists}
procedure StUpcase(var S : VarString);
{-uppercase the string}
var
I : Byte;
Len : Byte absolute S;
begin
for I := 1 to Len do
S[I] := UpCase(S[I]);
end; {Stupcase}
function GetCursorCommand : Char;
{-return cursor equivalent keys. Also allows Esc, 'C', and 'R'.}
const
CursorSet : set of Char =
[^H, ^R, ^C, ^E, ^X, ^W, ^Z, ^A, ^S, ^D, ^F, ^M, 'C', 'R', ^T, ^B, #27];
var
Ch : Char;
begin
repeat
Ch := ReadKey;
if (Ch = #0) then begin
Ch := ReadKey;
case Ch of
#75 : Ch := ^S;
#77 : Ch := ^D;
#72 : Ch := ^E;
#80 : Ch := ^X;
#71 : Ch := ^T;
#73 : Ch := ^R;
#81 : Ch := ^C;
#79 : Ch := ^B;
end;
end
else
Ch := UpCase(Ch);
until (Ch in CursorSet);
GetCursorCommand := Ch;
end; {GetCursorCommand}
function Getkey(Prompt, Choices : VarString) : Char;
{-return a legal menu choice}
var
Ch : Char;
begin
Write(prompt);
repeat
Ch := UpCase(ReadKey);
until Pos(Ch, choices) <> 0;
Getkey := Ch;
end; {GetKey}
function YesNo(Prompt : VarString; Default : Char) : Boolean;
{-return True for yes, False for no}
var
Ch : Char;
begin
Write(Prompt, ' (Y/N/<Enter> for ', Default, ') ');
repeat
Ch := ReadKey;
if Ch = ^M then
Ch := Default;
Ch := UpCase(Ch);
until (Ch = 'Y') or (Ch = 'N');
WriteLn(Ch);
YesNo := (Ch = 'Y');
end; {YesNo}
procedure ClrEol(Col, Row, Attr : Integer);
{-clear to end of line}
begin
BlankLine[0] := Chr(81-Col);
EdFastWrite(BlankLine, Row, Col, Attr);
end;
procedure Center(Row, Attr : Byte; Prompt : VarString);
{-write the prompt centered on row with attribute attr}
begin
ClrEol(1, Row, Attr);
EdFastWrite(Prompt, Row, 41-(Length(Prompt) shr 1), Attr);
end; {Center}
function CenterPad(S : VarString; PadCh : Char; Width : Byte) : VarString;
{-pad s on both sides with padch to total size width}
var
T : VarString;
begin
if Length(S) >= Width then
CenterPad := S
else begin
FillChar(T[1], Width, PadCh);
T[0] := Chr(Width);
Move(S[1], T[1+((Width-Length(S)) shr 1)], Length(S));
CenterPad := T;
end;
end; {CenterPad}
function Pad(S : VarString; W : Byte) : VarString;
{-return a string right padded with blanks to width w}
begin
while Length(S) < W do
S := S+' ';
Pad := S;
end; {pad}
function TextRepresentation(var K) : VarString;
{-return a text representation of the keys}
var
KR : KeyRec absolute K;
P, Len : Byte;
Ch : Char;
Dis : VarString;
begin {TextRepresentation}
with KR do begin
P := 1;
Len := Length(Keys);
Dis := '';
while P <= Len do begin
Ch := Keys[P];
case Ch of
#0 : if (P = Len) then
{a lone null, where did it come from?}
Dis := Dis+'<Null>'
else begin
{an escape sequence}
P := Succ(P);
Dis := Dis + EscapeSequence(Keys[P]);
end;
#27 : {escape key}
Dis := Dis + '<Esc>';
#1..#31 : {control char}
Dis := Dis + '<Ctrl' + Chr(Ord(Ch)+64) + '>';
#127 : {ctrl-backspace = ASCII DEL}
Dis := Dis + '<CtrlBks>';
else {normal char - shouldn't be any used as commands}
Dis := Dis + Ch;
end; {case}
P := Succ(P);
end; {while}
end; {with}
TextRepresentation := Dis;
end; {TextRepresentation}
procedure HaltError(Msg : Varstring);
{-Display an error message and halt}
begin {HaltError}
RestoreScreen;
WriteLn;
WriteLn(Msg);
Halt(1);
end; {HaltError}
{$L SEARCH}
{$F+}
function Search(var Buffer; BufLength : Word; St : String) : Word; external;
{-Search through Buffer for St. BufLength is length of range to search.
Returns 0 if not found. Otherwise, the result is the index into an
array whose lower bound is 1. Subtract 1 for 0-based arrays.}
{$F-}
function FindString(IdString : String; var Deft; Size : Word) : LongInt;
{-return the location of IdString in ProgramFile and read Size bytes into
Deft.}
const
SeekErrorMsg : string[30] = 'Seek error while reading from ';
ReadErrorMsg : string[22] = 'I/O error reading from ';
var
I, BufPos,
IdSize, BufSize : Word;
FSTemp : LongInt;
label
FoundIdString;
begin
IdSize := Succ(Length(IdString));
BufSize := SizeOf(SearchBuffer);
{if we have a full buffer, see if it contains the ID string}
if BufferFull then begin
BufPos := Search(BufPtr^, BytesRead, IdString);
if BufPos <> 0 then
goto FoundIdString;
end;
{point at start of file}
Seek(ProgramFile, 0);
if (IOResult <> 0) then
HaltError(SeekErrorMsg + ProgramName);
{Read the first bufferful}
BlockRead(ProgramFile, BufPtr^, BufSize, BytesRead);
if (IOResult <> 0) then
HaltError(ReadErrorMsg + ProgramName);
{set flag to indicate the buffer is full}
BufferFull := True;
{keep track of file pointer}
FilePtr := BytesRead;
{scan the first buffer}
BufPos := Search(BufPtr^, BytesRead, IdString);
{loop until IdString found or end of file reached}
while (BufPos = 0) and (BytesRead >= IdSize) do begin
{Move the tail end of the buffer to the front of the buffer}
Move(BufPtr^[BytesRead-IdSize], BufPtr^, IdSize);
{Read the next bufferful}
BlockRead(ProgramFile, BufPtr^[IdSize], BufSize-IdSize, BytesRead);
{keep track of where we are in the file}
FilePtr := FilePtr + BytesRead;
{adjust BytesRead to indicate the actual number of bytes in the buffer}
BytesRead := BytesRead + IdSize;
{search the buffer for the IdString}
BufPos := Search(BufPtr^, BytesRead, IdString);
end;
FoundIdString:
if (BufPos = 0) then
FSTemp := 0
else begin
{account for fact that BufPtr^ is a 0-based array}
Dec(BufPos);
{calculate the actual position in the file}
FSTemp := (FilePtr - BytesRead) + BufPos + IdSize;
{get the existing default parameter area into Deft}
{Use contents of existing buffer if possible}
if (BytesRead - BufPos) > Size then
Move(BufPtr^[BufPos + IdSize], Deft, Size)
else begin
{seek to the right location}
Seek(ProgramFile, FSTemp);
if (IOResult <> 0) then
HaltError(SeekErrorMsg + ProgramName);
{read directly into Deft from ProgramFile}
BlockRead(ProgramFile, Deft, Size, I);
if I <> Size then
HaltError(ReadErrorMsg + ProgramName);
end;
end;
FindString := FSTemp;
end; {findstring}
function ModifyDefaults(FileOfst : LongInt; var B; Size : Word) : Boolean;
{-Write modified default settings back to disk, returning a success flag}
var
BytesWritten : Word;
begin {ModifyDefaults}
{seek into file}
Seek(ProgramFile, FileOfst);
if (IOResult <> 0) then
HaltError('Seek error while writing to '+ ProgramName);
{write modified defaults}
BlockWrite(ProgramFile, B, Size, BytesWritten);
{return success/failure flag}
ModifyDefaults := (BytesWritten = Size);
end; {ModifyDefaults}
procedure Initialize(Name, Version : String);
{-Set up for installation}
begin {Initialize}
{setup screen}
SetColor(TiColor);
ClrScr;
{save the name of the program for other routines}
ProgramName := Name;
{signon message}
WriteLn(^M^J, ProgramName, ' Installation Program Version ', Version, ^M^J);
{Make sure executable file is found}
if not(FileExists(ProgramName, ProgramFile)) then
HaltError('Executable file '+ProgramName+' not found');
{get a work area}
New(BufPtr);
BufferFull := False;
{anything else}
FillChar(BlankLine[1], 80, #32);
end; {Initialize}
procedure CleanUp;
{-Clean up at end of program}
begin {CleanUp}
Close(ProgramFile);
RestoreScreen;
end; {CleanUp}
end.