home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
High Voltage Shareware
/
high1.zip
/
high1
/
DIR13
/
4UTILS76.ZIP
/
HANDLEIN.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1993-10-31
|
6KB
|
227 lines
UNIT HandleIniFile;
(* ----------------------------------------------------------------------
Part of 4DESC - A Simple 4DOS File Description Editor
and 4FF - 4DOS File Finder
(c) 1992, 1993 Copyright by
David Frey, & Tom Bowden
Urdorferstrasse 30 1575 Canberra Drive
8952 Schlieren ZH Stone Mountain, GA 30088-3629
Switzerland USA
Code created using Turbo Pascal 7.0 (c) Borland International 1990
DISCLAIMER: This unit is freeware: you are allowed to use, copy
and change it free of charge, but you may not sell or hire
this part of 4DESC. The copyright remains in our hands.
If you make any (considerable) changes to the source code,
please let us know. (send a copy or a listing).
We would like to see what you have done.
We, David Frey and Tom Bowden, the authors, provide absolutely
no warranty of any kind. The user of this software takes the
entire risk of damages, failures, data losses or other
incidents.
This unit handles the reading of settings stored in 4UTILS.INI.
----------------------------------------------------------------------- *)
INTERFACE
CONST INIFileExists : BOOLEAN = FALSE;
PROCEDURE ReadIniFile;
FUNCTION ReadSettingsChar(Section,Name: STRING; Default: CHAR): CHAR;
FUNCTION ReadSettingsString(Section,Name: STRING; Default: STRING): STRING;
FUNCTION ReadSettingsInt(Section,Name: STRING; Default: INTEGER): INTEGER;
FUNCTION ReadSettingsColor(Section,Name: STRING; Default: INTEGER): BYTE;
IMPLEMENTATION USES Dos, Crt, StringDateHandling;
CONST INIFileName = '4UTILS.INI';
MaxLines = 41;
VAR INIFile : TEXT;
INIPath : DirStr;
MaxLine : INTEGER;
Line : ARRAY[1..MaxLines] OF STRING[48];
Name : NameStr;
Ext : ExtStr;
IORes : INTEGER;
PROCEDURE ReadIniFile;
VAR semicol : BYTE;
ReadLine: STRING;
s : STRING;
PROCEDURE SearchPath;
(* Search for INIFile in the PATH *)
BEGIN
INIPath := FSearch(INIFileName,GetEnv('PATH'));
IF INIPath > '' THEN
BEGIN
{$I-}
Assign(INIFile,INIPath);
Reset(INIFile);
INIFileExists := (IOResult = 0);
{$I+}
END;
END; (* SearchPath *)
BEGIN (* ReadIniFile *)
(* Search strategy for 4UTILS.INI :
i) Directory where this application has started from.
ii) Environment variable 4UTILS
iii) Path *)
FSplit(ParamStr(0),INIPath,Name,Ext);
{$I-}
Assign(INIFile,INIPath+INIFileName);
Reset(INIFile);
{$I+}
INIFileExists := (IOResult = 0);
IF NOT INIFileExists THEN
BEGIN
INIPath := GetEnv('4UTILS');
IF INIPath > '' THEN
BEGIN
IF INIPath[Length(INIPath)] <> '\' THEN INIPath := INIPath + '\';
{$I-}
Assign(INIFile,INIPath+INIFileName);
Reset(INIFile);
{$I+}
INIFileExists := (IOResult = 0);
IF NOT INIFileExists THEN SearchPath;
END
ELSE SearchPath;
END;
IF INIFileExists THEN
BEGIN
MaxLine := 1;
WHILE NOT Eof(INIFile) AND (MaxLine <= MaxLines) DO
BEGIN
ReadLn(INIFile,ReadLine);
StripLeadingSpaces(ReadLine);
IF (ReadLine[1] <> ';') THEN
BEGIN
semicol := Pos(';',ReadLine);
IF (semicol > 0) AND
(DownStr(Copy(ReadLine,1,10)) <> 'delimiters') THEN
ReadLine := Copy(ReadLine,1,semicol-1);
IF Length(ReadLine) > 0 THEN
BEGIN
StripTrailingSpaces(ReadLine);
Line[MaxLine] := DownStr(ReadLine); INC(MaxLine);
END;
END; (* IF Line[1] <> ';' .. *)
END; (* WHILE *)
IF NOT Eof(INIFile) THEN
BEGIN
WriteLn('WARNING: ',INIFileName,' exceeds ',MaxLines,' lines!');
WriteLn(' Parts of your settings will be ignored.');
Delay(5000);
END;
{$I-}
Close(INIFile); IORes := IOResult;
{$I+}
END; (* INIFileExists .. *)
END; (* ReadIniFile *)
(* A collection of functions to read Strings/Values/Color names out of
a initialisation file. *)
FUNCTION ReadSettingsString(Section,Name: STRING; Default: STRING): STRING;
VAR LineNr: BYTE;
eq : BYTE;
s,res : STRING;
BEGIN
LineNr := 1;
Section := '['+DownStr(Section)+']';
WHILE (Line[LineNr] <> Section) AND (LineNr <= MaxLine) DO INC(LineNr);
IF Line[LineNr] = Section THEN
BEGIN
DownString(Name); res := ''; s := '';
REPEAT
s := Line[LineNr]; eq := Pos('=',s);
IF eq > 0 THEN s := Copy(s,1,eq-1);
StripTrailingSpaces(s);
INC(LineNr);
UNTIL (s = Name) OR (LineNr > MaxLine);
IF s = Name THEN
BEGIN
res := Copy(Line[LineNr-1],eq+1,255);
StripLeadingSpaces(res);
END
ELSE res := Default;
END
ELSE res:= Default;
ReadSettingsString := res;
END;
FUNCTION ReadSettingsChar(Section,Name: STRING; Default: CHAR): CHAR;
VAR s : STRING;
BEGIN
s := ReadSettingsString(Section,Name,Default);
ReadSettingsChar := s[1];
END;
FUNCTION ReadSettingsInt(Section,Name: STRING; Default: INTEGER): INTEGER;
VAR s : STRING;
res: INTEGER;
v : INTEGER;
BEGIN
Str(Default,s);
s := ReadSettingsString(Section,Name,s);
Val(s,v,res);
IF res > 0 THEN v := Default;
ReadSettingsInt := v;
END;
FUNCTION ReadSettingsColor(Section,Name: STRING; Default: INTEGER): BYTE;
CONST color : ARRAY[0..15] OF STRING[12] =
('black' ,'blue' ,'green' ,'cyan' ,
'red' ,'magenta' ,'brown' ,'lightgray',
'darkgray','lightblue' ,'lightgreen','lightcyan',
'lightred','lightmagenta','yellow' ,'white');
VAR s : STRING;
c : BYTE;
BEGIN
Str(Default,s);
s := ReadSettingsString(Section,Name,'');
IF s > '' THEN
BEGIN
c := 0;
WHILE (color[c] <> s) AND (c<16) DO INC(c);
IF color[c] <> s THEN c := Default;
END
ELSE c := Default;
ReadSettingsColor := c;
END;
BEGIN
ReadIniFile;
END.