HierarchicalStorage.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  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::DoKeyExistsInternal(const UnicodeString & SubKey)
  938. {
  939. CacheSections();
  940. bool Result = false;
  941. UnicodeString NewKey = ExcludeTrailingBackslash(CurrentSubKey + SubKey);
  942. if (FSections->Count > 0)
  943. {
  944. int Index = -1;
  945. Result = FSections->Find(NewKey, Index);
  946. if (!Result &&
  947. (Index < FSections->Count) &&
  948. (FSections->Strings[Index].SubString(1, NewKey.Length()+1) == NewKey + L"\\"))
  949. {
  950. Result = true;
  951. }
  952. }
  953. return Result;
  954. }
  955. //---------------------------------------------------------------------------
  956. bool __fastcall TCustomIniFileStorage::DoOpenSubKey(const UnicodeString & SubKey, bool CanCreate)
  957. {
  958. bool Result =
  959. CanCreate ||
  960. DoKeyExistsInternal(SubKey);
  961. return Result;
  962. }
  963. //---------------------------------------------------------------------------
  964. bool __fastcall TCustomIniFileStorage::OpenRootKey(bool CanCreate)
  965. {
  966. // Not supported with master storage.
  967. // Actually currently, we use OpenRootKey with TRegistryStorage only.
  968. DebugAssert(FMasterStorage.get() == NULL);
  969. return THierarchicalStorage::OpenRootKey(CanCreate);
  970. }
  971. //---------------------------------------------------------------------------
  972. bool __fastcall TCustomIniFileStorage::OpenSubKey(const UnicodeString & Key, bool CanCreate)
  973. {
  974. bool Result;
  975. {
  976. TAutoFlag Flag(FOpeningSubKey);
  977. Result = THierarchicalStorage::OpenSubKey(Key, CanCreate);
  978. }
  979. if (FMasterStorage.get() != NULL)
  980. {
  981. if (FMasterStorageOpenFailures > 0)
  982. {
  983. FMasterStorageOpenFailures++;
  984. }
  985. else
  986. {
  987. bool MasterResult = FMasterStorage->OpenSubKey(Key, CanCreate);
  988. if (!Result && MasterResult)
  989. {
  990. Result = THierarchicalStorage::OpenSubKey(Key, true);
  991. DebugAssert(Result);
  992. }
  993. else if (Result && !MasterResult)
  994. {
  995. FMasterStorageOpenFailures++;
  996. }
  997. }
  998. }
  999. return Result;
  1000. }
  1001. //---------------------------------------------------------------------------
  1002. void __fastcall TCustomIniFileStorage::DoCloseSubKey()
  1003. {
  1004. // What we are called to restore previous key from OpenSubKey,
  1005. // when opening path component fails, the master storage was not involved yet
  1006. if (!FOpeningSubKey && (FMasterStorage.get() != NULL))
  1007. {
  1008. if (FMasterStorageOpenFailures > 0)
  1009. {
  1010. FMasterStorageOpenFailures--;
  1011. }
  1012. else
  1013. {
  1014. FMasterStorage->CloseSubKey();
  1015. }
  1016. }
  1017. }
  1018. //---------------------------------------------------------------------------
  1019. void __fastcall TCustomIniFileStorage::DoDeleteSubKey(const UnicodeString & SubKey)
  1020. {
  1021. try
  1022. {
  1023. ResetCache();
  1024. FIniFile->EraseSection(CurrentSubKey + MungeKeyName(SubKey));
  1025. }
  1026. catch (...)
  1027. {
  1028. }
  1029. if (HandleByMasterStorage())
  1030. {
  1031. if (FMasterStorage->CanWrite())
  1032. {
  1033. FMasterStorage->DoDeleteSubKey(SubKey);
  1034. }
  1035. }
  1036. }
  1037. //---------------------------------------------------------------------------
  1038. void __fastcall TCustomIniFileStorage::DoGetSubKeyNames(TStrings * Strings)
  1039. {
  1040. Strings->Clear();
  1041. if (HandleByMasterStorage())
  1042. {
  1043. FMasterStorage->GetSubKeyNames(Strings);
  1044. }
  1045. CacheSections();
  1046. for (int i = 0; i < FSections->Count; i++)
  1047. {
  1048. UnicodeString Section = FSections->Strings[i];
  1049. if (AnsiCompareText(CurrentSubKey,
  1050. Section.SubString(1, CurrentSubKey.Length())) == 0)
  1051. {
  1052. UnicodeString SubSection = Section.SubString(CurrentSubKey.Length() + 1,
  1053. Section.Length() - CurrentSubKey.Length());
  1054. int P = SubSection.Pos(L"\\");
  1055. if (P)
  1056. {
  1057. SubSection.SetLength(P - 1);
  1058. }
  1059. if (Strings->IndexOf(SubSection) < 0)
  1060. {
  1061. Strings->Add(UnMungeStr(SubSection));
  1062. }
  1063. }
  1064. }
  1065. }
  1066. //---------------------------------------------------------------------------
  1067. void __fastcall TCustomIniFileStorage::DoGetValueNames(TStrings * Strings)
  1068. {
  1069. if (HandleByMasterStorage())
  1070. {
  1071. FMasterStorage->GetValueNames(Strings);
  1072. }
  1073. FIniFile->ReadSection(CurrentSection, Strings);
  1074. for (int Index = 0; Index < Strings->Count; Index++)
  1075. {
  1076. Strings->Strings[Index] = UnMungeIniName(Strings->Strings[Index]);
  1077. }
  1078. }
  1079. //---------------------------------------------------------------------------
  1080. bool __fastcall TCustomIniFileStorage::DoKeyExists(const UnicodeString & SubKey, bool AForceAnsi)
  1081. {
  1082. return
  1083. (HandleByMasterStorage() && FMasterStorage->DoKeyExists(SubKey, AForceAnsi)) ||
  1084. DoKeyExistsInternal(MungeStr(SubKey, AForceAnsi, false));
  1085. }
  1086. //---------------------------------------------------------------------------
  1087. bool __fastcall TCustomIniFileStorage::DoValueExistsInternal(const UnicodeString & Value)
  1088. {
  1089. return FIniFile->ValueExists(CurrentSection, MungeIniName(Value));
  1090. }
  1091. //---------------------------------------------------------------------------
  1092. bool __fastcall TCustomIniFileStorage::DoValueExists(const UnicodeString & Value)
  1093. {
  1094. return
  1095. (HandleByMasterStorage() && FMasterStorage->ValueExists(Value)) ||
  1096. DoValueExistsInternal(Value);
  1097. }
  1098. //---------------------------------------------------------------------------
  1099. bool __fastcall TCustomIniFileStorage::DoDeleteValue(const UnicodeString & Name)
  1100. {
  1101. bool Result = true;
  1102. if (HandleByMasterStorage())
  1103. {
  1104. Result = FMasterStorage->DeleteValue(Name);
  1105. }
  1106. ResetCache();
  1107. FIniFile->DeleteKey(CurrentSection, MungeIniName(Name));
  1108. return Result;
  1109. }
  1110. //---------------------------------------------------------------------------
  1111. bool __fastcall TCustomIniFileStorage::HandleByMasterStorage()
  1112. {
  1113. return
  1114. (FMasterStorage.get() != NULL) &&
  1115. (FMasterStorageOpenFailures == 0);
  1116. }
  1117. //---------------------------------------------------------------------------
  1118. bool __fastcall TCustomIniFileStorage::HandleReadByMasterStorage(const UnicodeString & Name)
  1119. {
  1120. return HandleByMasterStorage() && !DoValueExistsInternal(Name);
  1121. }
  1122. //---------------------------------------------------------------------------
  1123. size_t __fastcall TCustomIniFileStorage::DoBinaryDataSize(const UnicodeString & Name)
  1124. {
  1125. if (HandleReadByMasterStorage(Name))
  1126. {
  1127. return FMasterStorage->BinaryDataSize(Name);
  1128. }
  1129. else
  1130. {
  1131. return ReadStringRaw(Name, L"").Length() / 2;
  1132. }
  1133. }
  1134. //---------------------------------------------------------------------------
  1135. bool __fastcall TCustomIniFileStorage::DoReadBool(const UnicodeString & Name, bool Default)
  1136. {
  1137. if (HandleReadByMasterStorage(Name))
  1138. {
  1139. return FMasterStorage->ReadBool(Name, Default);
  1140. }
  1141. else
  1142. {
  1143. return FIniFile->ReadBool(CurrentSection, MungeIniName(Name), Default);
  1144. }
  1145. }
  1146. //---------------------------------------------------------------------------
  1147. int __fastcall TCustomIniFileStorage::DoReadInteger(const UnicodeString & Name, int Default)
  1148. {
  1149. int Result;
  1150. if (HandleReadByMasterStorage(Name))
  1151. {
  1152. Result = FMasterStorage->ReadInteger(Name, Default);
  1153. }
  1154. else
  1155. {
  1156. Result = FIniFile->ReadInteger(CurrentSection, MungeIniName(Name), Default);
  1157. }
  1158. return Result;
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. __int64 __fastcall TCustomIniFileStorage::DoReadInt64(const UnicodeString & Name, __int64 Default)
  1162. {
  1163. __int64 Result;
  1164. if (HandleReadByMasterStorage(Name))
  1165. {
  1166. Result = FMasterStorage->ReadInt64(Name, Default);
  1167. }
  1168. else
  1169. {
  1170. Result = Default;
  1171. UnicodeString Str;
  1172. Str = ReadStringRaw(Name, L"");
  1173. if (!Str.IsEmpty())
  1174. {
  1175. Result = StrToInt64Def(Str, Default);
  1176. }
  1177. }
  1178. return Result;
  1179. }
  1180. //---------------------------------------------------------------------------
  1181. TDateTime __fastcall TCustomIniFileStorage::DoReadDateTime(const UnicodeString & Name, TDateTime Default)
  1182. {
  1183. TDateTime Result;
  1184. if (HandleReadByMasterStorage(Name))
  1185. {
  1186. Result = FMasterStorage->ReadDateTime(Name, Default);
  1187. }
  1188. else
  1189. {
  1190. UnicodeString Value = FIniFile->ReadString(CurrentSection, MungeIniName(Name), L"");
  1191. if (Value.IsEmpty())
  1192. {
  1193. Result = Default;
  1194. }
  1195. else
  1196. {
  1197. try
  1198. {
  1199. RawByteString Raw = HexToBytes(Value);
  1200. if (static_cast<size_t>(Raw.Length()) == sizeof(Result))
  1201. {
  1202. memcpy(&Result, Raw.c_str(), sizeof(Result));
  1203. }
  1204. else
  1205. {
  1206. Result = StrToDateTime(Value);
  1207. }
  1208. }
  1209. catch(...)
  1210. {
  1211. Result = Default;
  1212. }
  1213. }
  1214. }
  1215. return Result;
  1216. }
  1217. //---------------------------------------------------------------------------
  1218. double __fastcall TCustomIniFileStorage::DoReadFloat(const UnicodeString & Name, double Default)
  1219. {
  1220. double Result;
  1221. if (HandleReadByMasterStorage(Name))
  1222. {
  1223. Result = FMasterStorage->ReadFloat(Name, Default);
  1224. }
  1225. else
  1226. {
  1227. UnicodeString Value = FIniFile->ReadString(CurrentSection, MungeIniName(Name), L"");
  1228. if (Value.IsEmpty())
  1229. {
  1230. Result = Default;
  1231. }
  1232. else
  1233. {
  1234. try
  1235. {
  1236. RawByteString Raw = HexToBytes(Value);
  1237. if (static_cast<size_t>(Raw.Length()) == sizeof(Result))
  1238. {
  1239. memcpy(&Result, Raw.c_str(), sizeof(Result));
  1240. }
  1241. else
  1242. {
  1243. Result = static_cast<double>(StrToFloat(Value));
  1244. }
  1245. }
  1246. catch(...)
  1247. {
  1248. Result = Default;
  1249. }
  1250. }
  1251. }
  1252. return Result;
  1253. }
  1254. //---------------------------------------------------------------------------
  1255. UnicodeString __fastcall TCustomIniFileStorage::DoReadStringRaw(const UnicodeString & Name, const UnicodeString & Default)
  1256. {
  1257. UnicodeString Result;
  1258. if (HandleReadByMasterStorage(Name))
  1259. {
  1260. Result = FMasterStorage->ReadStringRaw(Name, Default);
  1261. }
  1262. else
  1263. {
  1264. Result = FIniFile->ReadString(CurrentSection, MungeIniName(Name), Default);
  1265. }
  1266. return Result;
  1267. }
  1268. //---------------------------------------------------------------------------
  1269. size_t __fastcall TCustomIniFileStorage::DoReadBinaryData(const UnicodeString & Name, void * Buffer, size_t Size)
  1270. {
  1271. size_t Len;
  1272. if (HandleReadByMasterStorage(Name))
  1273. {
  1274. Size = FMasterStorage->ReadBinaryData(Name, Buffer, Size);
  1275. }
  1276. else
  1277. {
  1278. RawByteString Value = HexToBytes(ReadStringRaw(Name, L""));
  1279. Len = Value.Length();
  1280. if (Size > Len)
  1281. {
  1282. Size = Len;
  1283. }
  1284. DebugAssert(Buffer);
  1285. memcpy(Buffer, Value.c_str(), Size);
  1286. }
  1287. return Size;
  1288. }
  1289. //---------------------------------------------------------------------------
  1290. void __fastcall TCustomIniFileStorage::DoWriteBool(const UnicodeString & Name, bool Value)
  1291. {
  1292. if (HandleByMasterStorage())
  1293. {
  1294. FMasterStorage->WriteBool(Name, Value);
  1295. }
  1296. ResetCache();
  1297. FIniFile->WriteBool(CurrentSection, MungeIniName(Name), Value);
  1298. }
  1299. //---------------------------------------------------------------------------
  1300. void __fastcall TCustomIniFileStorage::DoWriteInteger(const UnicodeString & Name, int Value)
  1301. {
  1302. if (HandleByMasterStorage())
  1303. {
  1304. FMasterStorage->WriteInteger(Name, Value);
  1305. }
  1306. ResetCache();
  1307. FIniFile->WriteInteger(CurrentSection, MungeIniName(Name), Value);
  1308. }
  1309. //---------------------------------------------------------------------------
  1310. void __fastcall TCustomIniFileStorage::DoWriteInt64(const UnicodeString & Name, __int64 Value)
  1311. {
  1312. if (HandleByMasterStorage())
  1313. {
  1314. FMasterStorage->WriteInt64(Name, Value);
  1315. }
  1316. DoWriteStringRawInternal(Name, IntToStr(Value));
  1317. }
  1318. //---------------------------------------------------------------------------
  1319. void __fastcall TCustomIniFileStorage::DoWriteStringRawInternal(const UnicodeString & Name, const UnicodeString & Value)
  1320. {
  1321. ResetCache();
  1322. FIniFile->WriteString(CurrentSection, MungeIniName(Name), Value);
  1323. }
  1324. //---------------------------------------------------------------------------
  1325. void __fastcall TCustomIniFileStorage::DoWriteStringRaw(const UnicodeString & Name, const UnicodeString & Value)
  1326. {
  1327. if (HandleByMasterStorage())
  1328. {
  1329. FMasterStorage->WriteStringRaw(Name, Value);
  1330. }
  1331. DoWriteStringRawInternal(Name, Value);
  1332. }
  1333. //---------------------------------------------------------------------------
  1334. void __fastcall TCustomIniFileStorage::DoWriteBinaryData(const UnicodeString & Name, const void * Buffer, int Size)
  1335. {
  1336. if (HandleByMasterStorage())
  1337. {
  1338. FMasterStorage->WriteBinaryData(Name, Buffer, Size);
  1339. }
  1340. DoWriteStringRawInternal(Name, BytesToHex(RawByteString(static_cast<const char*>(Buffer), Size)));
  1341. }
  1342. //===========================================================================
  1343. TIniFileStorage * __fastcall TIniFileStorage::CreateFromPath(const UnicodeString & AStorage)
  1344. {
  1345. // The code was originally inline in the parent contructor call in the TIniFileStorage::TIniFileStorage [public originally].
  1346. // But if the TMemIniFile constructor throws (e.g. because the INI file is locked), the exception causes access violation.
  1347. // Moving the code to a factory solves this.
  1348. TMemIniFile * IniFile = new TMemIniFile(AStorage);
  1349. return new TIniFileStorage(AStorage, IniFile);
  1350. }
  1351. //---------------------------------------------------------------------------
  1352. __fastcall TIniFileStorage::TIniFileStorage(const UnicodeString & AStorage, TCustomIniFile * IniFile):
  1353. TCustomIniFileStorage(AStorage, IniFile)
  1354. {
  1355. FOriginal = new TStringList();
  1356. dynamic_cast<TMemIniFile *>(FIniFile)->GetStrings(FOriginal);
  1357. ApplyOverrides();
  1358. }
  1359. //---------------------------------------------------------------------------
  1360. void __fastcall TIniFileStorage::Flush()
  1361. {
  1362. if (FMasterStorage.get() != NULL)
  1363. {
  1364. FMasterStorage->Flush();
  1365. }
  1366. if (FOriginal != NULL)
  1367. {
  1368. std::unique_ptr<TStrings> Strings(new TStringList);
  1369. std::unique_ptr<TStrings> Original(FOriginal);
  1370. FOriginal = NULL;
  1371. dynamic_cast<TMemIniFile *>(FIniFile)->GetStrings(Strings.get());
  1372. if (!Strings->Equals(Original.get()))
  1373. {
  1374. int Attr;
  1375. // preserve attributes (especially hidden)
  1376. bool Exists = FileExists(ApiPath(Storage));
  1377. if (Exists)
  1378. {
  1379. Attr = GetFileAttributes(ApiPath(Storage).c_str());
  1380. }
  1381. else
  1382. {
  1383. Attr = FILE_ATTRIBUTE_NORMAL;
  1384. }
  1385. if (FLAGSET(Attr, FILE_ATTRIBUTE_READONLY) && ForceSave)
  1386. {
  1387. SetFileAttributes(ApiPath(Storage).c_str(), Attr & ~FILE_ATTRIBUTE_READONLY);
  1388. }
  1389. HANDLE Handle = CreateFile(ApiPath(Storage).c_str(), GENERIC_READ | GENERIC_WRITE,
  1390. 0, NULL, CREATE_ALWAYS, Attr, 0);
  1391. if (Handle == INVALID_HANDLE_VALUE)
  1392. {
  1393. // "access denied" errors upon implicit saves to existing file are ignored
  1394. if (Explicit || !Exists || (GetLastError() != ERROR_ACCESS_DENIED))
  1395. {
  1396. throw EOSExtException(FMTLOAD((Exists ? WRITE_ERROR : CREATE_FILE_ERROR), (Storage)));
  1397. }
  1398. }
  1399. else
  1400. {
  1401. std::unique_ptr<TStream> Stream(new THandleStream(int(Handle)));
  1402. try
  1403. {
  1404. Strings->SaveToStream(Stream.get());
  1405. }
  1406. __finally
  1407. {
  1408. CloseHandle(Handle);
  1409. }
  1410. }
  1411. }
  1412. }
  1413. }
  1414. //---------------------------------------------------------------------------
  1415. __fastcall TIniFileStorage::~TIniFileStorage()
  1416. {
  1417. Flush();
  1418. }
  1419. //---------------------------------------------------------------------------
  1420. void __fastcall TIniFileStorage::ApplyOverrides()
  1421. {
  1422. UnicodeString OverridesKey = IncludeTrailingBackslash(L"Override");
  1423. CacheSections();
  1424. for (int i = 0; i < FSections->Count; i++)
  1425. {
  1426. UnicodeString Section = FSections->Strings[i];
  1427. if (SameText(OverridesKey,
  1428. Section.SubString(1, OverridesKey.Length())))
  1429. {
  1430. UnicodeString SubKey = Section.SubString(OverridesKey.Length() + 1,
  1431. Section.Length() - OverridesKey.Length());
  1432. // this all uses raw names (munged)
  1433. TStrings * Names = new TStringList;
  1434. try
  1435. {
  1436. FIniFile->ReadSection(Section, Names);
  1437. for (int ii = 0; ii < Names->Count; ii++)
  1438. {
  1439. UnicodeString Name = Names->Strings[ii];
  1440. UnicodeString Value = FIniFile->ReadString(Section, Name, L"");
  1441. FIniFile->WriteString(SubKey, Name, Value);
  1442. }
  1443. }
  1444. __finally
  1445. {
  1446. delete Names;
  1447. }
  1448. FIniFile->EraseSection(Section);
  1449. ResetCache();
  1450. }
  1451. }
  1452. }
  1453. //===========================================================================
  1454. enum TWriteMode { wmAllow, wmFail, wmIgnore };
  1455. //---------------------------------------------------------------------------
  1456. class TOptionsIniFile : public TCustomIniFile
  1457. {
  1458. public:
  1459. __fastcall TOptionsIniFile(TStrings * Options, TWriteMode WriteMode, const UnicodeString & RootKey);
  1460. virtual UnicodeString __fastcall ReadString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Default);
  1461. virtual void __fastcall WriteString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Value);
  1462. virtual void __fastcall ReadSection(const UnicodeString Section, TStrings * Strings);
  1463. virtual void __fastcall ReadSections(TStrings* Strings);
  1464. virtual void __fastcall ReadSectionValues(const UnicodeString Section, TStrings* Strings);
  1465. virtual void __fastcall EraseSection(const UnicodeString Section);
  1466. virtual void __fastcall DeleteKey(const UnicodeString Section, const UnicodeString Ident);
  1467. virtual void __fastcall UpdateFile();
  1468. // Hoisted overload
  1469. void __fastcall ReadSections(const UnicodeString Section, TStrings* Strings);
  1470. // Ntb, we can implement ValueExists more efficiently than the TCustomIniFile.ValueExists
  1471. private:
  1472. TStrings * FOptions;
  1473. TWriteMode FWriteMode;
  1474. UnicodeString FRootKey;
  1475. bool __fastcall AllowWrite();
  1476. void __fastcall NotImplemented();
  1477. bool __fastcall AllowSection(const UnicodeString & Section);
  1478. UnicodeString __fastcall FormatKey(const UnicodeString & Section, const UnicodeString & Ident);
  1479. };
  1480. //---------------------------------------------------------------------------
  1481. __fastcall TOptionsIniFile::TOptionsIniFile(TStrings * Options, TWriteMode WriteMode, const UnicodeString & RootKey) :
  1482. TCustomIniFile(UnicodeString())
  1483. {
  1484. FOptions = Options;
  1485. FWriteMode = WriteMode;
  1486. FRootKey = RootKey;
  1487. if (!FRootKey.IsEmpty())
  1488. {
  1489. FRootKey += PathDelim;
  1490. }
  1491. }
  1492. //---------------------------------------------------------------------------
  1493. void __fastcall TOptionsIniFile::NotImplemented()
  1494. {
  1495. throw Exception(L"Not implemented");
  1496. }
  1497. //---------------------------------------------------------------------------
  1498. bool __fastcall TOptionsIniFile::AllowWrite()
  1499. {
  1500. switch (FWriteMode)
  1501. {
  1502. case wmAllow:
  1503. return true;
  1504. case wmFail:
  1505. NotImplemented();
  1506. return false; // never gets here
  1507. case wmIgnore:
  1508. return false;
  1509. default:
  1510. DebugFail();
  1511. return false;
  1512. }
  1513. }
  1514. //---------------------------------------------------------------------------
  1515. bool __fastcall TOptionsIniFile::AllowSection(const UnicodeString & Section)
  1516. {
  1517. UnicodeString Name = Section;
  1518. if (!Name.IsEmpty())
  1519. {
  1520. Name += PathDelim;
  1521. }
  1522. bool Result = SameText(Name.SubString(1, FRootKey.Length()), FRootKey);
  1523. return Result;
  1524. }
  1525. //---------------------------------------------------------------------------
  1526. UnicodeString __fastcall TOptionsIniFile::FormatKey(const UnicodeString & Section, const UnicodeString & Ident)
  1527. {
  1528. UnicodeString Result = Section;
  1529. if (!Result.IsEmpty())
  1530. {
  1531. Result += PathDelim;
  1532. }
  1533. Result += Ident; // Can be empty, when called from a contructor, AllowSection or ReadSection
  1534. if (DebugAlwaysTrue(AllowSection(Section)))
  1535. {
  1536. Result.Delete(1, FRootKey.Length());
  1537. }
  1538. return Result;
  1539. }
  1540. //---------------------------------------------------------------------------
  1541. UnicodeString __fastcall TOptionsIniFile::ReadString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Default)
  1542. {
  1543. UnicodeString Value;
  1544. if (!AllowSection(Section))
  1545. {
  1546. Value = Default;
  1547. }
  1548. else
  1549. {
  1550. UnicodeString Name = FormatKey(Section, Ident);
  1551. int Index = FOptions->IndexOfName(Name);
  1552. if (Index >= 0)
  1553. {
  1554. Value = FOptions->ValueFromIndex[Index];
  1555. }
  1556. else
  1557. {
  1558. Value = Default;
  1559. }
  1560. }
  1561. return Value;
  1562. }
  1563. //---------------------------------------------------------------------------
  1564. void __fastcall TOptionsIniFile::WriteString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Value)
  1565. {
  1566. if (AllowWrite() &&
  1567. DebugAlwaysTrue(AllowSection(Section)))
  1568. {
  1569. UnicodeString Name = FormatKey(Section, Ident);
  1570. FOptions->Values[Name] = Value;
  1571. }
  1572. }
  1573. //---------------------------------------------------------------------------
  1574. void __fastcall TOptionsIniFile::ReadSection(const UnicodeString Section, TStrings * Strings)
  1575. {
  1576. if (AllowSection(Section))
  1577. {
  1578. UnicodeString SectionPrefix = FormatKey(Section, UnicodeString());
  1579. Strings->BeginUpdate();
  1580. try
  1581. {
  1582. for (int Index = 0; Index < FOptions->Count; Index++)
  1583. {
  1584. UnicodeString Name = FOptions->Names[Index];
  1585. if (SameText(Name.SubString(1, SectionPrefix.Length()), SectionPrefix) &&
  1586. (LastDelimiter(PathDelim, Name) <= SectionPrefix.Length()))
  1587. {
  1588. Strings->Add(Name.SubString(SectionPrefix.Length() + 1, Name.Length() - SectionPrefix.Length()));
  1589. }
  1590. }
  1591. }
  1592. __finally
  1593. {
  1594. Strings->EndUpdate();
  1595. }
  1596. }
  1597. }
  1598. //---------------------------------------------------------------------------
  1599. void __fastcall TOptionsIniFile::ReadSections(TStrings * Strings)
  1600. {
  1601. std::unique_ptr<TStringList> Sections(CreateSortedStringList());
  1602. for (int Index = 0; Index < FOptions->Count; Index++)
  1603. {
  1604. UnicodeString Name = FOptions->Names[Index];
  1605. int P = LastDelimiter(PathDelim, Name);
  1606. if (P > 0)
  1607. {
  1608. UnicodeString Section = Name.SubString(1, P - 1);
  1609. if (Sections->IndexOf(Section) < 0)
  1610. {
  1611. Sections->Add(Section);
  1612. }
  1613. }
  1614. }
  1615. for (int Index = 0; Index < Sections->Count; Index++)
  1616. {
  1617. Strings->Add(FRootKey + Sections->Strings[Index]);
  1618. }
  1619. }
  1620. //---------------------------------------------------------------------------
  1621. void __fastcall TOptionsIniFile::ReadSectionValues(const UnicodeString Section, TStrings * /*Strings*/)
  1622. {
  1623. NotImplemented();
  1624. }
  1625. //---------------------------------------------------------------------------
  1626. void __fastcall TOptionsIniFile::EraseSection(const UnicodeString Section)
  1627. {
  1628. if (AllowWrite())
  1629. {
  1630. NotImplemented();
  1631. }
  1632. }
  1633. //---------------------------------------------------------------------------
  1634. void __fastcall TOptionsIniFile::DeleteKey(const UnicodeString Section, const UnicodeString Ident)
  1635. {
  1636. if (AllowWrite() &&
  1637. DebugAlwaysTrue(AllowSection(Section)))
  1638. {
  1639. UnicodeString Name = FormatKey(Section, Ident);
  1640. int Index = FOptions->IndexOfName(Name);
  1641. if (Index >= 0)
  1642. {
  1643. FOptions->Delete(Index);
  1644. }
  1645. }
  1646. }
  1647. //---------------------------------------------------------------------------
  1648. void __fastcall TOptionsIniFile::UpdateFile()
  1649. {
  1650. if (AllowWrite())
  1651. {
  1652. // noop
  1653. }
  1654. }
  1655. //---------------------------------------------------------------------------
  1656. void __fastcall TOptionsIniFile::ReadSections(const UnicodeString /*Section*/, TStrings * /*Strings*/)
  1657. {
  1658. NotImplemented();
  1659. }
  1660. //===========================================================================
  1661. __fastcall TOptionsStorage::TOptionsStorage(TStrings * Options, bool AllowWrite):
  1662. TCustomIniFileStorage(
  1663. UnicodeString(L"Command-line options"),
  1664. new TOptionsIniFile(Options, (AllowWrite ? wmAllow : wmFail), UnicodeString()))
  1665. {
  1666. }
  1667. //---------------------------------------------------------------------------
  1668. __fastcall TOptionsStorage::TOptionsStorage(TStrings * Options, const UnicodeString & RootKey, THierarchicalStorage * MasterStorage) :
  1669. TCustomIniFileStorage(
  1670. UnicodeString(L"Command-line options overriding " + MasterStorage->Source),
  1671. new TOptionsIniFile(Options, wmIgnore, RootKey))
  1672. {
  1673. FMasterStorage.reset(MasterStorage);
  1674. }
  1675. //---------------------------------------------------------------------------
  1676. bool __fastcall TOptionsStorage::GetTemporary()
  1677. {
  1678. return true;
  1679. }