regini.inc 8.0 KB

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