HierarchicalStorage.cpp 46 KB

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