home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 5 / ctrom5b.zip / ctrom5b / PROGRAM / DELPHI / EASY_INI / EASYINI.PAS < prev    next >
Pascal/Delphi Source File  |  1995-04-27  |  8KB  |  270 lines

  1. unit EasyINI;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinProcs, Classes, Forms;
  7.  
  8. type
  9.   TFileLocation = (ApplicationPath, WindowsPath, PathInFileName);
  10.   TIniEditor = class(TComponent)
  11.   private
  12.     FFileName: string;
  13.     FFilePath: string;
  14.     FFileLocation: TFileLocation;
  15.     procedure SetFileName(strFileName: string);
  16.     procedure SetFileLocation(Location: TFileLocation);
  17.     procedure AppendFilePath(var FullPath: string);
  18.   public
  19.     constructor Create(AOwner: TComponent); override;
  20.   published
  21.     property FileName: string read FFileName write SetFileName;
  22.     property FileLocation: TFileLocation read FFileLocation write SetFileLocation;
  23.     procedure WriteBool(Section, Entry: string; Value: boolean);
  24.     procedure WriteChar(Section, Entry: string; Value: char);
  25.     procedure WriteInt(Section, Entry: string; Value: LongInt);
  26.     procedure WriteReal(Section, Entry: string; Value: Extended);
  27.     procedure WriteStr(Section, Entry, Value: string);
  28.     function ReadBool(Section, Entry: string; DefaultValue: boolean): boolean;
  29.     function ReadChar(Section, Entry: string; DefaultValue: char): char;
  30.     function ReadInt(Section, Entry: string; DefaultValue: LongInt): LongInt;
  31.     function ReadReal(Section, Entry: string; DefaultValue: Extended): Extended;
  32.     function ReadStr(Section, Entry, DefaultValue: string): string;
  33.     procedure EraseSection(Section: string);
  34.     procedure EraseEntry(Section, Entry: string);
  35.   end;
  36.  
  37. procedure Register;
  38.  
  39. implementation
  40.  
  41. constructor TIniEditor.Create(AOwner: TComponent);
  42. begin
  43.   inherited Create(AOwner);
  44.   FFileName := '';
  45.   FFileLocation := ApplicationPath;
  46. end;
  47.  
  48. procedure TIniEditor.SetFileName(strFileName: string);
  49. begin
  50.   if strFileName <> FFileName then
  51.     FFileName := strFileName;
  52. end;
  53.  
  54. procedure TIniEditor.SetFileLocation(Location: TFileLocation);
  55. var
  56.   i: integer;
  57.   szPath: PChar;
  58.   strPath: string;
  59. begin
  60.   if Location <> FFileLocation then
  61.     FFileLocation := Location;
  62.   if FFileLocation = ApplicationPath then
  63.   begin
  64.     {Set path to the application. Data files will be in DATA directory below application}
  65.     strPath := Application.ExeName;
  66.     i := Length(strPath);
  67.     while (i >= 1) and (strPath[i] <> '\') do
  68.       dec(i);
  69.     FFilePath := Copy(strPath,1,i);
  70.   end
  71.   else
  72.   if FFileLocation = WindowsPath then
  73.   begin
  74.     szPath := StrAlloc(144);
  75.     GetWindowsDirectory(szPath, 144);
  76.     FFilePath := StrPas(szPath);
  77.     if FFilePath[Length(FFilePath)] <> '\' then
  78.       FFilePath := FFilePath + '\';
  79.     StrDispose(szPath);
  80.   end
  81.   else
  82.     FFilePath := '';
  83. end;
  84.  
  85. function StrToBool(strVal: string): boolean;
  86. begin
  87.   if (UpperCase(strVal) = 'TRUE') or (UpperCase(strVal) = 'YES') or (strVal = '1') then
  88.     Result := True
  89.   else
  90.     Result := False;
  91. end;
  92.  
  93. function BoolToStr(bVal: boolean): string;
  94. begin
  95.   if bVal then
  96.     Result := 'True'
  97.   else
  98.     Result := 'False';
  99. end;
  100.  
  101. procedure TIniEditor.WriteBool(Section, Entry: string; Value: boolean);
  102. begin
  103.   WriteStr(Section, Entry, BoolToStr(Value));
  104. end;
  105.  
  106. function TIniEditor.ReadBool(Section, Entry: string; DefaultValue: boolean): boolean;
  107. var
  108.   iniStrVal: string;
  109. begin
  110.   iniStrVal := ReadStr(Section, Entry, BoolToStr(DefaultValue));
  111.   result := StrToBool(iniStrVal);
  112. end;
  113.  
  114. procedure TIniEditor.WriteChar(Section, Entry: string; Value: char);
  115. begin
  116.   WriteStr(Section, Entry, Value);
  117. end;
  118.  
  119. function TIniEditor.ReadChar(Section, Entry: string; DefaultValue: char): char;
  120. var
  121.   iniVal: string;
  122. begin
  123.   iniVal := DefaultValue;
  124.   iniVal := ReadStr(Section, Entry, DefaultValue);
  125.   result := iniVal[1];
  126. end;
  127.  
  128. procedure TIniEditor.WriteReal(Section, Entry: string; Value: Extended);
  129. begin
  130.   WriteStr(Section, Entry, FormatFloat('',Value));
  131. end;
  132.  
  133. procedure TIniEditor.WriteInt(Section, Entry: string; Value: LongInt);
  134. begin
  135.   WriteStr(Section, Entry, IntToStr(Value));
  136. end;
  137.  
  138. function TIniEditor.ReadReal(Section, Entry: string; DefaultValue: Extended): Extended;
  139. var
  140.   StrValue: string;
  141.   strDefault: string;
  142. begin
  143.   try
  144.     strDefault := FloatToStr(DefaultValue);
  145.     StrValue := ReadStr(Section, Entry, StrDefault);
  146.     result := StrToFloat(strValue);
  147.   except
  148.     result := DefaultValue;
  149.   end;
  150. end;
  151.  
  152. function TIniEditor.ReadInt(Section, Entry: string; DefaultValue: LongInt): LongInt;
  153. begin
  154.   try
  155.     result := Trunc(ReadReal(Section, Entry, DefaultValue));
  156.   except
  157.     result := DefaultValue;
  158.   end;
  159. end;
  160.  
  161. procedure TIniEditor.EraseSection(Section: string);
  162. begin
  163.   WriteStr(Section, '', '');
  164. end;
  165.  
  166. procedure TIniEditor.EraseEntry(Section, Entry: string);
  167. begin
  168.   WriteStr(Section, Entry, '');
  169. end;
  170.  
  171. procedure TIniEditor.AppendFilePath(var FullPath: string);
  172. begin
  173.   if (FFilePath = '') or (FFileName[2] = ':') then
  174.     FullPath := FFileName
  175.   else
  176.   if FFileName[1] = '\' then
  177.     FullPath := FFilePath + Copy(FFileName, 2, Length(FFileName)-1)
  178.   else
  179.     FullPath := FFilePath + FFileName;
  180.   if Pos('.',FullPath) = 0 then
  181.     FullPath := FullPath + '.INI';
  182. end;
  183.  
  184. procedure TIniEditor.WriteStr(Section, Entry, Value: string);
  185. var
  186.   szEntry, szFile, szSection, szValue: PChar;
  187.   Path: string;
  188. begin
  189.   if (Section <> '') and (FFileName <> '') then
  190.   begin
  191.     { Allocate some memory for PChars }
  192.     szSection := StrAlloc(256);
  193.     szEntry := StrAlloc(256);
  194.     szValue := StrAlloc(256);
  195.     szFile := StrAlloc(256);
  196.     try
  197.       { Add path and filename }
  198.       AppendFilePath(Path);
  199.       { Copy our properties into the PChars }
  200.       StrPCopy(szFile, Path);
  201.       StrPCopy(szSection, Section);
  202.       StrPCopy(szEntry, Entry);
  203.       if Value = '' then
  204.       begin
  205.         if Entry = '' then
  206.           {Erase Entry by passing NIL for its value.}
  207.           WritePrivateProfileString(szSection, NIL, NIL, szFile)
  208.         else
  209.           {Erase Entry by passing NIL for its value.}
  210.           WritePrivateProfileString(szSection, szEntry, NIL, szFile);
  211.       end
  212.       else
  213.       begin
  214.         StrPCopy(szValue, Value);
  215.         WritePrivateProfileString(szSection, szEntry, szValue, szFile);
  216.       end;
  217.     finally
  218.       StrDispose(szEntry);
  219.       StrDispose(szFile);
  220.       StrDispose(szSection);
  221.       StrDispose(szValue);
  222.     end;
  223.   end;
  224. end;
  225.  
  226. function TIniEditor.ReadStr(Section, Entry, DefaultValue: string): string;
  227. var
  228.   Path: string;
  229.   szSection, szEntry, szDefaultValue, szValue, szFile: PChar;
  230. begin
  231.   if (Section <> '') and (Entry <> '') and (FFileName <> '') then
  232.   begin
  233.     { Allocate some memory for PChars }
  234.     szSection := StrAlloc(256);
  235.     szEntry := StrAlloc(256);
  236.     szDefaultValue := StrAlloc(256);
  237.     szValue := StrAlloc(256);
  238.     szFile := StrAlloc(256);
  239.     try
  240.       { Add path and filename }
  241.       AppendFilePath(Path);
  242.       { Copy our properties into the PChars }
  243.       StrPCopy(szFile, Path);
  244.       StrPCopy(szSection, Section);
  245.       StrPCopy(szEntry, Entry);
  246.       StrPCopy(szDefaultValue, DefaultValue);
  247.       { Read the profile string }
  248.       GetPrivateProfileString(szSection, szEntry, szDefaultValue, szValue, 256, szFile);
  249.       Result := StrPas(szValue);
  250.     finally
  251.       StrDispose(szSection);
  252.       StrDispose(szEntry);
  253.       StrDispose(szValue);
  254.       StrDispose(szDefaultValue);
  255.       StrDispose(szFile);
  256.     end;
  257.   end
  258.   else
  259.   begin
  260.     Result := DefaultValue;
  261.   end;
  262. end;
  263.  
  264. procedure Register;
  265. begin
  266.   RegisterComponents('Data Access', [TIniEditor]);
  267. end;
  268.  
  269. end.
  270.