source: 2.19_branch/Sibyl/SPCC/INIFILES.PAS@ 376

Last change on this file since 376 was 7, checked in by RBRi, 19 years ago

+ sibyl staff

  • Property svn:eol-style set to native
File size: 17.1 KB
Line 
1
2{ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
3 º º
4 º Sibyl Portable Component Classes º
5 º º
6 º Copyright (C) 1995,97 SpeedSoft Germany, All rights reserved. º
7 º º
8 ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍŒ}
9
10Unit IniFiles;
11
12{ TIniFile: Standard (bin„res) 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
19Interface
20
21Uses Classes,SysUtils;
22
23{$IFDEF OS2}
24Uses PMSHL; { OS/2 profile FUNCTIONs }
25{$ENDIF}
26{$IFDEF Win95}
27Uses WinBase;
28{$ENDIF}
29
30Type
31 EIniFileError = Class(Exception);
32
33Type
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
64Type
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
108Function GetDefaultINI: String;
109 { Get Name Of INI-File that matches Program Path & Name With
110 extension .INI instead Of .EXE }
111
112Implementation
113
114Uses
115 SysUtils;
116
117Const
118 Null = 0;
119
120Type
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
132Function CutStr(Var S: String; C: Char): String;
133Var
134 P: Integer;
135Begin
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;
147End;
148
149Function TrimStr(Const S: String): String;
150Var
151 L, R: Integer;
152Begin
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);
158End;
159
160Function GetDefaultINI: String;
161Begin
162 Result := ExpandFileName(ChangeFileExt(ParamStr(0), '.INI'));
163End;
164
165{ TIniSection }
166
167Procedure TIniSection.FreeItem(AObject:TObject);
168Var
169 P: PString;
170Begin
171 P := PString(AObject);
172 DisposeStr(P);
173 Inherited FreeItem(AObject);
174End;
175
176{Procedure TIniSection.Put(Index: LongInt; Const S: String);
177Var
178 Ident, Value: String;
179Begin
180 Value := S;
181 Ident := CutStr(Value, '=');
182 SetValue(Ident, Value);
183End;}
184
185Function TIniSection.GetValue(Const Name: String): String;
186Var
187 I: LongInt;
188Begin
189 If Find(Name, I) Then Result := PString(Objects[I])^
190 Else Result := '';
191End;
192
193Function TIniSection.FindValue(Const Name: String; Var Value: String): LongInt;
194Begin
195 If Find(Name, Result) Then Value := PString(Objects[Result])^
196 Else
197 Begin
198 Value := '';
199 Result := -1;
200 End;
201End;
202
203Procedure TIniSection.SetValue(Const Name, Value: String);
204Var
205 I: LongInt;
206 P: PString;
207 OldSorted: Boolean;
208Begin
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;
222End;
223
224{ TIniFile }
225
226Constructor TIniFile.Create(Const FileName: String);
227Begin
228 {$IFDEF OS2}
229 FHandle := PrfOpenProfile(AppHandle, FileName);
230 If FHandle = Null Then Error(LoadNLSStr(SCannotOpenIniFile)+'.');
231 {$ENDIF}
232 FFileName := NewStr(FileName);
233End;
234
235Destructor TIniFile.Destroy;
236Begin
237 {$IFDEF OS2}
238 PrfCloseProfile(FHandle);
239 {$ENDIF}
240 DisposeStr(FFileName);
241
242 Inherited Destroy;
243End;
244
245Procedure TIniFile.Erase(Const section, Ident: String);
246Begin
247 {$IFDEF OS2}
248 PrfWriteProfileString(FHandle, section, Ident, Nil);
249 {$ENDIF}
250 {$IFDEF Win95}
251 WritePrivateProfileString(section,Ident,Nil,FileName);
252 {$ENDIF}
253End;
254
255Procedure TIniFile.EraseSection(Const section: String);
256Begin
257 {$IFDEF OS2}
258 PrfWriteProfileString(FHandle, section, Nil, Nil);
259 {$ENDIF}
260 {$IFDEF Win95}
261 WritePrivateProfileString(section,Nil,Nil,FileName);
262 {$ENDIF}
263End;
264
265Procedure TIniFile.Error(Const Msg: String);
266Begin
267 Raise EIniFileError.Create(Msg);
268End;
269
270Function TIniFile.GetFileName: String;
271Begin
272 Result := FFileName^;
273End;
274
275Function TIniFile.ReadString(Const section, Ident, Default: String): String;
276Var
277 OutBuf: cstring;
278Begin
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}
288End;
289
290Function TIniFile.ReadInteger(Const section, Ident: String; Default: LongInt): LongInt;
291Begin
292 {$IFDEF OS2}
293 Result := PrfQueryProfileInt(FHandle, section, Ident, Default);
294 {$ENDIF}
295 {$IFDEF Win95}
296 Result := GetPrivateProfileInt(section,Ident,Default,FileName);
297 {$ENDIF}
298End;
299
300Function TIniFile.ReadBool(Const section, Ident: String; Default: Boolean): Boolean;
301Var
302 L: LongInt;
303Begin
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}
311End;
312
313Procedure TIniFile.ReadSection(Const section: String; AStrings: TStrings);
314Var
315 Size, RealSize: LongWord;
316 Buffer, Pos: PChar;
317 S: String;
318Begin
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;
347End;
348
349Procedure TIniFile.ReadSectionValues(Const section: String; AStrings: TStrings);
350Var
351 Temp: TIniSection;
352 I: LongInt;
353Begin
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;
364End;
365
366Procedure TIniFile.WriteString(Const section, Ident, Value: String);
367Var
368 CSection, CIdent, CValue: cstring[256];
369Begin
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}
381End;
382
383Procedure TIniFile.WriteInteger(Const section, Ident: String; Value: LongInt);
384Begin
385 WriteString(section, Ident, IntToStr(Value));
386End;
387
388Procedure TIniFile.WriteBool(Const section, Ident: String; Value: Boolean);
389Var
390 C: Char;
391Begin
392 If Value Then C := '1' Else C := '0';
393 WriteString(section, Ident, C);
394End;
395
396{ TAsciiIniFile }
397
398Constructor TAsciiIniFile.Create(Const FileName: String);
399Var
400 Source: Text;
401 S, T: String;
402Begin
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;
451End;
452
453{To Setup the Sort Value Of section And Ident}
454Procedure TAsciiIniFile.InitIniFile;
455Begin
456End;
457
458Destructor TAsciiIniFile.Destroy;
459Var
460 I: LongInt;
461Begin
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;
471End;
472
473Procedure TAsciiIniFile.Erase(Const section, Ident: String);
474Var
475 I: LongInt;
476Begin
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;
485End;
486
487Procedure TAsciiIniFile.EraseSection(Const section: String);
488Var
489 I: LongInt;
490 S: TIniSection;
491Begin
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;
504End;
505
506Procedure TAsciiIniFile.WriteToSection(Const section: String);
507Var
508 I: LongInt;
509Begin
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;
527End;
528
529Function TAsciiIniFile.ReadFromSection(Const section: String): Boolean;
530Var
531 I: LongInt;
532Begin
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 zurck, wenn section unbekannt}
545End;
546
547{Function TAsciiIniFile.GetFileName: String;
548Begin
549 Result := FFileName^;
550End;}
551
552Function TAsciiIniFile.ReadBool(Const section, Ident: String; Default: Boolean): Boolean;
553Var
554 S: String;
555Begin
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;
568End;
569
570Function TAsciiIniFile.ReadInteger(Const section, Ident: String; Default: LongInt): LongInt;
571Var
572 S: String;
573Begin
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;
581End;
582
583Function TAsciiIniFile.ReadString(Const section, Ident, Default: String): String;
584Begin
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;
591End;
592
593Procedure TAsciiIniFile.ReadSection(Const section: String; AStrings: TStrings);
594Var
595 N: LongInt;
596Begin
597 If ReadFromSection(section) Then
598 Begin
599 For N := 0 To FList.Count - 1 Do AStrings.Add(FList.Strings[N]);
600 End;
601End;
602
603Procedure TAsciiIniFile.ReadSections(AStrings: TStrings);
604Var
605 N: LongInt;
606Begin
607 For N := 0 To FSections.Count - 1 Do AStrings.Add(FSections.Strings[N]);
608End;
609
610Procedure TAsciiIniFile.ReadSectionValues(Const section: String; AStrings: TStrings);
611Var
612 N: LongInt;
613Begin
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;
619End;
620
621// Save the ini file from memory to the file.
622Procedure TAsciiIniFile.Refresh;
623Var
624 Dest: Text;
625 N, I: LongInt;
626 L: TIniSection;
627 S: String;
628Begin
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;
649End;
650
651Procedure TAsciiIniFile.WriteBool(Const section, Ident: String; Value: Boolean);
652Begin
653 FChanged := True;
654 WriteToSection(section);
655 If Value Then TIniSection(FList).values[Ident]:=TrueString
656 Else TIniSection(FList).values[Ident]:=FalseString;
657End;
658
659Procedure TAsciiIniFile.WriteInteger(Const section, Ident: String; Value: LongInt);
660Begin
661 FChanged := True;
662 WriteToSection(section);
663 TIniSection(FList).values[Ident]:=IntToStr(Value);
664End;
665
666Procedure TAsciiIniFile.WriteString(Const section, Ident, Value: String);
667Begin
668 FChanged := True;
669 WriteToSection(section);
670 TIniSection(FList).values[Ident]:=Value;
671End;
672
673End.
674
675{ Žnderungen: 26.11.95 Sections werden sortiert, Eintr„ge 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 J”rg: 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
Note: See TracBrowser for help on using the repository browser.