HierarchicalStorage.cpp 47 KB

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