HierarchicalStorage.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  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(AnsiString(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. UnicodeString __fastcall PuttyMungeStr(const UnicodeString & Str)
  72. {
  73. return MungeStr(Str, false, false);
  74. }
  75. //---------------------------------------------------------------------------
  76. UnicodeString __fastcall MungeIniName(const UnicodeString & Str)
  77. {
  78. int P = Str.Pos(L"=");
  79. // make this fast for now
  80. if (P > 0)
  81. {
  82. return ReplaceStr(Str, L"=", L"%3D");
  83. }
  84. else
  85. {
  86. return Str;
  87. }
  88. }
  89. //---------------------------------------------------------------------------
  90. UnicodeString __fastcall UnMungeIniName(const UnicodeString & Str)
  91. {
  92. int P = Str.Pos(L"%3D");
  93. // make this fast for now
  94. if (P > 0)
  95. {
  96. return ReplaceStr(Str, L"%3D", L"=");
  97. }
  98. else
  99. {
  100. return Str;
  101. }
  102. }
  103. //===========================================================================
  104. __fastcall THierarchicalStorage::THierarchicalStorage(const UnicodeString & AStorage)
  105. {
  106. FStorage = AStorage;
  107. FKeyHistory = new TStringList();
  108. AccessMode = smRead;
  109. Explicit = false;
  110. ForceSave = false;
  111. // While this was implemented in 5.0 already, for some reason
  112. // it was disabled (by mistake?). So although enabled for 5.6.1 only,
  113. // data written in Unicode/UTF8 can be read by all versions back to 5.0.
  114. ForceAnsi = false;
  115. MungeStringValues = true;
  116. }
  117. //---------------------------------------------------------------------------
  118. __fastcall THierarchicalStorage::~THierarchicalStorage()
  119. {
  120. delete FKeyHistory;
  121. }
  122. //---------------------------------------------------------------------------
  123. void __fastcall THierarchicalStorage::Flush()
  124. {
  125. }
  126. //---------------------------------------------------------------------------
  127. void __fastcall THierarchicalStorage::SetAccessMode(TStorageAccessMode value)
  128. {
  129. FAccessMode = value;
  130. }
  131. //---------------------------------------------------------------------------
  132. UnicodeString __fastcall THierarchicalStorage::GetCurrentSubKeyMunged()
  133. {
  134. if (FKeyHistory->Count > 0)
  135. {
  136. return FKeyHistory->Strings[FKeyHistory->Count - 1];
  137. }
  138. else
  139. {
  140. return UnicodeString();
  141. }
  142. }
  143. //---------------------------------------------------------------------------
  144. UnicodeString __fastcall THierarchicalStorage::GetCurrentSubKey()
  145. {
  146. return UnMungeStr(GetCurrentSubKeyMunged());
  147. }
  148. //---------------------------------------------------------------------------
  149. bool __fastcall THierarchicalStorage::OpenRootKey(bool CanCreate)
  150. {
  151. return OpenSubKey(UnicodeString(), CanCreate);
  152. }
  153. //---------------------------------------------------------------------------
  154. UnicodeString __fastcall THierarchicalStorage::MungeKeyName(const UnicodeString & Key)
  155. {
  156. UnicodeString Result = MungeStr(Key, ForceAnsi, false);
  157. // if there's already ANSI-munged subkey, keep ANSI munging
  158. if ((Result != Key) && !ForceAnsi && DoKeyExists(Key, true))
  159. {
  160. Result = MungeStr(Key, true, false);
  161. }
  162. return Result;
  163. }
  164. //---------------------------------------------------------------------------
  165. bool __fastcall THierarchicalStorage::OpenSubKey(const UnicodeString & Key, bool CanCreate, bool Path)
  166. {
  167. UnicodeString MungedKey;
  168. if (Path)
  169. {
  170. DebugAssert(Key.IsEmpty() || (Key[Key.Length()] != L'\\'));
  171. UnicodeString Buf(Key);
  172. while (!Buf.IsEmpty())
  173. {
  174. if (!MungedKey.IsEmpty())
  175. {
  176. MungedKey += L'\\';
  177. }
  178. MungedKey += MungeKeyName(CutToChar(Buf, L'\\', false));
  179. }
  180. }
  181. else
  182. {
  183. MungedKey = MungeKeyName(Key);
  184. }
  185. bool Result = DoOpenSubKey(MungedKey, CanCreate);
  186. if (Result)
  187. {
  188. FKeyHistory->Add(IncludeTrailingBackslash(CurrentSubKey+MungedKey));
  189. }
  190. return Result;
  191. }
  192. //---------------------------------------------------------------------------
  193. void __fastcall THierarchicalStorage::CloseSubKey()
  194. {
  195. if (FKeyHistory->Count == 0)
  196. {
  197. throw Exception(UnicodeString());
  198. }
  199. else
  200. {
  201. FKeyHistory->Delete(FKeyHistory->Count - 1);
  202. }
  203. }
  204. //---------------------------------------------------------------------------
  205. void __fastcall THierarchicalStorage::CloseAll()
  206. {
  207. while (!CurrentSubKey.IsEmpty())
  208. {
  209. CloseSubKey();
  210. }
  211. }
  212. //---------------------------------------------------------------------------
  213. void __fastcall THierarchicalStorage::ClearSubKeys()
  214. {
  215. std::unique_ptr<TStringList> SubKeys(new TStringList());
  216. GetSubKeyNames(SubKeys.get());
  217. for (int Index = 0; Index < SubKeys->Count; Index++)
  218. {
  219. RecursiveDeleteSubKey(SubKeys->Strings[Index]);
  220. }
  221. }
  222. //---------------------------------------------------------------------------
  223. void __fastcall THierarchicalStorage::RecursiveDeleteSubKey(const UnicodeString & Key)
  224. {
  225. if (OpenSubKey(Key, false))
  226. {
  227. ClearSubKeys();
  228. CloseSubKey();
  229. }
  230. DeleteSubKey(Key);
  231. }
  232. //---------------------------------------------------------------------------
  233. bool __fastcall THierarchicalStorage::HasSubKeys()
  234. {
  235. std::unique_ptr<TStrings> SubKeys(new TStringList());
  236. GetSubKeyNames(SubKeys.get());
  237. bool Result = (SubKeys->Count > 0);
  238. return Result;
  239. }
  240. //---------------------------------------------------------------------------
  241. bool __fastcall THierarchicalStorage::HasSubKey(const UnicodeString & SubKey)
  242. {
  243. bool Result = OpenSubKey(SubKey, false);
  244. if (Result)
  245. {
  246. CloseSubKey();
  247. }
  248. return Result;
  249. }
  250. //---------------------------------------------------------------------------
  251. bool __fastcall THierarchicalStorage::KeyExists(const UnicodeString & SubKey)
  252. {
  253. return DoKeyExists(SubKey, ForceAnsi);
  254. }
  255. //---------------------------------------------------------------------------
  256. void __fastcall THierarchicalStorage::ReadValues(TStrings * Strings, bool MaintainKeys)
  257. {
  258. std::unique_ptr<TStrings> Names(new TStringList());
  259. GetValueNames(Names.get());
  260. for (int Index = 0; Index < Names->Count; Index++)
  261. {
  262. if (MaintainKeys)
  263. {
  264. Strings->Add(FORMAT(L"%s=%s", (Names->Strings[Index], ReadString(Names->Strings[Index], UnicodeString()))));
  265. }
  266. else
  267. {
  268. Strings->Add(ReadString(Names->Strings[Index], UnicodeString()));
  269. }
  270. }
  271. }
  272. //---------------------------------------------------------------------------
  273. void __fastcall THierarchicalStorage::ClearValues()
  274. {
  275. std::unique_ptr<TStrings> Names(new TStringList());
  276. GetValueNames(Names.get());
  277. for (int Index = 0; Index < Names->Count; Index++)
  278. {
  279. DeleteValue(Names->Strings[Index]);
  280. }
  281. }
  282. //---------------------------------------------------------------------------
  283. void __fastcall THierarchicalStorage::WriteValues(TStrings * Strings, bool MaintainKeys)
  284. {
  285. ClearValues();
  286. if (Strings != NULL)
  287. {
  288. for (int Index = 0; Index < Strings->Count; Index++)
  289. {
  290. if (MaintainKeys)
  291. {
  292. DebugAssert(Strings->Strings[Index].Pos(L"=") > 1);
  293. WriteString(Strings->Names[Index], Strings->Values[Strings->Names[Index]]);
  294. }
  295. else
  296. {
  297. WriteString(IntToStr(Index), Strings->Strings[Index]);
  298. }
  299. }
  300. }
  301. }
  302. //---------------------------------------------------------------------------
  303. UnicodeString __fastcall THierarchicalStorage::ReadString(const UnicodeString & Name, const UnicodeString & Default)
  304. {
  305. UnicodeString Result;
  306. if (MungeStringValues)
  307. {
  308. Result = UnMungeStr(ReadStringRaw(Name, MungeStr(Default, ForceAnsi, true)));
  309. }
  310. else
  311. {
  312. Result = ReadStringRaw(Name, Default);
  313. }
  314. return Result;
  315. }
  316. //---------------------------------------------------------------------------
  317. RawByteString __fastcall THierarchicalStorage::ReadBinaryData(const UnicodeString & Name)
  318. {
  319. size_t Size = BinaryDataSize(Name);
  320. RawByteString Value;
  321. Value.SetLength(Size);
  322. ReadBinaryData(Name, Value.c_str(), Size);
  323. return Value;
  324. }
  325. //---------------------------------------------------------------------------
  326. RawByteString __fastcall THierarchicalStorage::ReadStringAsBinaryData(const UnicodeString & Name, const RawByteString & Default)
  327. {
  328. UnicodeString UnicodeDefault = AnsiToString(Default);
  329. // This should be exactly the same operation as calling ReadString in
  330. // C++Builder 6 (non-Unicode) on Unicode-based OS
  331. // (conversion is done by Ansi layer of the OS)
  332. UnicodeString String = ReadString(Name, UnicodeDefault);
  333. AnsiString Ansi = AnsiString(String);
  334. RawByteString Result = RawByteString(Ansi.c_str(), Ansi.Length());
  335. return Result;
  336. }
  337. //---------------------------------------------------------------------------
  338. void __fastcall THierarchicalStorage::WriteString(const UnicodeString & Name, const UnicodeString & Value)
  339. {
  340. if (MungeStringValues)
  341. {
  342. WriteStringRaw(Name, MungeStr(Value, ForceAnsi, true));
  343. }
  344. else
  345. {
  346. WriteStringRaw(Name, Value);
  347. }
  348. }
  349. //---------------------------------------------------------------------------
  350. void __fastcall THierarchicalStorage::WriteBinaryData(const UnicodeString & Name, const RawByteString & Value)
  351. {
  352. WriteBinaryData(Name, Value.c_str(), Value.Length());
  353. }
  354. //---------------------------------------------------------------------------
  355. void __fastcall THierarchicalStorage::WriteBinaryDataAsString(const UnicodeString & Name, const RawByteString & Value)
  356. {
  357. // This should be exactly the same operation as calling WriteString in
  358. // C++Builder 6 (non-Unicode) on Unicode-based OS
  359. // (conversion is done by Ansi layer of the OS)
  360. WriteString(Name, AnsiToString(Value));
  361. }
  362. //---------------------------------------------------------------------------
  363. UnicodeString __fastcall THierarchicalStorage::IncludeTrailingBackslash(const UnicodeString & S)
  364. {
  365. // expanded from ?: as it caused memory leaks
  366. if (S.IsEmpty())
  367. {
  368. return S;
  369. }
  370. else
  371. {
  372. return ::IncludeTrailingBackslash(S);
  373. }
  374. }
  375. //---------------------------------------------------------------------------
  376. UnicodeString __fastcall THierarchicalStorage::ExcludeTrailingBackslash(const UnicodeString & S)
  377. {
  378. // expanded from ?: as it caused memory leaks
  379. if (S.IsEmpty())
  380. {
  381. return S;
  382. }
  383. else
  384. {
  385. return ::ExcludeTrailingBackslash(S);
  386. }
  387. }
  388. //---------------------------------------------------------------------------
  389. bool __fastcall THierarchicalStorage::GetTemporary()
  390. {
  391. return false;
  392. }
  393. //===========================================================================
  394. __fastcall TRegistryStorage::TRegistryStorage(const UnicodeString & AStorage) :
  395. THierarchicalStorage(IncludeTrailingBackslash(AStorage))
  396. {
  397. FWowMode = 0;
  398. Init();
  399. };
  400. //---------------------------------------------------------------------------
  401. __fastcall TRegistryStorage::TRegistryStorage(const UnicodeString & AStorage, HKEY ARootKey, REGSAM WowMode):
  402. THierarchicalStorage(IncludeTrailingBackslash(AStorage))
  403. {
  404. FWowMode = WowMode;
  405. Init();
  406. FRegistry->RootKey = ARootKey;
  407. }
  408. //---------------------------------------------------------------------------
  409. void __fastcall TRegistryStorage::Init()
  410. {
  411. FRegistry = new TRegistry();
  412. FRegistry->Access = KEY_READ | FWowMode;
  413. }
  414. //---------------------------------------------------------------------------
  415. __fastcall TRegistryStorage::~TRegistryStorage()
  416. {
  417. delete FRegistry;
  418. };
  419. //---------------------------------------------------------------------------
  420. bool __fastcall TRegistryStorage::Copy(TRegistryStorage * Storage)
  421. {
  422. TRegistry * Registry = Storage->FRegistry;
  423. bool Result = true;
  424. std::unique_ptr<TStrings> Names(new TStringList());
  425. Registry->GetValueNames(Names.get());
  426. std::vector<unsigned char> Buffer(1024, 0);
  427. int Index = 0;
  428. while ((Index < Names->Count) && Result)
  429. {
  430. UnicodeString Name = MungeStr(Names->Strings[Index], ForceAnsi, false);
  431. unsigned long Size = Buffer.size();
  432. unsigned long Type;
  433. int RegResult;
  434. do
  435. {
  436. RegResult = RegQueryValueEx(Registry->CurrentKey, Name.c_str(), NULL, &Type, &Buffer[0], &Size);
  437. if (RegResult == ERROR_MORE_DATA)
  438. {
  439. Buffer.resize(Size);
  440. }
  441. } while (RegResult == ERROR_MORE_DATA);
  442. Result = (RegResult == ERROR_SUCCESS);
  443. if (Result)
  444. {
  445. RegResult = RegSetValueEx(FRegistry->CurrentKey, Name.c_str(), NULL, Type, &Buffer[0], Size);
  446. Result = (RegResult == ERROR_SUCCESS);
  447. }
  448. ++Index;
  449. }
  450. return Result;
  451. }
  452. //---------------------------------------------------------------------------
  453. UnicodeString __fastcall TRegistryStorage::GetSource()
  454. {
  455. return RootKeyToStr(FRegistry->RootKey) + L"\\" + Storage;
  456. }
  457. //---------------------------------------------------------------------------
  458. void __fastcall TRegistryStorage::SetAccessMode(TStorageAccessMode value)
  459. {
  460. THierarchicalStorage::SetAccessMode(value);
  461. if (FRegistry)
  462. {
  463. switch (AccessMode) {
  464. case smRead:
  465. FRegistry->Access = KEY_READ | FWowMode;
  466. break;
  467. case smReadWrite:
  468. default:
  469. FRegistry->Access = KEY_READ | KEY_WRITE | FWowMode;
  470. break;
  471. }
  472. }
  473. }
  474. //---------------------------------------------------------------------------
  475. bool __fastcall TRegistryStorage::DoOpenSubKey(const UnicodeString & SubKey, bool CanCreate)
  476. {
  477. UnicodeString PrevPath;
  478. bool WasOpened = (FRegistry->CurrentKey != NULL);
  479. if (WasOpened)
  480. {
  481. PrevPath = FRegistry->CurrentPath;
  482. DebugAssert(SamePaths(PrevPath, Storage + GetCurrentSubKeyMunged()));
  483. FRegistry->CloseKey();
  484. }
  485. UnicodeString K = ExcludeTrailingBackslash(Storage + CurrentSubKey + SubKey);
  486. bool Result = FRegistry->OpenKey(K, CanCreate);
  487. if (!Result && WasOpened)
  488. {
  489. FRegistry->OpenKey(PrevPath, false);
  490. }
  491. return Result;
  492. }
  493. //---------------------------------------------------------------------------
  494. void __fastcall TRegistryStorage::CloseSubKey()
  495. {
  496. FRegistry->CloseKey();
  497. THierarchicalStorage::CloseSubKey();
  498. if (FKeyHistory->Count)
  499. {
  500. FRegistry->OpenKey(Storage + GetCurrentSubKeyMunged(), True);
  501. }
  502. }
  503. //---------------------------------------------------------------------------
  504. bool __fastcall TRegistryStorage::DeleteSubKey(const UnicodeString & SubKey)
  505. {
  506. UnicodeString K;
  507. if (FKeyHistory->Count == 0)
  508. {
  509. K = Storage + CurrentSubKey;
  510. }
  511. K += MungeKeyName(SubKey);
  512. return FRegistry->DeleteKey(K);
  513. }
  514. //---------------------------------------------------------------------------
  515. void __fastcall TRegistryStorage::GetSubKeyNames(TStrings * Strings)
  516. {
  517. FRegistry->GetKeyNames(Strings);
  518. for (int Index = 0; Index < Strings->Count; Index++)
  519. {
  520. Strings->Strings[Index] = UnMungeStr(Strings->Strings[Index]);
  521. }
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TRegistryStorage::GetValueNames(TStrings * Strings)
  525. {
  526. FRegistry->GetValueNames(Strings);
  527. }
  528. //---------------------------------------------------------------------------
  529. bool __fastcall TRegistryStorage::DeleteValue(const UnicodeString & Name)
  530. {
  531. return FRegistry->DeleteValue(Name);
  532. }
  533. //---------------------------------------------------------------------------
  534. bool __fastcall TRegistryStorage::DoKeyExists(const UnicodeString & SubKey, bool AForceAnsi)
  535. {
  536. UnicodeString K = MungeStr(SubKey, AForceAnsi, false);
  537. bool Result = FRegistry->KeyExists(K);
  538. return Result;
  539. }
  540. //---------------------------------------------------------------------------
  541. bool __fastcall TRegistryStorage::ValueExists(const UnicodeString & Value)
  542. {
  543. bool Result = FRegistry->ValueExists(Value);
  544. return Result;
  545. }
  546. //---------------------------------------------------------------------------
  547. size_t __fastcall TRegistryStorage::BinaryDataSize(const UnicodeString & Name)
  548. {
  549. size_t Result = FRegistry->GetDataSize(Name);
  550. return Result;
  551. }
  552. //---------------------------------------------------------------------------
  553. bool __fastcall TRegistryStorage::ReadBool(const UnicodeString & Name, bool Default)
  554. {
  555. READ_REGISTRY(ReadBool);
  556. }
  557. //---------------------------------------------------------------------------
  558. TDateTime __fastcall TRegistryStorage::ReadDateTime(const UnicodeString & Name, TDateTime Default)
  559. {
  560. READ_REGISTRY(ReadDateTime);
  561. }
  562. //---------------------------------------------------------------------------
  563. double __fastcall TRegistryStorage::ReadFloat(const UnicodeString & Name, double Default)
  564. {
  565. READ_REGISTRY(ReadFloat);
  566. }
  567. //---------------------------------------------------------------------------
  568. int __fastcall TRegistryStorage::ReadInteger(const UnicodeString & Name, int Default)
  569. {
  570. READ_REGISTRY(ReadInteger);
  571. }
  572. //---------------------------------------------------------------------------
  573. __int64 __fastcall TRegistryStorage::ReadInt64(const UnicodeString & Name, __int64 Default)
  574. {
  575. __int64 Result;
  576. if (ReadBinaryData(Name, &Result, sizeof(Result)) == 0)
  577. {
  578. Result = Default;
  579. }
  580. return Result;
  581. }
  582. //---------------------------------------------------------------------------
  583. UnicodeString __fastcall TRegistryStorage::ReadStringRaw(const UnicodeString & Name, const UnicodeString & Default)
  584. {
  585. READ_REGISTRY(ReadString);
  586. }
  587. //---------------------------------------------------------------------------
  588. size_t __fastcall TRegistryStorage::ReadBinaryData(const UnicodeString & Name, void * Buffer, size_t Size)
  589. {
  590. size_t Result;
  591. if (FRegistry->ValueExists(Name))
  592. {
  593. try
  594. {
  595. Result = FRegistry->ReadBinaryData(Name, Buffer, Size);
  596. }
  597. catch(...)
  598. {
  599. Result = 0;
  600. }
  601. }
  602. else
  603. {
  604. Result = 0;
  605. }
  606. return Result;
  607. }
  608. //---------------------------------------------------------------------------
  609. void __fastcall TRegistryStorage::WriteBool(const UnicodeString & Name, bool Value)
  610. {
  611. WRITE_REGISTRY(WriteBool);
  612. }
  613. //---------------------------------------------------------------------------
  614. void __fastcall TRegistryStorage::WriteDateTime(const UnicodeString & Name, TDateTime Value)
  615. {
  616. WRITE_REGISTRY(WriteDateTime);
  617. }
  618. //---------------------------------------------------------------------------
  619. void __fastcall TRegistryStorage::WriteFloat(const UnicodeString & Name, double Value)
  620. {
  621. WRITE_REGISTRY(WriteFloat);
  622. }
  623. //---------------------------------------------------------------------------
  624. void __fastcall TRegistryStorage::WriteStringRaw(const UnicodeString & Name, const UnicodeString & Value)
  625. {
  626. WRITE_REGISTRY(WriteString);
  627. }
  628. //---------------------------------------------------------------------------
  629. void __fastcall TRegistryStorage::WriteInteger(const UnicodeString & Name, int Value)
  630. {
  631. WRITE_REGISTRY(WriteInteger);
  632. }
  633. //---------------------------------------------------------------------------
  634. void __fastcall TRegistryStorage::WriteInt64(const UnicodeString & Name, __int64 Value)
  635. {
  636. WriteBinaryData(Name, &Value, sizeof(Value));
  637. }
  638. //---------------------------------------------------------------------------
  639. void __fastcall TRegistryStorage::WriteBinaryData(const UnicodeString & Name, const void * Buffer, int Size)
  640. {
  641. try
  642. {
  643. FRegistry->WriteBinaryData(Name, const_cast<void *>(Buffer), Size);
  644. }
  645. catch(...)
  646. {
  647. }
  648. }
  649. //===========================================================================
  650. __fastcall TCustomIniFileStorage::TCustomIniFileStorage(const UnicodeString & Storage, TCustomIniFile * IniFile) :
  651. THierarchicalStorage(Storage),
  652. FIniFile(IniFile),
  653. FMasterStorageOpenFailures(0),
  654. FOpeningSubKey(false)
  655. {
  656. }
  657. //---------------------------------------------------------------------------
  658. __fastcall TCustomIniFileStorage::~TCustomIniFileStorage()
  659. {
  660. delete FIniFile;
  661. }
  662. //---------------------------------------------------------------------------
  663. UnicodeString __fastcall TCustomIniFileStorage::GetSource()
  664. {
  665. return Storage;
  666. }
  667. //---------------------------------------------------------------------------
  668. UnicodeString __fastcall TCustomIniFileStorage::GetCurrentSection()
  669. {
  670. return ExcludeTrailingBackslash(GetCurrentSubKeyMunged());
  671. }
  672. //---------------------------------------------------------------------------
  673. void __fastcall TCustomIniFileStorage::CacheSections()
  674. {
  675. if (FSections.get() == NULL)
  676. {
  677. FSections.reset(new TStringList());
  678. FIniFile->ReadSections(FSections.get());
  679. FSections->Sorted = true; // has to set only after reading as ReadSections reset it to false
  680. }
  681. }
  682. //---------------------------------------------------------------------------
  683. void __fastcall TCustomIniFileStorage::ResetCache()
  684. {
  685. FSections.reset(NULL);
  686. }
  687. //---------------------------------------------------------------------------
  688. void __fastcall TCustomIniFileStorage::SetAccessMode(TStorageAccessMode value)
  689. {
  690. if (FMasterStorage.get() != NULL)
  691. {
  692. FMasterStorage->AccessMode = value;
  693. }
  694. THierarchicalStorage::SetAccessMode(value);
  695. }
  696. //---------------------------------------------------------------------------
  697. bool __fastcall TCustomIniFileStorage::DoOpenSubKey(const UnicodeString & SubKey, bool CanCreate)
  698. {
  699. bool Result = CanCreate;
  700. if (!Result)
  701. {
  702. CacheSections();
  703. UnicodeString NewKey = ExcludeTrailingBackslash(CurrentSubKey+SubKey);
  704. if (FSections->Count > 0)
  705. {
  706. int Index = -1;
  707. Result = FSections->Find(NewKey, Index);
  708. if (!Result &&
  709. (Index < FSections->Count) &&
  710. (FSections->Strings[Index].SubString(1, NewKey.Length()+1) == NewKey + L"\\"))
  711. {
  712. Result = true;
  713. }
  714. }
  715. }
  716. return Result;
  717. }
  718. //---------------------------------------------------------------------------
  719. bool __fastcall TCustomIniFileStorage::OpenRootKey(bool CanCreate)
  720. {
  721. // Not supported with master storage.
  722. // Actually currently, we use OpenRootKey with TRegistryStorage only.
  723. DebugAssert(FMasterStorage.get() == NULL);
  724. return THierarchicalStorage::OpenRootKey(CanCreate);
  725. }
  726. //---------------------------------------------------------------------------
  727. bool __fastcall TCustomIniFileStorage::OpenSubKey(const UnicodeString & Key, bool CanCreate, bool Path)
  728. {
  729. bool Result;
  730. {
  731. TAutoFlag Flag(FOpeningSubKey);
  732. Result = THierarchicalStorage::OpenSubKey(Key, CanCreate, Path);
  733. }
  734. if (FMasterStorage.get() != NULL)
  735. {
  736. if (FMasterStorageOpenFailures > 0)
  737. {
  738. FMasterStorageOpenFailures++;
  739. }
  740. else
  741. {
  742. bool MasterResult = FMasterStorage->OpenSubKey(Key, CanCreate, Path);
  743. if (!Result && MasterResult)
  744. {
  745. Result = THierarchicalStorage::OpenSubKey(Key, true, Path);
  746. DebugAssert(Result);
  747. }
  748. else if (Result && !MasterResult)
  749. {
  750. FMasterStorageOpenFailures++;
  751. }
  752. }
  753. }
  754. return Result;
  755. }
  756. //---------------------------------------------------------------------------
  757. void __fastcall TCustomIniFileStorage::CloseSubKey()
  758. {
  759. THierarchicalStorage::CloseSubKey();
  760. // What we are called to restore previous key from OpenSubKey,
  761. // when opening path component fails, the master storage was not involved yet
  762. if (!FOpeningSubKey && (FMasterStorage.get() != NULL))
  763. {
  764. if (FMasterStorageOpenFailures > 0)
  765. {
  766. FMasterStorageOpenFailures--;
  767. }
  768. else
  769. {
  770. FMasterStorage->CloseSubKey();
  771. }
  772. }
  773. }
  774. //---------------------------------------------------------------------------
  775. bool __fastcall TCustomIniFileStorage::DeleteSubKey(const UnicodeString & SubKey)
  776. {
  777. bool Result;
  778. try
  779. {
  780. ResetCache();
  781. FIniFile->EraseSection(CurrentSubKey + MungeKeyName(SubKey));
  782. Result = true;
  783. }
  784. catch (...)
  785. {
  786. Result = false;
  787. }
  788. if (HandleByMasterStorage())
  789. {
  790. Result = FMasterStorage->DeleteSubKey(SubKey);
  791. }
  792. return Result;
  793. }
  794. //---------------------------------------------------------------------------
  795. void __fastcall TCustomIniFileStorage::GetSubKeyNames(TStrings * Strings)
  796. {
  797. Strings->Clear();
  798. if (HandleByMasterStorage())
  799. {
  800. FMasterStorage->GetSubKeyNames(Strings);
  801. }
  802. CacheSections();
  803. for (int i = 0; i < FSections->Count; i++)
  804. {
  805. UnicodeString Section = FSections->Strings[i];
  806. if (AnsiCompareText(CurrentSubKey,
  807. Section.SubString(1, CurrentSubKey.Length())) == 0)
  808. {
  809. UnicodeString SubSection = Section.SubString(CurrentSubKey.Length() + 1,
  810. Section.Length() - CurrentSubKey.Length());
  811. int P = SubSection.Pos(L"\\");
  812. if (P)
  813. {
  814. SubSection.SetLength(P - 1);
  815. }
  816. if (Strings->IndexOf(SubSection) < 0)
  817. {
  818. Strings->Add(UnMungeStr(SubSection));
  819. }
  820. }
  821. }
  822. }
  823. //---------------------------------------------------------------------------
  824. void __fastcall TCustomIniFileStorage::GetValueNames(TStrings * Strings)
  825. {
  826. if (HandleByMasterStorage())
  827. {
  828. FMasterStorage->GetValueNames(Strings);
  829. }
  830. FIniFile->ReadSection(CurrentSection, Strings);
  831. for (int Index = 0; Index < Strings->Count; Index++)
  832. {
  833. Strings->Strings[Index] = UnMungeIniName(Strings->Strings[Index]);
  834. }
  835. }
  836. //---------------------------------------------------------------------------
  837. bool __fastcall TCustomIniFileStorage::DoKeyExists(const UnicodeString & SubKey, bool AForceAnsi)
  838. {
  839. return
  840. (HandleByMasterStorage() && FMasterStorage->DoKeyExists(SubKey, AForceAnsi)) ||
  841. FIniFile->SectionExists(CurrentSubKey + MungeStr(SubKey, AForceAnsi, false));
  842. }
  843. //---------------------------------------------------------------------------
  844. bool __fastcall TCustomIniFileStorage::DoValueExists(const UnicodeString & Value)
  845. {
  846. return FIniFile->ValueExists(CurrentSection, MungeIniName(Value));
  847. }
  848. //---------------------------------------------------------------------------
  849. bool __fastcall TCustomIniFileStorage::ValueExists(const UnicodeString & Value)
  850. {
  851. return
  852. (HandleByMasterStorage() && FMasterStorage->ValueExists(Value)) ||
  853. DoValueExists(Value);
  854. }
  855. //---------------------------------------------------------------------------
  856. bool __fastcall TCustomIniFileStorage::DeleteValue(const UnicodeString & Name)
  857. {
  858. bool Result = true;
  859. if (HandleByMasterStorage())
  860. {
  861. Result = FMasterStorage->DeleteValue(Name);
  862. }
  863. ResetCache();
  864. FIniFile->DeleteKey(CurrentSection, MungeIniName(Name));
  865. return Result;
  866. }
  867. //---------------------------------------------------------------------------
  868. bool __fastcall TCustomIniFileStorage::HandleByMasterStorage()
  869. {
  870. return
  871. (FMasterStorage.get() != NULL) &&
  872. (FMasterStorageOpenFailures == 0);
  873. }
  874. //---------------------------------------------------------------------------
  875. bool __fastcall TCustomIniFileStorage::HandleReadByMasterStorage(const UnicodeString & Name)
  876. {
  877. return HandleByMasterStorage() && !DoValueExists(Name);
  878. }
  879. //---------------------------------------------------------------------------
  880. size_t __fastcall TCustomIniFileStorage::BinaryDataSize(const UnicodeString & Name)
  881. {
  882. if (HandleReadByMasterStorage(Name))
  883. {
  884. return FMasterStorage->BinaryDataSize(Name);
  885. }
  886. else
  887. {
  888. return ReadStringRaw(Name, L"").Length() / 2;
  889. }
  890. }
  891. //---------------------------------------------------------------------------
  892. bool __fastcall TCustomIniFileStorage::ReadBool(const UnicodeString & Name, bool Default)
  893. {
  894. if (HandleReadByMasterStorage(Name))
  895. {
  896. return FMasterStorage->ReadBool(Name, Default);
  897. }
  898. else
  899. {
  900. return FIniFile->ReadBool(CurrentSection, MungeIniName(Name), Default);
  901. }
  902. }
  903. //---------------------------------------------------------------------------
  904. int __fastcall TCustomIniFileStorage::ReadInteger(const UnicodeString & Name, int Default)
  905. {
  906. int Result;
  907. if (HandleReadByMasterStorage(Name))
  908. {
  909. Result = FMasterStorage->ReadInteger(Name, Default);
  910. }
  911. else
  912. {
  913. Result = FIniFile->ReadInteger(CurrentSection, MungeIniName(Name), Default);
  914. }
  915. return Result;
  916. }
  917. //---------------------------------------------------------------------------
  918. __int64 __fastcall TCustomIniFileStorage::ReadInt64(const UnicodeString & Name, __int64 Default)
  919. {
  920. __int64 Result;
  921. if (HandleReadByMasterStorage(Name))
  922. {
  923. Result = FMasterStorage->ReadInt64(Name, Default);
  924. }
  925. else
  926. {
  927. Result = Default;
  928. UnicodeString Str;
  929. Str = ReadStringRaw(Name, L"");
  930. if (!Str.IsEmpty())
  931. {
  932. Result = StrToInt64Def(Str, Default);
  933. }
  934. }
  935. return Result;
  936. }
  937. //---------------------------------------------------------------------------
  938. TDateTime __fastcall TCustomIniFileStorage::ReadDateTime(const UnicodeString & Name, TDateTime Default)
  939. {
  940. TDateTime Result;
  941. if (HandleReadByMasterStorage(Name))
  942. {
  943. Result = FMasterStorage->ReadDateTime(Name, Default);
  944. }
  945. else
  946. {
  947. UnicodeString Value = FIniFile->ReadString(CurrentSection, MungeIniName(Name), L"");
  948. if (Value.IsEmpty())
  949. {
  950. Result = Default;
  951. }
  952. else
  953. {
  954. try
  955. {
  956. RawByteString Raw = HexToBytes(Value);
  957. if (static_cast<size_t>(Raw.Length()) == sizeof(Result))
  958. {
  959. memcpy(&Result, Raw.c_str(), sizeof(Result));
  960. }
  961. else
  962. {
  963. Result = StrToDateTime(Value);
  964. }
  965. }
  966. catch(...)
  967. {
  968. Result = Default;
  969. }
  970. }
  971. }
  972. return Result;
  973. }
  974. //---------------------------------------------------------------------------
  975. double __fastcall TCustomIniFileStorage::ReadFloat(const UnicodeString & Name, double Default)
  976. {
  977. double Result;
  978. if (HandleReadByMasterStorage(Name))
  979. {
  980. Result = FMasterStorage->ReadFloat(Name, Default);
  981. }
  982. else
  983. {
  984. UnicodeString Value = FIniFile->ReadString(CurrentSection, MungeIniName(Name), L"");
  985. if (Value.IsEmpty())
  986. {
  987. Result = Default;
  988. }
  989. else
  990. {
  991. try
  992. {
  993. RawByteString Raw = HexToBytes(Value);
  994. if (static_cast<size_t>(Raw.Length()) == sizeof(Result))
  995. {
  996. memcpy(&Result, Raw.c_str(), sizeof(Result));
  997. }
  998. else
  999. {
  1000. Result = static_cast<double>(StrToFloat(Value));
  1001. }
  1002. }
  1003. catch(...)
  1004. {
  1005. Result = Default;
  1006. }
  1007. }
  1008. }
  1009. return Result;
  1010. }
  1011. //---------------------------------------------------------------------------
  1012. UnicodeString __fastcall TCustomIniFileStorage::ReadStringRaw(const UnicodeString & Name, const UnicodeString & Default)
  1013. {
  1014. UnicodeString Result;
  1015. if (HandleReadByMasterStorage(Name))
  1016. {
  1017. Result = FMasterStorage->ReadStringRaw(Name, Default);
  1018. }
  1019. else
  1020. {
  1021. Result = FIniFile->ReadString(CurrentSection, MungeIniName(Name), Default);
  1022. }
  1023. return Result;
  1024. }
  1025. //---------------------------------------------------------------------------
  1026. size_t __fastcall TCustomIniFileStorage::ReadBinaryData(const UnicodeString & Name, void * Buffer, size_t Size)
  1027. {
  1028. size_t Len;
  1029. if (HandleReadByMasterStorage(Name))
  1030. {
  1031. FMasterStorage->ReadBinaryData(Name, Buffer, Size);
  1032. }
  1033. else
  1034. {
  1035. RawByteString Value = HexToBytes(ReadStringRaw(Name, L""));
  1036. Len = Value.Length();
  1037. if (Size > Len)
  1038. {
  1039. Size = Len;
  1040. }
  1041. DebugAssert(Buffer);
  1042. memcpy(Buffer, Value.c_str(), Size);
  1043. }
  1044. return Size;
  1045. }
  1046. //---------------------------------------------------------------------------
  1047. void __fastcall TCustomIniFileStorage::WriteBool(const UnicodeString & Name, bool Value)
  1048. {
  1049. if (HandleByMasterStorage())
  1050. {
  1051. FMasterStorage->WriteBool(Name, Value);
  1052. }
  1053. ResetCache();
  1054. FIniFile->WriteBool(CurrentSection, MungeIniName(Name), Value);
  1055. }
  1056. //---------------------------------------------------------------------------
  1057. void __fastcall TCustomIniFileStorage::WriteInteger(const UnicodeString & Name, int Value)
  1058. {
  1059. if (HandleByMasterStorage())
  1060. {
  1061. FMasterStorage->WriteInteger(Name, Value);
  1062. }
  1063. ResetCache();
  1064. FIniFile->WriteInteger(CurrentSection, MungeIniName(Name), Value);
  1065. }
  1066. //---------------------------------------------------------------------------
  1067. void __fastcall TCustomIniFileStorage::WriteInt64(const UnicodeString & Name, __int64 Value)
  1068. {
  1069. if (HandleByMasterStorage())
  1070. {
  1071. FMasterStorage->WriteInt64(Name, Value);
  1072. }
  1073. DoWriteStringRaw(Name, IntToStr(Value));
  1074. }
  1075. //---------------------------------------------------------------------------
  1076. void __fastcall TCustomIniFileStorage::WriteDateTime(const UnicodeString & Name, TDateTime Value)
  1077. {
  1078. if (HandleByMasterStorage())
  1079. {
  1080. FMasterStorage->WriteDateTime(Name, Value);
  1081. }
  1082. DoWriteBinaryData(Name, &Value, sizeof(Value));
  1083. }
  1084. //---------------------------------------------------------------------------
  1085. void __fastcall TCustomIniFileStorage::WriteFloat(const UnicodeString & Name, double Value)
  1086. {
  1087. if (HandleByMasterStorage())
  1088. {
  1089. FMasterStorage->WriteFloat(Name, Value);
  1090. }
  1091. DoWriteBinaryData(Name, &Value, sizeof(Value));
  1092. }
  1093. //---------------------------------------------------------------------------
  1094. void __fastcall TCustomIniFileStorage::DoWriteStringRaw(const UnicodeString & Name, const UnicodeString & Value)
  1095. {
  1096. ResetCache();
  1097. FIniFile->WriteString(CurrentSection, MungeIniName(Name), Value);
  1098. }
  1099. //---------------------------------------------------------------------------
  1100. void __fastcall TCustomIniFileStorage::WriteStringRaw(const UnicodeString & Name, const UnicodeString & Value)
  1101. {
  1102. if (HandleByMasterStorage())
  1103. {
  1104. FMasterStorage->WriteStringRaw(Name, Value);
  1105. }
  1106. DoWriteStringRaw(Name, Value);
  1107. }
  1108. //---------------------------------------------------------------------------
  1109. void __fastcall TCustomIniFileStorage::DoWriteBinaryData(const UnicodeString & Name, const void * Buffer, int Size)
  1110. {
  1111. DoWriteStringRaw(Name, BytesToHex(RawByteString(static_cast<const char*>(Buffer), Size)));
  1112. }
  1113. //---------------------------------------------------------------------------
  1114. void __fastcall TCustomIniFileStorage::WriteBinaryData(const UnicodeString & Name, const void * Buffer, int Size)
  1115. {
  1116. if (HandleByMasterStorage())
  1117. {
  1118. FMasterStorage->WriteBinaryData(Name, Buffer, Size);
  1119. }
  1120. DoWriteBinaryData(Name, Buffer, Size);
  1121. }
  1122. //===========================================================================
  1123. TIniFileStorage * __fastcall TIniFileStorage::CreateFromPath(const UnicodeString & AStorage)
  1124. {
  1125. // The code was originally inline in the parent contructor call in the TIniFileStorage::TIniFileStorage [public originally].
  1126. // But if the TMemIniFile constructor throws (e.g. because the INI file is locked), the exception causes access violation.
  1127. // Moving the code to a factory solves this.
  1128. TMemIniFile * IniFile = new TMemIniFile(AStorage);
  1129. return new TIniFileStorage(AStorage, IniFile);
  1130. }
  1131. //---------------------------------------------------------------------------
  1132. __fastcall TIniFileStorage::TIniFileStorage(const UnicodeString & AStorage, TCustomIniFile * IniFile):
  1133. TCustomIniFileStorage(AStorage, IniFile)
  1134. {
  1135. FOriginal = new TStringList();
  1136. dynamic_cast<TMemIniFile *>(FIniFile)->GetStrings(FOriginal);
  1137. ApplyOverrides();
  1138. }
  1139. //---------------------------------------------------------------------------
  1140. void __fastcall TIniFileStorage::Flush()
  1141. {
  1142. if (FMasterStorage.get() != NULL)
  1143. {
  1144. FMasterStorage->Flush();
  1145. }
  1146. if (FOriginal != NULL)
  1147. {
  1148. std::unique_ptr<TStrings> Strings(new TStringList);
  1149. std::unique_ptr<TStrings> Original(FOriginal);
  1150. FOriginal = NULL;
  1151. dynamic_cast<TMemIniFile *>(FIniFile)->GetStrings(Strings.get());
  1152. if (!Strings->Equals(Original.get()))
  1153. {
  1154. int Attr;
  1155. // preserve attributes (especially hidden)
  1156. bool Exists = FileExists(ApiPath(Storage));
  1157. if (Exists)
  1158. {
  1159. Attr = GetFileAttributes(ApiPath(Storage).c_str());
  1160. }
  1161. else
  1162. {
  1163. Attr = FILE_ATTRIBUTE_NORMAL;
  1164. }
  1165. if (FLAGSET(Attr, FILE_ATTRIBUTE_READONLY) && ForceSave)
  1166. {
  1167. SetFileAttributes(ApiPath(Storage).c_str(), Attr & ~FILE_ATTRIBUTE_READONLY);
  1168. }
  1169. HANDLE Handle = CreateFile(ApiPath(Storage).c_str(), GENERIC_READ | GENERIC_WRITE,
  1170. 0, NULL, CREATE_ALWAYS, Attr, 0);
  1171. if (Handle == INVALID_HANDLE_VALUE)
  1172. {
  1173. // "access denied" errors upon implicit saves to existing file are ignored
  1174. if (Explicit || !Exists || (GetLastError() != ERROR_ACCESS_DENIED))
  1175. {
  1176. throw EOSExtException(FMTLOAD((Exists ? WRITE_ERROR : CREATE_FILE_ERROR), (Storage)));
  1177. }
  1178. }
  1179. else
  1180. {
  1181. std::unique_ptr<TStream> Stream(new THandleStream(int(Handle)));
  1182. try
  1183. {
  1184. Strings->SaveToStream(Stream.get());
  1185. }
  1186. __finally
  1187. {
  1188. CloseHandle(Handle);
  1189. }
  1190. }
  1191. }
  1192. }
  1193. }
  1194. //---------------------------------------------------------------------------
  1195. __fastcall TIniFileStorage::~TIniFileStorage()
  1196. {
  1197. Flush();
  1198. }
  1199. //---------------------------------------------------------------------------
  1200. void __fastcall TIniFileStorage::ApplyOverrides()
  1201. {
  1202. UnicodeString OverridesKey = IncludeTrailingBackslash(L"Override");
  1203. CacheSections();
  1204. for (int i = 0; i < FSections->Count; i++)
  1205. {
  1206. UnicodeString Section = FSections->Strings[i];
  1207. if (SameText(OverridesKey,
  1208. Section.SubString(1, OverridesKey.Length())))
  1209. {
  1210. UnicodeString SubKey = Section.SubString(OverridesKey.Length() + 1,
  1211. Section.Length() - OverridesKey.Length());
  1212. // this all uses raw names (munged)
  1213. TStrings * Names = new TStringList;
  1214. try
  1215. {
  1216. FIniFile->ReadSection(Section, Names);
  1217. for (int ii = 0; ii < Names->Count; ii++)
  1218. {
  1219. UnicodeString Name = Names->Strings[ii];
  1220. UnicodeString Value = FIniFile->ReadString(Section, Name, L"");
  1221. FIniFile->WriteString(SubKey, Name, Value);
  1222. }
  1223. }
  1224. __finally
  1225. {
  1226. delete Names;
  1227. }
  1228. FIniFile->EraseSection(Section);
  1229. ResetCache();
  1230. }
  1231. }
  1232. }
  1233. //===========================================================================
  1234. enum TWriteMode { wmAllow, wmFail, wmIgnore };
  1235. //---------------------------------------------------------------------------
  1236. class TOptionsIniFile : public TCustomIniFile
  1237. {
  1238. public:
  1239. __fastcall TOptionsIniFile(TStrings * Options, TWriteMode WriteMode, const UnicodeString & RootKey);
  1240. virtual UnicodeString __fastcall ReadString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Default);
  1241. virtual void __fastcall WriteString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Value);
  1242. virtual void __fastcall ReadSection(const UnicodeString Section, TStrings * Strings);
  1243. virtual void __fastcall ReadSections(TStrings* Strings);
  1244. virtual void __fastcall ReadSectionValues(const UnicodeString Section, TStrings* Strings);
  1245. virtual void __fastcall EraseSection(const UnicodeString Section);
  1246. virtual void __fastcall DeleteKey(const UnicodeString Section, const UnicodeString Ident);
  1247. virtual void __fastcall UpdateFile();
  1248. // Hoisted overload
  1249. void __fastcall ReadSections(const UnicodeString Section, TStrings* Strings);
  1250. // Ntb, we can implement ValueExists more efficiently than the TCustomIniFile.ValueExists
  1251. private:
  1252. TStrings * FOptions;
  1253. TWriteMode FWriteMode;
  1254. UnicodeString FRootKey;
  1255. bool __fastcall AllowWrite();
  1256. void __fastcall NotImplemented();
  1257. bool __fastcall AllowSection(const UnicodeString & Section);
  1258. UnicodeString __fastcall FormatKey(const UnicodeString & Section, const UnicodeString & Ident);
  1259. };
  1260. //---------------------------------------------------------------------------
  1261. __fastcall TOptionsIniFile::TOptionsIniFile(TStrings * Options, TWriteMode WriteMode, const UnicodeString & RootKey) :
  1262. TCustomIniFile(UnicodeString())
  1263. {
  1264. FOptions = Options;
  1265. FWriteMode = WriteMode;
  1266. FRootKey = RootKey;
  1267. if (!FRootKey.IsEmpty())
  1268. {
  1269. FRootKey += PathDelim;
  1270. }
  1271. }
  1272. //---------------------------------------------------------------------------
  1273. void __fastcall TOptionsIniFile::NotImplemented()
  1274. {
  1275. throw Exception(L"Not implemented");
  1276. }
  1277. //---------------------------------------------------------------------------
  1278. bool __fastcall TOptionsIniFile::AllowWrite()
  1279. {
  1280. switch (FWriteMode)
  1281. {
  1282. case wmAllow:
  1283. return true;
  1284. case wmFail:
  1285. NotImplemented();
  1286. return false; // never gets here
  1287. case wmIgnore:
  1288. return false;
  1289. default:
  1290. DebugFail();
  1291. return false;
  1292. }
  1293. }
  1294. //---------------------------------------------------------------------------
  1295. bool __fastcall TOptionsIniFile::AllowSection(const UnicodeString & Section)
  1296. {
  1297. UnicodeString Name = Section;
  1298. if (!Name.IsEmpty())
  1299. {
  1300. Name += PathDelim;
  1301. }
  1302. bool Result = SameText(Name.SubString(1, FRootKey.Length()), FRootKey);
  1303. return Result;
  1304. }
  1305. //---------------------------------------------------------------------------
  1306. UnicodeString __fastcall TOptionsIniFile::FormatKey(const UnicodeString & Section, const UnicodeString & Ident)
  1307. {
  1308. UnicodeString Result = Section;
  1309. if (!Result.IsEmpty())
  1310. {
  1311. Result += PathDelim;
  1312. }
  1313. Result += Ident; // Can be empty, when called from a contructor, AllowSection or ReadSection
  1314. if (DebugAlwaysTrue(AllowSection(Section)))
  1315. {
  1316. Result.Delete(1, FRootKey.Length());
  1317. }
  1318. return Result;
  1319. }
  1320. //---------------------------------------------------------------------------
  1321. UnicodeString __fastcall TOptionsIniFile::ReadString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Default)
  1322. {
  1323. UnicodeString Value;
  1324. if (!AllowSection(Section))
  1325. {
  1326. Value = Default;
  1327. }
  1328. else
  1329. {
  1330. UnicodeString Name = FormatKey(Section, Ident);
  1331. int Index = FOptions->IndexOfName(Name);
  1332. if (Index >= 0)
  1333. {
  1334. Value = FOptions->ValueFromIndex[Index];
  1335. }
  1336. else
  1337. {
  1338. Value = Default;
  1339. }
  1340. }
  1341. return Value;
  1342. }
  1343. //---------------------------------------------------------------------------
  1344. void __fastcall TOptionsIniFile::WriteString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Value)
  1345. {
  1346. if (AllowWrite() &&
  1347. DebugAlwaysTrue(AllowSection(Section)))
  1348. {
  1349. UnicodeString Name = FormatKey(Section, Ident);
  1350. FOptions->Values[Name] = Value;
  1351. }
  1352. }
  1353. //---------------------------------------------------------------------------
  1354. void __fastcall TOptionsIniFile::ReadSection(const UnicodeString Section, TStrings * Strings)
  1355. {
  1356. if (AllowSection(Section))
  1357. {
  1358. UnicodeString SectionPrefix = FormatKey(Section, UnicodeString());
  1359. Strings->BeginUpdate();
  1360. try
  1361. {
  1362. for (int Index = 0; Index < FOptions->Count; Index++)
  1363. {
  1364. UnicodeString Name = FOptions->Names[Index];
  1365. if (SameText(Name.SubString(1, SectionPrefix.Length()), SectionPrefix) &&
  1366. (LastDelimiter(PathDelim, Name) <= SectionPrefix.Length()))
  1367. {
  1368. Strings->Add(Name.SubString(SectionPrefix.Length() + 1, Name.Length() - SectionPrefix.Length()));
  1369. }
  1370. }
  1371. }
  1372. __finally
  1373. {
  1374. Strings->EndUpdate();
  1375. }
  1376. }
  1377. }
  1378. //---------------------------------------------------------------------------
  1379. void __fastcall TOptionsIniFile::ReadSections(TStrings * Strings)
  1380. {
  1381. std::unique_ptr<TStringList> Sections(CreateSortedStringList());
  1382. for (int Index = 0; Index < FOptions->Count; Index++)
  1383. {
  1384. UnicodeString Name = FOptions->Names[Index];
  1385. int P = LastDelimiter(PathDelim, Name);
  1386. if (P > 0)
  1387. {
  1388. UnicodeString Section = Name.SubString(1, P - 1);
  1389. if (Sections->IndexOf(Section) < 0)
  1390. {
  1391. Sections->Add(Section);
  1392. }
  1393. }
  1394. }
  1395. for (int Index = 0; Index < Sections->Count; Index++)
  1396. {
  1397. Strings->Add(FRootKey + Sections->Strings[Index]);
  1398. }
  1399. }
  1400. //---------------------------------------------------------------------------
  1401. void __fastcall TOptionsIniFile::ReadSectionValues(const UnicodeString Section, TStrings * /*Strings*/)
  1402. {
  1403. NotImplemented();
  1404. }
  1405. //---------------------------------------------------------------------------
  1406. void __fastcall TOptionsIniFile::EraseSection(const UnicodeString Section)
  1407. {
  1408. if (AllowWrite())
  1409. {
  1410. NotImplemented();
  1411. }
  1412. }
  1413. //---------------------------------------------------------------------------
  1414. void __fastcall TOptionsIniFile::DeleteKey(const UnicodeString Section, const UnicodeString Ident)
  1415. {
  1416. if (AllowWrite() &&
  1417. DebugAlwaysTrue(AllowSection(Section)))
  1418. {
  1419. UnicodeString Name = FormatKey(Section, Ident);
  1420. int Index = FOptions->IndexOfName(Name);
  1421. if (Index >= 0)
  1422. {
  1423. FOptions->Delete(Index);
  1424. }
  1425. }
  1426. }
  1427. //---------------------------------------------------------------------------
  1428. void __fastcall TOptionsIniFile::UpdateFile()
  1429. {
  1430. if (AllowWrite())
  1431. {
  1432. // noop
  1433. }
  1434. }
  1435. //---------------------------------------------------------------------------
  1436. void __fastcall TOptionsIniFile::ReadSections(const UnicodeString /*Section*/, TStrings * /*Strings*/)
  1437. {
  1438. NotImplemented();
  1439. }
  1440. //===========================================================================
  1441. __fastcall TOptionsStorage::TOptionsStorage(TStrings * Options, bool AllowWrite):
  1442. TCustomIniFileStorage(
  1443. UnicodeString(L"Command-line options"),
  1444. new TOptionsIniFile(Options, (AllowWrite ? wmAllow : wmFail), UnicodeString()))
  1445. {
  1446. }
  1447. //---------------------------------------------------------------------------
  1448. __fastcall TOptionsStorage::TOptionsStorage(TStrings * Options, const UnicodeString & RootKey, THierarchicalStorage * MasterStorage) :
  1449. TCustomIniFileStorage(
  1450. UnicodeString(L"Command-line options overriding " + MasterStorage->Source),
  1451. new TOptionsIniFile(Options, wmIgnore, RootKey))
  1452. {
  1453. FMasterStorage.reset(MasterStorage);
  1454. }
  1455. //---------------------------------------------------------------------------
  1456. bool __fastcall TOptionsStorage::GetTemporary()
  1457. {
  1458. return true;
  1459. }