1
0

HierarchicalStorage.cpp 62 KB

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