123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- {******************************************************************************
- TRegIniFile
- ******************************************************************************}
- constructor TRegIniFile.Create(const FN: String);
- begin
- Create(FN, KEY_ALL_ACCESS);
- end;
- constructor TRegIniFile.Create(const FN: String;aaccess:longword);
- begin
- inherited Create(aaccess);
- fFileName := FN;
- if fFileName<>'' then begin
- fPath := fFileName + '\';
- OpenKey(fFileName, aaccess <> KEY_READ);
- end
- else
- fPath := '';
- fPreferStringValues:=True; // Delphi compatibility
- end;
- procedure TRegIniFile.DeleteKey(const Section, Ident: String);
- begin
- if OpenSection(Section) then
- try
- DeleteValue(Ident);
- finally
- CloseSection;
- end;
- end;
- procedure TRegIniFile.EraseSection(const Section: string);
- begin
- inherited DeleteKey(Section);
- end;
- procedure TRegIniFile.ReadSection(const Section: string; Strings: TStrings);
- begin
- if OpenSection(Section) then
- try
- GetValueNames(Strings);
- finally
- CloseSection;
- end;
- end;
- procedure TRegIniFile.ReadSections(Strings: TStrings);
- begin
- GetKeyNames(Strings);
- end;
- procedure TRegIniFile.ReadSectionValues(const Section: string; Strings: TStrings);
- var
- ValList : TStringList;
- V : String;
- i : Integer;
- begin
- if OpenSection(Section) then
- try
- ValList := TStringList.Create;
- try
- GetValueNames(ValList);
- for i:=0 to ValList.Count-1 do
- begin
- V := inherited ReadString(ValList.Strings[i]);
- Strings.Add(ValList.Strings[i] + '=' + V);
- end;
- finally
- ValList.Free;
- end;
- finally
- CloseSection;
- end;
- end;
- procedure TRegIniFile.WriteBool(const Section, Ident: string; Value: Boolean);
- begin
- if OpenSection(Section,True) then
- try
- if not fPreferStringValues then
- inherited WriteBool(Ident,Value)
- else begin
- if ValueExists(Ident) and (GetDataType(Ident)=rdInteger) then
- inherited WriteBool(Ident,Value)
- else
- inherited WriteString(Ident,BoolToStr(Value));
- end;
- finally
- CloseSection;
- end;
- end;
- procedure TRegIniFile.WriteInteger(const Section, Ident: string; Value: LongInt);
- begin
- if OpenSection(Section,True) then
- try
- if not fPreferStringValues then
- inherited WriteInteger(Ident,Value)
- else begin
- if ValueExists(Ident) and (GetDataType(Ident)=rdInteger) then
- inherited WriteInteger(Ident,Value)
- else
- inherited WriteString(Ident,IntToStr(Value));
- end;
- finally
- CloseSection;
- end;
- end;
- procedure TRegIniFile.WriteString(const Section, Ident, Value: String);
- begin
- if OpenSection(Section,True) then
- try
- inherited WriteString(Ident,Value);
- finally
- CloseSection;
- end;
- end;
- procedure TRegIniFile.WriteDate(const Section, Ident: string; Value: TDateTime);
- begin
- if OpenSection(Section,true) then
- try
- if not fPreferStringValues then
- inherited WriteDate(Ident,Value)
- else if ValueExists(Ident) and (GetDataType(Ident)<>rdString) then
- inherited WriteDate(Ident,Value)
- else
- inherited WriteString(Ident,DateToStr(Value));
- finally
- CloseKey;
- end;
- end;
- procedure TRegIniFile.WriteDateTime(const Section, Ident: string; Value: TDateTime);
- begin
- if OpenSection(Section,true) then
- try
- if not fPreferStringValues then
- inherited WriteDateTime(Ident,Value)
- else if ValueExists(Ident) and (GetDataType(Ident)<>rdString) then
- inherited WriteDateTime(Ident,Value)
- else
- inherited WriteString(Ident,DateTimeToStr(Value));
- finally
- CloseKey;
- end;
- end;
- procedure TRegIniFile.WriteTime(const Section, Ident: string; Value: TDateTime);
- begin
- if OpenSection(Section,true) then
- try
- if not fPreferStringValues then
- inherited WriteTime(Ident,Value)
- else if ValueExists(Ident) and (GetDataType(Ident)<>rdString) then
- inherited WriteTime(Ident,Value)
- else
- inherited WriteString(Ident,TimeToStr(Value));
- finally
- CloseKey;
- end;
- end;
- procedure TRegIniFile.WriteFloat(const Section, Ident: string; Value: Double);
- begin
- if OpenSection(Section,true) then
- try
- if not fPreferStringValues then
- inherited WriteFloat(Ident,Value)
- else if ValueExists(Ident) and (GetDataType(Ident)<>rdString) then
- inherited WriteFloat(Ident,Value)
- else
- inherited WriteString(Ident,FloatToStr(Value));
- finally
- CloseKey;
- end;
- end;
-
- function TRegIniFile.ReadBool(const Section, Ident: string; Default: Boolean): Boolean;
- begin
- Result := Default;
- if OpenSection(Section) then
- try
- if ValueExists(Ident) then
- if (not fPreferStringValues) or (GetDataType(Ident)=rdInteger) then
- Result := inherited ReadBool(Ident)
- else
- Result := StrToBool(inherited ReadString(Ident));
- finally
- CloseSection;
- end;
- end;
- function TRegIniFile.ReadInteger(const Section, Ident: string; Default: LongInt): LongInt;
- begin
- Result := Default;
- if OpenSection(Section) then
- try
- if ValueExists(Ident) then
- if (not fPreferStringValues) or (GetDataType(Ident)=rdInteger) then
- Result := inherited ReadInteger(Ident)
- else
- Result := StrToInt(inherited ReadString(Ident));
- finally
- CloseSection;
- end;
- end;
- function TRegIniFile.ReadString(const Section, Ident, Default: String): String;
- begin
- Result := Default;
- if OpenSection(Section) then
- try
- if ValueExists(Ident) then
- Result := inherited ReadString(Ident);
- finally
- CloseSection;
- end;
- end;
- function TRegIniFile.ReadDate(const Section, Ident: string; Default: TDateTime):TDateTime;
- begin
- Result := Default;
- if OpenSection(Section) then
- try
- if ValueExists(Ident) then
- if (not fPreferStringValues) or (GetDataType(Ident)<>rdString) then
- Result := inherited ReadDate(Ident)
- else
- Result := StrToDateDef(inherited ReadString(Ident),Result);
- finally
- CloseSection;
- end;
- end;
- function TRegIniFile.ReadDateTime(const Section, Ident: string; Default: TDateTime):TDateTime;
- begin
- Result := Default;
- if OpenSection(Section) then
- try
- if ValueExists(Ident) then
- if (not fPreferStringValues) or (GetDataType(Ident)<>rdString) then
- Result := inherited ReadDateTime(Ident)
- else
- Result := StrToDateTimeDef(inherited ReadString(Ident),Result);
- finally
- CloseSection;
- end;
- end;
-
- function TRegIniFile.ReadTime(const Section, Ident: string; Default: TDateTime):TDateTime;
- begin
- Result := Default;
- if OpenSection(Section) then
- try
- if ValueExists(Ident) then
- if (not fPreferStringValues) or (GetDataType(Ident)<>rdString) then
- Result := inherited ReadTime(Ident)
- else
- Result := StrToTimeDef(inherited ReadString(Ident),Result);
- finally
- CloseSection;
- end;
- end;
- function TRegIniFile.ReadFloat(const Section, Ident: string; Default: Double): Double;
- begin
- Result := Default;
- if OpenSection(Section) then
- try
- if ValueExists(Ident) then
- if (not fPreferStringValues) or (GetDataType(Ident)<>rdString) then
- Result := inherited ReadFloat(Ident)
- else
- Result := StrToFloatDef(inherited ReadString(Ident),Result);
- finally
- CloseSection;
- end;
- end;
- function TRegIniFile.OpenSection(const Section: string; CreateSection : Boolean = false): boolean;
- var
- k: HKEY;
- S : String;
- begin
- S:=Section;
- If (S<>'') and (S[1] = '\') then
- Delete(S,1,1);
- if CreateSection then
- CreateKey('\'+FPath+S);
- if Section <> '' then
- begin
- k:=GetKey('\'+FPath+S);
- if k = 0 then
- begin
- Result:=False;
- exit;
- end;
- SetCurrentKey(k);
- end;
- Result:=True;
- end;
- procedure TRegIniFile.CloseSection;
- begin
- CloseKey(CurrentKey);
- end;
|