HierarchicalStorage.cpp 57 KB

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