HierarchicalStorage.cpp 51 KB

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