HierarchicalStorage.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Common.h"
  5. #include "Exceptions.h"
  6. #include "PuttyIntf.h"
  7. #include "HierarchicalStorage.h"
  8. #include <Interface.h>
  9. #include <TextsCore.h>
  10. #include <StrUtils.hpp>
  11. #include <vector>
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. //---------------------------------------------------------------------------
  15. // ValueExists test was probably added to avoid registry exceptions when debugging
  16. #define READ_REGISTRY(Method) \
  17. if (FRegistry->ValueExists(Name)) \
  18. try { return FRegistry->Method(Name); } catch(...) { return Default; } \
  19. else return Default;
  20. #define WRITE_REGISTRY(Method) \
  21. try { FRegistry->Method(Name, Value); } catch(...) { }
  22. //---------------------------------------------------------------------------
  23. UnicodeString __fastcall MungeStr(const UnicodeString & Str, bool ForceAnsi, bool Value)
  24. {
  25. RawByteString Source;
  26. if (ForceAnsi)
  27. {
  28. Source = RawByteString(PuttyStr(Str));
  29. }
  30. else
  31. {
  32. Source = RawByteString(UTF8String(Str));
  33. if (Source.Length() > Str.Length())
  34. {
  35. Source.Insert(Bom, 1);
  36. }
  37. }
  38. strbuf * sb = strbuf_new();
  39. escape_registry_key(Source.c_str(), sb);
  40. RawByteString Dest(sb->s);
  41. strbuf_free(sb);
  42. if (Value)
  43. {
  44. // We do not want to munge * in PasswordMask
  45. Dest = ReplaceStr(Dest, L"%2A", L"*");
  46. }
  47. return UnicodeString(Dest.c_str(), Dest.Length());
  48. }
  49. //---------------------------------------------------------------------------
  50. UnicodeString __fastcall UnMungeStr(const UnicodeString & Str)
  51. {
  52. // Str should contain ASCII characters only
  53. RawByteString Source = AnsiString(Str);
  54. strbuf * sb = strbuf_new();
  55. unescape_registry_key(Source.c_str(), sb);
  56. RawByteString Dest(sb->s);
  57. strbuf_free(sb);
  58. UnicodeString Result;
  59. if (Dest.SubString(1, LENOF(Bom)) == Bom)
  60. {
  61. Dest.Delete(1, LENOF(Bom));
  62. Result = UTF8ToString(Dest);
  63. }
  64. else
  65. {
  66. Result = AnsiToString(Dest);
  67. }
  68. return Result;
  69. }
  70. //---------------------------------------------------------------------------
  71. AnsiString PuttyStr(const UnicodeString & Str)
  72. {
  73. return AnsiString(Str);
  74. }
  75. //---------------------------------------------------------------------------
  76. UnicodeString __fastcall PuttyMungeStr(const UnicodeString & Str)
  77. {
  78. return MungeStr(Str, true, false);
  79. }
  80. //---------------------------------------------------------------------------
  81. UnicodeString __fastcall MungeIniName(const UnicodeString & Str)
  82. {
  83. int P = Str.Pos(L"=");
  84. // make this fast for now
  85. if (P > 0)
  86. {
  87. return ReplaceStr(Str, L"=", L"%3D");
  88. }
  89. else
  90. {
  91. return Str;
  92. }
  93. }
  94. //---------------------------------------------------------------------------
  95. UnicodeString __fastcall UnMungeIniName(const UnicodeString & Str)
  96. {
  97. int P = Str.Pos(L"%3D");
  98. // make this fast for now
  99. if (P > 0)
  100. {
  101. return ReplaceStr(Str, L"%3D", L"=");
  102. }
  103. else
  104. {
  105. return Str;
  106. }
  107. }
  108. //===========================================================================
  109. UnicodeString AccessValueName(L"Access");
  110. UnicodeString DefaultAccessString(L"inherit");
  111. //---------------------------------------------------------------------------
  112. __fastcall THierarchicalStorage::THierarchicalStorage(const UnicodeString & AStorage)
  113. {
  114. FStorage = AStorage;
  115. AccessMode = smRead;
  116. Explicit = false;
  117. ForceSave = false;
  118. // While this was implemented in 5.0 already, for some reason
  119. // it was disabled (by mistake?). So although enabled for 5.6.1 only,
  120. // data written in Unicode/UTF8 can be read by all versions back to 5.0.
  121. ForceAnsi = false;
  122. MungeStringValues = true;
  123. FFakeReadOnlyOpens = 0;
  124. FRootAccess = -1;
  125. }
  126. //---------------------------------------------------------------------------
  127. __fastcall THierarchicalStorage::~THierarchicalStorage()
  128. {
  129. }
  130. //---------------------------------------------------------------------------
  131. void __fastcall THierarchicalStorage::Flush()
  132. {
  133. }
  134. //---------------------------------------------------------------------------
  135. void __fastcall THierarchicalStorage::SetAccessMode(TStorageAccessMode value)
  136. {
  137. FAccessMode = value;
  138. }
  139. //---------------------------------------------------------------------------
  140. UnicodeString __fastcall THierarchicalStorage::GetCurrentSubKeyMunged()
  141. {
  142. if (!FKeyHistory.empty())
  143. {
  144. return FKeyHistory.back().Key;
  145. }
  146. else
  147. {
  148. return UnicodeString();
  149. }
  150. }
  151. //---------------------------------------------------------------------------
  152. UnicodeString __fastcall THierarchicalStorage::GetCurrentSubKey()
  153. {
  154. return UnMungeStr(GetCurrentSubKeyMunged());
  155. }
  156. //---------------------------------------------------------------------------
  157. void __fastcall THierarchicalStorage::ConfigureForPutty()
  158. {
  159. MungeStringValues = false;
  160. ForceAnsi = true;
  161. }
  162. //---------------------------------------------------------------------------
  163. bool __fastcall THierarchicalStorage::OpenRootKey(bool CanCreate)
  164. {
  165. return OpenSubKey(UnicodeString(), CanCreate);
  166. }
  167. //---------------------------------------------------------------------------
  168. UnicodeString __fastcall THierarchicalStorage::MungeKeyName(const UnicodeString & Key)
  169. {
  170. UnicodeString Result = MungeStr(Key, ForceAnsi, false);
  171. // if there's already ANSI-munged subkey, keep ANSI munging
  172. if ((Result != Key) && !ForceAnsi && CanRead() && DoKeyExists(Key, true))
  173. {
  174. Result = MungeStr(Key, true, false);
  175. }
  176. return Result;
  177. }
  178. //---------------------------------------------------------------------------
  179. UnicodeString __fastcall THierarchicalStorage::DoReadRootAccessString()
  180. {
  181. UnicodeString Result;
  182. if (OpenRootKey(false))
  183. {
  184. Result = ReadAccessString();
  185. CloseSubKey();
  186. }
  187. else
  188. {
  189. Result = DefaultAccessString;
  190. }
  191. return Result;
  192. }
  193. //---------------------------------------------------------------------------
  194. UnicodeString __fastcall THierarchicalStorage::ReadAccessString()
  195. {
  196. UnicodeString Result;
  197. if (!FKeyHistory.empty())
  198. {
  199. Result = ReadString(AccessValueName, DefaultAccessString);
  200. }
  201. else
  202. {
  203. Result = DoReadRootAccessString();
  204. }
  205. return Result;
  206. }
  207. //---------------------------------------------------------------------------
  208. unsigned int __fastcall THierarchicalStorage::ReadAccess(unsigned int CurrentAccess)
  209. {
  210. UnicodeString Access = ReadAccessString();
  211. unsigned int Result = 0;
  212. while (!Access.IsEmpty())
  213. {
  214. UnicodeString Token = CutToChar(Access, L',', true);
  215. if (SameText(Token, L"inherit"))
  216. {
  217. Result |= CurrentAccess;
  218. }
  219. else if (SameText(Token, "read"))
  220. {
  221. Result |= hsaRead;
  222. }
  223. else if (SameText(Token, "full"))
  224. {
  225. Result |= hsaRead | hsaWrite;
  226. }
  227. }
  228. return Result;
  229. }
  230. //---------------------------------------------------------------------------
  231. unsigned int __fastcall THierarchicalStorage::GetCurrentAccess()
  232. {
  233. unsigned int Result;
  234. if (!FKeyHistory.empty())
  235. {
  236. // We must have resolved root access when opening the sub key the latest.
  237. DebugAssert(FRootAccess >= 0);
  238. Result = FKeyHistory.back().Access;
  239. }
  240. else
  241. {
  242. if (FRootAccess < 0)
  243. {
  244. FRootAccess = hsaRead; // Prevent recursion to allow reading the access
  245. FRootAccess = ReadAccess(hsaRead | hsaWrite);
  246. }
  247. Result = FRootAccess;
  248. }
  249. return Result;
  250. }
  251. //---------------------------------------------------------------------------
  252. bool __fastcall THierarchicalStorage::OpenSubKeyPath(const UnicodeString & KeyPath, bool CanCreate)
  253. {
  254. DebugAssert(!KeyPath.IsEmpty() && (KeyPath[KeyPath.Length()] != L'\\'));
  255. bool Result;
  256. UnicodeString Buf(KeyPath);
  257. int Opens = 0;
  258. while (!Buf.IsEmpty())
  259. {
  260. UnicodeString SubKey = CutToChar(Buf, L'\\', false);
  261. Result = OpenSubKey(SubKey, CanCreate);
  262. if (Result)
  263. {
  264. Opens++;
  265. }
  266. }
  267. if (Result)
  268. {
  269. FKeyHistory.back().Levels = Opens;
  270. }
  271. else
  272. {
  273. while (Opens > 0)
  274. {
  275. CloseSubKey();
  276. Opens--;
  277. }
  278. }
  279. return Result;
  280. }
  281. //---------------------------------------------------------------------------
  282. bool __fastcall THierarchicalStorage::OpenSubKey(const UnicodeString & Key, bool CanCreate)
  283. {
  284. UnicodeString MungedKey = MungeKeyName(Key);
  285. bool Result;
  286. unsigned int InheritAccess;
  287. unsigned int Access;
  288. // For the first open, CanWrite > GetCurrentAccess > ReadAccess has a (needed) side effect of caching root access.
  289. if (!CanWrite() && CanCreate && !KeyExists(MungedKey))
  290. {
  291. InheritAccess = Access = 0; // do not even try to read the access, as the key is actually not opened
  292. FFakeReadOnlyOpens++;
  293. Result = true;
  294. }
  295. else
  296. {
  297. Access = hsaRead; // allow reading the access
  298. InheritAccess = GetCurrentAccess();
  299. Result = DoOpenSubKey(MungedKey, CanCreate);
  300. }
  301. if (Result)
  302. {
  303. TKeyEntry Entry;
  304. Entry.Key = IncludeTrailingBackslash(CurrentSubKey + MungedKey);
  305. Entry.Levels = 1;
  306. Entry.Access = Access;
  307. FKeyHistory.push_back(Entry);
  308. // Read the real access only now, that the key is finally opened (it's in FKeyHistory)
  309. FKeyHistory.back().Access = ReadAccess(InheritAccess);
  310. }
  311. return Result;
  312. }
  313. //---------------------------------------------------------------------------
  314. void __fastcall THierarchicalStorage::CloseSubKeyPath()
  315. {
  316. if (FKeyHistory.empty())
  317. {
  318. throw Exception(UnicodeString());
  319. }
  320. int Levels = FKeyHistory.back().Levels;
  321. FKeyHistory.back().Levels = 1; // to satify the assertion in CloseSubKey()
  322. while (Levels > 0)
  323. {
  324. CloseSubKey();
  325. Levels--;
  326. DebugAssert((Levels == 0) || (FKeyHistory.back().Levels == 1));
  327. }
  328. }
  329. //---------------------------------------------------------------------------
  330. void __fastcall THierarchicalStorage::CloseSubKey()
  331. {
  332. if (FKeyHistory.empty())
  333. {
  334. throw Exception(UnicodeString());
  335. }
  336. DebugAssert(FKeyHistory.back().Levels == 1);
  337. FKeyHistory.pop_back();
  338. if (FFakeReadOnlyOpens > 0)
  339. {
  340. FFakeReadOnlyOpens--;
  341. }
  342. else
  343. {
  344. DoCloseSubKey();
  345. }
  346. }
  347. //---------------------------------------------------------------------------
  348. void __fastcall THierarchicalStorage::CloseAll()
  349. {
  350. while (!CurrentSubKey.IsEmpty())
  351. {
  352. CloseSubKeyPath();
  353. }
  354. }
  355. //---------------------------------------------------------------------------
  356. void __fastcall THierarchicalStorage::ClearSubKeys()
  357. {
  358. std::unique_ptr<TStringList> SubKeys(new TStringList());
  359. GetSubKeyNames(SubKeys.get());
  360. for (int Index = 0; Index < SubKeys->Count; Index++)
  361. {
  362. RecursiveDeleteSubKey(SubKeys->Strings[Index]);
  363. }
  364. }
  365. //---------------------------------------------------------------------------
  366. void __fastcall THierarchicalStorage::RecursiveDeleteSubKey(const UnicodeString & Key)
  367. {
  368. bool CanWriteParent = CanWrite();
  369. if (OpenSubKey(Key, false))
  370. {
  371. ClearSubKeys();
  372. // Cannot delete the key itself, but can delete its contents, so at least delete the values
  373. // (which would otherwise be deleted implicitly by DoDeleteSubKey)
  374. if (!CanWriteParent && CanWrite())
  375. {
  376. ClearValues();
  377. }
  378. CloseSubKey();
  379. // Only if all subkeys were successfully deleted in ClearSubKeys
  380. if (CanWriteParent && HasSubKeys())
  381. {
  382. DoDeleteSubKey(Key);
  383. }
  384. }
  385. }
  386. //---------------------------------------------------------------------------
  387. bool __fastcall THierarchicalStorage::HasSubKeys()
  388. {
  389. std::unique_ptr<TStrings> SubKeys(new TStringList());
  390. GetSubKeyNames(SubKeys.get());
  391. bool Result = (SubKeys->Count > 0);
  392. return Result;
  393. }
  394. //---------------------------------------------------------------------------
  395. bool __fastcall THierarchicalStorage::DeleteValue(const UnicodeString & Name)
  396. {
  397. if (CanWrite())
  398. {
  399. return DoDeleteValue(Name);
  400. }
  401. else
  402. {
  403. return false;
  404. }
  405. }
  406. //---------------------------------------------------------------------------
  407. bool __fastcall THierarchicalStorage::KeyExists(const UnicodeString & SubKey)
  408. {
  409. if (CanRead())
  410. {
  411. return DoKeyExists(SubKey, ForceAnsi);
  412. }
  413. else
  414. {
  415. return false;
  416. }
  417. }
  418. //---------------------------------------------------------------------------
  419. bool __fastcall THierarchicalStorage::ValueExists(const UnicodeString & Value)
  420. {
  421. if (CanRead())
  422. {
  423. return DoValueExists(Value);
  424. }
  425. else
  426. {
  427. return false;
  428. }
  429. }
  430. //---------------------------------------------------------------------------
  431. void __fastcall THierarchicalStorage::ReadValues(TStrings * Strings, bool MaintainKeys)
  432. {
  433. std::unique_ptr<TStrings> Names(new TStringList());
  434. GetValueNames(Names.get());
  435. for (int Index = 0; Index < Names->Count; Index++)
  436. {
  437. if (MaintainKeys)
  438. {
  439. Strings->Add(FORMAT(L"%s=%s", (Names->Strings[Index], ReadString(Names->Strings[Index], UnicodeString()))));
  440. }
  441. else
  442. {
  443. Strings->Add(ReadString(Names->Strings[Index], UnicodeString()));
  444. }
  445. }
  446. }
  447. //---------------------------------------------------------------------------
  448. void __fastcall THierarchicalStorage::ClearValues()
  449. {
  450. std::unique_ptr<TStrings> Names(new TStringList());
  451. GetValueNames(Names.get());
  452. for (int Index = 0; Index < Names->Count; Index++)
  453. {
  454. DeleteValue(Names->Strings[Index]);
  455. }
  456. }
  457. //---------------------------------------------------------------------------
  458. void __fastcall THierarchicalStorage::WriteValues(TStrings * Strings, bool MaintainKeys)
  459. {
  460. ClearValues();
  461. if (Strings != NULL)
  462. {
  463. for (int Index = 0; Index < Strings->Count; Index++)
  464. {
  465. if (MaintainKeys)
  466. {
  467. DebugAssert(Strings->Strings[Index].Pos(L"=") > 1);
  468. WriteString(Strings->Names[Index], Strings->Values[Strings->Names[Index]]);
  469. }
  470. else
  471. {
  472. WriteString(IntToStr(Index), Strings->Strings[Index]);
  473. }
  474. }
  475. }
  476. }
  477. //---------------------------------------------------------------------------
  478. bool __fastcall THierarchicalStorage::HasAccess(unsigned int Access)
  479. {
  480. return
  481. FLAGSET(GetCurrentAccess(), Access) &&
  482. // There should never be any kind of access to a non-existent key
  483. DebugAlwaysTrue(FFakeReadOnlyOpens == 0);
  484. }
  485. //---------------------------------------------------------------------------
  486. bool __fastcall THierarchicalStorage::CanRead()
  487. {
  488. return HasAccess(hsaRead);
  489. }
  490. //---------------------------------------------------------------------------
  491. void __fastcall THierarchicalStorage::GetSubKeyNames(TStrings * Strings)
  492. {
  493. if (CanRead())
  494. {
  495. DoGetSubKeyNames(Strings);
  496. }
  497. else
  498. {
  499. Strings->Clear();
  500. }
  501. }
  502. //---------------------------------------------------------------------------
  503. void __fastcall THierarchicalStorage::GetValueNames(TStrings * Strings)
  504. {
  505. if (CanRead())
  506. {
  507. DoGetValueNames(Strings);
  508. int Index = 0;
  509. while (Index < Strings->Count)
  510. {
  511. if (SameText(Strings->Strings[Index], AccessValueName))
  512. {
  513. Strings->Delete(Index);
  514. }
  515. else
  516. {
  517. Index++;
  518. }
  519. }
  520. }
  521. else
  522. {
  523. Strings->Clear();
  524. }
  525. }
  526. //---------------------------------------------------------------------------
  527. bool __fastcall THierarchicalStorage::ReadBool(const UnicodeString & Name, bool Default)
  528. {
  529. if (CanRead())
  530. {
  531. return DoReadBool(Name, Default);
  532. }
  533. else
  534. {
  535. return Default;
  536. }
  537. }
  538. //---------------------------------------------------------------------------
  539. int __fastcall THierarchicalStorage::ReadInteger(const UnicodeString & Name, int Default)
  540. {
  541. if (CanRead())
  542. {
  543. return DoReadInteger(Name, Default);
  544. }
  545. else
  546. {
  547. return Default;
  548. }
  549. }
  550. //---------------------------------------------------------------------------
  551. __int64 __fastcall THierarchicalStorage::ReadInt64(const UnicodeString & Name, __int64 Default)
  552. {
  553. if (CanRead())
  554. {
  555. return DoReadInt64(Name, Default);
  556. }
  557. else
  558. {
  559. return Default;
  560. }
  561. }
  562. //---------------------------------------------------------------------------
  563. TDateTime __fastcall THierarchicalStorage::ReadDateTime(const UnicodeString & Name, TDateTime Default)
  564. {
  565. if (CanRead())
  566. {
  567. return DoReadDateTime(Name, Default);
  568. }
  569. else
  570. {
  571. return Default;
  572. }
  573. }
  574. //---------------------------------------------------------------------------
  575. double __fastcall THierarchicalStorage::ReadFloat(const UnicodeString & Name, double Default)
  576. {
  577. if (CanRead())
  578. {
  579. return DoReadFloat(Name, Default);
  580. }
  581. else
  582. {
  583. return Default;
  584. }
  585. }
  586. //---------------------------------------------------------------------------
  587. UnicodeString __fastcall THierarchicalStorage::ReadStringRaw(const UnicodeString & Name, const UnicodeString & Default)
  588. {
  589. if (CanRead())
  590. {
  591. return DoReadStringRaw(Name, Default);
  592. }
  593. else
  594. {
  595. return Default;
  596. }
  597. }
  598. //---------------------------------------------------------------------------
  599. size_t __fastcall THierarchicalStorage::ReadBinaryData(const UnicodeString & Name, void * Buffer, size_t Size)
  600. {
  601. if (CanRead())
  602. {
  603. return DoReadBinaryData(Name, Buffer, Size);
  604. }
  605. else
  606. {
  607. return 0;
  608. }
  609. }
  610. //---------------------------------------------------------------------------
  611. UnicodeString __fastcall THierarchicalStorage::ReadString(const UnicodeString & Name, const UnicodeString & Default)
  612. {
  613. UnicodeString Result;
  614. if (CanRead())
  615. {
  616. if (MungeStringValues)
  617. {
  618. Result = UnMungeStr(ReadStringRaw(Name, MungeStr(Default, ForceAnsi, true)));
  619. }
  620. else
  621. {
  622. Result = ReadStringRaw(Name, Default);
  623. }
  624. }
  625. else
  626. {
  627. Result = Default;
  628. }
  629. return Result;
  630. }
  631. //---------------------------------------------------------------------------
  632. size_t __fastcall THierarchicalStorage::BinaryDataSize(const UnicodeString & Name)
  633. {
  634. if (CanRead())
  635. {
  636. return DoBinaryDataSize(Name);
  637. }
  638. else
  639. {
  640. return 0;
  641. }
  642. }
  643. //---------------------------------------------------------------------------
  644. RawByteString __fastcall THierarchicalStorage::ReadBinaryData(const UnicodeString & Name)
  645. {
  646. size_t Size = BinaryDataSize(Name);
  647. RawByteString Value;
  648. Value.SetLength(Size);
  649. ReadBinaryData(Name, Value.c_str(), Size);
  650. return Value;
  651. }
  652. //---------------------------------------------------------------------------
  653. RawByteString __fastcall THierarchicalStorage::ReadStringAsBinaryData(const UnicodeString & Name, const RawByteString & Default)
  654. {
  655. UnicodeString UnicodeDefault = AnsiToString(Default);
  656. // This should be exactly the same operation as calling ReadString in
  657. // C++Builder 6 (non-Unicode) on Unicode-based OS
  658. // (conversion is done by Ansi layer of the OS)
  659. UnicodeString String = ReadString(Name, UnicodeDefault);
  660. AnsiString Ansi = AnsiString(String);
  661. RawByteString Result = RawByteString(Ansi.c_str(), Ansi.Length());
  662. return Result;
  663. }
  664. //---------------------------------------------------------------------------
  665. bool __fastcall THierarchicalStorage::CanWrite()
  666. {
  667. return HasAccess(hsaWrite);
  668. }
  669. //---------------------------------------------------------------------------
  670. void __fastcall THierarchicalStorage::WriteBool(const UnicodeString & Name, bool Value)
  671. {
  672. if (CanWrite())
  673. {
  674. DoWriteBool(Name, Value);
  675. }
  676. }
  677. //---------------------------------------------------------------------------
  678. void __fastcall THierarchicalStorage::WriteStringRaw(const UnicodeString & Name, const UnicodeString & Value)
  679. {
  680. if (CanWrite())
  681. {
  682. DoWriteStringRaw(Name, Value);
  683. }
  684. }
  685. //---------------------------------------------------------------------------
  686. void __fastcall THierarchicalStorage::WriteInteger(const UnicodeString & Name, int Value)
  687. {
  688. if (CanWrite())
  689. {
  690. DoWriteInteger(Name, Value);
  691. }
  692. }
  693. //---------------------------------------------------------------------------
  694. void __fastcall THierarchicalStorage::WriteInt64(const UnicodeString & Name, __int64 Value)
  695. {
  696. if (CanWrite())
  697. {
  698. DoWriteInt64(Name, Value);
  699. }
  700. }
  701. //---------------------------------------------------------------------------
  702. void __fastcall THierarchicalStorage::WriteDateTime(const UnicodeString & Name, TDateTime Value)
  703. {
  704. if (CanWrite())
  705. {
  706. // TRegistry.WriteDateTime does this internally
  707. DoWriteBinaryData(Name, &Value, sizeof(Value));
  708. }
  709. }
  710. //---------------------------------------------------------------------------
  711. void __fastcall THierarchicalStorage::WriteFloat(const UnicodeString & Name, double Value)
  712. {
  713. if (CanWrite())
  714. {
  715. // TRegistry.WriteFloat does this internally
  716. DoWriteBinaryData(Name, &Value, sizeof(Value));
  717. }
  718. }
  719. //---------------------------------------------------------------------------
  720. void __fastcall THierarchicalStorage::WriteString(const UnicodeString & Name, const UnicodeString & Value)
  721. {
  722. if (MungeStringValues)
  723. {
  724. WriteStringRaw(Name, MungeStr(Value, ForceAnsi, true));
  725. }
  726. else
  727. {
  728. WriteStringRaw(Name, Value);
  729. }
  730. }
  731. //---------------------------------------------------------------------------
  732. void __fastcall THierarchicalStorage::WriteBinaryData(const UnicodeString & Name, const void * Buffer, int Size)
  733. {
  734. if (CanWrite())
  735. {
  736. DoWriteBinaryData(Name, Buffer, Size);
  737. }
  738. }
  739. //---------------------------------------------------------------------------
  740. void __fastcall THierarchicalStorage::WriteBinaryData(const UnicodeString & Name, const RawByteString & Value)
  741. {
  742. WriteBinaryData(Name, Value.c_str(), Value.Length());
  743. }
  744. //---------------------------------------------------------------------------
  745. void __fastcall THierarchicalStorage::WriteBinaryDataAsString(const UnicodeString & Name, const RawByteString & Value)
  746. {
  747. // This should be exactly the same operation as calling WriteString in
  748. // C++Builder 6 (non-Unicode) on Unicode-based OS
  749. // (conversion is done by Ansi layer of the OS)
  750. WriteString(Name, AnsiToString(Value));
  751. }
  752. //---------------------------------------------------------------------------
  753. UnicodeString __fastcall THierarchicalStorage::IncludeTrailingBackslash(const UnicodeString & S)
  754. {
  755. // expanded from ?: as it caused memory leaks
  756. if (S.IsEmpty())
  757. {
  758. return S;
  759. }
  760. else
  761. {
  762. return ::IncludeTrailingBackslash(S);
  763. }
  764. }
  765. //---------------------------------------------------------------------------
  766. UnicodeString __fastcall THierarchicalStorage::ExcludeTrailingBackslash(const UnicodeString & S)
  767. {
  768. // expanded from ?: as it caused memory leaks
  769. if (S.IsEmpty())
  770. {
  771. return S;
  772. }
  773. else
  774. {
  775. return ::ExcludeTrailingBackslash(S);
  776. }
  777. }
  778. //---------------------------------------------------------------------------
  779. bool __fastcall THierarchicalStorage::GetTemporary()
  780. {
  781. return false;
  782. }
  783. //===========================================================================
  784. __fastcall TRegistryStorage::TRegistryStorage(const UnicodeString & AStorage) :
  785. THierarchicalStorage(IncludeTrailingBackslash(AStorage))
  786. {
  787. FWowMode = 0;
  788. Init();
  789. };
  790. //---------------------------------------------------------------------------
  791. __fastcall TRegistryStorage::TRegistryStorage(const UnicodeString & AStorage, HKEY ARootKey, REGSAM WowMode):
  792. THierarchicalStorage(IncludeTrailingBackslash(AStorage))
  793. {
  794. FWowMode = WowMode;
  795. Init();
  796. FRegistry->RootKey = ARootKey;
  797. }
  798. //---------------------------------------------------------------------------
  799. void __fastcall TRegistryStorage::Init()
  800. {
  801. FRegistry = new TRegistry();
  802. FRegistry->Access = KEY_READ | FWowMode;
  803. }
  804. //---------------------------------------------------------------------------
  805. __fastcall TRegistryStorage::~TRegistryStorage()
  806. {
  807. delete FRegistry;
  808. };
  809. //---------------------------------------------------------------------------
  810. // Used only in OpenSessionInPutty
  811. bool __fastcall TRegistryStorage::Copy(TRegistryStorage * Storage)
  812. {
  813. TRegistry * Registry = Storage->FRegistry;
  814. bool Result = true;
  815. std::unique_ptr<TStrings> Names(new TStringList());
  816. Registry->GetValueNames(Names.get());
  817. std::vector<unsigned char> Buffer(1024, 0);
  818. int Index = 0;
  819. while ((Index < Names->Count) && Result)
  820. {
  821. UnicodeString Name = MungeStr(Names->Strings[Index], ForceAnsi, false);
  822. unsigned long Size = Buffer.size();
  823. unsigned long Type;
  824. int RegResult;
  825. do
  826. {
  827. RegResult = RegQueryValueEx(Registry->CurrentKey, Name.c_str(), NULL, &Type, &Buffer[0], &Size);
  828. if (RegResult == ERROR_MORE_DATA)
  829. {
  830. Buffer.resize(Size);
  831. }
  832. } while (RegResult == ERROR_MORE_DATA);
  833. Result = (RegResult == ERROR_SUCCESS);
  834. if (Result)
  835. {
  836. RegResult = RegSetValueEx(FRegistry->CurrentKey, Name.c_str(), NULL, Type, &Buffer[0], Size);
  837. Result = (RegResult == ERROR_SUCCESS);
  838. }
  839. ++Index;
  840. }
  841. return Result;
  842. }
  843. //---------------------------------------------------------------------------
  844. UnicodeString __fastcall TRegistryStorage::GetSource()
  845. {
  846. return RootKeyToStr(FRegistry->RootKey) + L"\\" + Storage;
  847. }
  848. //---------------------------------------------------------------------------
  849. void __fastcall TRegistryStorage::SetAccessMode(TStorageAccessMode value)
  850. {
  851. THierarchicalStorage::SetAccessMode(value);
  852. if (FRegistry)
  853. {
  854. switch (AccessMode) {
  855. case smRead:
  856. FRegistry->Access = KEY_READ | FWowMode;
  857. break;
  858. case smReadWrite:
  859. default:
  860. FRegistry->Access = KEY_READ | KEY_WRITE | FWowMode;
  861. break;
  862. }
  863. }
  864. }
  865. //---------------------------------------------------------------------------
  866. bool __fastcall TRegistryStorage::DoOpenSubKey(const UnicodeString & SubKey, bool CanCreate)
  867. {
  868. UnicodeString PrevPath;
  869. bool WasOpened = (FRegistry->CurrentKey != NULL);
  870. if (WasOpened)
  871. {
  872. PrevPath = FRegistry->CurrentPath;
  873. DebugAssert(SamePaths(PrevPath, Storage + GetCurrentSubKeyMunged()));
  874. FRegistry->CloseKey();
  875. }
  876. UnicodeString K = ExcludeTrailingBackslash(Storage + CurrentSubKey + SubKey);
  877. bool Result = FRegistry->OpenKey(K, CanCreate);
  878. if (!Result && WasOpened)
  879. {
  880. FRegistry->OpenKey(PrevPath, false);
  881. }
  882. return Result;
  883. }
  884. //---------------------------------------------------------------------------
  885. void __fastcall TRegistryStorage::DoCloseSubKey()
  886. {
  887. FRegistry->CloseKey();
  888. if (!FKeyHistory.empty())
  889. {
  890. FRegistry->OpenKey(Storage + GetCurrentSubKeyMunged(), True);
  891. }
  892. }
  893. //---------------------------------------------------------------------------
  894. void __fastcall TRegistryStorage::DoDeleteSubKey(const UnicodeString & SubKey)
  895. {
  896. UnicodeString K;
  897. if (FKeyHistory.empty())
  898. {
  899. K = Storage + CurrentSubKey;
  900. }
  901. K += MungeKeyName(SubKey);
  902. FRegistry->DeleteKey(K);
  903. }
  904. //---------------------------------------------------------------------------
  905. void __fastcall TRegistryStorage::DoGetSubKeyNames(TStrings * Strings)
  906. {
  907. FRegistry->GetKeyNames(Strings);
  908. for (int Index = 0; Index < Strings->Count; Index++)
  909. {
  910. Strings->Strings[Index] = UnMungeStr(Strings->Strings[Index]);
  911. }
  912. }
  913. //---------------------------------------------------------------------------
  914. void __fastcall TRegistryStorage::DoGetValueNames(TStrings * Strings)
  915. {
  916. FRegistry->GetValueNames(Strings);
  917. }
  918. //---------------------------------------------------------------------------
  919. bool __fastcall TRegistryStorage::DoDeleteValue(const UnicodeString & Name)
  920. {
  921. return FRegistry->DeleteValue(Name);
  922. }
  923. //---------------------------------------------------------------------------
  924. bool __fastcall TRegistryStorage::DoKeyExists(const UnicodeString & SubKey, bool AForceAnsi)
  925. {
  926. UnicodeString K = MungeStr(SubKey, AForceAnsi, false);
  927. bool Result = FRegistry->KeyExists(K);
  928. return Result;
  929. }
  930. //---------------------------------------------------------------------------
  931. bool __fastcall TRegistryStorage::DoValueExists(const UnicodeString & Value)
  932. {
  933. bool Result = FRegistry->ValueExists(Value);
  934. return Result;
  935. }
  936. //---------------------------------------------------------------------------
  937. size_t __fastcall TRegistryStorage::DoBinaryDataSize(const UnicodeString & Name)
  938. {
  939. size_t Result = FRegistry->GetDataSize(Name);
  940. return Result;
  941. }
  942. //---------------------------------------------------------------------------
  943. bool __fastcall TRegistryStorage::DoReadBool(const UnicodeString & Name, bool Default)
  944. {
  945. READ_REGISTRY(ReadBool);
  946. }
  947. //---------------------------------------------------------------------------
  948. TDateTime __fastcall TRegistryStorage::DoReadDateTime(const UnicodeString & Name, TDateTime Default)
  949. {
  950. // Internally does what would DoReadBinaryData do (like in DoReadInt64)
  951. READ_REGISTRY(ReadDateTime);
  952. }
  953. //---------------------------------------------------------------------------
  954. double __fastcall TRegistryStorage::DoReadFloat(const UnicodeString & Name, double Default)
  955. {
  956. // Internally does what would DoReadBinaryData do (like in DoReadInt64)
  957. READ_REGISTRY(ReadFloat);
  958. }
  959. //---------------------------------------------------------------------------
  960. int __fastcall TRegistryStorage::DoReadInteger(const UnicodeString & Name, int Default)
  961. {
  962. READ_REGISTRY(ReadInteger);
  963. }
  964. //---------------------------------------------------------------------------
  965. __int64 __fastcall TRegistryStorage::DoReadInt64(const UnicodeString & Name, __int64 Default)
  966. {
  967. __int64 Result;
  968. if (DoReadBinaryData(Name, &Result, sizeof(Result)) == 0)
  969. {
  970. Result = Default;
  971. }
  972. return Result;
  973. }
  974. //---------------------------------------------------------------------------
  975. UnicodeString __fastcall TRegistryStorage::DoReadStringRaw(const UnicodeString & Name, const UnicodeString & Default)
  976. {
  977. READ_REGISTRY(ReadString);
  978. }
  979. //---------------------------------------------------------------------------
  980. size_t __fastcall TRegistryStorage::DoReadBinaryData(const UnicodeString & Name, void * Buffer, size_t Size)
  981. {
  982. size_t Result;
  983. if (FRegistry->ValueExists(Name))
  984. {
  985. try
  986. {
  987. Result = FRegistry->ReadBinaryData(Name, Buffer, Size);
  988. }
  989. catch(...)
  990. {
  991. Result = 0;
  992. }
  993. }
  994. else
  995. {
  996. Result = 0;
  997. }
  998. return Result;
  999. }
  1000. //---------------------------------------------------------------------------
  1001. void __fastcall TRegistryStorage::DoWriteBool(const UnicodeString & Name, bool Value)
  1002. {
  1003. WRITE_REGISTRY(WriteBool);
  1004. }
  1005. //---------------------------------------------------------------------------
  1006. void __fastcall TRegistryStorage::DoWriteStringRaw(const UnicodeString & Name, const UnicodeString & Value)
  1007. {
  1008. WRITE_REGISTRY(WriteString);
  1009. }
  1010. //---------------------------------------------------------------------------
  1011. void __fastcall TRegistryStorage::DoWriteInteger(const UnicodeString & Name, int Value)
  1012. {
  1013. WRITE_REGISTRY(WriteInteger);
  1014. }
  1015. //---------------------------------------------------------------------------
  1016. void __fastcall TRegistryStorage::DoWriteInt64(const UnicodeString & Name, __int64 Value)
  1017. {
  1018. WriteBinaryData(Name, &Value, sizeof(Value));
  1019. }
  1020. //---------------------------------------------------------------------------
  1021. void __fastcall TRegistryStorage::DoWriteBinaryData(const UnicodeString & Name, const void * Buffer, int Size)
  1022. {
  1023. try
  1024. {
  1025. FRegistry->WriteBinaryData(Name, const_cast<void *>(Buffer), Size);
  1026. }
  1027. catch(...)
  1028. {
  1029. }
  1030. }
  1031. //===========================================================================
  1032. __fastcall TCustomIniFileStorage::TCustomIniFileStorage(const UnicodeString & Storage, TCustomIniFile * IniFile) :
  1033. THierarchicalStorage(Storage),
  1034. FIniFile(IniFile),
  1035. FMasterStorageOpenFailures(0),
  1036. FOpeningSubKey(false)
  1037. {
  1038. }
  1039. //---------------------------------------------------------------------------
  1040. __fastcall TCustomIniFileStorage::~TCustomIniFileStorage()
  1041. {
  1042. delete FIniFile;
  1043. }
  1044. //---------------------------------------------------------------------------
  1045. UnicodeString __fastcall TCustomIniFileStorage::GetSource()
  1046. {
  1047. return Storage;
  1048. }
  1049. //---------------------------------------------------------------------------
  1050. UnicodeString __fastcall TCustomIniFileStorage::GetCurrentSection()
  1051. {
  1052. return ExcludeTrailingBackslash(GetCurrentSubKeyMunged());
  1053. }
  1054. //---------------------------------------------------------------------------
  1055. void __fastcall TCustomIniFileStorage::CacheSections()
  1056. {
  1057. if (FSections.get() == NULL)
  1058. {
  1059. FSections.reset(new TStringList());
  1060. FIniFile->ReadSections(FSections.get());
  1061. FSections->Sorted = true; // has to set only after reading as ReadSections reset it to false
  1062. }
  1063. }
  1064. //---------------------------------------------------------------------------
  1065. void __fastcall TCustomIniFileStorage::ResetCache()
  1066. {
  1067. FSections.reset(NULL);
  1068. }
  1069. //---------------------------------------------------------------------------
  1070. void __fastcall TCustomIniFileStorage::SetAccessMode(TStorageAccessMode value)
  1071. {
  1072. if (FMasterStorage.get() != NULL)
  1073. {
  1074. FMasterStorage->AccessMode = value;
  1075. }
  1076. THierarchicalStorage::SetAccessMode(value);
  1077. }
  1078. //---------------------------------------------------------------------------
  1079. bool __fastcall TCustomIniFileStorage::DoKeyExistsInternal(const UnicodeString & SubKey)
  1080. {
  1081. CacheSections();
  1082. bool Result = false;
  1083. UnicodeString NewKey = ExcludeTrailingBackslash(CurrentSubKey + SubKey);
  1084. if (FSections->Count > 0)
  1085. {
  1086. int Index = -1;
  1087. Result = FSections->Find(NewKey, Index);
  1088. if (!Result &&
  1089. (Index < FSections->Count) &&
  1090. (FSections->Strings[Index].SubString(1, NewKey.Length()+1) == NewKey + L"\\"))
  1091. {
  1092. Result = true;
  1093. }
  1094. }
  1095. return Result;
  1096. }
  1097. //---------------------------------------------------------------------------
  1098. bool __fastcall TCustomIniFileStorage::DoOpenSubKey(const UnicodeString & SubKey, bool CanCreate)
  1099. {
  1100. bool Result =
  1101. CanCreate ||
  1102. DoKeyExistsInternal(SubKey);
  1103. return Result;
  1104. }
  1105. //---------------------------------------------------------------------------
  1106. bool __fastcall TCustomIniFileStorage::OpenRootKey(bool CanCreate)
  1107. {
  1108. // Not supported with master storage.
  1109. // Actually currently, we use OpenRootKey with TRegistryStorage only.
  1110. DebugAssert(FMasterStorage.get() == NULL);
  1111. return THierarchicalStorage::OpenRootKey(CanCreate);
  1112. }
  1113. //---------------------------------------------------------------------------
  1114. bool __fastcall TCustomIniFileStorage::OpenSubKey(const UnicodeString & Key, bool CanCreate)
  1115. {
  1116. bool Result;
  1117. // To cache root access in advance, otherwise we end up calling outselves, what TAutoFlag does not like
  1118. GetCurrentAccess();
  1119. {
  1120. TAutoFlag Flag(FOpeningSubKey);
  1121. Result = THierarchicalStorage::OpenSubKey(Key, CanCreate);
  1122. }
  1123. if (FMasterStorage.get() != NULL)
  1124. {
  1125. if (FMasterStorageOpenFailures > 0)
  1126. {
  1127. FMasterStorageOpenFailures++;
  1128. }
  1129. else
  1130. {
  1131. bool MasterResult = FMasterStorage->OpenSubKey(Key, CanCreate);
  1132. if (!Result && MasterResult)
  1133. {
  1134. Result = THierarchicalStorage::OpenSubKey(Key, true);
  1135. DebugAssert(Result);
  1136. }
  1137. else if (Result && !MasterResult)
  1138. {
  1139. FMasterStorageOpenFailures++;
  1140. }
  1141. }
  1142. }
  1143. return Result;
  1144. }
  1145. //---------------------------------------------------------------------------
  1146. void __fastcall TCustomIniFileStorage::DoCloseSubKey()
  1147. {
  1148. // What we are called to restore previous key from OpenSubKey,
  1149. // when opening path component fails, the master storage was not involved yet
  1150. if (!FOpeningSubKey && (FMasterStorage.get() != NULL))
  1151. {
  1152. if (FMasterStorageOpenFailures > 0)
  1153. {
  1154. FMasterStorageOpenFailures--;
  1155. }
  1156. else
  1157. {
  1158. FMasterStorage->CloseSubKey();
  1159. }
  1160. }
  1161. }
  1162. //---------------------------------------------------------------------------
  1163. void __fastcall TCustomIniFileStorage::DoDeleteSubKey(const UnicodeString & SubKey)
  1164. {
  1165. try
  1166. {
  1167. ResetCache();
  1168. FIniFile->EraseSection(CurrentSubKey + MungeKeyName(SubKey));
  1169. }
  1170. catch (...)
  1171. {
  1172. }
  1173. if (HandleByMasterStorage())
  1174. {
  1175. if (FMasterStorage->CanWrite())
  1176. {
  1177. FMasterStorage->DoDeleteSubKey(SubKey);
  1178. }
  1179. }
  1180. }
  1181. //---------------------------------------------------------------------------
  1182. void __fastcall TCustomIniFileStorage::DoGetSubKeyNames(TStrings * Strings)
  1183. {
  1184. Strings->Clear();
  1185. if (HandleByMasterStorage())
  1186. {
  1187. FMasterStorage->GetSubKeyNames(Strings);
  1188. }
  1189. CacheSections();
  1190. for (int i = 0; i < FSections->Count; i++)
  1191. {
  1192. UnicodeString Section = FSections->Strings[i];
  1193. if (AnsiCompareText(CurrentSubKey,
  1194. Section.SubString(1, CurrentSubKey.Length())) == 0)
  1195. {
  1196. UnicodeString SubSection = Section.SubString(CurrentSubKey.Length() + 1,
  1197. Section.Length() - CurrentSubKey.Length());
  1198. int P = SubSection.Pos(L"\\");
  1199. if (P)
  1200. {
  1201. SubSection.SetLength(P - 1);
  1202. }
  1203. if (Strings->IndexOf(SubSection) < 0)
  1204. {
  1205. Strings->Add(UnMungeStr(SubSection));
  1206. }
  1207. }
  1208. }
  1209. }
  1210. //---------------------------------------------------------------------------
  1211. void __fastcall TCustomIniFileStorage::DoGetValueNames(TStrings * Strings)
  1212. {
  1213. if (HandleByMasterStorage())
  1214. {
  1215. FMasterStorage->GetValueNames(Strings);
  1216. }
  1217. FIniFile->ReadSection(CurrentSection, Strings);
  1218. for (int Index = 0; Index < Strings->Count; Index++)
  1219. {
  1220. Strings->Strings[Index] = UnMungeIniName(Strings->Strings[Index]);
  1221. }
  1222. }
  1223. //---------------------------------------------------------------------------
  1224. bool __fastcall TCustomIniFileStorage::DoKeyExists(const UnicodeString & SubKey, bool AForceAnsi)
  1225. {
  1226. return
  1227. (HandleByMasterStorage() && FMasterStorage->DoKeyExists(SubKey, AForceAnsi)) ||
  1228. DoKeyExistsInternal(MungeStr(SubKey, AForceAnsi, false));
  1229. }
  1230. //---------------------------------------------------------------------------
  1231. bool __fastcall TCustomIniFileStorage::DoValueExistsInternal(const UnicodeString & Value)
  1232. {
  1233. return FIniFile->ValueExists(CurrentSection, MungeIniName(Value));
  1234. }
  1235. //---------------------------------------------------------------------------
  1236. bool __fastcall TCustomIniFileStorage::DoValueExists(const UnicodeString & Value)
  1237. {
  1238. return
  1239. (HandleByMasterStorage() && FMasterStorage->ValueExists(Value)) ||
  1240. DoValueExistsInternal(Value);
  1241. }
  1242. //---------------------------------------------------------------------------
  1243. bool __fastcall TCustomIniFileStorage::DoDeleteValue(const UnicodeString & Name)
  1244. {
  1245. bool Result = true;
  1246. if (HandleByMasterStorage())
  1247. {
  1248. Result = FMasterStorage->DeleteValue(Name);
  1249. }
  1250. ResetCache();
  1251. FIniFile->DeleteKey(CurrentSection, MungeIniName(Name));
  1252. return Result;
  1253. }
  1254. //---------------------------------------------------------------------------
  1255. bool __fastcall TCustomIniFileStorage::HandleByMasterStorage()
  1256. {
  1257. return
  1258. (FMasterStorage.get() != NULL) &&
  1259. (FMasterStorageOpenFailures == 0);
  1260. }
  1261. //---------------------------------------------------------------------------
  1262. bool __fastcall TCustomIniFileStorage::HandleReadByMasterStorage(const UnicodeString & Name)
  1263. {
  1264. return HandleByMasterStorage() && !DoValueExistsInternal(Name);
  1265. }
  1266. //---------------------------------------------------------------------------
  1267. size_t __fastcall TCustomIniFileStorage::DoBinaryDataSize(const UnicodeString & Name)
  1268. {
  1269. if (HandleReadByMasterStorage(Name))
  1270. {
  1271. return FMasterStorage->BinaryDataSize(Name);
  1272. }
  1273. else
  1274. {
  1275. return ReadStringRaw(Name, L"").Length() / 2;
  1276. }
  1277. }
  1278. //---------------------------------------------------------------------------
  1279. bool __fastcall TCustomIniFileStorage::DoReadBool(const UnicodeString & Name, bool Default)
  1280. {
  1281. if (HandleReadByMasterStorage(Name))
  1282. {
  1283. return FMasterStorage->ReadBool(Name, Default);
  1284. }
  1285. else
  1286. {
  1287. return FIniFile->ReadBool(CurrentSection, MungeIniName(Name), Default);
  1288. }
  1289. }
  1290. //---------------------------------------------------------------------------
  1291. int __fastcall TCustomIniFileStorage::DoReadInteger(const UnicodeString & Name, int Default)
  1292. {
  1293. int Result;
  1294. if (HandleReadByMasterStorage(Name))
  1295. {
  1296. Result = FMasterStorage->ReadInteger(Name, Default);
  1297. }
  1298. else
  1299. {
  1300. Result = FIniFile->ReadInteger(CurrentSection, MungeIniName(Name), Default);
  1301. }
  1302. return Result;
  1303. }
  1304. //---------------------------------------------------------------------------
  1305. __int64 __fastcall TCustomIniFileStorage::DoReadInt64(const UnicodeString & Name, __int64 Default)
  1306. {
  1307. __int64 Result;
  1308. if (HandleReadByMasterStorage(Name))
  1309. {
  1310. Result = FMasterStorage->ReadInt64(Name, Default);
  1311. }
  1312. else
  1313. {
  1314. Result = Default;
  1315. UnicodeString Str;
  1316. Str = ReadStringRaw(Name, L"");
  1317. if (!Str.IsEmpty())
  1318. {
  1319. Result = StrToInt64Def(Str, Default);
  1320. }
  1321. }
  1322. return Result;
  1323. }
  1324. //---------------------------------------------------------------------------
  1325. TDateTime __fastcall TCustomIniFileStorage::DoReadDateTime(const UnicodeString & Name, TDateTime Default)
  1326. {
  1327. TDateTime Result;
  1328. if (HandleReadByMasterStorage(Name))
  1329. {
  1330. Result = FMasterStorage->ReadDateTime(Name, Default);
  1331. }
  1332. else
  1333. {
  1334. UnicodeString Value = FIniFile->ReadString(CurrentSection, MungeIniName(Name), L"");
  1335. if (Value.IsEmpty())
  1336. {
  1337. Result = Default;
  1338. }
  1339. else
  1340. {
  1341. try
  1342. {
  1343. RawByteString Raw = HexToBytes(Value);
  1344. if (static_cast<size_t>(Raw.Length()) == sizeof(Result))
  1345. {
  1346. memcpy(&Result, Raw.c_str(), sizeof(Result));
  1347. }
  1348. else
  1349. {
  1350. Result = StrToDateTime(Value);
  1351. }
  1352. }
  1353. catch(...)
  1354. {
  1355. Result = Default;
  1356. }
  1357. }
  1358. }
  1359. return Result;
  1360. }
  1361. //---------------------------------------------------------------------------
  1362. double __fastcall TCustomIniFileStorage::DoReadFloat(const UnicodeString & Name, double Default)
  1363. {
  1364. double Result;
  1365. if (HandleReadByMasterStorage(Name))
  1366. {
  1367. Result = FMasterStorage->ReadFloat(Name, Default);
  1368. }
  1369. else
  1370. {
  1371. UnicodeString Value = FIniFile->ReadString(CurrentSection, MungeIniName(Name), L"");
  1372. if (Value.IsEmpty())
  1373. {
  1374. Result = Default;
  1375. }
  1376. else
  1377. {
  1378. try
  1379. {
  1380. RawByteString Raw = HexToBytes(Value);
  1381. if (static_cast<size_t>(Raw.Length()) == sizeof(Result))
  1382. {
  1383. memcpy(&Result, Raw.c_str(), sizeof(Result));
  1384. }
  1385. else
  1386. {
  1387. Result = static_cast<double>(StrToFloat(Value));
  1388. }
  1389. }
  1390. catch(...)
  1391. {
  1392. Result = Default;
  1393. }
  1394. }
  1395. }
  1396. return Result;
  1397. }
  1398. //---------------------------------------------------------------------------
  1399. UnicodeString __fastcall TCustomIniFileStorage::DoReadStringRaw(const UnicodeString & Name, const UnicodeString & Default)
  1400. {
  1401. UnicodeString Result;
  1402. if (HandleReadByMasterStorage(Name))
  1403. {
  1404. Result = FMasterStorage->ReadStringRaw(Name, Default);
  1405. }
  1406. else
  1407. {
  1408. Result = FIniFile->ReadString(CurrentSection, MungeIniName(Name), Default);
  1409. }
  1410. return Result;
  1411. }
  1412. //---------------------------------------------------------------------------
  1413. size_t __fastcall TCustomIniFileStorage::DoReadBinaryData(const UnicodeString & Name, void * Buffer, size_t Size)
  1414. {
  1415. size_t Len;
  1416. if (HandleReadByMasterStorage(Name))
  1417. {
  1418. Size = FMasterStorage->ReadBinaryData(Name, Buffer, Size);
  1419. }
  1420. else
  1421. {
  1422. RawByteString Value = HexToBytes(ReadStringRaw(Name, L""));
  1423. Len = Value.Length();
  1424. if (Size > Len)
  1425. {
  1426. Size = Len;
  1427. }
  1428. DebugAssert(Buffer);
  1429. memcpy(Buffer, Value.c_str(), Size);
  1430. }
  1431. return Size;
  1432. }
  1433. //---------------------------------------------------------------------------
  1434. void __fastcall TCustomIniFileStorage::DoWriteBool(const UnicodeString & Name, bool Value)
  1435. {
  1436. if (HandleByMasterStorage())
  1437. {
  1438. FMasterStorage->WriteBool(Name, Value);
  1439. }
  1440. ResetCache();
  1441. FIniFile->WriteBool(CurrentSection, MungeIniName(Name), Value);
  1442. }
  1443. //---------------------------------------------------------------------------
  1444. void __fastcall TCustomIniFileStorage::DoWriteInteger(const UnicodeString & Name, int Value)
  1445. {
  1446. if (HandleByMasterStorage())
  1447. {
  1448. FMasterStorage->WriteInteger(Name, Value);
  1449. }
  1450. ResetCache();
  1451. FIniFile->WriteInteger(CurrentSection, MungeIniName(Name), Value);
  1452. }
  1453. //---------------------------------------------------------------------------
  1454. void __fastcall TCustomIniFileStorage::DoWriteInt64(const UnicodeString & Name, __int64 Value)
  1455. {
  1456. if (HandleByMasterStorage())
  1457. {
  1458. FMasterStorage->WriteInt64(Name, Value);
  1459. }
  1460. DoWriteStringRawInternal(Name, IntToStr(Value));
  1461. }
  1462. //---------------------------------------------------------------------------
  1463. void __fastcall TCustomIniFileStorage::DoWriteStringRawInternal(const UnicodeString & Name, const UnicodeString & Value)
  1464. {
  1465. ResetCache();
  1466. FIniFile->WriteString(CurrentSection, MungeIniName(Name), Value);
  1467. }
  1468. //---------------------------------------------------------------------------
  1469. void __fastcall TCustomIniFileStorage::DoWriteStringRaw(const UnicodeString & Name, const UnicodeString & Value)
  1470. {
  1471. if (HandleByMasterStorage())
  1472. {
  1473. FMasterStorage->WriteStringRaw(Name, Value);
  1474. }
  1475. DoWriteStringRawInternal(Name, Value);
  1476. }
  1477. //---------------------------------------------------------------------------
  1478. void __fastcall TCustomIniFileStorage::DoWriteBinaryData(const UnicodeString & Name, const void * Buffer, int Size)
  1479. {
  1480. if (HandleByMasterStorage())
  1481. {
  1482. FMasterStorage->WriteBinaryData(Name, Buffer, Size);
  1483. }
  1484. DoWriteStringRawInternal(Name, BytesToHex(RawByteString(static_cast<const char*>(Buffer), Size)));
  1485. }
  1486. //---------------------------------------------------------------------------
  1487. UnicodeString __fastcall TCustomIniFileStorage::DoReadRootAccessString()
  1488. {
  1489. UnicodeString Result;
  1490. if (OpenSubKey(L"_", false))
  1491. {
  1492. Result = ReadAccessString();
  1493. CloseSubKey();
  1494. }
  1495. else
  1496. {
  1497. Result = DefaultAccessString;
  1498. }
  1499. return Result;
  1500. }
  1501. //===========================================================================
  1502. TIniFileStorage * __fastcall TIniFileStorage::CreateFromPath(const UnicodeString & AStorage)
  1503. {
  1504. // The code was originally inline in the parent contructor call in the TIniFileStorage::TIniFileStorage [public originally].
  1505. // But if the TMemIniFile constructor throws (e.g. because the INI file is locked), the exception causes access violation.
  1506. // Moving the code to a factory solves this.
  1507. TMemIniFile * IniFile = new TMemIniFile(AStorage);
  1508. return new TIniFileStorage(AStorage, IniFile);
  1509. }
  1510. //---------------------------------------------------------------------------
  1511. __fastcall TIniFileStorage::TIniFileStorage(const UnicodeString & AStorage, TCustomIniFile * IniFile):
  1512. TCustomIniFileStorage(AStorage, IniFile)
  1513. {
  1514. FOriginal = new TStringList();
  1515. dynamic_cast<TMemIniFile *>(FIniFile)->GetStrings(FOriginal);
  1516. ApplyOverrides();
  1517. }
  1518. //---------------------------------------------------------------------------
  1519. void __fastcall TIniFileStorage::Flush()
  1520. {
  1521. if (FMasterStorage.get() != NULL)
  1522. {
  1523. FMasterStorage->Flush();
  1524. }
  1525. if (FOriginal != NULL)
  1526. {
  1527. std::unique_ptr<TStrings> Strings(new TStringList);
  1528. std::unique_ptr<TStrings> Original(FOriginal);
  1529. FOriginal = NULL;
  1530. dynamic_cast<TMemIniFile *>(FIniFile)->GetStrings(Strings.get());
  1531. if (!Strings->Equals(Original.get()))
  1532. {
  1533. int Attr;
  1534. // preserve attributes (especially hidden)
  1535. bool Exists = FileExists(ApiPath(Storage));
  1536. if (Exists)
  1537. {
  1538. Attr = GetFileAttributes(ApiPath(Storage).c_str());
  1539. }
  1540. else
  1541. {
  1542. Attr = FILE_ATTRIBUTE_NORMAL;
  1543. }
  1544. if (FLAGSET(Attr, FILE_ATTRIBUTE_READONLY) && ForceSave)
  1545. {
  1546. SetFileAttributes(ApiPath(Storage).c_str(), Attr & ~FILE_ATTRIBUTE_READONLY);
  1547. }
  1548. HANDLE Handle;
  1549. int Error;
  1550. bool Retry;
  1551. int Trying = 0;
  1552. do
  1553. {
  1554. Error = 0;
  1555. Handle =
  1556. CreateFile(ApiPath(Storage).c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, Attr, 0);
  1557. if (Handle == INVALID_HANDLE_VALUE)
  1558. {
  1559. Error = GetLastError();
  1560. }
  1561. Retry = (Error == ERROR_SHARING_VIOLATION) && (Trying < 2000);
  1562. if (Retry)
  1563. {
  1564. const int Step = 100;
  1565. Sleep(Step);
  1566. Trying += Step;
  1567. }
  1568. }
  1569. while (Retry);
  1570. if (Handle == INVALID_HANDLE_VALUE)
  1571. {
  1572. // "access denied" errors upon implicit saves to existing file are ignored
  1573. if (Explicit || !Exists || (Error != ERROR_ACCESS_DENIED))
  1574. {
  1575. throw EOSExtException(FMTLOAD((Exists ? WRITE_ERROR : CREATE_FILE_ERROR), (Storage)));
  1576. }
  1577. }
  1578. else
  1579. {
  1580. try
  1581. {
  1582. std::unique_ptr<TStream> Stream(new TSafeHandleStream(int(Handle)));
  1583. try
  1584. {
  1585. Strings->SaveToStream(Stream.get());
  1586. }
  1587. __finally
  1588. {
  1589. CloseHandle(Handle);
  1590. }
  1591. }
  1592. catch (Exception & E)
  1593. {
  1594. throw ExtException(&E, FMTLOAD(WRITE_ERROR, (Storage)));
  1595. }
  1596. }
  1597. }
  1598. }
  1599. }
  1600. //---------------------------------------------------------------------------
  1601. __fastcall TIniFileStorage::~TIniFileStorage()
  1602. {
  1603. Flush();
  1604. }
  1605. //---------------------------------------------------------------------------
  1606. void __fastcall TIniFileStorage::ApplyOverrides()
  1607. {
  1608. UnicodeString OverridesKey = IncludeTrailingBackslash(L"Override");
  1609. CacheSections();
  1610. for (int i = 0; i < FSections->Count; i++)
  1611. {
  1612. UnicodeString Section = FSections->Strings[i];
  1613. if (SameText(OverridesKey,
  1614. Section.SubString(1, OverridesKey.Length())))
  1615. {
  1616. UnicodeString SubKey = Section.SubString(OverridesKey.Length() + 1,
  1617. Section.Length() - OverridesKey.Length());
  1618. // this all uses raw names (munged)
  1619. TStrings * Names = new TStringList;
  1620. try
  1621. {
  1622. FIniFile->ReadSection(Section, Names);
  1623. for (int ii = 0; ii < Names->Count; ii++)
  1624. {
  1625. UnicodeString Name = Names->Strings[ii];
  1626. UnicodeString Value = FIniFile->ReadString(Section, Name, L"");
  1627. FIniFile->WriteString(SubKey, Name, Value);
  1628. }
  1629. }
  1630. __finally
  1631. {
  1632. delete Names;
  1633. }
  1634. FIniFile->EraseSection(Section);
  1635. ResetCache();
  1636. }
  1637. }
  1638. }
  1639. //===========================================================================
  1640. enum TWriteMode { wmAllow, wmFail, wmIgnore };
  1641. //---------------------------------------------------------------------------
  1642. class TOptionsIniFile : public TCustomIniFile
  1643. {
  1644. public:
  1645. __fastcall TOptionsIniFile(TStrings * Options, TWriteMode WriteMode, const UnicodeString & RootKey);
  1646. virtual UnicodeString __fastcall ReadString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Default);
  1647. virtual void __fastcall WriteString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Value);
  1648. virtual void __fastcall ReadSection(const UnicodeString Section, TStrings * Strings);
  1649. virtual void __fastcall ReadSections(TStrings* Strings);
  1650. virtual void __fastcall ReadSectionValues(const UnicodeString Section, TStrings* Strings);
  1651. virtual void __fastcall EraseSection(const UnicodeString Section);
  1652. virtual void __fastcall DeleteKey(const UnicodeString Section, const UnicodeString Ident);
  1653. virtual void __fastcall UpdateFile();
  1654. // Hoisted overload
  1655. void __fastcall ReadSections(const UnicodeString Section, TStrings* Strings);
  1656. // Ntb, we can implement ValueExists more efficiently than the TCustomIniFile.ValueExists
  1657. private:
  1658. TStrings * FOptions;
  1659. TWriteMode FWriteMode;
  1660. UnicodeString FRootKey;
  1661. bool __fastcall AllowWrite();
  1662. void __fastcall NotImplemented();
  1663. bool __fastcall AllowSection(const UnicodeString & Section);
  1664. UnicodeString __fastcall FormatKey(const UnicodeString & Section, const UnicodeString & Ident);
  1665. };
  1666. //---------------------------------------------------------------------------
  1667. __fastcall TOptionsIniFile::TOptionsIniFile(TStrings * Options, TWriteMode WriteMode, const UnicodeString & RootKey) :
  1668. TCustomIniFile(UnicodeString())
  1669. {
  1670. FOptions = Options;
  1671. FWriteMode = WriteMode;
  1672. FRootKey = RootKey;
  1673. if (!FRootKey.IsEmpty())
  1674. {
  1675. FRootKey += PathDelim;
  1676. }
  1677. }
  1678. //---------------------------------------------------------------------------
  1679. void __fastcall TOptionsIniFile::NotImplemented()
  1680. {
  1681. throw Exception(L"Not implemented");
  1682. }
  1683. //---------------------------------------------------------------------------
  1684. bool __fastcall TOptionsIniFile::AllowWrite()
  1685. {
  1686. switch (FWriteMode)
  1687. {
  1688. case wmAllow:
  1689. return true;
  1690. case wmFail:
  1691. NotImplemented();
  1692. return false; // never gets here
  1693. case wmIgnore:
  1694. return false;
  1695. default:
  1696. DebugFail();
  1697. return false;
  1698. }
  1699. }
  1700. //---------------------------------------------------------------------------
  1701. bool __fastcall TOptionsIniFile::AllowSection(const UnicodeString & Section)
  1702. {
  1703. UnicodeString Name = Section;
  1704. if (!Name.IsEmpty())
  1705. {
  1706. Name += PathDelim;
  1707. }
  1708. bool Result = SameText(Name.SubString(1, FRootKey.Length()), FRootKey);
  1709. return Result;
  1710. }
  1711. //---------------------------------------------------------------------------
  1712. UnicodeString __fastcall TOptionsIniFile::FormatKey(const UnicodeString & Section, const UnicodeString & Ident)
  1713. {
  1714. UnicodeString Result = Section;
  1715. if (!Result.IsEmpty())
  1716. {
  1717. Result += PathDelim;
  1718. }
  1719. Result += Ident; // Can be empty, when called from a contructor, AllowSection or ReadSection
  1720. if (DebugAlwaysTrue(AllowSection(Section)))
  1721. {
  1722. Result.Delete(1, FRootKey.Length());
  1723. }
  1724. return Result;
  1725. }
  1726. //---------------------------------------------------------------------------
  1727. UnicodeString __fastcall TOptionsIniFile::ReadString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Default)
  1728. {
  1729. UnicodeString Value;
  1730. if (!AllowSection(Section))
  1731. {
  1732. Value = Default;
  1733. }
  1734. else
  1735. {
  1736. UnicodeString Name = FormatKey(Section, Ident);
  1737. int Index = FOptions->IndexOfName(Name);
  1738. if (Index >= 0)
  1739. {
  1740. Value = FOptions->ValueFromIndex[Index];
  1741. }
  1742. else
  1743. {
  1744. Value = Default;
  1745. }
  1746. }
  1747. return Value;
  1748. }
  1749. //---------------------------------------------------------------------------
  1750. void __fastcall TOptionsIniFile::WriteString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Value)
  1751. {
  1752. if (AllowWrite() &&
  1753. DebugAlwaysTrue(AllowSection(Section)))
  1754. {
  1755. UnicodeString Name = FormatKey(Section, Ident);
  1756. SetStringValueEvenIfEmpty(FOptions, Name, Value);
  1757. }
  1758. }
  1759. //---------------------------------------------------------------------------
  1760. void __fastcall TOptionsIniFile::ReadSection(const UnicodeString Section, TStrings * Strings)
  1761. {
  1762. if (AllowSection(Section))
  1763. {
  1764. UnicodeString SectionPrefix = FormatKey(Section, UnicodeString());
  1765. Strings->BeginUpdate();
  1766. try
  1767. {
  1768. for (int Index = 0; Index < FOptions->Count; Index++)
  1769. {
  1770. UnicodeString Name = FOptions->Names[Index];
  1771. if (SameText(Name.SubString(1, SectionPrefix.Length()), SectionPrefix) &&
  1772. (LastDelimiter(PathDelim, Name) <= SectionPrefix.Length()))
  1773. {
  1774. Strings->Add(Name.SubString(SectionPrefix.Length() + 1, Name.Length() - SectionPrefix.Length()));
  1775. }
  1776. }
  1777. }
  1778. __finally
  1779. {
  1780. Strings->EndUpdate();
  1781. }
  1782. }
  1783. }
  1784. //---------------------------------------------------------------------------
  1785. void __fastcall TOptionsIniFile::ReadSections(TStrings * Strings)
  1786. {
  1787. std::unique_ptr<TStringList> Sections(CreateSortedStringList());
  1788. for (int Index = 0; Index < FOptions->Count; Index++)
  1789. {
  1790. UnicodeString Name = FOptions->Names[Index];
  1791. int P = LastDelimiter(PathDelim, Name);
  1792. if (P > 0)
  1793. {
  1794. UnicodeString Section = Name.SubString(1, P - 1);
  1795. if (Sections->IndexOf(Section) < 0)
  1796. {
  1797. Sections->Add(Section);
  1798. }
  1799. }
  1800. }
  1801. for (int Index = 0; Index < Sections->Count; Index++)
  1802. {
  1803. Strings->Add(FRootKey + Sections->Strings[Index]);
  1804. }
  1805. }
  1806. //---------------------------------------------------------------------------
  1807. void __fastcall TOptionsIniFile::ReadSectionValues(const UnicodeString Section, TStrings * /*Strings*/)
  1808. {
  1809. NotImplemented();
  1810. }
  1811. //---------------------------------------------------------------------------
  1812. void __fastcall TOptionsIniFile::EraseSection(const UnicodeString Section)
  1813. {
  1814. if (AllowWrite())
  1815. {
  1816. NotImplemented();
  1817. }
  1818. }
  1819. //---------------------------------------------------------------------------
  1820. void __fastcall TOptionsIniFile::DeleteKey(const UnicodeString Section, const UnicodeString Ident)
  1821. {
  1822. if (AllowWrite() &&
  1823. DebugAlwaysTrue(AllowSection(Section)))
  1824. {
  1825. UnicodeString Name = FormatKey(Section, Ident);
  1826. int Index = FOptions->IndexOfName(Name);
  1827. if (Index >= 0)
  1828. {
  1829. FOptions->Delete(Index);
  1830. }
  1831. }
  1832. }
  1833. //---------------------------------------------------------------------------
  1834. void __fastcall TOptionsIniFile::UpdateFile()
  1835. {
  1836. if (AllowWrite())
  1837. {
  1838. // noop
  1839. }
  1840. }
  1841. //---------------------------------------------------------------------------
  1842. void __fastcall TOptionsIniFile::ReadSections(const UnicodeString /*Section*/, TStrings * /*Strings*/)
  1843. {
  1844. NotImplemented();
  1845. }
  1846. //===========================================================================
  1847. __fastcall TOptionsStorage::TOptionsStorage(TStrings * Options, bool AllowWrite):
  1848. TCustomIniFileStorage(
  1849. UnicodeString(L"Command-line options"),
  1850. new TOptionsIniFile(Options, (AllowWrite ? wmAllow : wmFail), UnicodeString()))
  1851. {
  1852. }
  1853. //---------------------------------------------------------------------------
  1854. __fastcall TOptionsStorage::TOptionsStorage(TStrings * Options, const UnicodeString & RootKey, THierarchicalStorage * MasterStorage) :
  1855. TCustomIniFileStorage(
  1856. UnicodeString(L"Command-line options overriding " + MasterStorage->Source),
  1857. new TOptionsIniFile(Options, wmIgnore, RootKey))
  1858. {
  1859. FMasterStorage.reset(MasterStorage);
  1860. }
  1861. //---------------------------------------------------------------------------
  1862. bool __fastcall TOptionsStorage::GetTemporary()
  1863. {
  1864. return true;
  1865. }