home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Hacker Chronicles 2
/
HACKER2.BIN
/
147.FILESYST.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1989-03-17
|
17KB
|
426 lines
(****************************************************************************)
(*** ***)
(*** FileIO saves, retrieves, or deletes files from disk. Data files ***)
(*** will be saved as follows: ***)
(*** 1. Number of elements (= NumPoints). ***)
(*** 2. x, y (the time domain data point) ***)
(*** 3. From 0 up to MaxInfo number of information lines. ***)
(*** ***)
(*** Additionally, files may be "imported" from the Device Damage ***)
(*** Testing (DDT) programs. When imported, the DDT file is translated ***)
(*** to the format listed above. The operator is also given the option ***)
(*** of scaling the integer values of the DDT files to "true" values. ***)
(*** ***)
(*** A directory may also be shown by giving a file mask. The file mask ***)
(*** is of the form [d:][path][filename][.ext]. Wildcards may be used. ***)
(*** ***)
(****************************************************************************)
UNIT FileSystem;
INTERFACE
USES
DOS,
{$IFDEF DOSCrt}
DOSCrt,
{$ELSE}
Crt,
{$ENDIF}
Extended_Reals,
TextOps,
Global;
PROCEDURE FileIO;
(****************************************************************************)
IMPLEMENTATION
VAR
counter : INTEGER; (* counter variables *)
OKSave : BOOLEAN; (* overwrite old file? *)
Name : text; (* file input/output stream *)
choice : CHAR;
{----------------------------------------------------------------------------}
{- -}
{- Modify searches a filename for three conditions: -}
{- 1. No filename given (length = 0); -}
{- 2. Drive name specified (e.g. a:); -}
{- 3. Full path name specified (e.g. \fft\sine.raw). -}
{- If none of these conditions is true, then the default data directory -}
{- is added to the path of the filename (e.g. if the default data -}
{- directory is 'c:\fft' and the filename is 'sine.raw', the resulting -}
{- filename from Modify will be 'c:\fft\sine.raw'.) -}
{- -}
{----------------------------------------------------------------------------}
PROCEDURE Modify (VAR filename : string);
BEGIN {Modify}
IF (length (filename) > 0) AND
(filename[2] <> ':') AND
(filename[1] <> '\')
THEN filename:=DefaultDataDir+'\'+filename;
END; {Modify}
{----------------------------------------------------------------------------}
{- -}
{- SaveFile tests if the specified filename already exists. If so, the -}
{- user is asked if the old file should be overwritten. If so, or if -}
{- the old file does not exist, then the data is saved as filename and -}
{- the user is returned to the Main Menu, otherwise the user is -}
{- returned to the FileIO Menu. -}
{- -}
{----------------------------------------------------------------------------}
PROCEDURE SaveFile;
VAR
filename : string;
i : INTEGER;
BEGIN {SaveFile}
OKSave:=FALSE;
WriteXY ('Save to file: ',StartColumn,24);
ReadLn (filename);
Modify (filename);
{- Does file exist? If so, is it safe to overwrite it? -}
IF NOT EXIST (filename)
THEN BEGIN
IF length (filename) > 0
THEN OKSave:=TRUE
ELSE OKSave:=FALSE;
END {THEN}
ELSE BEGIN
PrintErrorMsg ('File already exists: overwrite (y/n)? ',
StartColumn,21,TRUE,choice);
IF (UpCase(choice)='Y')
THEN OKSave:=TRUE
ELSE OKSave:=FALSE;
END; {ELSE}
{- If it is safe to write to the file, then save the data. -}
IF OKSave THEN BEGIN
{$I-} (* Turn off error checking. *)
WriteXY ('Saving file. Please wait ... ',StartColumn,22);
Assign (Name,filename);
Rewrite (Name);
WriteLn (Name,NumPoints);
FOR i:=0 TO NumPoints-1 DO
WriteLn (Name,time^[i]:precision,' ',
ampl^[i]:precision);
FOR i:=1 TO MaxInfo DO
WriteLn (Name,info[i]);
Close (Name);
GotoXY (StartColumn,22); ClrEOL;
GotoXY (StartColumn,24); ClrEOL;
{$I+} (* Turn error checking on. *)
IF (IOResult <> 0) THEN BEGIN
PrintErrorMsg ('Error on disk. File not saved! ',
StartColumn,24,FALSE,choice);
END; {IF}
END; {IF}
END; {SaveFile}
{----------------------------------------------------------------------------}
{- -}
{- RetrieveFile first calls EXIST. If the file exists, then the file -}
{- is read and the user is returned to the Main Menu; otherwise the -}
{- user is given an error message and returned to the FileIO Menu. -}
{- -}
{----------------------------------------------------------------------------}
PROCEDURE RetrieveFile;
PROCEDURE ReadReal ( VAR st : string;
VAR x : REAL
);
VAR
error : INTEGER;
x_str : string [33];
BEGIN {ReadReal}
x_str:=st;
WHILE x_str[1] = ' ' DO
x_str:=Copy (x_str,2,length(x_str));
IF (x_str[1] = '.')
THEN x_str:='0'+x_str
ELSE IF ((x_str[1] = '-') AND (x_str[2] = '.'))
THEN BEGIN
x_str[1]:='0';
x_str:='-'+x_str;
END; {ELSE-IF}
Val (x_str,x,error);
IF error <> 0 THEN BEGIN
st:=Copy (x_str,error,length(x_str));
x_str:=Copy (x_str,1,error-1);
Val (x_str,x,error);
END; {IF}
END; {ReadReal}
VAR
delta_x : REAL; (* distance between time values *)
filename : string;
i : INTEGER; (* temporary counter variable *)
j : INTEGER;
y : string[33];
BEGIN {RetrieveFile}
GotoXY (StartColumn,22); ClrEOL;
WriteXY ('File to read: ',StartColumn,24);
ReadLn (filename);
Modify (filename);
{- Does file exist? If not, ERROR! If so, read the file. -}
IF NOT EXIST (filename)
THEN BEGIN
IF length (filename) > 0 THEN
PrintErrorMsg ('File does not exist!',
StartColumn+2,22,FALSE,choice);
WriteXY ('Your Choice? ',StartColumn,24);
END {THEN}
ELSE BEGIN
WriteXY ('Reading. Please wait ...',StartColumn,22);
Assign (Name,filename);
Reset (Name);
TRANS:=FALSE;
ORIG:=TRUE;
ACCEPT:=FALSE;
{--- Initialize array variables ---}
FillChar (time^, SizeOf(time^), 0);
FillChar (ampl^, SizeOf(ampl^), 0);
FillChar (freq^, SizeOf(freq^), 0);
FillChar (mag^, SizeOf(mag^), 0);
FillChar (phase^,SizeOf(phase^),0);
ReadLn (Name,NumPoints);
{--- Read (x,y) coordinate pairs ---}
FOR i:=0 TO NumPoints-1 DO BEGIN
ReadLn (Name,y);
ReadReal (y,time^[i]);
ReadReal (y,ampl^[i]);
END; {FOR}
i:=1;
WHILE (NOT EOF (Name)) AND (i <= MaxInfo) DO BEGIN
ReadLn (Name,info[i]);
INC (i,1);
END; {WHILE}
FOR j:=i+1 TO MaxInfo DO
info[i]:=blank;
Close (Name);
END; {ELSE}
END; {RetrieveFile}
{----------------------------------------------------------------------------}
{- -}
{- DeleteFile deletes a file, if present, from disk, after asking the -}
{- user to verify that he wants to delete it. -}
{- -}
{----------------------------------------------------------------------------}
PROCEDURE DeleteFile;
VAR
filename : string;
BEGIN {DeleteFile}
WriteXY ('Delete filename: ',StartColumn,24);
ReadLn (filename);
Modify (filename);
IF EXIST (filename) THEN BEGIN
PrintErrorMsg ('Delete file (y/n)? ',StartColumn+2,22,TRUE,choice);
IF (UpCase (choice) = 'Y') THEN Erase (Name);
END; {THEN}
END; {DeleteFile}
{----------------------------------------------------------------------------}
{- -}
{- This procedure lists the directory of the current (logged) drive. -}
{- -}
{----------------------------------------------------------------------------}
PROCEDURE DiskDirectory;
CONST
MaxDirectoryEntries = 200;
DefaultDrive = 0;
Search = $30; {search for directories and files }
TYPE
MaxEntries = 1..MaxDirectoryEntries;
Colors = 1..8;
VAR
NamR : array [1..MaxDirectoryEntries] of string [12];
EntryDir : array [1..MaxDirectoryEntries] of BOOLEAN;
DefaultDir : string;
buffer : string;
temp : SearchRec;
DirColor : byte;
DirBack : byte;
EntryNumber : byte;
NumEntries : byte;
NumScreens : byte;
x : byte;
y : byte;
z : byte;
ch : CHAR;
Drive : byte;
temp_int : longint;
BEGIN {DiskDirectory}
DirColor:=abs((7-ForeColor) mod 16);
DirBack :=abs((7-BackColor) mod 16);
ClrScr;
GetDir (DefaultDrive,DefaultDir);
ChDir (DefaultDataDir);
FillChar (NamR,SizeOf(NamR),0);
FillChar (buffer,SizeOf(buffer),0);
buffer[0]:=CHAR(0);
WriteXY ('File mask: ',1,1); ReadLn (buffer);
IF (length(buffer) = 0)
THEN BEGIN
buffer:='*.*';
IF DefaultDataDir[2] = ':'
THEN Drive:=ord(UpCase(DefaultDataDir[1]))-ord('A')+1
ELSE Drive:=DefaultDrive;
WriteXY (DefaultDataDir,12,1);
END {THEN}
ELSE BEGIN
IF buffer[2] = ':' {Get drive number}
THEN BEGIN
Drive:=ord(UpCase(buffer[1]))-(ord('A')-1);
IF length(buffer) = 2 THEN buffer:=buffer+'*.*';
END {THEN}
ELSE BEGIN
IF DefaultDataDir[2] = ':'
THEN Drive:=ord(UpCase(DefaultDataDir[1]))-ord('A')+1
ELSE Drive:=DefaultDrive;
END; {ELSE}
END; {ELSE}
EntryNumber:=0;
FindFirst (buffer,search,temp);
IF (DosError = 0) AND (temp.Attr = Directory) THEN BEGIN
{$I-} ChDir (buffer); {$I+}
IF IOResult = 0 THEN buffer:='*.*';
FindFirst (buffer,search,temp);
END; {IF}
WHILE (DosError = 0) DO BEGIN
EntryNumber:=succ(EntryNumber);
NamR[EntryNumber]:=temp.Name;
IF temp.attr = Directory
THEN EntryDir [EntryNumber]:=TRUE
ELSE EntryDir [EntryNumber]:=FALSE;
FindNext (temp);
END; {WHILE}
NumEntries:=EntryNumber;
NumScreens:=(NumEntries-1) div 72 +1;
IF (NumEntries >= 1)
THEN BEGIN
EntryNumber:=1;
FOR z:=1 TO NumScreens DO BEGIN
FOR y:=3 TO 20 DO
FOR x:=1 TO 4 DO BEGIN
GotoXY (20*x-19,y);
IF EntryDir[EntryNumber]
THEN BEGIN
TextColor (DirColor);
TextBackground (DirBack);
END {THEN}
ELSE BEGIN
TextColor (ForeColor);
TextBackGround (BackColor);
END; {ELSE}
write (NamR[EntryNumber]);
EntryNumber:=succ(EntryNumber);
END; {FOR}
IF (NumScreens > 1) THEN IF (z < NumScreens) THEN BEGIN
WriteXY ('Press any key for more entries....',1,24);
ch:=ReadKey;
ClrScr;
WriteXY ('File mask: '+buffer,1,1);
END; {IF}
END; {FOR}
END {THEN}
ELSE BEGIN
PrintErrorMsg ('File not found! ',4,5,FALSE,ch);
END; {ELSE}
GotoXY (1,22);
TextColor (ForeColor);
TextBackground (BackColor);
temp_int:=DiskFree (Drive) div 1000;
IF Drive > 0
THEN ch:=CHAR(Drive+ord('A')-1)
ELSE ch:='C';
Write ('Drive ',ch,': has ',temp_int,' kB free.');
{$I-} ChDir (DefaultDir); {$I+}
ch:=CHAR(IOResult); {Dummy assignment }
WriteXY ('Press any key to return ...',1,24);
ch:=ReadKey;
ClrScr;
END; {DiskDirectory}
PROCEDURE ReadInfoLines;
VAR
ch : CHAR;
int : byte;
BEGIN {ReadInfoLines}
ClrScr;
FOR int:=1 TO MaxInfo DO
WriteLn (info[int]);
WriteXY ('Press any key to return to the File I/O menu ...',1,25);
ch:=ReadKey;
ClrScr;
END; {ReadInfoLines}
PROCEDURE FileIOMenu;
BEGIN
{- Set up window, print FileIO Menu, prompt for choice. -}
ClrScr;
WriteXY ('File I/O Menu' ,StartColumn+10,3);
WriteXY ('Select Option by typing a number:',StartColumn ,5);
WriteXY ('1. Save Data to disk' ,StartColumn+8 ,8);
WriteXY ('2. Retrieve Data from disk' ,StartColumn+8 ,10);
WriteXY ('3. Delete Data from disk' ,StartColumn+8 ,12);
WriteXY ('4. Disk Directory' ,StartColumn+8 ,14);
WriteXY ('5. Display Information Lines' ,StartColumn+8 ,16);
WriteXY ('9. Exit to Main Menu' ,StartColumn+8 ,18);
WriteXY ('Your choice?' ,StartColumn ,24);
GotoXY (StartColumn+13,24); ClrEOL;
REPEAT
choice:=ReadKey;
UNTIL choice IN ['1'..'5','9'];
END; {FileIOMenu}
PROCEDURE FileIO;
BEGIN {FileIO}
WHILE TRUE DO BEGIN
FileIOMenu;
CASE choice OF (* Choice: *)
'1': SaveFile; (* Save file. *)
'2': RetrieveFile; (* Retrieve file. *)
'3': DeleteFile; (* Delete file. *)
'4': DiskDirectory; (* Disk Directory. *)
'5': ReadInfoLines; (* Display descriptive info. *)
'9': BEGIN (* Return. *)
ClrScr;
Exit;
END;
END; {CASE}
END; {WHILE}
END; {FileIO}
(****************************************************************************)
BEGIN {Initialization}
END. {Initialization}