| 1 |
|
|---|
| 2 | {ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
|
|---|
| 3 | º º
|
|---|
| 4 | º Sibyl Portable Component Classes º
|
|---|
| 5 | º º
|
|---|
| 6 | º Copyright (C) 1995,97 SpeedSoft Germany, All rights reserved. º
|
|---|
| 7 | º º
|
|---|
| 8 | ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍŒ}
|
|---|
| 9 |
|
|---|
| 10 | Unit IniFiles;
|
|---|
| 11 |
|
|---|
| 12 | { TIniFile: Standard (binres) OS/2 Inifile
|
|---|
| 13 | TAsciiIniFile: Text-Inifile, lesbar, mit Editor zu bearbeiten.
|
|---|
| 14 |
|
|---|
| 15 | Beide benutzen exakt das gleiche Interface und sind bis
|
|---|
| 16 | auf Die neue Methode 'Erase' kompatibel zu den normalen
|
|---|
| 17 | Delphi-IniFiles. }
|
|---|
| 18 |
|
|---|
| 19 | Interface
|
|---|
| 20 |
|
|---|
| 21 | Uses Classes,SysUtils;
|
|---|
| 22 |
|
|---|
| 23 | {$IFDEF OS2}
|
|---|
| 24 | Uses PMSHL; { OS/2 profile FUNCTIONs }
|
|---|
| 25 | {$ENDIF}
|
|---|
| 26 | {$IFDEF Win95}
|
|---|
| 27 | Uses WinBase;
|
|---|
| 28 | {$ENDIF}
|
|---|
| 29 |
|
|---|
| 30 | Type
|
|---|
| 31 | EIniFileError = Class(Exception);
|
|---|
| 32 |
|
|---|
| 33 | Type
|
|---|
| 34 | TIniFile = Class(TObject)
|
|---|
| 35 | Private
|
|---|
| 36 | FFileName: PString; // Physical Name Of File
|
|---|
| 37 | {$IFDEF OS2}
|
|---|
| 38 | FHandle: HINI; // profile Handle
|
|---|
| 39 | {$ENDIF}
|
|---|
| 40 | Function GetFileName: String;
|
|---|
| 41 |
|
|---|
| 42 | Protected
|
|---|
| 43 | Procedure Error(Const Msg: String); Virtual;
|
|---|
| 44 |
|
|---|
| 45 | Public
|
|---|
| 46 | Constructor Create(Const FileName: String);Virtual;
|
|---|
| 47 | Destructor Destroy; Override;
|
|---|
| 48 | Procedure Erase(Const section, Ident: String); Virtual;
|
|---|
| 49 | Procedure EraseSection(Const section: String); Virtual;
|
|---|
| 50 | Function ReadString(Const section, Ident, Default: String): String; Virtual;
|
|---|
| 51 | Function ReadInteger(Const section, Ident: String; Default: LongInt): LongInt; Virtual;
|
|---|
| 52 | Function ReadBool(Const section, Ident: String; Default: Boolean): Boolean; Virtual;
|
|---|
| 53 | Procedure ReadSection(Const section: String; AStrings: TStrings); Virtual;
|
|---|
| 54 | {Procedure ReadSections(AStrings: TStrings); fehlt noch}
|
|---|
| 55 | Procedure ReadSectionValues(Const section: String; AStrings: TStrings); Virtual;
|
|---|
| 56 | Procedure WriteString(Const section, Ident, Value: String); Virtual;
|
|---|
| 57 | Procedure WriteInteger(Const section, Ident: String; Value: LongInt); Virtual;
|
|---|
| 58 | Procedure WriteBool(Const section, Ident: String; Value: Boolean); Virtual;
|
|---|
| 59 |
|
|---|
| 60 | Property FileName: String
|
|---|
| 61 | Read GetFileName;
|
|---|
| 62 | End;
|
|---|
| 63 |
|
|---|
| 64 | Type
|
|---|
| 65 | {$HINTS OFF}
|
|---|
| 66 | TAsciiIniFile = Class(TIniFile)
|
|---|
| 67 | Private
|
|---|
| 68 | //FFileName: PString; // Physical Name Of File
|
|---|
| 69 | FSections: TStringList; // List Of Sections And their Names
|
|---|
| 70 | FName: PString; // Name Of Last used section
|
|---|
| 71 | FList: TStringList; // List Of Last used section
|
|---|
| 72 | FChanged: Boolean; // has the Data been changed?
|
|---|
| 73 | Procedure WriteToSection(Const section: String);
|
|---|
| 74 | Function ReadFromSection(Const section: String): Boolean;
|
|---|
| 75 | //Function GetFileName: String;
|
|---|
| 76 |
|
|---|
| 77 | Protected
|
|---|
| 78 | TrueString: String[7];
|
|---|
| 79 | FalseString: String[7];
|
|---|
| 80 | SectionSort: Boolean;
|
|---|
| 81 | IdentSort: Boolean;
|
|---|
| 82 | Protected
|
|---|
| 83 | Procedure InitIniFile;Virtual;
|
|---|
| 84 |
|
|---|
| 85 | Public
|
|---|
| 86 | Constructor Create(Const FileName: String);Virtual;
|
|---|
| 87 | Destructor Destroy; Override;
|
|---|
| 88 | Procedure Erase(Const section, Ident: String); Override;
|
|---|
| 89 | Procedure EraseSection(Const section: String); Override;
|
|---|
| 90 | Function ReadString(Const section, Ident, Default: String): String; Override;
|
|---|
| 91 | Function ReadInteger(Const section, Ident: String; Default: LongInt): LongInt; Override;
|
|---|
| 92 | Function ReadBool(Const section, Ident: String; Default: Boolean): Boolean; Override;
|
|---|
| 93 | Procedure ReadSection(Const section: String; AStrings: TStrings); Override;
|
|---|
| 94 | Procedure ReadSections(AStrings: TStrings);
|
|---|
| 95 | Procedure ReadSectionValues(Const section: String; AStrings: TStrings); Override;
|
|---|
| 96 | Procedure Refresh;
|
|---|
| 97 | Procedure WriteString(Const section, Ident, Value: String); Override;
|
|---|
| 98 | Procedure WriteInteger(Const section, Ident: String; Value: LongInt); Override;
|
|---|
| 99 | Procedure WriteBool(Const section, Ident: String; Value: Boolean); Override;
|
|---|
| 100 |
|
|---|
| 101 | Property FileName: String
|
|---|
| 102 | Read GetFileName;
|
|---|
| 103 | Property changed: Boolean
|
|---|
| 104 | Read FChanged Write FChanged;
|
|---|
| 105 | End;
|
|---|
| 106 | {$HINTS ON}
|
|---|
| 107 |
|
|---|
| 108 | Function GetDefaultINI: String;
|
|---|
| 109 | { Get Name Of INI-File that matches Program Path & Name With
|
|---|
| 110 | extension .INI instead Of .EXE }
|
|---|
| 111 |
|
|---|
| 112 | Implementation
|
|---|
| 113 |
|
|---|
| 114 | Uses
|
|---|
| 115 | SysUtils;
|
|---|
| 116 |
|
|---|
| 117 | Const
|
|---|
| 118 | Null = 0;
|
|---|
| 119 |
|
|---|
| 120 | Type
|
|---|
| 121 | {$HINTS OFF}
|
|---|
| 122 | TIniSection = Class(TStringList)
|
|---|
| 123 | Private
|
|---|
| 124 | Function GetValue(Const Name: String): String;
|
|---|
| 125 | Procedure SetValue(Const Name, Value: String);
|
|---|
| 126 | Function FindValue(Const Name: String; Var Value: String): LongInt;
|
|---|
| 127 | Procedure FreeItem(AObject:TObject);Override;
|
|---|
| 128 | Property values[Const Name: String]: String Read GetValue Write SetValue;
|
|---|
| 129 | End;
|
|---|
| 130 | {$HINTS ON}
|
|---|
| 131 |
|
|---|
| 132 | Function CutStr(Var S: String; C: Char): String;
|
|---|
| 133 | Var
|
|---|
| 134 | P: Integer;
|
|---|
| 135 | Begin
|
|---|
| 136 | P := Pos(C, S);
|
|---|
| 137 | If P = 0 Then
|
|---|
| 138 | Begin
|
|---|
| 139 | Result := S;
|
|---|
| 140 | SetLength(S, 0);
|
|---|
| 141 | End
|
|---|
| 142 | Else
|
|---|
| 143 | Begin
|
|---|
| 144 | Result := Copy(S, 1, P - 1);
|
|---|
| 145 | Delete(S, 1, P);
|
|---|
| 146 | End;
|
|---|
| 147 | End;
|
|---|
| 148 |
|
|---|
| 149 | Function TrimStr(Const S: String): String;
|
|---|
| 150 | Var
|
|---|
| 151 | L, R: Integer;
|
|---|
| 152 | Begin
|
|---|
| 153 | R := Length(S);
|
|---|
| 154 | While (R > 0) And (S[R] = ' ') Do Dec(R);
|
|---|
| 155 | L := 1;
|
|---|
| 156 | While (L <= R) And (S[L] = ' ') Do Inc(L);
|
|---|
| 157 | Result := Copy(S, L, R - L + 1);
|
|---|
| 158 | End;
|
|---|
| 159 |
|
|---|
| 160 | Function GetDefaultINI: String;
|
|---|
| 161 | Begin
|
|---|
| 162 | Result := ExpandFileName(ChangeFileExt(ParamStr(0), '.INI'));
|
|---|
| 163 | End;
|
|---|
| 164 |
|
|---|
| 165 | { TIniSection }
|
|---|
| 166 |
|
|---|
| 167 | Procedure TIniSection.FreeItem(AObject:TObject);
|
|---|
| 168 | Var
|
|---|
| 169 | P: PString;
|
|---|
| 170 | Begin
|
|---|
| 171 | P := PString(AObject);
|
|---|
| 172 | DisposeStr(P);
|
|---|
| 173 | Inherited FreeItem(AObject);
|
|---|
| 174 | End;
|
|---|
| 175 |
|
|---|
| 176 | {Procedure TIniSection.Put(Index: LongInt; Const S: String);
|
|---|
| 177 | Var
|
|---|
| 178 | Ident, Value: String;
|
|---|
| 179 | Begin
|
|---|
| 180 | Value := S;
|
|---|
| 181 | Ident := CutStr(Value, '=');
|
|---|
| 182 | SetValue(Ident, Value);
|
|---|
| 183 | End;}
|
|---|
| 184 |
|
|---|
| 185 | Function TIniSection.GetValue(Const Name: String): String;
|
|---|
| 186 | Var
|
|---|
| 187 | I: LongInt;
|
|---|
| 188 | Begin
|
|---|
| 189 | If Find(Name, I) Then Result := PString(Objects[I])^
|
|---|
| 190 | Else Result := '';
|
|---|
| 191 | End;
|
|---|
| 192 |
|
|---|
| 193 | Function TIniSection.FindValue(Const Name: String; Var Value: String): LongInt;
|
|---|
| 194 | Begin
|
|---|
| 195 | If Find(Name, Result) Then Value := PString(Objects[Result])^
|
|---|
| 196 | Else
|
|---|
| 197 | Begin
|
|---|
| 198 | Value := '';
|
|---|
| 199 | Result := -1;
|
|---|
| 200 | End;
|
|---|
| 201 | End;
|
|---|
| 202 |
|
|---|
| 203 | Procedure TIniSection.SetValue(Const Name, Value: String);
|
|---|
| 204 | Var
|
|---|
| 205 | I: LongInt;
|
|---|
| 206 | P: PString;
|
|---|
| 207 | OldSorted: Boolean;
|
|---|
| 208 | Begin
|
|---|
| 209 | If Find(Name, I) Then
|
|---|
| 210 | Begin
|
|---|
| 211 | P := PString(Objects[I]);
|
|---|
| 212 | DisposeStr(P);
|
|---|
| 213 | PutObject(I, TObject(NewStr(Value)));
|
|---|
| 214 | End
|
|---|
| 215 | Else
|
|---|
| 216 | Begin
|
|---|
| 217 | OldSorted := sorted;
|
|---|
| 218 | sorted := False;
|
|---|
| 219 | InsertObject(I, Name, TObject(NewStr(Value)));
|
|---|
| 220 | sorted := OldSorted;
|
|---|
| 221 | End;
|
|---|
| 222 | End;
|
|---|
| 223 |
|
|---|
| 224 | { TIniFile }
|
|---|
| 225 |
|
|---|
| 226 | Constructor TIniFile.Create(Const FileName: String);
|
|---|
| 227 | Begin
|
|---|
| 228 | {$IFDEF OS2}
|
|---|
| 229 | FHandle := PrfOpenProfile(AppHandle, FileName);
|
|---|
| 230 | If FHandle = Null Then Error(LoadNLSStr(SCannotOpenIniFile)+'.');
|
|---|
| 231 | {$ENDIF}
|
|---|
| 232 | FFileName := NewStr(FileName);
|
|---|
| 233 | End;
|
|---|
| 234 |
|
|---|
| 235 | Destructor TIniFile.Destroy;
|
|---|
| 236 | Begin
|
|---|
| 237 | {$IFDEF OS2}
|
|---|
| 238 | PrfCloseProfile(FHandle);
|
|---|
| 239 | {$ENDIF}
|
|---|
| 240 | DisposeStr(FFileName);
|
|---|
| 241 |
|
|---|
| 242 | Inherited Destroy;
|
|---|
| 243 | End;
|
|---|
| 244 |
|
|---|
| 245 | Procedure TIniFile.Erase(Const section, Ident: String);
|
|---|
| 246 | Begin
|
|---|
| 247 | {$IFDEF OS2}
|
|---|
| 248 | PrfWriteProfileString(FHandle, section, Ident, Nil);
|
|---|
| 249 | {$ENDIF}
|
|---|
| 250 | {$IFDEF Win95}
|
|---|
| 251 | WritePrivateProfileString(section,Ident,Nil,FileName);
|
|---|
| 252 | {$ENDIF}
|
|---|
| 253 | End;
|
|---|
| 254 |
|
|---|
| 255 | Procedure TIniFile.EraseSection(Const section: String);
|
|---|
| 256 | Begin
|
|---|
| 257 | {$IFDEF OS2}
|
|---|
| 258 | PrfWriteProfileString(FHandle, section, Nil, Nil);
|
|---|
| 259 | {$ENDIF}
|
|---|
| 260 | {$IFDEF Win95}
|
|---|
| 261 | WritePrivateProfileString(section,Nil,Nil,FileName);
|
|---|
| 262 | {$ENDIF}
|
|---|
| 263 | End;
|
|---|
| 264 |
|
|---|
| 265 | Procedure TIniFile.Error(Const Msg: String);
|
|---|
| 266 | Begin
|
|---|
| 267 | Raise EIniFileError.Create(Msg);
|
|---|
| 268 | End;
|
|---|
| 269 |
|
|---|
| 270 | Function TIniFile.GetFileName: String;
|
|---|
| 271 | Begin
|
|---|
| 272 | Result := FFileName^;
|
|---|
| 273 | End;
|
|---|
| 274 |
|
|---|
| 275 | Function TIniFile.ReadString(Const section, Ident, Default: String): String;
|
|---|
| 276 | Var
|
|---|
| 277 | OutBuf: cstring;
|
|---|
| 278 | Begin
|
|---|
| 279 | {$IFDEF OS2}
|
|---|
| 280 | Fillchar(OutBuf, 255, 0); {sometimes the #0 character is not copied (cdp.ini)}
|
|---|
| 281 | PrfQueryProfileString(FHandle, Section, Ident, Default, OutBuf, 255);
|
|---|
| 282 | Result := OutBuf;
|
|---|
| 283 | {$ENDIF}
|
|---|
| 284 | {$IFDEF Win95}
|
|---|
| 285 | SetLength(Result,GetPrivateProfileString(
|
|---|
| 286 | Section,Ident,Default,cstring(Result[1]),255,FileName));
|
|---|
| 287 | {$ENDIF}
|
|---|
| 288 | End;
|
|---|
| 289 |
|
|---|
| 290 | Function TIniFile.ReadInteger(Const section, Ident: String; Default: LongInt): LongInt;
|
|---|
| 291 | Begin
|
|---|
| 292 | {$IFDEF OS2}
|
|---|
| 293 | Result := PrfQueryProfileInt(FHandle, section, Ident, Default);
|
|---|
| 294 | {$ENDIF}
|
|---|
| 295 | {$IFDEF Win95}
|
|---|
| 296 | Result := GetPrivateProfileInt(section,Ident,Default,FileName);
|
|---|
| 297 | {$ENDIF}
|
|---|
| 298 | End;
|
|---|
| 299 |
|
|---|
| 300 | Function TIniFile.ReadBool(Const section, Ident: String; Default: Boolean): Boolean;
|
|---|
| 301 | Var
|
|---|
| 302 | L: LongInt;
|
|---|
| 303 | Begin
|
|---|
| 304 | If Default Then L := 1 Else L := 0;
|
|---|
| 305 | {$IFDEF OS2}
|
|---|
| 306 | Result := (PrfQueryProfileInt(FHandle, section, Ident, L) <> 0);
|
|---|
| 307 | {$ENDIF}
|
|---|
| 308 | {$IFDEF Win95}
|
|---|
| 309 | Result := (GetPrivateProfileInt(section,Ident,L,FileName) <> 0);
|
|---|
| 310 | {$ENDIF}
|
|---|
| 311 | End;
|
|---|
| 312 |
|
|---|
| 313 | Procedure TIniFile.ReadSection(Const section: String; AStrings: TStrings);
|
|---|
| 314 | Var
|
|---|
| 315 | Size, RealSize: LongWord;
|
|---|
| 316 | Buffer, Pos: PChar;
|
|---|
| 317 | S: String;
|
|---|
| 318 | Begin
|
|---|
| 319 | {$IFDEF OS2}
|
|---|
| 320 | If Not PrfQueryProfileSize(FHandle, section, Nil, Size) Then Exit;
|
|---|
| 321 | If Size=0 Then exit;
|
|---|
| 322 | {$ENDIF}
|
|---|
| 323 | {$IFDEF Win95}
|
|---|
| 324 | //??????????????????????????????????????????????????????????
|
|---|
| 325 | Size:=8192;
|
|---|
| 326 | {$ENDIF}
|
|---|
| 327 | GetMem(Buffer, Size);
|
|---|
| 328 | Try
|
|---|
| 329 | {$IFDEF OS2}
|
|---|
| 330 | PrfQueryProfileString(FHandle, section, Nil, Nil, Buffer^, Size);
|
|---|
| 331 | Buffer[Size - 1] := #0;
|
|---|
| 332 | {$ENDIF}
|
|---|
| 333 | {$IFDEF Win95}
|
|---|
| 334 | Buffer[GetPrivateProfileString(section,Nil,Nil,Buffer^,Size,FileName)-1] := #0;
|
|---|
| 335 | {$ENDIF}
|
|---|
| 336 | Pos := Buffer;
|
|---|
| 337 | While Pos[0] <> #0 Do
|
|---|
| 338 | Begin
|
|---|
| 339 | S := StrPas(Pos);
|
|---|
| 340 | Inc(Pos, Length(S) + 1);
|
|---|
| 341 | Dec(RealSize, Length(S) + 1);
|
|---|
| 342 | AStrings.Add(S);
|
|---|
| 343 | End;
|
|---|
| 344 | Finally
|
|---|
| 345 | FreeMem(Buffer, Size);
|
|---|
| 346 | End;
|
|---|
| 347 | End;
|
|---|
| 348 |
|
|---|
| 349 | Procedure TIniFile.ReadSectionValues(Const section: String; AStrings: TStrings);
|
|---|
| 350 | Var
|
|---|
| 351 | Temp: TIniSection;
|
|---|
| 352 | I: LongInt;
|
|---|
| 353 | Begin
|
|---|
| 354 | Temp := TIniSection.Create;
|
|---|
| 355 | Temp.sorted := True;
|
|---|
| 356 | Temp.Duplicates := dupIgnore;
|
|---|
| 357 | Try
|
|---|
| 358 | ReadSection(section, Temp);
|
|---|
| 359 | For I := 0 To Temp.Count - 1 Do
|
|---|
| 360 | AStrings.values[Temp.Strings[I]]:=ReadString(section, Temp.Strings[I], '');
|
|---|
| 361 | Finally
|
|---|
| 362 | Temp.Destroy;
|
|---|
| 363 | End;
|
|---|
| 364 | End;
|
|---|
| 365 |
|
|---|
| 366 | Procedure TIniFile.WriteString(Const section, Ident, Value: String);
|
|---|
| 367 | Var
|
|---|
| 368 | CSection, CIdent, CValue: cstring[256];
|
|---|
| 369 | Begin
|
|---|
| 370 | CSection := section;
|
|---|
| 371 | CIdent := Ident;
|
|---|
| 372 | CValue := Value;
|
|---|
| 373 | {$IFDEF OS2}
|
|---|
| 374 | If Not PrfWriteProfileString(FHandle, CSection, CIdent, CValue) Then
|
|---|
| 375 | Error(LoadNLSStr(SWriteError)+'.');
|
|---|
| 376 | {$ENDIF}
|
|---|
| 377 | {$IFDEF Win95}
|
|---|
| 378 | If Not WritePrivateProfileString(CSection,CIdent,CValue,FileName) Then
|
|---|
| 379 | Error(LoadNLSStr(SWriteError)+'.');
|
|---|
| 380 | {$ENDIF}
|
|---|
| 381 | End;
|
|---|
| 382 |
|
|---|
| 383 | Procedure TIniFile.WriteInteger(Const section, Ident: String; Value: LongInt);
|
|---|
| 384 | Begin
|
|---|
| 385 | WriteString(section, Ident, IntToStr(Value));
|
|---|
| 386 | End;
|
|---|
| 387 |
|
|---|
| 388 | Procedure TIniFile.WriteBool(Const section, Ident: String; Value: Boolean);
|
|---|
| 389 | Var
|
|---|
| 390 | C: Char;
|
|---|
| 391 | Begin
|
|---|
| 392 | If Value Then C := '1' Else C := '0';
|
|---|
| 393 | WriteString(section, Ident, C);
|
|---|
| 394 | End;
|
|---|
| 395 |
|
|---|
| 396 | { TAsciiIniFile }
|
|---|
| 397 |
|
|---|
| 398 | Constructor TAsciiIniFile.Create(Const FileName: String);
|
|---|
| 399 | Var
|
|---|
| 400 | Source: Text;
|
|---|
| 401 | S, T: String;
|
|---|
| 402 | Begin
|
|---|
| 403 | SectionSort := False;
|
|---|
| 404 | IdentSort := False;
|
|---|
| 405 |
|
|---|
| 406 | InitIniFile;
|
|---|
| 407 |
|
|---|
| 408 | FSections := TStringList.Create;
|
|---|
| 409 | FSections.Duplicates := dupIgnore;
|
|---|
| 410 | FSections.sorted := SectionSort;
|
|---|
| 411 | TrueString := 'True';
|
|---|
| 412 | FalseString := 'False';
|
|---|
| 413 |
|
|---|
| 414 | FFileName := NewStr(FileName);
|
|---|
| 415 | FName := NullStr;
|
|---|
| 416 |
|
|---|
| 417 | If FFileName <> NullStr Then
|
|---|
| 418 | Begin
|
|---|
| 419 | Assign(Source, FileName);
|
|---|
| 420 | {$I-}
|
|---|
| 421 | Reset(Source);
|
|---|
| 422 | {$I+}
|
|---|
| 423 | If IOResult = 0 Then
|
|---|
| 424 | Begin
|
|---|
| 425 | While Not Eof(Source) Do
|
|---|
| 426 | Begin
|
|---|
| 427 | ReadLn(Source, S);
|
|---|
| 428 | If Length(S) <> 0 Then
|
|---|
| 429 | Begin
|
|---|
| 430 | If S[1] = '[' Then
|
|---|
| 431 | Begin
|
|---|
| 432 | { New section }
|
|---|
| 433 | Delete(S, 1, 1);
|
|---|
| 434 | WriteToSection(CutStr(S, ']'));
|
|---|
| 435 | End
|
|---|
| 436 | Else
|
|---|
| 437 | If Not (S[1] In [';', '#', '%']) Then
|
|---|
| 438 | Begin
|
|---|
| 439 | { New entry }
|
|---|
| 440 | If FList = Nil Then WriteToSection('');
|
|---|
| 441 | T := CutStr(S, '=');
|
|---|
| 442 | FList.AddObject(TrimStr(T), TObject(NewStr(TrimStr(S))));
|
|---|
| 443 | End;
|
|---|
| 444 | End;
|
|---|
| 445 | End;
|
|---|
| 446 | Close(Source);
|
|---|
| 447 | End;
|
|---|
| 448 | // else - unable to open file. That's OK,
|
|---|
| 449 | // we will create it later - AaronL
|
|---|
| 450 | End;
|
|---|
| 451 | End;
|
|---|
| 452 |
|
|---|
| 453 | {To Setup the Sort Value Of section And Ident}
|
|---|
| 454 | Procedure TAsciiIniFile.InitIniFile;
|
|---|
| 455 | Begin
|
|---|
| 456 | End;
|
|---|
| 457 |
|
|---|
| 458 | Destructor TAsciiIniFile.Destroy;
|
|---|
| 459 | Var
|
|---|
| 460 | I: LongInt;
|
|---|
| 461 | Begin
|
|---|
| 462 | Refresh;
|
|---|
| 463 | DisposeStr(FName);
|
|---|
| 464 | DisposeStr(FFileName);
|
|---|
| 465 | For I := 0 To FSections.Count - 1 Do
|
|---|
| 466 | Begin
|
|---|
| 467 | FList := TIniSection(FSections.Objects[I]);
|
|---|
| 468 | FList.Destroy;
|
|---|
| 469 | End;
|
|---|
| 470 | FSections.Destroy;
|
|---|
| 471 | End;
|
|---|
| 472 |
|
|---|
| 473 | Procedure TAsciiIniFile.Erase(Const section, Ident: String);
|
|---|
| 474 | Var
|
|---|
| 475 | I: LongInt;
|
|---|
| 476 | Begin
|
|---|
| 477 | If ReadFromSection(section) Then
|
|---|
| 478 | Begin
|
|---|
| 479 | If FList.Find(Ident, I) Then
|
|---|
| 480 | Begin
|
|---|
| 481 | FList.Delete(I);
|
|---|
| 482 | FChanged := True;
|
|---|
| 483 | End;
|
|---|
| 484 | End;
|
|---|
| 485 | End;
|
|---|
| 486 |
|
|---|
| 487 | Procedure TAsciiIniFile.EraseSection(Const section: String);
|
|---|
| 488 | Var
|
|---|
| 489 | I: LongInt;
|
|---|
| 490 | S: TIniSection;
|
|---|
| 491 | Begin
|
|---|
| 492 | If FSections.Find(section, I) Then
|
|---|
| 493 | Begin
|
|---|
| 494 | S := TIniSection(FSections.Objects[I]);
|
|---|
| 495 | S.Destroy;
|
|---|
| 496 | FSections.Delete(I);
|
|---|
| 497 | If S = FList Then
|
|---|
| 498 | Begin
|
|---|
| 499 | AssignStr(FName, '');
|
|---|
| 500 | FList := Nil;
|
|---|
| 501 | End;
|
|---|
| 502 | FChanged := True;
|
|---|
| 503 | End;
|
|---|
| 504 | End;
|
|---|
| 505 |
|
|---|
| 506 | Procedure TAsciiIniFile.WriteToSection(Const section: String);
|
|---|
| 507 | Var
|
|---|
| 508 | I: LongInt;
|
|---|
| 509 | Begin
|
|---|
| 510 | If CompareText(section, FName^) <> 0 Then
|
|---|
| 511 | Begin
|
|---|
| 512 | If FSections.Find(section, I) Then
|
|---|
| 513 | Begin
|
|---|
| 514 | AssignStr(FName, section);
|
|---|
| 515 | FList := TIniSection(FSections.Objects[I]);
|
|---|
| 516 | End
|
|---|
| 517 | Else
|
|---|
| 518 | Begin
|
|---|
| 519 | FList := TIniSection.Create;
|
|---|
| 520 | FList.Duplicates := dupAccept;
|
|---|
| 521 | FList.sorted := IdentSort;
|
|---|
| 522 | FList.CaseSensitive := False;
|
|---|
| 523 | FSections.AddObject(section, FList);
|
|---|
| 524 | AssignStr(FName, section);
|
|---|
| 525 | End;
|
|---|
| 526 | End;
|
|---|
| 527 | End;
|
|---|
| 528 |
|
|---|
| 529 | Function TAsciiIniFile.ReadFromSection(Const section: String): Boolean;
|
|---|
| 530 | Var
|
|---|
| 531 | I: LongInt;
|
|---|
| 532 | Begin
|
|---|
| 533 | Result := True; {!}
|
|---|
| 534 | If CompareText(section, FName^) <> 0 Then
|
|---|
| 535 | Begin
|
|---|
| 536 | If FSections.Find(section, I) Then
|
|---|
| 537 | Begin
|
|---|
| 538 | AssignStr(FName, section);
|
|---|
| 539 | FList := TIniSection(FSections.Objects[I]);
|
|---|
| 540 | End
|
|---|
| 541 | Else Result := False; {!}
|
|---|
| 542 | End;
|
|---|
| 543 | // Result := (FList <> Nil);
|
|---|
| 544 | {liefert sonst Die Letzte zurck, wenn section unbekannt}
|
|---|
| 545 | End;
|
|---|
| 546 |
|
|---|
| 547 | {Function TAsciiIniFile.GetFileName: String;
|
|---|
| 548 | Begin
|
|---|
| 549 | Result := FFileName^;
|
|---|
| 550 | End;}
|
|---|
| 551 |
|
|---|
| 552 | Function TAsciiIniFile.ReadBool(Const section, Ident: String; Default: Boolean): Boolean;
|
|---|
| 553 | Var
|
|---|
| 554 | S: String;
|
|---|
| 555 | Begin
|
|---|
| 556 | If ReadFromSection(section) Then
|
|---|
| 557 | With TIniSection(FList) { As TIniSection} Do
|
|---|
| 558 | Begin
|
|---|
| 559 | If FindValue(Ident, S) = -1 Then Result := Default
|
|---|
| 560 | Else
|
|---|
| 561 | Begin
|
|---|
| 562 | If CompareText(S, TrueString) = 0 Then Result := True
|
|---|
| 563 | Else If CompareText(S, FalseString) = 0 Then Result := False
|
|---|
| 564 | Else Result := Default;
|
|---|
| 565 | End;
|
|---|
| 566 | End
|
|---|
| 567 | Else Result := Default;
|
|---|
| 568 | End;
|
|---|
| 569 |
|
|---|
| 570 | Function TAsciiIniFile.ReadInteger(Const section, Ident: String; Default: LongInt): LongInt;
|
|---|
| 571 | Var
|
|---|
| 572 | S: String;
|
|---|
| 573 | Begin
|
|---|
| 574 | If ReadFromSection(section) Then
|
|---|
| 575 | With TIniSection(FList) { As TIniSection} Do
|
|---|
| 576 | Begin
|
|---|
| 577 | If FindValue(Ident, S) = -1 Then Result := Default
|
|---|
| 578 | Else Result := StrToIntDef(S, Default);
|
|---|
| 579 | End
|
|---|
| 580 | Else Result := Default;
|
|---|
| 581 | End;
|
|---|
| 582 |
|
|---|
| 583 | Function TAsciiIniFile.ReadString(Const section, Ident, Default: String): String;
|
|---|
| 584 | Begin
|
|---|
| 585 | If ReadFromSection(section) Then
|
|---|
| 586 | With TIniSection(FList) { As TIniSection} Do
|
|---|
| 587 | Begin
|
|---|
| 588 | If FindValue(Ident, Result) = -1 Then Result := Default;
|
|---|
| 589 | End
|
|---|
| 590 | Else Result := Default;
|
|---|
| 591 | End;
|
|---|
| 592 |
|
|---|
| 593 | Procedure TAsciiIniFile.ReadSection(Const section: String; AStrings: TStrings);
|
|---|
| 594 | Var
|
|---|
| 595 | N: LongInt;
|
|---|
| 596 | Begin
|
|---|
| 597 | If ReadFromSection(section) Then
|
|---|
| 598 | Begin
|
|---|
| 599 | For N := 0 To FList.Count - 1 Do AStrings.Add(FList.Strings[N]);
|
|---|
| 600 | End;
|
|---|
| 601 | End;
|
|---|
| 602 |
|
|---|
| 603 | Procedure TAsciiIniFile.ReadSections(AStrings: TStrings);
|
|---|
| 604 | Var
|
|---|
| 605 | N: LongInt;
|
|---|
| 606 | Begin
|
|---|
| 607 | For N := 0 To FSections.Count - 1 Do AStrings.Add(FSections.Strings[N]);
|
|---|
| 608 | End;
|
|---|
| 609 |
|
|---|
| 610 | Procedure TAsciiIniFile.ReadSectionValues(Const section: String; AStrings: TStrings);
|
|---|
| 611 | Var
|
|---|
| 612 | N: LongInt;
|
|---|
| 613 | Begin
|
|---|
| 614 | If ReadFromSection(section) Then
|
|---|
| 615 | Begin
|
|---|
| 616 | For N := 0 To FList.Count - 1 Do
|
|---|
| 617 | AStrings.Add(FList.Strings[N] + '=' + PString(FList.Objects[N])^);
|
|---|
| 618 | End;
|
|---|
| 619 | End;
|
|---|
| 620 |
|
|---|
| 621 | // Save the ini file from memory to the file.
|
|---|
| 622 | Procedure TAsciiIniFile.Refresh;
|
|---|
| 623 | Var
|
|---|
| 624 | Dest: Text;
|
|---|
| 625 | N, I: LongInt;
|
|---|
| 626 | L: TIniSection;
|
|---|
| 627 | S: String;
|
|---|
| 628 | Begin
|
|---|
| 629 | If FChanged And (FFileName <> NullStr) Then
|
|---|
| 630 | Begin
|
|---|
| 631 | Assign(Dest, FileName);
|
|---|
| 632 | Rewrite(Dest);
|
|---|
| 633 | For N := 0 To FSections.Count - 1 Do
|
|---|
| 634 | Begin
|
|---|
| 635 | S := FSections.Strings[N];
|
|---|
| 636 | If Length(S) <> 0 Then
|
|---|
| 637 | Begin
|
|---|
| 638 | WriteLn(Dest, '[' + S + ']');
|
|---|
| 639 | WriteLn(Dest);
|
|---|
| 640 | End;
|
|---|
| 641 | L := TIniSection(FSections.Objects[N]);
|
|---|
| 642 | For I := 0 To L.Count - 1 Do
|
|---|
| 643 | WriteLn(Dest, L.Strings[I], '=', PString(L.Objects[I])^);
|
|---|
| 644 | If N < FSections.Count Then WriteLn(Dest);
|
|---|
| 645 | FChanged := False;
|
|---|
| 646 | End;
|
|---|
| 647 | Close(Dest);
|
|---|
| 648 | End;
|
|---|
| 649 | End;
|
|---|
| 650 |
|
|---|
| 651 | Procedure TAsciiIniFile.WriteBool(Const section, Ident: String; Value: Boolean);
|
|---|
| 652 | Begin
|
|---|
| 653 | FChanged := True;
|
|---|
| 654 | WriteToSection(section);
|
|---|
| 655 | If Value Then TIniSection(FList).values[Ident]:=TrueString
|
|---|
| 656 | Else TIniSection(FList).values[Ident]:=FalseString;
|
|---|
| 657 | End;
|
|---|
| 658 |
|
|---|
| 659 | Procedure TAsciiIniFile.WriteInteger(Const section, Ident: String; Value: LongInt);
|
|---|
| 660 | Begin
|
|---|
| 661 | FChanged := True;
|
|---|
| 662 | WriteToSection(section);
|
|---|
| 663 | TIniSection(FList).values[Ident]:=IntToStr(Value);
|
|---|
| 664 | End;
|
|---|
| 665 |
|
|---|
| 666 | Procedure TAsciiIniFile.WriteString(Const section, Ident, Value: String);
|
|---|
| 667 | Begin
|
|---|
| 668 | FChanged := True;
|
|---|
| 669 | WriteToSection(section);
|
|---|
| 670 | TIniSection(FList).values[Ident]:=Value;
|
|---|
| 671 | End;
|
|---|
| 672 |
|
|---|
| 673 | End.
|
|---|
| 674 |
|
|---|
| 675 | { nderungen: 26.11.95 Sections werden sortiert, Eintrge aber nicht
|
|---|
| 676 | 28.11.95 alles wird sortiert, schneller durch Trennung
|
|---|
| 677 | von Ident und Value (TIniSection).
|
|---|
| 678 | 30.11.95 Fehler In TIniSection korrigiert, ES
|
|---|
| 679 | fehlten Get/Put
|
|---|
| 680 | 16.12.95 neue Funktion GetDefaultINI()
|
|---|
| 681 | 16-Aug-97 Jrg: Bugfix for TIniFile.ReadString.
|
|---|
| 682 |
|
|---|
| 683 | To-Do: - Eventuell ReadData / WriteData einbauen
|
|---|
| 684 | - Eventuell ReadSectionNames einbauen = ReadSections
|
|---|
| 685 | - wenn String -> cstring Fehler In SP/2 behoben,
|
|---|
| 686 | Workaround entfernen.
|
|---|
| 687 | }
|
|---|
| 688 |
|
|---|
| 689 |
|
|---|
| 690 |
|
|---|