HierarchicalStorage.cpp 47 KB

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