HierarchicalStorage.cpp 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006
  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. // noop
  1176. }
  1177. //---------------------------------------------------------------------------
  1178. void __fastcall TCustomIniFileStorage::CloseSubKey()
  1179. {
  1180. // What we are called to restore previous key from OpenSubKey,
  1181. // when opening path component fails, the master storage was not involved yet
  1182. if (!FOpeningSubKey && (FMasterStorage.get() != NULL))
  1183. {
  1184. if (FMasterStorageOpenFailures > 0)
  1185. {
  1186. FMasterStorageOpenFailures--;
  1187. }
  1188. else
  1189. {
  1190. FMasterStorage->CloseSubKey();
  1191. }
  1192. }
  1193. THierarchicalStorage::CloseSubKey();
  1194. }
  1195. //---------------------------------------------------------------------------
  1196. void __fastcall TCustomIniFileStorage::DoDeleteSubKey(const UnicodeString & SubKey)
  1197. {
  1198. try
  1199. {
  1200. ResetCache();
  1201. FIniFile->EraseSection(CurrentSubKey + MungeKeyName(SubKey));
  1202. }
  1203. catch (...)
  1204. {
  1205. }
  1206. if (HandleByMasterStorage())
  1207. {
  1208. if (FMasterStorage->CanWrite())
  1209. {
  1210. FMasterStorage->DoDeleteSubKey(SubKey);
  1211. }
  1212. }
  1213. }
  1214. //---------------------------------------------------------------------------
  1215. void __fastcall TCustomIniFileStorage::DoGetSubKeyNames(TStrings * Strings)
  1216. {
  1217. Strings->Clear();
  1218. if (HandleByMasterStorage())
  1219. {
  1220. FMasterStorage->GetSubKeyNames(Strings);
  1221. }
  1222. CacheSections();
  1223. for (int i = 0; i < FSections->Count; i++)
  1224. {
  1225. UnicodeString Section = FSections->Strings[i];
  1226. if (AnsiCompareText(CurrentSubKey,
  1227. Section.SubString(1, CurrentSubKey.Length())) == 0)
  1228. {
  1229. UnicodeString SubSection = Section.SubString(CurrentSubKey.Length() + 1,
  1230. Section.Length() - CurrentSubKey.Length());
  1231. int P = SubSection.Pos(L"\\");
  1232. if (P)
  1233. {
  1234. SubSection.SetLength(P - 1);
  1235. }
  1236. if (Strings->IndexOf(SubSection) < 0)
  1237. {
  1238. Strings->Add(UnMungeStr(SubSection));
  1239. }
  1240. }
  1241. }
  1242. }
  1243. //---------------------------------------------------------------------------
  1244. void __fastcall TCustomIniFileStorage::DoGetValueNames(TStrings * Strings)
  1245. {
  1246. if (HandleByMasterStorage())
  1247. {
  1248. FMasterStorage->GetValueNames(Strings);
  1249. }
  1250. FIniFile->ReadSection(CurrentSection, Strings);
  1251. for (int Index = 0; Index < Strings->Count; Index++)
  1252. {
  1253. Strings->Strings[Index] = UnMungeIniName(Strings->Strings[Index]);
  1254. }
  1255. }
  1256. //---------------------------------------------------------------------------
  1257. bool __fastcall TCustomIniFileStorage::DoKeyExists(const UnicodeString & SubKey, bool AForceAnsi)
  1258. {
  1259. return
  1260. (HandleByMasterStorage() && FMasterStorage->DoKeyExists(SubKey, AForceAnsi)) ||
  1261. DoKeyExistsInternal(MungeStr(SubKey, AForceAnsi, false));
  1262. }
  1263. //---------------------------------------------------------------------------
  1264. bool __fastcall TCustomIniFileStorage::DoValueExistsInternal(const UnicodeString & Value)
  1265. {
  1266. return FIniFile->ValueExists(CurrentSection, MungeIniName(Value));
  1267. }
  1268. //---------------------------------------------------------------------------
  1269. bool __fastcall TCustomIniFileStorage::DoValueExists(const UnicodeString & Value)
  1270. {
  1271. return
  1272. (HandleByMasterStorage() && FMasterStorage->ValueExists(Value)) ||
  1273. DoValueExistsInternal(Value);
  1274. }
  1275. //---------------------------------------------------------------------------
  1276. bool __fastcall TCustomIniFileStorage::DoDeleteValue(const UnicodeString & Name)
  1277. {
  1278. bool Result = true;
  1279. if (HandleByMasterStorage())
  1280. {
  1281. Result = FMasterStorage->DeleteValue(Name);
  1282. }
  1283. ResetCache();
  1284. FIniFile->DeleteKey(CurrentSection, MungeIniName(Name));
  1285. return Result;
  1286. }
  1287. //---------------------------------------------------------------------------
  1288. bool __fastcall TCustomIniFileStorage::HandleByMasterStorage()
  1289. {
  1290. return
  1291. (FMasterStorage.get() != NULL) &&
  1292. (FMasterStorageOpenFailures == 0);
  1293. }
  1294. //---------------------------------------------------------------------------
  1295. bool __fastcall TCustomIniFileStorage::HandleReadByMasterStorage(const UnicodeString & Name)
  1296. {
  1297. return HandleByMasterStorage() && !DoValueExistsInternal(Name);
  1298. }
  1299. //---------------------------------------------------------------------------
  1300. size_t __fastcall TCustomIniFileStorage::DoBinaryDataSize(const UnicodeString & Name)
  1301. {
  1302. if (HandleReadByMasterStorage(Name))
  1303. {
  1304. return FMasterStorage->BinaryDataSize(Name);
  1305. }
  1306. else
  1307. {
  1308. return ReadStringRaw(Name, L"").Length() / 2;
  1309. }
  1310. }
  1311. //---------------------------------------------------------------------------
  1312. bool __fastcall TCustomIniFileStorage::DoReadBool(const UnicodeString & Name, bool Default)
  1313. {
  1314. if (HandleReadByMasterStorage(Name))
  1315. {
  1316. return FMasterStorage->ReadBool(Name, Default);
  1317. }
  1318. else
  1319. {
  1320. int IntDefault = int(Default);
  1321. int Int = DoReadIntegerWithMapping(Name, IntDefault, &BoolMapping);
  1322. return (Int != 0);
  1323. }
  1324. }
  1325. //---------------------------------------------------------------------------
  1326. int __fastcall TCustomIniFileStorage::DoReadIntegerWithMapping(const UnicodeString & Name, int Default, const TIntMapping * Mapping)
  1327. {
  1328. int Result;
  1329. bool ReadAsInteger = true;
  1330. UnicodeString MungedName = MungeIniName(Name);
  1331. if (Mapping != NULL) // optimization
  1332. {
  1333. UnicodeString S = FIniFile->ReadString(CurrentSection, MungedName, EmptyStr);
  1334. if (!S.IsEmpty())
  1335. {
  1336. S = S.LowerCase();
  1337. TIntMapping::const_iterator I = Mapping->find(S);
  1338. if (I != Mapping->end())
  1339. {
  1340. Result = I->second;
  1341. ReadAsInteger = false;
  1342. }
  1343. }
  1344. }
  1345. if (ReadAsInteger)
  1346. {
  1347. Result = FIniFile->ReadInteger(CurrentSection, MungedName, Default);
  1348. }
  1349. return Result;
  1350. }
  1351. //---------------------------------------------------------------------------
  1352. int __fastcall TCustomIniFileStorage::DoReadInteger(const UnicodeString & Name, int Default, const TIntMapping * Mapping)
  1353. {
  1354. int Result;
  1355. if (HandleReadByMasterStorage(Name))
  1356. {
  1357. Result = FMasterStorage->ReadIntegerWithMapping(Name, Default, Mapping);
  1358. }
  1359. else
  1360. {
  1361. Result = DoReadIntegerWithMapping(Name, Default, Mapping);
  1362. }
  1363. return Result;
  1364. }
  1365. //---------------------------------------------------------------------------
  1366. __int64 __fastcall TCustomIniFileStorage::DoReadInt64(const UnicodeString & Name, __int64 Default)
  1367. {
  1368. __int64 Result;
  1369. if (HandleReadByMasterStorage(Name))
  1370. {
  1371. Result = FMasterStorage->ReadInt64(Name, Default);
  1372. }
  1373. else
  1374. {
  1375. Result = Default;
  1376. UnicodeString Str;
  1377. Str = ReadStringRaw(Name, L"");
  1378. if (!Str.IsEmpty())
  1379. {
  1380. Result = StrToInt64Def(Str, Default);
  1381. }
  1382. }
  1383. return Result;
  1384. }
  1385. //---------------------------------------------------------------------------
  1386. TDateTime __fastcall TCustomIniFileStorage::DoReadDateTime(const UnicodeString & Name, TDateTime Default)
  1387. {
  1388. TDateTime Result;
  1389. if (HandleReadByMasterStorage(Name))
  1390. {
  1391. Result = FMasterStorage->ReadDateTime(Name, Default);
  1392. }
  1393. else
  1394. {
  1395. UnicodeString Value = FIniFile->ReadString(CurrentSection, MungeIniName(Name), L"");
  1396. if (Value.IsEmpty())
  1397. {
  1398. Result = Default;
  1399. }
  1400. else
  1401. {
  1402. try
  1403. {
  1404. RawByteString Raw = HexToBytes(Value);
  1405. if (static_cast<size_t>(Raw.Length()) == sizeof(Result))
  1406. {
  1407. memcpy(&Result, Raw.c_str(), sizeof(Result));
  1408. }
  1409. else
  1410. {
  1411. Result = StrToDateTime(Value);
  1412. }
  1413. }
  1414. catch(...)
  1415. {
  1416. Result = Default;
  1417. }
  1418. }
  1419. }
  1420. return Result;
  1421. }
  1422. //---------------------------------------------------------------------------
  1423. double __fastcall TCustomIniFileStorage::DoReadFloat(const UnicodeString & Name, double Default)
  1424. {
  1425. double Result;
  1426. if (HandleReadByMasterStorage(Name))
  1427. {
  1428. Result = FMasterStorage->ReadFloat(Name, Default);
  1429. }
  1430. else
  1431. {
  1432. UnicodeString Value = FIniFile->ReadString(CurrentSection, MungeIniName(Name), L"");
  1433. if (Value.IsEmpty())
  1434. {
  1435. Result = Default;
  1436. }
  1437. else
  1438. {
  1439. try
  1440. {
  1441. RawByteString Raw = HexToBytes(Value);
  1442. if (static_cast<size_t>(Raw.Length()) == sizeof(Result))
  1443. {
  1444. memcpy(&Result, Raw.c_str(), sizeof(Result));
  1445. }
  1446. else
  1447. {
  1448. Result = static_cast<double>(StrToFloat(Value));
  1449. }
  1450. }
  1451. catch(...)
  1452. {
  1453. Result = Default;
  1454. }
  1455. }
  1456. }
  1457. return Result;
  1458. }
  1459. //---------------------------------------------------------------------------
  1460. UnicodeString __fastcall TCustomIniFileStorage::DoReadStringRaw(const UnicodeString & Name, const UnicodeString & Default)
  1461. {
  1462. UnicodeString Result;
  1463. if (HandleReadByMasterStorage(Name))
  1464. {
  1465. Result = FMasterStorage->ReadStringRaw(Name, Default);
  1466. }
  1467. else
  1468. {
  1469. Result = FIniFile->ReadString(CurrentSection, MungeIniName(Name), Default);
  1470. }
  1471. return Result;
  1472. }
  1473. //---------------------------------------------------------------------------
  1474. size_t __fastcall TCustomIniFileStorage::DoReadBinaryData(const UnicodeString & Name, void * Buffer, size_t Size)
  1475. {
  1476. size_t Len;
  1477. if (HandleReadByMasterStorage(Name))
  1478. {
  1479. Size = FMasterStorage->ReadBinaryData(Name, Buffer, Size);
  1480. }
  1481. else
  1482. {
  1483. RawByteString Value = HexToBytes(ReadStringRaw(Name, L""));
  1484. Len = Value.Length();
  1485. if (Size > Len)
  1486. {
  1487. Size = Len;
  1488. }
  1489. DebugAssert(Buffer);
  1490. memcpy(Buffer, Value.c_str(), Size);
  1491. }
  1492. return Size;
  1493. }
  1494. //---------------------------------------------------------------------------
  1495. void __fastcall TCustomIniFileStorage::DoWriteBool(const UnicodeString & Name, bool Value)
  1496. {
  1497. if (HandleByMasterStorage())
  1498. {
  1499. FMasterStorage->WriteBool(Name, Value);
  1500. }
  1501. ResetCache();
  1502. FIniFile->WriteBool(CurrentSection, MungeIniName(Name), Value);
  1503. }
  1504. //---------------------------------------------------------------------------
  1505. void __fastcall TCustomIniFileStorage::DoWriteInteger(const UnicodeString & Name, int Value)
  1506. {
  1507. if (HandleByMasterStorage())
  1508. {
  1509. FMasterStorage->WriteInteger(Name, Value);
  1510. }
  1511. ResetCache();
  1512. FIniFile->WriteInteger(CurrentSection, MungeIniName(Name), Value);
  1513. }
  1514. //---------------------------------------------------------------------------
  1515. void __fastcall TCustomIniFileStorage::DoWriteInt64(const UnicodeString & Name, __int64 Value)
  1516. {
  1517. if (HandleByMasterStorage())
  1518. {
  1519. FMasterStorage->WriteInt64(Name, Value);
  1520. }
  1521. DoWriteStringRawInternal(Name, IntToStr(Value));
  1522. }
  1523. //---------------------------------------------------------------------------
  1524. void __fastcall TCustomIniFileStorage::DoWriteStringRawInternal(const UnicodeString & Name, const UnicodeString & Value)
  1525. {
  1526. ResetCache();
  1527. FIniFile->WriteString(CurrentSection, MungeIniName(Name), Value);
  1528. }
  1529. //---------------------------------------------------------------------------
  1530. void __fastcall TCustomIniFileStorage::DoWriteStringRaw(const UnicodeString & Name, const UnicodeString & Value)
  1531. {
  1532. if (HandleByMasterStorage())
  1533. {
  1534. FMasterStorage->WriteStringRaw(Name, Value);
  1535. }
  1536. DoWriteStringRawInternal(Name, Value);
  1537. }
  1538. //---------------------------------------------------------------------------
  1539. void __fastcall TCustomIniFileStorage::DoWriteBinaryData(const UnicodeString & Name, const void * Buffer, int Size)
  1540. {
  1541. if (HandleByMasterStorage())
  1542. {
  1543. FMasterStorage->WriteBinaryData(Name, Buffer, Size);
  1544. }
  1545. DoWriteStringRawInternal(Name, BytesToHex(RawByteString(static_cast<const char*>(Buffer), Size)));
  1546. }
  1547. //---------------------------------------------------------------------------
  1548. UnicodeString __fastcall TCustomIniFileStorage::DoReadRootAccessString()
  1549. {
  1550. UnicodeString Result;
  1551. if (OpenSubKey(L"_", false))
  1552. {
  1553. Result = ReadAccessString();
  1554. CloseSubKey();
  1555. }
  1556. else
  1557. {
  1558. Result = DefaultAccessString;
  1559. }
  1560. return Result;
  1561. }
  1562. //---------------------------------------------------------------------------
  1563. unsigned int __fastcall TCustomIniFileStorage::GetCurrentAccess()
  1564. {
  1565. unsigned int Result;
  1566. // The way THierarchicalStorage::OpenSubKey is implemented, the access will be zero for non-existing keys in
  1567. // configuration overrides => delegating access handling to the master storage (which still should read overriden access keys)
  1568. if (FMasterStorage.get() != NULL)
  1569. {
  1570. Result = FMasterStorage->GetCurrentAccess();
  1571. }
  1572. else
  1573. {
  1574. Result = THierarchicalStorage::GetCurrentAccess();
  1575. }
  1576. return Result;
  1577. }
  1578. //---------------------------------------------------------------------------
  1579. bool __fastcall TCustomIniFileStorage::HasAccess(unsigned int Access)
  1580. {
  1581. bool Result;
  1582. // Avoid the FFakeReadOnlyOpens assertion in THierarchicalStorage::HasAccess, which is not valid for overriden configuration
  1583. if (HandleByMasterStorage())
  1584. {
  1585. Result = FMasterStorage->HasAccess(Access);
  1586. }
  1587. else
  1588. {
  1589. Result = THierarchicalStorage::HasAccess(Access);
  1590. }
  1591. return Result;
  1592. }
  1593. //===========================================================================
  1594. TIniFileStorage * __fastcall TIniFileStorage::CreateFromPath(const UnicodeString & AStorage)
  1595. {
  1596. // The code was originally inline in the parent contructor call in the TIniFileStorage::TIniFileStorage [public originally].
  1597. // But if the TMemIniFile constructor throws (e.g. because the INI file is locked), the exception causes access violation.
  1598. // Moving the code to a factory solves this.
  1599. TMemIniFile * IniFile = new TMemIniFile(AStorage);
  1600. return new TIniFileStorage(AStorage, IniFile);
  1601. }
  1602. //---------------------------------------------------------------------------
  1603. TIniFileStorage * __fastcall TIniFileStorage::CreateNul()
  1604. {
  1605. // Before we passed "nul", but we have report of a system, where opening "nul" file fails.
  1606. // Passing an empty string is even more efficient, as it does not even try to read the file.
  1607. TMemIniFile * IniFile = new TMemIniFile(EmptyStr);
  1608. return new TIniFileStorage(INI_NUL, IniFile);
  1609. }
  1610. //---------------------------------------------------------------------------
  1611. __fastcall TIniFileStorage::TIniFileStorage(const UnicodeString & AStorage, TCustomIniFile * IniFile):
  1612. TCustomIniFileStorage(AStorage, IniFile)
  1613. {
  1614. FOriginal = new TStringList();
  1615. dynamic_cast<TMemIniFile *>(FIniFile)->GetStrings(FOriginal);
  1616. ApplyOverrides();
  1617. }
  1618. //---------------------------------------------------------------------------
  1619. void __fastcall TIniFileStorage::Flush()
  1620. {
  1621. if (FMasterStorage.get() != NULL)
  1622. {
  1623. FMasterStorage->Flush();
  1624. }
  1625. if ((FOriginal != NULL) && !FIniFile->FileName.IsEmpty())
  1626. {
  1627. std::unique_ptr<TStrings> Strings(new TStringList);
  1628. std::unique_ptr<TStrings> Original(FOriginal);
  1629. FOriginal = NULL;
  1630. dynamic_cast<TMemIniFile *>(FIniFile)->GetStrings(Strings.get());
  1631. if (!Strings->Equals(Original.get()))
  1632. {
  1633. int Attr;
  1634. // preserve attributes (especially hidden)
  1635. bool Exists = FileExists(ApiPath(Storage));
  1636. if (Exists)
  1637. {
  1638. Attr = GetFileAttributes(ApiPath(Storage).c_str());
  1639. }
  1640. else
  1641. {
  1642. Attr = FILE_ATTRIBUTE_NORMAL;
  1643. }
  1644. if (FLAGSET(Attr, FILE_ATTRIBUTE_READONLY) && ForceSave)
  1645. {
  1646. SetFileAttributes(ApiPath(Storage).c_str(), Attr & ~FILE_ATTRIBUTE_READONLY);
  1647. }
  1648. HANDLE Handle;
  1649. int Error;
  1650. bool Retry;
  1651. int Trying = 0;
  1652. do
  1653. {
  1654. Error = 0;
  1655. Handle =
  1656. CreateFile(ApiPath(Storage).c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, Attr, 0);
  1657. if (Handle == INVALID_HANDLE_VALUE)
  1658. {
  1659. Error = GetLastError();
  1660. }
  1661. Retry = (Error == ERROR_SHARING_VIOLATION) && (Trying < 2000);
  1662. if (Retry)
  1663. {
  1664. const int Step = 100;
  1665. Sleep(Step);
  1666. Trying += Step;
  1667. }
  1668. }
  1669. while (Retry);
  1670. if (Handle == INVALID_HANDLE_VALUE)
  1671. {
  1672. // "access denied" errors upon implicit saves to existing file are ignored
  1673. if (Explicit || !Exists || (Error != ERROR_ACCESS_DENIED))
  1674. {
  1675. throw EOSExtException(FMTLOAD((Exists ? WRITE_ERROR : CREATE_FILE_ERROR), (Storage)));
  1676. }
  1677. }
  1678. else
  1679. {
  1680. try
  1681. {
  1682. std::unique_ptr<TStream> Stream(new TSafeHandleStream(int(Handle)));
  1683. try
  1684. {
  1685. Strings->SaveToStream(Stream.get());
  1686. }
  1687. __finally
  1688. {
  1689. CloseHandle(Handle);
  1690. }
  1691. }
  1692. catch (Exception & E)
  1693. {
  1694. throw ExtException(&E, FMTLOAD(WRITE_ERROR, (Storage)));
  1695. }
  1696. }
  1697. }
  1698. }
  1699. }
  1700. //---------------------------------------------------------------------------
  1701. __fastcall TIniFileStorage::~TIniFileStorage()
  1702. {
  1703. Flush();
  1704. }
  1705. //---------------------------------------------------------------------------
  1706. void __fastcall TIniFileStorage::ApplyOverrides()
  1707. {
  1708. UnicodeString OverridesKey = IncludeTrailingBackslash(L"Override");
  1709. CacheSections();
  1710. for (int i = 0; i < FSections->Count; i++)
  1711. {
  1712. UnicodeString Section = FSections->Strings[i];
  1713. if (SameText(OverridesKey,
  1714. Section.SubString(1, OverridesKey.Length())))
  1715. {
  1716. UnicodeString SubKey = Section.SubString(OverridesKey.Length() + 1,
  1717. Section.Length() - OverridesKey.Length());
  1718. // this all uses raw names (munged)
  1719. TStrings * Names = new TStringList;
  1720. try
  1721. {
  1722. FIniFile->ReadSection(Section, Names);
  1723. for (int ii = 0; ii < Names->Count; ii++)
  1724. {
  1725. UnicodeString Name = Names->Strings[ii];
  1726. UnicodeString Value = FIniFile->ReadString(Section, Name, L"");
  1727. FIniFile->WriteString(SubKey, Name, Value);
  1728. }
  1729. }
  1730. __finally
  1731. {
  1732. delete Names;
  1733. }
  1734. FIniFile->EraseSection(Section);
  1735. ResetCache();
  1736. }
  1737. }
  1738. }
  1739. //===========================================================================
  1740. enum TWriteMode { wmAllow, wmFail, wmIgnore };
  1741. //---------------------------------------------------------------------------
  1742. class TOptionsIniFile : public TCustomIniFile
  1743. {
  1744. public:
  1745. __fastcall TOptionsIniFile(TStrings * Options, TWriteMode WriteMode, const UnicodeString & RootKey);
  1746. virtual UnicodeString __fastcall ReadString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Default);
  1747. virtual void __fastcall WriteString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Value);
  1748. virtual void __fastcall ReadSection(const UnicodeString Section, TStrings * Strings);
  1749. virtual void __fastcall ReadSections(TStrings* Strings);
  1750. virtual void __fastcall ReadSectionValues(const UnicodeString Section, TStrings* Strings);
  1751. virtual void __fastcall EraseSection(const UnicodeString Section);
  1752. virtual void __fastcall DeleteKey(const UnicodeString Section, const UnicodeString Ident);
  1753. virtual void __fastcall UpdateFile();
  1754. // Hoisted overload
  1755. void __fastcall ReadSections(const UnicodeString Section, TStrings* Strings);
  1756. // Ntb, we can implement ValueExists more efficiently than the TCustomIniFile.ValueExists
  1757. private:
  1758. TStrings * FOptions;
  1759. TWriteMode FWriteMode;
  1760. UnicodeString FRootKey;
  1761. bool __fastcall AllowWrite();
  1762. bool __fastcall AllowSection(const UnicodeString & Section);
  1763. UnicodeString __fastcall FormatKey(const UnicodeString & Section, const UnicodeString & Ident);
  1764. };
  1765. //---------------------------------------------------------------------------
  1766. __fastcall TOptionsIniFile::TOptionsIniFile(TStrings * Options, TWriteMode WriteMode, const UnicodeString & RootKey) :
  1767. TCustomIniFile(UnicodeString())
  1768. {
  1769. FOptions = Options;
  1770. FWriteMode = WriteMode;
  1771. FRootKey = RootKey;
  1772. if (!FRootKey.IsEmpty())
  1773. {
  1774. FRootKey += PathDelim;
  1775. }
  1776. }
  1777. //---------------------------------------------------------------------------
  1778. bool __fastcall TOptionsIniFile::AllowWrite()
  1779. {
  1780. switch (FWriteMode)
  1781. {
  1782. case wmAllow:
  1783. return true;
  1784. case wmFail:
  1785. NotImplemented();
  1786. return false; // never gets here
  1787. case wmIgnore:
  1788. return false;
  1789. default:
  1790. DebugFail();
  1791. return false;
  1792. }
  1793. }
  1794. //---------------------------------------------------------------------------
  1795. bool __fastcall TOptionsIniFile::AllowSection(const UnicodeString & Section)
  1796. {
  1797. UnicodeString Name = Section;
  1798. if (!Name.IsEmpty())
  1799. {
  1800. Name += PathDelim;
  1801. }
  1802. bool Result = SameText(Name.SubString(1, FRootKey.Length()), FRootKey);
  1803. return Result;
  1804. }
  1805. //---------------------------------------------------------------------------
  1806. UnicodeString __fastcall TOptionsIniFile::FormatKey(const UnicodeString & Section, const UnicodeString & Ident)
  1807. {
  1808. UnicodeString Result = Section;
  1809. if (!Result.IsEmpty())
  1810. {
  1811. Result += PathDelim;
  1812. }
  1813. Result += Ident; // Can be empty, when called from a contructor, AllowSection or ReadSection
  1814. if (DebugAlwaysTrue(AllowSection(Section)))
  1815. {
  1816. Result.Delete(1, FRootKey.Length());
  1817. }
  1818. return Result;
  1819. }
  1820. //---------------------------------------------------------------------------
  1821. UnicodeString __fastcall TOptionsIniFile::ReadString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Default)
  1822. {
  1823. UnicodeString Value;
  1824. if (!AllowSection(Section))
  1825. {
  1826. Value = Default;
  1827. }
  1828. else
  1829. {
  1830. UnicodeString Name = FormatKey(Section, Ident);
  1831. int Index = FOptions->IndexOfName(Name);
  1832. if (Index >= 0)
  1833. {
  1834. Value = FOptions->ValueFromIndex[Index];
  1835. }
  1836. else
  1837. {
  1838. Value = Default;
  1839. }
  1840. }
  1841. return Value;
  1842. }
  1843. //---------------------------------------------------------------------------
  1844. void __fastcall TOptionsIniFile::WriteString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Value)
  1845. {
  1846. if (AllowWrite() &&
  1847. DebugAlwaysTrue(AllowSection(Section)))
  1848. {
  1849. UnicodeString Name = FormatKey(Section, Ident);
  1850. SetStringValueEvenIfEmpty(FOptions, Name, Value);
  1851. }
  1852. }
  1853. //---------------------------------------------------------------------------
  1854. void __fastcall TOptionsIniFile::ReadSection(const UnicodeString Section, TStrings * Strings)
  1855. {
  1856. if (AllowSection(Section))
  1857. {
  1858. UnicodeString SectionPrefix = FormatKey(Section, UnicodeString());
  1859. Strings->BeginUpdate();
  1860. try
  1861. {
  1862. for (int Index = 0; Index < FOptions->Count; Index++)
  1863. {
  1864. UnicodeString Name = FOptions->Names[Index];
  1865. if (SameText(Name.SubString(1, SectionPrefix.Length()), SectionPrefix) &&
  1866. (LastDelimiter(PathDelim, Name) <= SectionPrefix.Length()))
  1867. {
  1868. Strings->Add(Name.SubString(SectionPrefix.Length() + 1, Name.Length() - SectionPrefix.Length()));
  1869. }
  1870. }
  1871. }
  1872. __finally
  1873. {
  1874. Strings->EndUpdate();
  1875. }
  1876. }
  1877. }
  1878. //---------------------------------------------------------------------------
  1879. void __fastcall TOptionsIniFile::ReadSections(TStrings * Strings)
  1880. {
  1881. std::unique_ptr<TStringList> Sections(CreateSortedStringList());
  1882. for (int Index = 0; Index < FOptions->Count; Index++)
  1883. {
  1884. UnicodeString Name = FOptions->Names[Index];
  1885. int P = LastDelimiter(PathDelim, Name);
  1886. if (P > 0)
  1887. {
  1888. UnicodeString Section = Name.SubString(1, P - 1);
  1889. if (Sections->IndexOf(Section) < 0)
  1890. {
  1891. Sections->Add(Section);
  1892. }
  1893. }
  1894. }
  1895. for (int Index = 0; Index < Sections->Count; Index++)
  1896. {
  1897. Strings->Add(FRootKey + Sections->Strings[Index]);
  1898. }
  1899. }
  1900. //---------------------------------------------------------------------------
  1901. void __fastcall TOptionsIniFile::ReadSectionValues(const UnicodeString Section, TStrings * /*Strings*/)
  1902. {
  1903. NotImplemented();
  1904. }
  1905. //---------------------------------------------------------------------------
  1906. void __fastcall TOptionsIniFile::EraseSection(const UnicodeString Section)
  1907. {
  1908. if (AllowWrite())
  1909. {
  1910. NotImplemented();
  1911. }
  1912. }
  1913. //---------------------------------------------------------------------------
  1914. void __fastcall TOptionsIniFile::DeleteKey(const UnicodeString Section, const UnicodeString Ident)
  1915. {
  1916. if (AllowWrite() &&
  1917. DebugAlwaysTrue(AllowSection(Section)))
  1918. {
  1919. UnicodeString Name = FormatKey(Section, Ident);
  1920. int Index = FOptions->IndexOfName(Name);
  1921. if (Index >= 0)
  1922. {
  1923. FOptions->Delete(Index);
  1924. }
  1925. }
  1926. }
  1927. //---------------------------------------------------------------------------
  1928. void __fastcall TOptionsIniFile::UpdateFile()
  1929. {
  1930. if (AllowWrite())
  1931. {
  1932. // noop
  1933. }
  1934. }
  1935. //---------------------------------------------------------------------------
  1936. void __fastcall TOptionsIniFile::ReadSections(const UnicodeString /*Section*/, TStrings * /*Strings*/)
  1937. {
  1938. NotImplemented();
  1939. }
  1940. //===========================================================================
  1941. __fastcall TOptionsStorage::TOptionsStorage(TStrings * Options, bool AllowWrite):
  1942. TCustomIniFileStorage(
  1943. UnicodeString(L"Command-line options"),
  1944. new TOptionsIniFile(Options, (AllowWrite ? wmAllow : wmFail), UnicodeString()))
  1945. {
  1946. }
  1947. //---------------------------------------------------------------------------
  1948. __fastcall TOptionsStorage::TOptionsStorage(TStrings * Options, const UnicodeString & RootKey, THierarchicalStorage * MasterStorage) :
  1949. TCustomIniFileStorage(
  1950. UnicodeString(L"Command-line options overriding " + MasterStorage->Source),
  1951. new TOptionsIniFile(Options, wmIgnore, RootKey))
  1952. {
  1953. FMasterStorage.reset(MasterStorage);
  1954. }
  1955. //---------------------------------------------------------------------------
  1956. bool __fastcall TOptionsStorage::GetTemporary()
  1957. {
  1958. return true;
  1959. }