HierarchicalStorage.cpp 60 KB

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