You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

KOLCE_IniFile.inc 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. {$ifdef read_interface}
  2. type
  3. PIniFile = ^TIniFile;
  4. { TIniFile }
  5. TIniFile = object( TObj )
  6. private
  7. procedure SetMode(const AValue: TIniFileMode);
  8. protected
  9. fMode: TIniFileMode;
  10. fFileName: KOLString;
  11. fSection: KOLString;
  12. fData: PStrList;
  13. protected
  14. function FindSection(var First, Last: Integer): Boolean;
  15. function FindKey(Key: String; First, Last: Integer; var Value: String): Integer;
  16. public
  17. destructor Destroy; virtual;
  18. property Mode: TIniFileMode read fMode write SetMode;
  19. property FileName: KOLString read fFileName;
  20. property Section: KOLString read fSection write fSection;
  21. function ValueInteger( const Key: KOLString; Value: Integer ): Integer;
  22. function ValueString( const Key: KOLString; const Value: KOLString ): KOLString;
  23. function ValueBoolean( const Key: KOLString; Value: Boolean ): Boolean;
  24. function ValueData( const Key: KOLString; Value: Pointer; Count: Integer ): Boolean;
  25. procedure ClearAll;
  26. procedure ClearSection;
  27. procedure ClearKey( const Key: KOLString );
  28. procedure GetSectionNames(Names: {$IFNDEF UNICODE_CTRLS}PStrList{$ELSE}PWStrList{$ENDIF});
  29. procedure SectionData(Names: {$IFNDEF UNICODE_CTRLS}PStrList{$ELSE}PWStrList{$ENDIF});
  30. procedure UpdateFile;
  31. end;
  32. {$endif read_interface}
  33. {$ifdef read_implementation}
  34. function OpenIniFile( const FileName: KOLString ): PIniFile;
  35. begin
  36. New(Result, Create);
  37. Result.fFileName := FileName;
  38. Result.fData:= NewStrList;
  39. Result.fMode:= ifmRead;
  40. Result.fData.LoadFromFile(FileName);
  41. end;
  42. function GetSection(var S: String): Boolean;
  43. var L: Integer;
  44. begin
  45. S:= Trim(S);
  46. L:= Length(S);
  47. Result:= (L > 2) and (S[1] = '[') and (S[L] = ']');
  48. if Result then begin
  49. Delete(S, L, 1); Delete(S, 1, 1);
  50. S:= Trim(S);
  51. // Result:= (S <> '');
  52. end;
  53. end;
  54. procedure TIniFile.ClearAll;
  55. begin
  56. fData.Clear;
  57. fData.SaveToFile(fFileName);
  58. end;
  59. procedure TIniFile.ClearKey(const Key: KOLString);
  60. var k, F, L: Integer;
  61. S: String;
  62. begin
  63. if FindSection(F, L) then begin
  64. k:= FindKey(Key, F, L, S);
  65. if k >= 0 then fData.Delete(k);
  66. fData.SaveToFile(fFileName);
  67. end;
  68. end;
  69. procedure TIniFile.ClearSection;
  70. var i, F, L: Integer;
  71. begin
  72. if FindSection(F, L) then begin
  73. for i := L downto F do begin
  74. fData.Delete(i);
  75. end;
  76. fData.SaveToFile(fFileName);
  77. end;
  78. end;
  79. destructor TIniFile.Destroy;
  80. begin
  81. if fMode = ifmWrite then fData.SaveToFile(fFileName);
  82. fData.Free;
  83. fFileName := '';
  84. fSection := '';
  85. inherited;
  86. end;
  87. procedure TIniFile.GetSectionNames(Names: {$IFNDEF UNICODE_CTRLS}PStrList{$ELSE}PWStrList{$ENDIF});
  88. var i: Integer;
  89. S: String;
  90. begin
  91. for i:= 0 to fData.Count-1 do begin
  92. S:= fData.Items[i];
  93. if GetSection(S) then Names.Add(S)
  94. end;
  95. end;
  96. procedure TIniFile.SectionData(Names: {$IFNDEF UNICODE_CTRLS}PStrList{$ELSE}PWStrList{$ENDIF});
  97. var i, F, L: Integer;
  98. S: String;
  99. begin
  100. if fMode = ifmRead then begin
  101. if FindSection(F, L) then
  102. for i := F+1 to L do begin
  103. S:= Trim(fData.Items[i]);
  104. if S <> '' then Names.Add(S);
  105. end;
  106. end
  107. else begin
  108. ClearSection;
  109. fData.Add('[' + fSection + ']');
  110. for i:= 0 to Names.Count-1 do begin
  111. S:= Trim(Names.Items[i]);
  112. if S <> '' then fData.Add(S);
  113. end;
  114. end;
  115. end;
  116. procedure TIniFile.UpdateFile;
  117. begin
  118. if fMode = ifmRead then fData.LoadFromFile(fFileName)
  119. else fData.SaveToFile(fFileName);
  120. end;
  121. function TIniFile.ValueBoolean(const Key: KOLString; Value: Boolean): Boolean;
  122. begin
  123. Result:= ValueInteger(Key, Ord(Value)) <> 0;
  124. end;
  125. function TIniFile.ValueData(const Key: KOLString; Value: Pointer; Count: Integer): Boolean;
  126. var i: Integer;
  127. S: String;
  128. begin
  129. if fMode = ifmRead then begin
  130. S:= ValueString(Key, '');
  131. for i:= 0 to Min(Length(S) div 2, Count) - 1 do begin
  132. PByte(Value)^:= Hex2Int(Copy(S, 1+i*2, 2));
  133. cardinal(Value):= cardinal(Value) + 1;
  134. Result:= True;
  135. end;
  136. end
  137. else begin
  138. S:= '';
  139. for i:= 0 to Count - 1 do begin
  140. S:= S + Int2Hex(PByte(Value)^, 2);
  141. cardinal(Value):= cardinal(Value) + 1;
  142. Result:= True;
  143. end;
  144. ValueString(Key, S);
  145. end;
  146. end;
  147. function TIniFile.ValueInteger(const Key: KOLString; Value: Integer): Integer;
  148. begin
  149. Result:= Str2Int(ValueString(Key, Int2Str(Value)));
  150. end;
  151. function TIniFile.ValueString( const Key: KOLString; const Value: KOLString ): KOLString;
  152. var k, F, L: Integer;
  153. S: String;
  154. fSect: Boolean;
  155. begin
  156. fSect:= FindSection(F, L);
  157. if fSect then k:= FindKey(Key, F, L, S) else k:= -1;
  158. if fMode = ifmRead then begin
  159. if fSect and (k > 0) then Result:= S else Result := Value;
  160. end
  161. else begin
  162. if not fSect then begin
  163. fData.Add('[' + fSection + ']');
  164. fData.Add(''); k:= fData.Count-1;
  165. end;
  166. if k < 0 then begin k:= L+1; fData.Insert(k, ''); end;
  167. fData.Items[k]:= Key+'='+Value;
  168. end;
  169. end;
  170. procedure TIniFile.SetMode(const AValue: TIniFileMode);
  171. begin
  172. if fMode = AValue then Exit;
  173. if fMode = ifmWrite then fData.SaveToFile(fFileName)
  174. else fData.LoadFromFile(fFileName);
  175. fMode:= AValue;
  176. end;
  177. function TIniFile.FindSection(var First, Last: Integer): Boolean;
  178. var i: Integer;
  179. Sec, S: String;
  180. begin
  181. Result:= False;
  182. Sec := Trim(fSection);
  183. for i:= 0 to fData.Count-1 do begin
  184. S:= fData.Items[i];
  185. if GetSection(S) and (AnsiCompareStrNoCase(S, Sec) = 0) then begin
  186. First:= i; Result:= True; Break;
  187. end;
  188. end;
  189. if Result then begin
  190. for i:= First+1 to fData.Count-1 do begin
  191. S:= fData.Items[i];
  192. if GetSection(S) then begin
  193. Last:= i-1; Exit;
  194. end;
  195. end;
  196. Last:= fData.Count-1;
  197. end;
  198. end;
  199. function TIniFile.FindKey(Key: String; First, Last: Integer; var Value: String): Integer;
  200. var i, k: Integer;
  201. S: String;
  202. begin
  203. Result:= -1;
  204. Key:= Trim(Key);
  205. for i:= First to Last do begin
  206. S:= fData.Items[i];
  207. k:= Pos('=', S);
  208. if k > 0 then begin
  209. if AnsiCompareStrNoCase(Key, Trim(Copy(S, 1, k-1))) = 0 then Result:= i;
  210. if Result >= 0 then begin
  211. Delete(S, 1, k);
  212. Value:= Trim(S);
  213. k:= Length(Value);
  214. if (k > 0) and (Value[1] = '"') and (Value[k] = '"') then begin
  215. Delete(Value, k, 1); Delete(Value, 1, 1);
  216. end;
  217. Exit;
  218. end;
  219. end;
  220. end;
  221. end;
  222. {$endif read_implementation}