HierarchicalStorage.cpp 47 KB

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