HierarchicalStorage.cpp 52 KB

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