HierarchicalStorage.cpp 63 KB

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