2013-03-03 5 views
2

설정 용 정수를 저장하는 INI 파일이 있습니다. 섹션 이름은 다음과 같이 저장됩니다 : 나는 최고 섹션 번호의 색상 번호 +1을 증가 새로운 섹션을 만들 수있는 방법을 강구해야INI 파일의 섹션 번호를 늘리십시오.

[ColorScheme_2] 
name=Dark Purple Gradient 
BackgroundColor=224 
BackgroundBottom=2 
BackgroundTop=25 
... 

[ColorScheme_3] 
name=Retro 
BackgroundColor=5 
BackgroundBottom=21 
BackgroundTop=8 
... 

. 내가 현재 colorscheme 이름을 나열 comboBox, 그래서 사용자가 INI 파일에 저장하면 기존의 구성표가 그냥 덮어 씁니다. ComboBox 텍스트를 검사하여 기존 섹션인지 확인하고 그렇지 않으면 증가 된 이름으로 새 필드를 만드시겠습니까? (위의 예제 코드에서 ColorScheme_2ColorScheme_3이 이미 있으므로 다음 섹션을 만들려면 ColorScheme_4이됩니다).

+4

사용'ReadSections'은 물론, 섹션을 읽을 수 있습니다. 그들을 파싱하십시오. 번호를 늘리십시오. –

+1

[ColorScheme \ 2] 및 ReadSubsections와 같은 하위 섹션을 사용하여 숫자 부분 만 검색 할 수 있습니다. 하지만 가장 좋은 해결책은 콜렉션을 사용하고 콜렉션과 아이템을 Ini에 직렬화하는 것입니다. 숫자에 대해서는 걱정할 필요가 없습니다 –

답변

8

당신은 ReadSections 방법을 사용하여 모든 섹션을 읽을 수 있습니다 다음, 반환 된 문자열 목록을 반복하고는 가장 높은 FOUND 인덱스 값 저장하기 위해 각 항목을 구문 분석 :

uses 
    IniFiles; 

function GetMaxSectionIndex(const AFileName: string): Integer; 
var 
    S: string; 
    I: Integer; 
    Index: Integer; 
    IniFile: TIniFile; 
    Sections: TStringList; 
const 
    ColorScheme = 'ColorScheme_'; 
begin 
    Result := 0; 
    IniFile := TIniFile.Create(AFileName); 
    try 
    Sections := TStringList.Create; 
    try 
     IniFile.ReadSections(Sections); 
     for I := 0 to Sections.Count - 1 do 
     begin 
     S := Sections[I]; 
     if Pos(ColorScheme, S) = 1 then 
     begin 
      Delete(S, 1, Length(ColorScheme)); 
      if TryStrToInt(S, Index) then 
      if Index > Result then 
       Result := Index; 
     end; 
     end; 
    finally 
     Sections.Free; 
    end; 
    finally 
    IniFile.Free; 
    end; 
end; 

그리고 사용 :

procedure TForm1.Button1Click(Sender: TObject); 
begin 
    ShowMessage(IntToStr(GetMaxSectionIndex('d:\Config.ini'))); 
end; 
+0

'if StartsText (ColorScheme, S)'대신'StartsText (ColorScheme, S)'를 사용합니다. Pos (ColorScheme, S) = 1'이다. –

+2

@RemyLebeau, 내 델파이 버전이이 기능을 지원한다면 나도 그렇다. OP는 버전 정보를 제공하지 않았습니다. – kobik

+2

'(Ansi) StartsText()'는 Delphi 6 이후부터 사용 가능합니다. –

4

ComboBox 텍스트를 확인하여 기존 섹션인지 확인하고 그렇지 않은 경우 증가 된 이름으로 새 필드를 만들 수 있습니까? 이처럼

:

const 
    cPrefix = 'ColorScheme_'; 
var 
    Ini: TIniFile; 
    Sections: TStringList; 
    SectionName: String; 
    I, Number, MaxNumber: Integer; 
begin 
    Ini := TIniFile.Create('myfile.ini') 
    try 
    SectionName := ComboBox1.Text; 
    Sections := TStringList.Create; 
    try 
     Ini.ReadSections(Sections); 
     Sections.CaseSensitive := False; 
     if Sections.IndexOf(SectionName) = -1 then 
     begin 
     MaxNumber := 0; 
     for I := 0 to Sections.Count-1 do 
     begin 
      if StartsText(cPrefix, Sections[I]) then 
      begin 
      if TryStrToInt(Copy(Sections[I], Length(cPrefix)+1, MaxInt), Number) then 
      begin 
       if Number > MaxNumber then 
       MaxNumber := Number; 
      end; 
      end; 
     end; 
     SectionName := Format('%s%d', [cPrefix, MaxNumber+1]); 
     end; 
    finally 
     Sections.Free; 
    end; 
    // use SectionName as needed... 
    finally 
    Ini.Free; 
    end; 
end; 
+1

'Format ('% s % d', [cPrefix, MaxNumber + 1])' – kobik

+0

네, 그 부분에 복사/붙여 넣기 오타가있었습니다. –

+2

'StartsText'에 대해 들어 본 적 없어요. 만나서 반갑습니다. [+1] – TLama

관련 문제