HierarchicalStorage.cpp 47 KB

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