HierarchicalStorage.cpp 60 KB

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