inicol.pp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. {$mode objfpc}
  2. {$h+}
  3. unit inicol;
  4. interface
  5. Uses SysUtils,Classes,Inifiles;
  6. Type
  7. TIniCollectionItem = Class(TCollectionItem)
  8. protected
  9. function GetSectionName: String; virtual; abstract;
  10. procedure SetSectionName(const Value: String); virtual; abstract;
  11. Public
  12. Procedure SaveToIni(Ini: TCustomInifile; Section : String); Virtual; Abstract;
  13. Procedure LoadFromIni(Ini: TCustomInifile; Section : String); Virtual; Abstract;
  14. Procedure SaveToFile(FileName : String; Section : String);
  15. Procedure LoadFromFile(FileName : String; Section : String);
  16. Property SectionName : String Read GetSectionName Write SetSectionName;
  17. end;
  18. TIniCollection = Class(TCollection)
  19. private
  20. FFileName: String;
  21. FGlobalSection: String;
  22. protected
  23. FPrefix: String; // Descendent must set this.
  24. FSectionPrefix : String; // Descendent must set this too.
  25. Public
  26. Procedure Load;
  27. Procedure Save;
  28. Procedure SaveToIni(Ini: TCustomInifile; Section : String); virtual;
  29. Procedure SaveToFile(AFileName : String; Section : String);
  30. Procedure LoadFromIni(Ini: TCustomInifile; Section : String); virtual;
  31. Procedure LoadFromFile(AFileName : String; Section : String);
  32. Property Prefix : String Read FPrefix;
  33. Property SectionPrefix : String Read FSectionPrefix;
  34. Property FileName : String Read FFileName Write FFileName;
  35. Property GlobalSection : String Read FGlobalSection Write FGlobalSection;
  36. end;
  37. EIniCol = Class(Exception);
  38. Const
  39. KeyCount = 'Count';
  40. SGlobal = 'Global';
  41. implementation
  42. { TIniCollectionItem }
  43. resourcestring
  44. SErrNoFileName = '%s: Geen bestandsnaam gespecifieerd.';
  45. SErrNoSection = '%s: Geen [global] sectie gespecifieerd.';
  46. procedure TIniCollectionItem.LoadFromFile(FileName, Section: String);
  47. Var
  48. Ini : TMemInifile;
  49. begin
  50. Ini:=TMemInifile.Create(FileName);
  51. Try
  52. LoadFromIni(Ini,Section);
  53. Finally
  54. Ini.Free;
  55. end;
  56. end;
  57. procedure TIniCollectionItem.SaveToFile(FileName, Section: String);
  58. Var
  59. Ini : TMemInifile;
  60. begin
  61. Ini:=TMemInifile.Create(FileName);
  62. Try
  63. SaveToIni(Ini,Section);
  64. Ini.UpdateFile;
  65. Finally
  66. Ini.Free;
  67. end;
  68. end;
  69. { TIniCollection }
  70. procedure TIniCollection.Load;
  71. begin
  72. If (FFileName='') then
  73. Raise EIniCol.CreateFmt(SErrNoFileName,[ClassName]);
  74. If (GlobalSection='') then
  75. Raise EIniCol.CreateFmt(SErrNoSection,[ClassName]);
  76. LoadFromFile(FFileName,GlobalSection)
  77. end;
  78. procedure TIniCollection.LoadFromFile(AFileName, Section: String);
  79. Var
  80. Ini : TMemIniFile;
  81. begin
  82. Ini:=TMemInifile.Create(AFileName);
  83. Try
  84. LoadFromIni(Ini,Section);
  85. FFileName:=AFileName;
  86. FGlobalSection:=Section;
  87. Finally
  88. ini.Free;
  89. end;
  90. end;
  91. procedure TIniCollection.LoadFromIni(Ini: TCustomInifile; Section: String);
  92. Var
  93. ACount,I : Integer;
  94. N,SP : String;
  95. begin
  96. Clear;
  97. SP:=FSectionPrefix;
  98. If (SP<>'') then
  99. SP:=SP+'_';
  100. ACount:=Ini.ReadInteger(Section,KeyCount,0);
  101. For I:=1 to ACount do
  102. begin
  103. N:=Ini.ReadString(Section,Prefix+IntToStr(I),'');
  104. If (N<>'') then
  105. With Add as TIniCollectionItem do
  106. begin
  107. SectionName:=N;
  108. LoadFromIni(Ini,SP+N);
  109. end;
  110. end;
  111. end;
  112. procedure TIniCollection.Save;
  113. begin
  114. If (FFileName='') then
  115. Raise EIniCol.CreateFmt(SErrNoFileName,[ClassName]);
  116. If (GlobalSection='') then
  117. Raise EIniCol.CreateFmt(SErrNoSection,[ClassName]);
  118. SaveToFile(FFileName,GlobalSection)
  119. end;
  120. procedure TIniCollection.SaveToFile(AFileName, Section: String);
  121. Var
  122. Ini : TMemIniFile;
  123. begin
  124. Ini:=TMemInifile.Create(AFileName);
  125. Try
  126. Ini.CacheUpdates:=True;
  127. SaveToIni(Ini,Section);
  128. Ini.UpdateFile;
  129. finally
  130. Ini.Free;
  131. end;
  132. end;
  133. procedure TIniCollection.SaveToIni(Ini: TCustomInifile; Section: String);
  134. Var
  135. S,V,SP : String;
  136. I : Integer;
  137. CI : TIniCollectionItem;
  138. begin
  139. SP:=FSectionPrefix;
  140. if (SP<>'') then
  141. SP:=SP+'_';
  142. Ini.WriteInteger(Section,KeyCount,Count);
  143. For I:=0 to Count-1 do
  144. begin
  145. CI:=(Items[i]) as TIniCollectionItem;
  146. With CI do
  147. begin
  148. V:=SectionName;
  149. S:=Prefix+IntToStr(I+1);
  150. Ini.WriteString(Section,S,V);
  151. CI.SaveToIni(Ini,SP+V);
  152. end;
  153. end;
  154. end;
  155. end.