HierarchicalStorage.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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 <TextsCore.h>
  9. #include <StrUtils.hpp>
  10. #include <vector>
  11. //---------------------------------------------------------------------------
  12. #pragma package(smart_init)
  13. //---------------------------------------------------------------------------
  14. #define READ_REGISTRY(Method) \
  15. if (FRegistry->ValueExists(Name)) \
  16. try { return FRegistry->Method(Name); } catch(...) { FFailed++; return Default; } \
  17. else return Default;
  18. #define WRITE_REGISTRY(Method) \
  19. try { FRegistry->Method(Name, Value); } catch(...) { FFailed++; }
  20. //---------------------------------------------------------------------------
  21. UnicodeString __fastcall MungeStr(const UnicodeString Str, bool ForceAnsi)
  22. {
  23. RawByteString Source;
  24. if (ForceAnsi)
  25. {
  26. Source = AnsiString(Str);
  27. }
  28. else
  29. {
  30. Source = UTF8String(Str);
  31. if (Source.Length() > Str.Length())
  32. {
  33. Source.Insert(Bom, 1);
  34. }
  35. }
  36. // should contain ASCII characters only
  37. RawByteString Dest;
  38. Dest.SetLength(Source.Length() * 3 + 1);
  39. putty_mungestr(Source.c_str(), Dest.c_str());
  40. PackStr(Dest);
  41. return UnicodeString(Dest.c_str(), Dest.Length());
  42. }
  43. //---------------------------------------------------------------------------
  44. UnicodeString __fastcall UnMungeStr(const UnicodeString Str)
  45. {
  46. // Str should contain ASCII characters only
  47. RawByteString Source = AnsiString(Str);
  48. RawByteString Dest;
  49. Dest.SetLength(Source.Length() + 1);
  50. putty_unmungestr(Source.c_str(), Dest.c_str(), Dest.Length());
  51. UnicodeString Result;
  52. if (Dest.SubString(1, LENOF(Bom)) == Bom)
  53. {
  54. Dest.Delete(1, LENOF(Bom));
  55. Result = UTF8String(Dest.c_str());
  56. }
  57. else
  58. {
  59. Result = AnsiString(Dest.c_str());
  60. }
  61. return Result;
  62. }
  63. //---------------------------------------------------------------------------
  64. UnicodeString __fastcall PuttyMungeStr(const UnicodeString Str)
  65. {
  66. return MungeStr(Str, false);
  67. }
  68. //---------------------------------------------------------------------------
  69. UnicodeString __fastcall MungeIniName(const UnicodeString Str)
  70. {
  71. int P = Str.Pos(L"=");
  72. // make this fast for now
  73. if (P > 0)
  74. {
  75. return ReplaceStr(Str, L"=", L"%3D");
  76. }
  77. else
  78. {
  79. return Str;
  80. }
  81. }
  82. //---------------------------------------------------------------------------
  83. UnicodeString __fastcall UnMungeIniName(const UnicodeString Str)
  84. {
  85. int P = Str.Pos(L"%3D");
  86. // make this fast for now
  87. if (P > 0)
  88. {
  89. return ReplaceStr(Str, L"%3D", L"=");
  90. }
  91. else
  92. {
  93. return Str;
  94. }
  95. }
  96. //===========================================================================
  97. __fastcall THierarchicalStorage::THierarchicalStorage(const UnicodeString AStorage)
  98. {
  99. FStorage = AStorage;
  100. FKeyHistory = new TStringList();
  101. AccessMode = smRead;
  102. Explicit = false;
  103. ForceAnsi = true;
  104. MungeStringValues = true;
  105. }
  106. //---------------------------------------------------------------------------
  107. __fastcall THierarchicalStorage::~THierarchicalStorage()
  108. {
  109. delete FKeyHistory;
  110. }
  111. //---------------------------------------------------------------------------
  112. void __fastcall THierarchicalStorage::Flush()
  113. {
  114. }
  115. //---------------------------------------------------------------------------
  116. void __fastcall THierarchicalStorage::SetAccessMode(TStorageAccessMode value)
  117. {
  118. FAccessMode = value;
  119. }
  120. //---------------------------------------------------------------------------
  121. UnicodeString __fastcall THierarchicalStorage::GetCurrentSubKeyMunged()
  122. {
  123. if (FKeyHistory->Count) return FKeyHistory->Strings[FKeyHistory->Count-1];
  124. else return L"";
  125. }
  126. //---------------------------------------------------------------------------
  127. UnicodeString __fastcall THierarchicalStorage::GetCurrentSubKey()
  128. {
  129. return UnMungeStr(GetCurrentSubKeyMunged());
  130. }
  131. //---------------------------------------------------------------------------
  132. bool __fastcall THierarchicalStorage::OpenRootKey(bool CanCreate)
  133. {
  134. return OpenSubKey(L"", CanCreate);
  135. }
  136. //---------------------------------------------------------------------------
  137. UnicodeString __fastcall THierarchicalStorage::MungeKeyName(UnicodeString Key)
  138. {
  139. UnicodeString Result = MungeStr(Key, ForceAnsi);
  140. // if there's already ANSI-munged subkey, keep ANSI munging
  141. if ((Result != Key) && !ForceAnsi && DoKeyExists(Key, true))
  142. {
  143. Result = MungeStr(Key, true);
  144. }
  145. return Result;
  146. }
  147. //---------------------------------------------------------------------------
  148. bool __fastcall THierarchicalStorage::OpenSubKey(UnicodeString Key, bool CanCreate, bool Path)
  149. {
  150. bool Result;
  151. UnicodeString MungedKey;
  152. if (Path)
  153. {
  154. assert(Key.IsEmpty() || (Key[Key.Length()] != L'\\'));
  155. Result = true;
  156. while (!Key.IsEmpty() && Result)
  157. {
  158. if (!MungedKey.IsEmpty())
  159. {
  160. MungedKey += L'\\';
  161. }
  162. MungedKey += MungeKeyName(CutToChar(Key, L'\\', false));
  163. Result = DoOpenSubKey(MungedKey, CanCreate);
  164. }
  165. // hack to restore last opened key for registry storage
  166. if (!Result)
  167. {
  168. FKeyHistory->Add(IncludeTrailingBackslash(CurrentSubKey+MungedKey));
  169. CloseSubKey();
  170. }
  171. }
  172. else
  173. {
  174. MungedKey = MungeKeyName(Key);
  175. Result = DoOpenSubKey(MungedKey, CanCreate);
  176. }
  177. if (Result)
  178. {
  179. FKeyHistory->Add(IncludeTrailingBackslash(CurrentSubKey+MungedKey));
  180. }
  181. return Result;
  182. }
  183. //---------------------------------------------------------------------------
  184. void __fastcall THierarchicalStorage::CloseSubKey()
  185. {
  186. if (FKeyHistory->Count == 0) throw Exception(L"");
  187. else FKeyHistory->Delete(FKeyHistory->Count-1);
  188. }
  189. //---------------------------------------------------------------------------
  190. void __fastcall THierarchicalStorage::ClearSubKeys()
  191. {
  192. TStringList *SubKeys = new TStringList();
  193. try
  194. {
  195. GetSubKeyNames(SubKeys);
  196. for (int Index = 0; Index < SubKeys->Count; Index++)
  197. {
  198. RecursiveDeleteSubKey(SubKeys->Strings[Index]);
  199. }
  200. }
  201. __finally
  202. {
  203. delete SubKeys;
  204. }
  205. }
  206. //---------------------------------------------------------------------------
  207. void __fastcall THierarchicalStorage::RecursiveDeleteSubKey(const UnicodeString Key)
  208. {
  209. if (OpenSubKey(Key, false))
  210. {
  211. ClearSubKeys();
  212. CloseSubKey();
  213. }
  214. DeleteSubKey(Key);
  215. }
  216. //---------------------------------------------------------------------------
  217. bool __fastcall THierarchicalStorage::HasSubKeys()
  218. {
  219. bool Result;
  220. TStrings * SubKeys = new TStringList();
  221. try
  222. {
  223. GetSubKeyNames(SubKeys);
  224. Result = (SubKeys->Count > 0);
  225. }
  226. __finally
  227. {
  228. delete SubKeys;
  229. }
  230. return Result;
  231. }
  232. //---------------------------------------------------------------------------
  233. bool __fastcall THierarchicalStorage::HasSubKey(const UnicodeString SubKey)
  234. {
  235. bool Result = OpenSubKey(SubKey, false);
  236. if (Result)
  237. {
  238. CloseSubKey();
  239. }
  240. return Result;
  241. }
  242. //---------------------------------------------------------------------------
  243. bool __fastcall THierarchicalStorage::KeyExists(const UnicodeString SubKey)
  244. {
  245. return DoKeyExists(SubKey, ForceAnsi);
  246. }
  247. //---------------------------------------------------------------------------
  248. void __fastcall THierarchicalStorage::ReadValues(Classes::TStrings* Strings,
  249. bool MaintainKeys)
  250. {
  251. TStrings * Names = new TStringList();
  252. try
  253. {
  254. GetValueNames(Names);
  255. for (int Index = 0; Index < Names->Count; Index++)
  256. {
  257. if (MaintainKeys)
  258. {
  259. Strings->Add(FORMAT(L"%s=%s", (Names->Strings[Index],
  260. ReadString(Names->Strings[Index], L""))));
  261. }
  262. else
  263. {
  264. Strings->Add(ReadString(Names->Strings[Index], L""));
  265. }
  266. }
  267. }
  268. __finally
  269. {
  270. delete Names;
  271. }
  272. }
  273. //---------------------------------------------------------------------------
  274. void __fastcall THierarchicalStorage::ClearValues()
  275. {
  276. TStrings * Names = new TStringList();
  277. try
  278. {
  279. GetValueNames(Names);
  280. for (int Index = 0; Index < Names->Count; Index++)
  281. {
  282. DeleteValue(Names->Strings[Index]);
  283. }
  284. }
  285. __finally
  286. {
  287. delete Names;
  288. }
  289. }
  290. //---------------------------------------------------------------------------
  291. void __fastcall THierarchicalStorage::WriteValues(Classes::TStrings * Strings,
  292. bool MaintainKeys)
  293. {
  294. ClearValues();
  295. if (Strings)
  296. {
  297. for (int Index = 0; Index < Strings->Count; Index++)
  298. {
  299. if (MaintainKeys)
  300. {
  301. assert(Strings->Strings[Index].Pos(L"=") > 1);
  302. WriteString(Strings->Names[Index], Strings->Values[Strings->Names[Index]]);
  303. }
  304. else
  305. {
  306. WriteString(IntToStr(Index), Strings->Strings[Index]);
  307. }
  308. }
  309. }
  310. }
  311. //---------------------------------------------------------------------------
  312. UnicodeString __fastcall THierarchicalStorage::ReadString(const UnicodeString Name, const UnicodeString Default)
  313. {
  314. UnicodeString Result;
  315. if (MungeStringValues)
  316. {
  317. Result = UnMungeStr(ReadStringRaw(Name, MungeStr(Default, ForceAnsi)));
  318. }
  319. else
  320. {
  321. Result = ReadStringRaw(Name, Default);
  322. }
  323. return Result;
  324. }
  325. //---------------------------------------------------------------------------
  326. RawByteString __fastcall THierarchicalStorage::ReadBinaryData(const UnicodeString Name)
  327. {
  328. size_t Size = BinaryDataSize(Name);
  329. RawByteString Value;
  330. Value.SetLength(Size);
  331. ReadBinaryData(Name, Value.c_str(), Size);
  332. return Value;
  333. }
  334. //---------------------------------------------------------------------------
  335. RawByteString __fastcall THierarchicalStorage::ReadStringAsBinaryData(const UnicodeString Name, const RawByteString Default)
  336. {
  337. UnicodeString UnicodeDefault = UnicodeString(AnsiString(Default.c_str(), Default.Length()));
  338. // This should be exactly the same operation as calling ReadString in
  339. // C++Builder 6 (non-Unicode) on Unicode-based OS
  340. // (conversion is done by Ansi layer of the OS)
  341. UnicodeString String = ReadString(Name, UnicodeDefault);
  342. AnsiString Ansi = AnsiString(String);
  343. RawByteString Result = RawByteString(Ansi.c_str(), Ansi.Length());
  344. return Result;
  345. }
  346. //---------------------------------------------------------------------------
  347. void __fastcall THierarchicalStorage::WriteString(const UnicodeString Name, const UnicodeString Value)
  348. {
  349. if (MungeStringValues)
  350. {
  351. WriteStringRaw(Name, MungeStr(Value, ForceAnsi));
  352. }
  353. else
  354. {
  355. WriteStringRaw(Name, Value);
  356. }
  357. }
  358. //---------------------------------------------------------------------------
  359. void __fastcall THierarchicalStorage::WriteBinaryData(const UnicodeString Name,
  360. const RawByteString Value)
  361. {
  362. WriteBinaryData(Name, Value.c_str(), Value.Length());
  363. }
  364. //---------------------------------------------------------------------------
  365. void __fastcall THierarchicalStorage::WriteBinaryDataAsString(const UnicodeString Name, const RawByteString Value)
  366. {
  367. // This should be exactly the same operation as calling WriteString in
  368. // C++Builder 6 (non-Unicode) on Unicode-based OS
  369. // (conversion is done by Ansi layer of the OS)
  370. AnsiString Ansi = AnsiString(Value.c_str(), Value.Length());
  371. WriteString(Name, UnicodeString(Ansi));
  372. }
  373. //---------------------------------------------------------------------------
  374. UnicodeString __fastcall THierarchicalStorage::IncludeTrailingBackslash(const UnicodeString & S)
  375. {
  376. // expanded from ?: as it caused memory leaks
  377. if (S.IsEmpty())
  378. {
  379. return S;
  380. }
  381. else
  382. {
  383. return ::IncludeTrailingBackslash(S);
  384. }
  385. }
  386. //---------------------------------------------------------------------------
  387. UnicodeString __fastcall THierarchicalStorage::ExcludeTrailingBackslash(const UnicodeString & S)
  388. {
  389. // expanded from ?: as it caused memory leaks
  390. if (S.IsEmpty())
  391. {
  392. return S;
  393. }
  394. else
  395. {
  396. return ::ExcludeTrailingBackslash(S);
  397. }
  398. }
  399. //===========================================================================
  400. __fastcall TRegistryStorage::TRegistryStorage(const UnicodeString AStorage):
  401. THierarchicalStorage(IncludeTrailingBackslash(AStorage))
  402. {
  403. Init();
  404. };
  405. //---------------------------------------------------------------------------
  406. __fastcall TRegistryStorage::TRegistryStorage(const UnicodeString AStorage, HKEY ARootKey):
  407. THierarchicalStorage(IncludeTrailingBackslash(AStorage))
  408. {
  409. Init();
  410. FRegistry->RootKey = ARootKey;
  411. }
  412. //---------------------------------------------------------------------------
  413. void __fastcall TRegistryStorage::Init()
  414. {
  415. FFailed = 0;
  416. FRegistry = new TRegistry();
  417. FRegistry->Access = KEY_READ;
  418. }
  419. //---------------------------------------------------------------------------
  420. __fastcall TRegistryStorage::~TRegistryStorage()
  421. {
  422. delete FRegistry;
  423. };
  424. //---------------------------------------------------------------------------
  425. bool __fastcall TRegistryStorage::Copy(TRegistryStorage * Storage)
  426. {
  427. TRegistry * Registry = Storage->FRegistry;
  428. bool Result = true;
  429. TStrings * Names = new TStringList();
  430. try
  431. {
  432. Registry->GetValueNames(Names);
  433. std::vector<unsigned char> Buffer(1024, 0);
  434. int Index = 0;
  435. while ((Index < Names->Count) && Result)
  436. {
  437. UnicodeString Name = MungeStr(Names->Strings[Index], ForceAnsi);
  438. unsigned long Size = Buffer.size();
  439. unsigned long Type;
  440. int RegResult;
  441. do
  442. {
  443. RegResult = RegQueryValueEx(Registry->CurrentKey, Name.c_str(), NULL,
  444. &Type, &Buffer[0], &Size);
  445. if (RegResult == ERROR_MORE_DATA)
  446. {
  447. Buffer.resize(Size);
  448. }
  449. } while (RegResult == ERROR_MORE_DATA);
  450. Result = (RegResult == ERROR_SUCCESS);
  451. if (Result)
  452. {
  453. RegResult = RegSetValueEx(FRegistry->CurrentKey, Name.c_str(), NULL, Type,
  454. &Buffer[0], Size);
  455. Result = (RegResult == ERROR_SUCCESS);
  456. }
  457. ++Index;
  458. }
  459. }
  460. __finally
  461. {
  462. delete Names;
  463. }
  464. return Result;
  465. }
  466. //---------------------------------------------------------------------------
  467. UnicodeString __fastcall TRegistryStorage::GetSource()
  468. {
  469. return RootKeyToStr(FRegistry->RootKey) + L"\\" + Storage;
  470. }
  471. //---------------------------------------------------------------------------
  472. void __fastcall TRegistryStorage::SetAccessMode(TStorageAccessMode value)
  473. {
  474. THierarchicalStorage::SetAccessMode(value);
  475. if (FRegistry)
  476. {
  477. switch (AccessMode) {
  478. case smRead:
  479. FRegistry->Access = KEY_READ;
  480. break;
  481. case smReadWrite:
  482. default:
  483. FRegistry->Access = KEY_READ | KEY_WRITE;
  484. break;
  485. }
  486. }
  487. }
  488. //---------------------------------------------------------------------------
  489. bool __fastcall TRegistryStorage::DoOpenSubKey(const UnicodeString SubKey, bool CanCreate)
  490. {
  491. if (FKeyHistory->Count > 0) FRegistry->CloseKey();
  492. UnicodeString K = ExcludeTrailingBackslash(Storage + CurrentSubKey + SubKey);
  493. return FRegistry->OpenKey(K, CanCreate);
  494. }
  495. //---------------------------------------------------------------------------
  496. void __fastcall TRegistryStorage::CloseSubKey()
  497. {
  498. FRegistry->CloseKey();
  499. THierarchicalStorage::CloseSubKey();
  500. if (FKeyHistory->Count)
  501. {
  502. FRegistry->OpenKey(Storage + GetCurrentSubKeyMunged(), True);
  503. }
  504. }
  505. //---------------------------------------------------------------------------
  506. bool __fastcall TRegistryStorage::DeleteSubKey(const UnicodeString SubKey)
  507. {
  508. UnicodeString K;
  509. if (FKeyHistory->Count == 0) K = Storage + CurrentSubKey;
  510. K += MungeKeyName(SubKey);
  511. return FRegistry->DeleteKey(K);
  512. }
  513. //---------------------------------------------------------------------------
  514. void __fastcall TRegistryStorage::GetSubKeyNames(Classes::TStrings* Strings)
  515. {
  516. FRegistry->GetKeyNames(Strings);
  517. for (int Index = 0; Index < Strings->Count; Index++)
  518. {
  519. Strings->Strings[Index] = UnMungeStr(Strings->Strings[Index]);
  520. }
  521. }
  522. //---------------------------------------------------------------------------
  523. void __fastcall TRegistryStorage::GetValueNames(Classes::TStrings* Strings)
  524. {
  525. FRegistry->GetValueNames(Strings);
  526. }
  527. //---------------------------------------------------------------------------
  528. bool __fastcall TRegistryStorage::DeleteValue(const UnicodeString Name)
  529. {
  530. return FRegistry->DeleteValue(Name);
  531. }
  532. //---------------------------------------------------------------------------
  533. bool __fastcall TRegistryStorage::DoKeyExists(const UnicodeString SubKey, bool AForceAnsi)
  534. {
  535. UnicodeString K = MungeStr(SubKey, AForceAnsi);
  536. bool Result = FRegistry->KeyExists(K);
  537. return Result;
  538. }
  539. //---------------------------------------------------------------------------
  540. bool __fastcall TRegistryStorage::ValueExists(const UnicodeString Value)
  541. {
  542. bool Result = FRegistry->ValueExists(Value);
  543. return Result;
  544. }
  545. //---------------------------------------------------------------------------
  546. size_t __fastcall TRegistryStorage::BinaryDataSize(const UnicodeString Name)
  547. {
  548. size_t Result = FRegistry->GetDataSize(Name);
  549. return Result;
  550. }
  551. //---------------------------------------------------------------------------
  552. bool __fastcall TRegistryStorage::ReadBool(const UnicodeString Name, bool Default)
  553. {
  554. READ_REGISTRY(ReadBool);
  555. }
  556. //---------------------------------------------------------------------------
  557. TDateTime __fastcall TRegistryStorage::ReadDateTime(const UnicodeString Name, TDateTime Default)
  558. {
  559. READ_REGISTRY(ReadDateTime);
  560. }
  561. //---------------------------------------------------------------------------
  562. double __fastcall TRegistryStorage::ReadFloat(const UnicodeString Name, double Default)
  563. {
  564. READ_REGISTRY(ReadFloat);
  565. }
  566. //---------------------------------------------------------------------------
  567. int __fastcall TRegistryStorage::ReadInteger(const UnicodeString Name, int Default)
  568. {
  569. READ_REGISTRY(ReadInteger);
  570. }
  571. //---------------------------------------------------------------------------
  572. __int64 __fastcall TRegistryStorage::ReadInt64(const UnicodeString Name, __int64 Default)
  573. {
  574. __int64 Result = Default;
  575. if (FRegistry->ValueExists(Name))
  576. {
  577. try
  578. {
  579. FRegistry->ReadBinaryData(Name, &Result, sizeof(Result));
  580. }
  581. catch(...)
  582. {
  583. FFailed++;
  584. }
  585. }
  586. return Result;
  587. }
  588. //---------------------------------------------------------------------------
  589. UnicodeString __fastcall TRegistryStorage::ReadStringRaw(const UnicodeString Name, const UnicodeString Default)
  590. {
  591. READ_REGISTRY(ReadString);
  592. }
  593. //---------------------------------------------------------------------------
  594. size_t __fastcall TRegistryStorage::ReadBinaryData(const UnicodeString Name,
  595. void * Buffer, size_t Size)
  596. {
  597. size_t Result;
  598. if (FRegistry->ValueExists(Name))
  599. {
  600. try
  601. {
  602. Result = FRegistry->ReadBinaryData(Name, Buffer, Size);
  603. }
  604. catch(...)
  605. {
  606. Result = 0;
  607. FFailed++;
  608. }
  609. }
  610. else
  611. {
  612. Result = 0;
  613. }
  614. return Result;
  615. }
  616. //---------------------------------------------------------------------------
  617. void __fastcall TRegistryStorage::WriteBool(const UnicodeString Name, bool Value)
  618. {
  619. WRITE_REGISTRY(WriteBool);
  620. }
  621. //---------------------------------------------------------------------------
  622. void __fastcall TRegistryStorage::WriteDateTime(const UnicodeString Name, TDateTime Value)
  623. {
  624. WRITE_REGISTRY(WriteDateTime);
  625. }
  626. //---------------------------------------------------------------------------
  627. void __fastcall TRegistryStorage::WriteFloat(const UnicodeString Name, double Value)
  628. {
  629. WRITE_REGISTRY(WriteFloat);
  630. }
  631. //---------------------------------------------------------------------------
  632. void __fastcall TRegistryStorage::WriteStringRaw(const UnicodeString Name, const UnicodeString Value)
  633. {
  634. WRITE_REGISTRY(WriteString);
  635. }
  636. //---------------------------------------------------------------------------
  637. void __fastcall TRegistryStorage::WriteInteger(const UnicodeString Name, int Value)
  638. {
  639. WRITE_REGISTRY(WriteInteger);
  640. }
  641. //---------------------------------------------------------------------------
  642. void __fastcall TRegistryStorage::WriteInt64(const UnicodeString Name, __int64 Value)
  643. {
  644. try
  645. {
  646. FRegistry->WriteBinaryData(Name, &Value, sizeof(Value));
  647. }
  648. catch(...)
  649. {
  650. FFailed++;
  651. }
  652. }
  653. //---------------------------------------------------------------------------
  654. void __fastcall TRegistryStorage::WriteBinaryData(const UnicodeString Name,
  655. const void * Buffer, int Size)
  656. {
  657. try
  658. {
  659. FRegistry->WriteBinaryData(Name, const_cast<void *>(Buffer), Size);
  660. }
  661. catch(...)
  662. {
  663. FFailed++;
  664. }
  665. }
  666. //---------------------------------------------------------------------------
  667. int __fastcall TRegistryStorage::GetFailed()
  668. {
  669. int Result = FFailed;
  670. FFailed = 0;
  671. return Result;
  672. }
  673. //===========================================================================
  674. __fastcall TCustomIniFileStorage::TCustomIniFileStorage(const UnicodeString Storage, TCustomIniFile * IniFile) :
  675. THierarchicalStorage(Storage),
  676. FIniFile(IniFile)
  677. {
  678. }
  679. //---------------------------------------------------------------------------
  680. __fastcall TCustomIniFileStorage::~TCustomIniFileStorage()
  681. {
  682. delete FIniFile;
  683. }
  684. //---------------------------------------------------------------------------
  685. UnicodeString __fastcall TCustomIniFileStorage::GetSource()
  686. {
  687. return Storage;
  688. }
  689. //---------------------------------------------------------------------------
  690. UnicodeString __fastcall TCustomIniFileStorage::GetCurrentSection()
  691. {
  692. return ExcludeTrailingBackslash(GetCurrentSubKeyMunged());
  693. }
  694. //---------------------------------------------------------------------------
  695. bool __fastcall TCustomIniFileStorage::DoOpenSubKey(const UnicodeString SubKey, bool CanCreate)
  696. {
  697. bool Result = CanCreate;
  698. if (!Result)
  699. {
  700. TStringList * Sections = new TStringList();
  701. try
  702. {
  703. Sections->Sorted = true;
  704. FIniFile->ReadSections(Sections);
  705. UnicodeString NewKey = ExcludeTrailingBackslash(CurrentSubKey+SubKey);
  706. if (Sections->Count)
  707. {
  708. int Index = -1;
  709. Result = Sections->Find(NewKey, Index);
  710. if (!Result && Index < Sections->Count &&
  711. Sections->Strings[Index].SubString(1, NewKey.Length()+1) == NewKey + L"\\")
  712. {
  713. Result = true;
  714. }
  715. }
  716. }
  717. __finally
  718. {
  719. delete Sections;
  720. }
  721. }
  722. return Result;
  723. }
  724. //---------------------------------------------------------------------------
  725. bool __fastcall TCustomIniFileStorage::DeleteSubKey(const UnicodeString SubKey)
  726. {
  727. bool Result;
  728. try
  729. {
  730. FIniFile->EraseSection(CurrentSubKey + MungeKeyName(SubKey));
  731. Result = true;
  732. }
  733. catch (...)
  734. {
  735. Result = false;
  736. }
  737. return Result;
  738. }
  739. //---------------------------------------------------------------------------
  740. void __fastcall TCustomIniFileStorage::GetSubKeyNames(Classes::TStrings* Strings)
  741. {
  742. TStrings * Sections = new TStringList();
  743. try
  744. {
  745. Strings->Clear();
  746. FIniFile->ReadSections(Sections);
  747. for (int i = 0; i < Sections->Count; i++)
  748. {
  749. UnicodeString Section = Sections->Strings[i];
  750. if (AnsiCompareText(CurrentSubKey,
  751. Section.SubString(1, CurrentSubKey.Length())) == 0)
  752. {
  753. UnicodeString SubSection = Section.SubString(CurrentSubKey.Length() + 1,
  754. Section.Length() - CurrentSubKey.Length());
  755. int P = SubSection.Pos(L"\\");
  756. if (P)
  757. {
  758. SubSection.SetLength(P - 1);
  759. }
  760. if (Strings->IndexOf(SubSection) < 0)
  761. {
  762. Strings->Add(UnMungeStr(SubSection));
  763. }
  764. }
  765. }
  766. }
  767. __finally
  768. {
  769. delete Sections;
  770. }
  771. }
  772. //---------------------------------------------------------------------------
  773. void __fastcall TCustomIniFileStorage::GetValueNames(Classes::TStrings* Strings)
  774. {
  775. FIniFile->ReadSection(CurrentSection, Strings);
  776. for (int Index = 0; Index < Strings->Count; Index++)
  777. {
  778. Strings->Strings[Index] = UnMungeIniName(Strings->Strings[Index]);
  779. }
  780. }
  781. //---------------------------------------------------------------------------
  782. bool __fastcall TCustomIniFileStorage::DoKeyExists(const UnicodeString SubKey, bool AForceAnsi)
  783. {
  784. return FIniFile->SectionExists(CurrentSubKey + MungeStr(SubKey, AForceAnsi));
  785. }
  786. //---------------------------------------------------------------------------
  787. bool __fastcall TCustomIniFileStorage::ValueExists(const UnicodeString Value)
  788. {
  789. return FIniFile->ValueExists(CurrentSection, MungeIniName(Value));
  790. }
  791. //---------------------------------------------------------------------------
  792. bool __fastcall TCustomIniFileStorage::DeleteValue(const UnicodeString Name)
  793. {
  794. FIniFile->DeleteKey(CurrentSection, MungeIniName(Name));
  795. return true;
  796. }
  797. //---------------------------------------------------------------------------
  798. size_t __fastcall TCustomIniFileStorage::BinaryDataSize(const UnicodeString Name)
  799. {
  800. return ReadStringRaw(Name, L"").Length() / 2;
  801. }
  802. //---------------------------------------------------------------------------
  803. bool __fastcall TCustomIniFileStorage::ReadBool(const UnicodeString Name, bool Default)
  804. {
  805. return FIniFile->ReadBool(CurrentSection, MungeIniName(Name), Default);
  806. }
  807. //---------------------------------------------------------------------------
  808. int __fastcall TCustomIniFileStorage::ReadInteger(const UnicodeString Name, int Default)
  809. {
  810. int Result = FIniFile->ReadInteger(CurrentSection, MungeIniName(Name), Default);
  811. return Result;
  812. }
  813. //---------------------------------------------------------------------------
  814. __int64 __fastcall TCustomIniFileStorage::ReadInt64(const UnicodeString Name, __int64 Default)
  815. {
  816. __int64 Result = Default;
  817. UnicodeString Str;
  818. Str = ReadStringRaw(Name, L"");
  819. if (!Str.IsEmpty())
  820. {
  821. Result = StrToInt64Def(Str, Default);
  822. }
  823. return Result;
  824. }
  825. //---------------------------------------------------------------------------
  826. TDateTime __fastcall TCustomIniFileStorage::ReadDateTime(const UnicodeString Name, TDateTime Default)
  827. {
  828. TDateTime Result;
  829. UnicodeString Value = FIniFile->ReadString(CurrentSection, MungeIniName(Name), L"");
  830. if (Value.IsEmpty())
  831. {
  832. Result = Default;
  833. }
  834. else
  835. {
  836. try
  837. {
  838. RawByteString Raw = HexToBytes(Value);
  839. if (static_cast<size_t>(Raw.Length()) == sizeof(Result))
  840. {
  841. memcpy(&Result, Raw.c_str(), sizeof(Result));
  842. }
  843. else
  844. {
  845. Result = StrToDateTime(Value);
  846. }
  847. }
  848. catch(...)
  849. {
  850. Result = Default;
  851. }
  852. }
  853. return Result;
  854. }
  855. //---------------------------------------------------------------------------
  856. double __fastcall TCustomIniFileStorage::ReadFloat(const UnicodeString Name, double Default)
  857. {
  858. double Result;
  859. UnicodeString Value = FIniFile->ReadString(CurrentSection, MungeIniName(Name), L"");
  860. if (Value.IsEmpty())
  861. {
  862. Result = Default;
  863. }
  864. else
  865. {
  866. try
  867. {
  868. RawByteString Raw = HexToBytes(Value);
  869. if (static_cast<size_t>(Raw.Length()) == sizeof(Result))
  870. {
  871. memcpy(&Result, Raw.c_str(), sizeof(Result));
  872. }
  873. else
  874. {
  875. Result = static_cast<double>(StrToFloat(Value));
  876. }
  877. }
  878. catch(...)
  879. {
  880. Result = Default;
  881. }
  882. }
  883. return Result;
  884. }
  885. //---------------------------------------------------------------------------
  886. UnicodeString __fastcall TCustomIniFileStorage::ReadStringRaw(const UnicodeString Name, UnicodeString Default)
  887. {
  888. AnsiString Result = FIniFile->ReadString(CurrentSection, MungeIniName(Name), Default);
  889. return Result;
  890. }
  891. //---------------------------------------------------------------------------
  892. size_t __fastcall TCustomIniFileStorage::ReadBinaryData(const UnicodeString Name,
  893. void * Buffer, size_t Size)
  894. {
  895. RawByteString Value = HexToBytes(ReadStringRaw(Name, L""));
  896. size_t Len = Value.Length();
  897. if (Size > Len)
  898. {
  899. Size = Len;
  900. }
  901. assert(Buffer);
  902. memcpy(Buffer, Value.c_str(), Size);
  903. return Size;
  904. }
  905. //---------------------------------------------------------------------------
  906. void __fastcall TCustomIniFileStorage::WriteBool(const UnicodeString Name, bool Value)
  907. {
  908. FIniFile->WriteBool(CurrentSection, MungeIniName(Name), Value);
  909. }
  910. //---------------------------------------------------------------------------
  911. void __fastcall TCustomIniFileStorage::WriteInteger(const UnicodeString Name, int Value)
  912. {
  913. FIniFile->WriteInteger(CurrentSection, MungeIniName(Name), Value);
  914. }
  915. //---------------------------------------------------------------------------
  916. void __fastcall TCustomIniFileStorage::WriteInt64(const UnicodeString Name, __int64 Value)
  917. {
  918. WriteStringRaw(Name, IntToStr(Value));
  919. }
  920. //---------------------------------------------------------------------------
  921. void __fastcall TCustomIniFileStorage::WriteDateTime(const UnicodeString Name, TDateTime Value)
  922. {
  923. WriteBinaryData(Name, &Value, sizeof(Value));
  924. }
  925. //---------------------------------------------------------------------------
  926. void __fastcall TCustomIniFileStorage::WriteFloat(const UnicodeString Name, double Value)
  927. {
  928. WriteBinaryData(Name, &Value, sizeof(Value));
  929. }
  930. //---------------------------------------------------------------------------
  931. void __fastcall TCustomIniFileStorage::WriteStringRaw(const UnicodeString Name, const UnicodeString Value)
  932. {
  933. FIniFile->WriteString(CurrentSection, MungeIniName(Name), Value);
  934. }
  935. //---------------------------------------------------------------------------
  936. void __fastcall TCustomIniFileStorage::WriteBinaryData(const UnicodeString Name,
  937. const void * Buffer, int Size)
  938. {
  939. WriteStringRaw(Name, BytesToHex(RawByteString(static_cast<const char*>(Buffer), Size)));
  940. }
  941. //===========================================================================
  942. __fastcall TIniFileStorage::TIniFileStorage(const UnicodeString AStorage):
  943. TCustomIniFileStorage(AStorage, new TMemIniFile(AStorage))
  944. {
  945. FOriginal = new TStringList();
  946. dynamic_cast<TMemIniFile *>(FIniFile)->GetStrings(FOriginal);
  947. ApplyOverrides();
  948. }
  949. //---------------------------------------------------------------------------
  950. void __fastcall TIniFileStorage::Flush()
  951. {
  952. if (FOriginal != NULL)
  953. {
  954. TStrings * Strings = new TStringList;
  955. try
  956. {
  957. dynamic_cast<TMemIniFile *>(FIniFile)->GetStrings(Strings);
  958. if (!Strings->Equals(FOriginal))
  959. {
  960. int Attr;
  961. // preserve attributes (especially hidden)
  962. bool Exists = FileExists(Storage);
  963. if (Exists)
  964. {
  965. Attr = GetFileAttributes(UnicodeString(Storage).c_str());
  966. }
  967. else
  968. {
  969. Attr = FILE_ATTRIBUTE_NORMAL;
  970. }
  971. HANDLE Handle = CreateFile(UnicodeString(Storage).c_str(), GENERIC_READ | GENERIC_WRITE,
  972. 0, NULL, CREATE_ALWAYS, Attr, 0);
  973. if (Handle == INVALID_HANDLE_VALUE)
  974. {
  975. // "access denied" errors upon implicit saves to existing file are ignored
  976. if (Explicit || !Exists || (GetLastError() != ERROR_ACCESS_DENIED))
  977. {
  978. try
  979. {
  980. RaiseLastOSError();
  981. }
  982. catch(Exception & E)
  983. {
  984. throw ExtException(&E, FMTLOAD(CREATE_FILE_ERROR, (Storage)));
  985. }
  986. }
  987. }
  988. else
  989. {
  990. TStream * Stream = new THandleStream(int(Handle));
  991. try
  992. {
  993. Strings->SaveToStream(Stream);
  994. }
  995. __finally
  996. {
  997. CloseHandle(Handle);
  998. delete Stream;
  999. }
  1000. }
  1001. }
  1002. }
  1003. __finally
  1004. {
  1005. delete FOriginal;
  1006. FOriginal = NULL;
  1007. delete Strings;
  1008. }
  1009. }
  1010. }
  1011. //---------------------------------------------------------------------------
  1012. __fastcall TIniFileStorage::~TIniFileStorage()
  1013. {
  1014. Flush();
  1015. }
  1016. //---------------------------------------------------------------------------
  1017. void __fastcall TIniFileStorage::ApplyOverrides()
  1018. {
  1019. UnicodeString OverridesKey = IncludeTrailingBackslash(L"Override");
  1020. TStrings * Sections = new TStringList();
  1021. try
  1022. {
  1023. Sections->Clear();
  1024. FIniFile->ReadSections(Sections);
  1025. for (int i = 0; i < Sections->Count; i++)
  1026. {
  1027. UnicodeString Section = Sections->Strings[i];
  1028. if (AnsiSameText(OverridesKey,
  1029. Section.SubString(1, OverridesKey.Length())))
  1030. {
  1031. UnicodeString SubKey = Section.SubString(OverridesKey.Length() + 1,
  1032. Section.Length() - OverridesKey.Length());
  1033. // this all uses raw names (munged)
  1034. TStrings * Names = new TStringList;
  1035. try
  1036. {
  1037. FIniFile->ReadSection(Section, Names);
  1038. for (int ii = 0; ii < Names->Count; ii++)
  1039. {
  1040. UnicodeString Name = Names->Strings[ii];
  1041. UnicodeString Value = FIniFile->ReadString(Section, Name, L"");
  1042. FIniFile->WriteString(SubKey, Name, Value);
  1043. }
  1044. }
  1045. __finally
  1046. {
  1047. delete Names;
  1048. }
  1049. FIniFile->EraseSection(Section);
  1050. }
  1051. }
  1052. }
  1053. __finally
  1054. {
  1055. delete Sections;
  1056. }
  1057. }
  1058. //===========================================================================
  1059. #define NOT_IMPLEMENTED throw Exception("Not implemented")
  1060. //===========================================================================
  1061. class TOptionsIniFile : public TCustomIniFile
  1062. {
  1063. public:
  1064. __fastcall TOptionsIniFile(TStrings * Options);
  1065. virtual UnicodeString __fastcall ReadString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Default);
  1066. virtual void __fastcall WriteString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Value);
  1067. virtual void __fastcall ReadSection(const UnicodeString Section, TStrings * Strings);
  1068. virtual void __fastcall ReadSections(TStrings* Strings);
  1069. virtual void __fastcall ReadSectionValues(const UnicodeString Section, TStrings* Strings);
  1070. virtual void __fastcall EraseSection(const UnicodeString Section);
  1071. virtual void __fastcall DeleteKey(const UnicodeString Section, const UnicodeString Ident);
  1072. virtual void __fastcall UpdateFile();
  1073. // Hoisted overload
  1074. void __fastcall ReadSections(const UnicodeString Section, TStrings* Strings);
  1075. private:
  1076. TStrings * FOptions;
  1077. };
  1078. //---------------------------------------------------------------------------
  1079. __fastcall TOptionsIniFile::TOptionsIniFile(TStrings * Options) :
  1080. TCustomIniFile(UnicodeString())
  1081. {
  1082. FOptions = Options;
  1083. }
  1084. //---------------------------------------------------------------------------
  1085. UnicodeString __fastcall TOptionsIniFile::ReadString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Default)
  1086. {
  1087. assert(Section.IsEmpty());
  1088. int Index = FOptions->IndexOfName(Ident);
  1089. UnicodeString Value;
  1090. if (Index >= 0)
  1091. {
  1092. Value = FOptions->ValueFromIndex[Index];
  1093. }
  1094. else
  1095. {
  1096. Value = Default;
  1097. }
  1098. return Value;
  1099. }
  1100. //---------------------------------------------------------------------------
  1101. void __fastcall TOptionsIniFile::WriteString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Value)
  1102. {
  1103. NOT_IMPLEMENTED;
  1104. }
  1105. //---------------------------------------------------------------------------
  1106. void __fastcall TOptionsIniFile::ReadSection(const UnicodeString Section, TStrings * Strings)
  1107. {
  1108. assert(Section.IsEmpty());
  1109. Strings->BeginUpdate();
  1110. try
  1111. {
  1112. for (int Index = 0; Index < FOptions->Count; Index++)
  1113. {
  1114. Strings->Add(FOptions->Names[Index]);
  1115. }
  1116. }
  1117. __finally
  1118. {
  1119. Strings->EndUpdate();
  1120. }
  1121. }
  1122. //---------------------------------------------------------------------------
  1123. void __fastcall TOptionsIniFile::ReadSections(TStrings * /*Strings*/)
  1124. {
  1125. NOT_IMPLEMENTED;
  1126. }
  1127. //---------------------------------------------------------------------------
  1128. void __fastcall TOptionsIniFile::ReadSectionValues(const UnicodeString Section, TStrings * /*Strings*/)
  1129. {
  1130. NOT_IMPLEMENTED;
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. void __fastcall TOptionsIniFile::EraseSection(const UnicodeString Section)
  1134. {
  1135. NOT_IMPLEMENTED;
  1136. }
  1137. //---------------------------------------------------------------------------
  1138. void __fastcall TOptionsIniFile::DeleteKey(const UnicodeString Section, const UnicodeString Ident)
  1139. {
  1140. NOT_IMPLEMENTED;
  1141. }
  1142. //---------------------------------------------------------------------------
  1143. void __fastcall TOptionsIniFile::UpdateFile()
  1144. {
  1145. NOT_IMPLEMENTED;
  1146. }
  1147. //---------------------------------------------------------------------------
  1148. void __fastcall TOptionsIniFile::ReadSections(const UnicodeString Section, TStrings* Strings)
  1149. {
  1150. TCustomIniFile::ReadSections(Section, Strings);
  1151. }
  1152. //===========================================================================
  1153. __fastcall TOptionsStorage::TOptionsStorage(TStrings * Options):
  1154. TCustomIniFileStorage(UnicodeString(L"Command-line options"), new TOptionsIniFile(Options))
  1155. {
  1156. }