HierarchicalStorage.cpp 48 KB

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