السلام عليكــم ورحمـة الله وبركاتــه ،،
كيف حالكم اليوم...
جئتكم هذه المرة بفكرة للتعامل مع ملفات ini عن طريق انجاز unit سميتها myIniFileUnit تحتوي على صنفين
الاول TSections يخزن sections الموجودة في الملف
الثاني TSection يخزن الاسطر
بالاضافة الى عدة دوال للتعامل مع الملف
- {*******************************************************}
- { }
- { Simple IniFile Unit }
- { }
- { }
- {*******************************************************}
- unit MyIniFileUnit;
- interface
- uses
- IniFiles,Classes, SysUtils,contnrs;
- var
- IniFileReader: TIniFile;
- type
- TSection = class
- public
- Rows: TStringList; // this will contain TRow
- SectionName: String;
- constructor Create(const t_SectionName:String);
- Destructor Destroy;override;
- procedure InsertRow(const t_keyname:string; const t_value:string);overload;
- procedure RemoveRow(t_Index: Integer);overload;
- procedure RemoveRow(const t_KeyName: String);overload;
- procedure EditValue(t_Index:Integer;const t_NewValue:string);overload;
- procedure EditValue(const t_keyname, t_NewValue: string);overload;
- procedure RenameKey(t_Index:Integer;const t_NewName:string);
- end;
- TSections = class
- public
- Items: TObjectList; // this will contain TSection
- constructor Create;
- destructor Destroy;override;
- procedure CreateSection(const t_SectionName:string);
- procedure InsertSection(t_Section:TSection);
- procedure RemoveSection(t_Index: Integer);overload;
- procedure RemoveSection(const t_SectionName: String);overload;
- procedure RenameSection(t_Index:Integer; const t_newName: string);overload;
- procedure LoadFromFile(const FilePath: string);
- procedure SaveToFile(const FilePath: string);
- procedure InsertRow(const t_SectionName, Keyname, Value:String);
- procedure RemoveRow(const t_SectionName:string; t_Index:Integer);overload;
- procedure RemoveRow(const t_SectionName, t_KeyName: string);overload;
- procedure EditValue(const t_SectionName, t_keyname, t_NewValue:string);
- procedure RenameKey(const t_SectionName, t_oldName, t_NewName:string);
- procedure GetSectionNames(t:TStrings);
- procedure GetRows(const t_SectionName: String; t:TStrings);
- function GetSectionsCount:Integer;
- function GetRowsCount(t_Index:Integer):Integer;
- end;
- implementation
- { TSection }
- constructor TSection.Create(const t_SectionName: String);
- begin
- SectionName := t_SectionName;
- Rows := TStringList.Create;
- end;
- destructor TSection.Destroy;
- begin
- FreeAndNil(Rows);
- inherited;
- end;
- procedure TSections.GetSectionNames(t:TStrings);
- begin
- IniFileReader.ReadSections(t);
- end;
- procedure TSection.EditValue(t_Index: Integer; const t_NewValue: string);
- begin
- end;
- procedure TSection.EditValue(const t_keyname, t_NewValue: string);
- var i,epos: Integer;
- begin
- begin
- begin
- end;
- end;
- end;
- procedure TSection.InsertRow(const t_keyname, t_value: string);
- begin
- Rows.Add(t_KeyName+'='+t_value);
- end;
- procedure TSection.RemoveRow(t_Index: Integer);
- begin
- begin
- Rows.Delete(t_Index);
- end;
- end;
- procedure TSection.RemoveRow(const t_KeyName: String);
- var i, epos:integer;
- begin
- begin
- begin
- Rows.Delete(i);
- Break;
- end;
- end;
- end;
- procedure TSection.RenameKey(t_Index: Integer; const t_NewName: string);
- begin
- end;
- { TSections }
- constructor TSections.Create;
- begin
- Items := TObjectList.Create;
- end;
- procedure TSections.CreateSection(const t_SectionName: string);
- begin
- Items.Add(TSection.Create(t_SectionName));
- end;
- destructor TSections.Destroy;
- begin
- FreeAndNil(Items);
- inherited;
- end;
- procedure TSections.SaveToFile(const FilePath: string);
- var i, j, ePos: Integer;
- t_Section: TSection;
- IniFileWriter: TIniFile;
- begin
- if not FileExists(FilePath) then
- FileCreate(FilePath);
- IniFileWriter := TIniFile.Create(FilePath);
- begin
- t_Section := TSection(Items[i]);
- with t_Section do
- begin
- begin
- IniFileWriter.WriteString(
- SectionName,
- end;
- end;
- end;
- end;
- procedure TSections.InsertSection(t_Section:TSection);
- begin
- Items.Add(t_Section);
- end;
- procedure TSections.LoadFromFile(const FilePath: string);
- var
- i: Integer;
- t_Section : TSection;
- t_Sections:TStrings;
- t_Rows : TStrings;
- begin
- //the main procedure t load every thing
- t_Sections := TStringList.Create;
- t_Rows := TStringList.Create;
- if FileExists(FilePath) then
- begin
- IniFileReader := TIniFile.Create(FilePath);
- IniFileReader.ReadSections(t_Sections);
- begin
- IniFileReader.ReadSectionValues(t_Sections[i],t_Rows);
- t_Section := TSection.Create(t_Sections.Strings[i]);
- t_Section.Rows.AddStrings(t_Rows);
- InsertSection(t_Section);
- end;
- end;
- end;
- procedure TSections.RemoveSection(t_Index: Integer);
- begin
- Items.Delete(t_Index);
- end;
- procedure TSections.RenameSection(t_Index: Integer;
- const t_newName: string);
- begin
- TSection(Items.Items[t_Index]).SectionName := t_NewName;
- end;
- procedure TSections.GetRows(const t_SectionName: String; t: TStrings);
- begin
- IniFileReader.ReadSectionValues(t_SectionName, t);
- end;
- function TSections.GetSectionsCount: Integer;
- begin
- end;
- function TSections.GetRowsCount(t_Index: Integer): Integer;
- begin
- else Result := -1;
- end;
- procedure TSections.RemoveSection(const t_SectionName: String);
- var i:Integer;
- begin
- begin
- if TSection(Items.Items[i]).SectionName = t_SectionName then
- begin
- Items.Delete(i);
- Break;
- end;
- end;
- end;
- procedure TSections.RenameSection(const t_oldName, t_NewName: string);
- var i:integer;
- begin
- begin
- if TSection(Items.Items[i]).SectionName = t_oldName then
- begin
- TSection(Items.Items[i]).SectionName := t_NewName;
- Break;
- end;
- end;
- end;
- procedure TSections.EditValue(const t_SectionName, t_keyname,t_NewValue: string);
- var i:integer;
- begin
- begin
- if TSection(Items.Items[i]).SectionName = t_SectionName then
- begin
- TSection(Items.Items[i]).EditValue(t_keyname,t_NewValue);
- Break;
- end;
- end;
- end;
- procedure TSections.InsertRow(const t_SectionName, Keyname, Value: String);
- var i:integer;
- begin
- begin
- if TSection(Items.Items[i]).SectionName = t_SectionName then
- begin
- TSection(Items.Items[i]).InsertRow(keyname,value);
- Break;
- end;
- end;
- end;
- procedure TSections.RemoveRow(const t_SectionName: String; t_Index: Integer);
- var i:integer;
- begin
- begin
- if TSection(Items.Items[i]).SectionName = t_SectionName then
- begin
- TSection(Items.Items[i]).RemoveRow(t_Index);
- Break;
- end;
- end;
- end;
- procedure TSections.RenameKey(const t_SectionName, t_oldName,
- t_NewName: string);
- begin
- end;
- procedure TSections.RemoveRow(const t_SectionName, t_KeyName: string);
- var i,j:integer;
- t_Section:TSection;
- begin
- begin
- t_Section := TSection(Items.Items[i]);
- if t_Section.SectionName = t_SectionName then
- begin
- begin
- TSection(Items.Items[i]).RemoveRow(j);
- Break;
- end;
- end;
- end;
- end;
- end.
الكود مازال يحتوي على نقائص و يحتاج الى مساهماتكم... ان كانت الفكرة تستحق ان تطور :D
اما ان كانت لا تستحق فسنرميها في سلة المهملات -_-
المهم لا اطيل عليكم.. مرفق كود الوحدة و مشروع للتجربة
ارجو الفائدة للجميع :wub:
ملاجظة المشروع ليس مكتمل ساكمله لاحقا بعد ان استفيق من النوم
