regini.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. {******************************************************************************
  2. TRegIniFile
  3. ******************************************************************************}
  4. constructor TRegIniFile.Create(const FN: String);
  5. begin
  6. Create(FN, KEY_ALL_ACCESS);
  7. end;
  8. constructor TRegIniFile.Create(const FN: String;aaccess:longword);
  9. begin
  10. inherited Create(aaccess);
  11. fFileName := FN;
  12. if fFileName<>'' then begin
  13. fPath := fFileName + '\';
  14. OpenKey(fFileName, aaccess <> KEY_READ);
  15. end
  16. else
  17. fPath := '';
  18. fPreferStringValues:=True; // Delphi compatibility
  19. end;
  20. procedure TRegIniFile.DeleteKey(const Section, Ident: String);
  21. begin
  22. if OpenSection(Section) then
  23. try
  24. DeleteValue(Ident);
  25. finally
  26. CloseSection;
  27. end;
  28. end;
  29. procedure TRegIniFile.EraseSection(const Section: string);
  30. begin
  31. inherited DeleteKey(Section);
  32. end;
  33. procedure TRegIniFile.ReadSection(const Section: string; Strings: TStrings);
  34. begin
  35. if OpenSection(Section) then
  36. try
  37. GetValueNames(Strings);
  38. finally
  39. CloseSection;
  40. end;
  41. end;
  42. procedure TRegIniFile.ReadSections(Strings: TStrings);
  43. begin
  44. GetKeyNames(Strings);
  45. end;
  46. procedure TRegIniFile.ReadSectionValues(const Section: string; Strings: TStrings);
  47. var
  48. ValList : TStringList;
  49. V : String;
  50. i : Integer;
  51. begin
  52. if OpenSection(Section) then
  53. try
  54. ValList := TStringList.Create;
  55. try
  56. GetValueNames(ValList);
  57. for i:=0 to ValList.Count-1 do
  58. begin
  59. V := inherited ReadString(ValList.Strings[i]);
  60. Strings.Add(ValList.Strings[i] + '=' + V);
  61. end;
  62. finally
  63. ValList.Free;
  64. end;
  65. finally
  66. CloseSection;
  67. end;
  68. end;
  69. procedure TRegIniFile.WriteBool(const Section, Ident: string; Value: Boolean);
  70. begin
  71. if OpenSection(Section,True) then
  72. try
  73. if not fPreferStringValues then
  74. inherited WriteBool(Ident,Value)
  75. else begin
  76. if ValueExists(Ident) and (GetDataType(Ident)=rdInteger) then
  77. inherited WriteBool(Ident,Value)
  78. else
  79. inherited WriteString(Ident,BoolToStr(Value));
  80. end;
  81. finally
  82. CloseSection;
  83. end;
  84. end;
  85. procedure TRegIniFile.WriteInteger(const Section, Ident: string; Value: LongInt);
  86. begin
  87. if OpenSection(Section,True) then
  88. try
  89. if not fPreferStringValues then
  90. inherited WriteInteger(Ident,Value)
  91. else begin
  92. if ValueExists(Ident) and (GetDataType(Ident)=rdInteger) then
  93. inherited WriteInteger(Ident,Value)
  94. else
  95. inherited WriteString(Ident,IntToStr(Value));
  96. end;
  97. finally
  98. CloseSection;
  99. end;
  100. end;
  101. procedure TRegIniFile.WriteString(const Section, Ident, Value: String);
  102. begin
  103. if OpenSection(Section,True) then
  104. try
  105. inherited WriteString(Ident,Value);
  106. finally
  107. CloseSection;
  108. end;
  109. end;
  110. procedure TRegIniFile.WriteDate(const Section, Ident: string; Value: TDateTime);
  111. begin
  112. if OpenSection(Section,true) then
  113. try
  114. if not fPreferStringValues then
  115. inherited WriteDate(Ident,Value)
  116. else if ValueExists(Ident) and (GetDataType(Ident)<>rdString) then
  117. inherited WriteDate(Ident,Value)
  118. else
  119. inherited WriteString(Ident,DateToStr(Value));
  120. finally
  121. CloseKey;
  122. end;
  123. end;
  124. procedure TRegIniFile.WriteDateTime(const Section, Ident: string; Value: TDateTime);
  125. begin
  126. if OpenSection(Section,true) then
  127. try
  128. if not fPreferStringValues then
  129. inherited WriteDateTime(Ident,Value)
  130. else if ValueExists(Ident) and (GetDataType(Ident)<>rdString) then
  131. inherited WriteDateTime(Ident,Value)
  132. else
  133. inherited WriteString(Ident,DateTimeToStr(Value));
  134. finally
  135. CloseKey;
  136. end;
  137. end;
  138. procedure TRegIniFile.WriteTime(const Section, Ident: string; Value: TDateTime);
  139. begin
  140. if OpenSection(Section,true) then
  141. try
  142. if not fPreferStringValues then
  143. inherited WriteTime(Ident,Value)
  144. else if ValueExists(Ident) and (GetDataType(Ident)<>rdString) then
  145. inherited WriteTime(Ident,Value)
  146. else
  147. inherited WriteString(Ident,TimeToStr(Value));
  148. finally
  149. CloseKey;
  150. end;
  151. end;
  152. procedure TRegIniFile.WriteFloat(const Section, Ident: string; Value: Double);
  153. begin
  154. if OpenSection(Section,true) then
  155. try
  156. if not fPreferStringValues then
  157. inherited WriteFloat(Ident,Value)
  158. else if ValueExists(Ident) and (GetDataType(Ident)<>rdString) then
  159. inherited WriteFloat(Ident,Value)
  160. else
  161. inherited WriteString(Ident,FloatToStr(Value));
  162. finally
  163. CloseKey;
  164. end;
  165. end;
  166. function TRegIniFile.ReadBool(const Section, Ident: string; Default: Boolean): Boolean;
  167. begin
  168. Result := Default;
  169. if OpenSection(Section) then
  170. try
  171. if ValueExists(Ident) then
  172. if (not fPreferStringValues) or (GetDataType(Ident)=rdInteger) then
  173. Result := inherited ReadBool(Ident)
  174. else
  175. Result := StrToBool(inherited ReadString(Ident));
  176. finally
  177. CloseSection;
  178. end;
  179. end;
  180. function TRegIniFile.ReadInteger(const Section, Ident: string; Default: LongInt): LongInt;
  181. begin
  182. Result := Default;
  183. if OpenSection(Section) then
  184. try
  185. if ValueExists(Ident) then
  186. if (not fPreferStringValues) or (GetDataType(Ident)=rdInteger) then
  187. Result := inherited ReadInteger(Ident)
  188. else
  189. Result := StrToInt(inherited ReadString(Ident));
  190. finally
  191. CloseSection;
  192. end;
  193. end;
  194. function TRegIniFile.ReadString(const Section, Ident, Default: String): String;
  195. begin
  196. Result := Default;
  197. if OpenSection(Section) then
  198. try
  199. if ValueExists(Ident) then
  200. Result := inherited ReadString(Ident);
  201. finally
  202. CloseSection;
  203. end;
  204. end;
  205. function TRegIniFile.ReadDate(const Section, Ident: string; Default: TDateTime):TDateTime;
  206. begin
  207. Result := Default;
  208. if OpenSection(Section) then
  209. try
  210. if ValueExists(Ident) then
  211. if (not fPreferStringValues) or (GetDataType(Ident)<>rdString) then
  212. Result := inherited ReadDate(Ident)
  213. else
  214. Result := StrToDateDef(inherited ReadString(Ident),Result);
  215. finally
  216. CloseSection;
  217. end;
  218. end;
  219. function TRegIniFile.ReadDateTime(const Section, Ident: string; Default: TDateTime):TDateTime;
  220. begin
  221. Result := Default;
  222. if OpenSection(Section) then
  223. try
  224. if ValueExists(Ident) then
  225. if (not fPreferStringValues) or (GetDataType(Ident)<>rdString) then
  226. Result := inherited ReadDateTime(Ident)
  227. else
  228. Result := StrToDateTimeDef(inherited ReadString(Ident),Result);
  229. finally
  230. CloseSection;
  231. end;
  232. end;
  233. function TRegIniFile.ReadTime(const Section, Ident: string; Default: TDateTime):TDateTime;
  234. begin
  235. Result := Default;
  236. if OpenSection(Section) then
  237. try
  238. if ValueExists(Ident) then
  239. if (not fPreferStringValues) or (GetDataType(Ident)<>rdString) then
  240. Result := inherited ReadTime(Ident)
  241. else
  242. Result := StrToTimeDef(inherited ReadString(Ident),Result);
  243. finally
  244. CloseSection;
  245. end;
  246. end;
  247. function TRegIniFile.ReadFloat(const Section, Ident: string; Default: Double): Double;
  248. begin
  249. Result := Default;
  250. if OpenSection(Section) then
  251. try
  252. if ValueExists(Ident) then
  253. if (not fPreferStringValues) or (GetDataType(Ident)<>rdString) then
  254. Result := inherited ReadFloat(Ident)
  255. else
  256. Result := StrToFloatDef(inherited ReadString(Ident),Result);
  257. finally
  258. CloseSection;
  259. end;
  260. end;
  261. function TRegIniFile.OpenSection(const Section: string; CreateSection : Boolean = false): boolean;
  262. var
  263. k: HKEY;
  264. S : String;
  265. begin
  266. S:=Section;
  267. If (S<>'') and (S[1] = '\') then
  268. Delete(S,1,1);
  269. if CreateSection then
  270. CreateKey('\'+FPath+S);
  271. if Section <> '' then
  272. begin
  273. k:=GetKey('\'+FPath+S);
  274. if k = 0 then
  275. begin
  276. Result:=False;
  277. exit;
  278. end;
  279. SetCurrentKey(k);
  280. end;
  281. Result:=True;
  282. end;
  283. procedure TRegIniFile.CloseSection;
  284. begin
  285. CloseKey(CurrentKey);
  286. end;