HierarchicalStorage.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Common.h"
  5. #include "PuttyIntf.h"
  6. #include "HierarchicalStorage.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. //---------------------------------------------------------------------------
  10. #define READ_REGISTRY(Method) \
  11. if (FRegistry->ValueExists(Name)) \
  12. try { return FRegistry->Method(Name); } catch(...) { FFailed++; return Default; } \
  13. else return Default;
  14. #define WRITE_REGISTRY(Method) \
  15. try { FRegistry->Method(Name, Value); } catch(...) { FFailed++; }
  16. //---------------------------------------------------------------------------
  17. AnsiString __fastcall MungeStr(const AnsiString Str)
  18. {
  19. AnsiString Result;
  20. Result.SetLength(Str.Length() * 3 + 1);
  21. putty_mungestr(Str.c_str(), Result.c_str());
  22. PackStr(Result);
  23. return Result;
  24. }
  25. //---------------------------------------------------------------------------
  26. AnsiString __fastcall UnMungeStr(const AnsiString Str)
  27. {
  28. AnsiString Result;
  29. Result.SetLength(Str.Length() * 3 + 1);
  30. putty_unmungestr(Str.c_str(), Result.c_str(), Result.Length());
  31. PackStr(Result);
  32. return Result;
  33. }
  34. //---------------------------------------------------------------------------
  35. AnsiString __fastcall SimpleMungeStr(const AnsiString Str)
  36. {
  37. AnsiString Result = Str;
  38. for (int i = 1; i < Result.Length(); i++)
  39. {
  40. if ((Result[i] == '\\') || (Result[i] == '[') || (Result[i] == ']'))
  41. {
  42. Result[i] = '_';
  43. }
  44. }
  45. return Result;
  46. }
  47. //===========================================================================
  48. __fastcall THierarchicalStorage::THierarchicalStorage(const AnsiString AStorage)
  49. {
  50. FStorage = AStorage;
  51. FKeyHistory = new TStringList();
  52. AccessMode = smRead;
  53. }
  54. //---------------------------------------------------------------------------
  55. __fastcall THierarchicalStorage::~THierarchicalStorage()
  56. {
  57. delete FKeyHistory;
  58. }
  59. //---------------------------------------------------------------------------
  60. void __fastcall THierarchicalStorage::SetAccessMode(TStorageAccessMode value)
  61. {
  62. FAccessMode = value;
  63. }
  64. //---------------------------------------------------------------------------
  65. AnsiString __fastcall THierarchicalStorage::GetCurrentSubKey()
  66. {
  67. if (FKeyHistory->Count) return FKeyHistory->Strings[FKeyHistory->Count-1];
  68. else return "";
  69. }
  70. //---------------------------------------------------------------------------
  71. bool __fastcall THierarchicalStorage::OpenRootKey(bool CanCreate)
  72. {
  73. return OpenSubKey("", CanCreate);
  74. }
  75. //---------------------------------------------------------------------------
  76. bool __fastcall THierarchicalStorage::OpenSubKey(const AnsiString SubKey, bool )
  77. {
  78. FKeyHistory->Add(IncludeTrailingBackslash(CurrentSubKey+SubKey));
  79. return true;
  80. }
  81. //---------------------------------------------------------------------------
  82. bool __fastcall THierarchicalStorage::CreateSubKey(const AnsiString SubKey)
  83. {
  84. FKeyHistory->Add(IncludeTrailingBackslash(CurrentSubKey+SubKey));
  85. return true;
  86. }
  87. //---------------------------------------------------------------------------
  88. void __fastcall THierarchicalStorage::CloseSubKey()
  89. {
  90. if (FKeyHistory->Count == 0) throw Exception("");
  91. else FKeyHistory->Delete(FKeyHistory->Count-1);
  92. }
  93. //---------------------------------------------------------------------------
  94. void __fastcall THierarchicalStorage::ClearSubKeys()
  95. {
  96. TStringList *SubKeys = new TStringList();
  97. try
  98. {
  99. GetSubKeyNames(SubKeys);
  100. for (int Index = 0; Index < SubKeys->Count; Index++)
  101. {
  102. RecursiveDeleteSubKey(SubKeys->Strings[Index]);
  103. }
  104. }
  105. __finally
  106. {
  107. delete SubKeys;
  108. }
  109. }
  110. //---------------------------------------------------------------------------
  111. void __fastcall THierarchicalStorage::RecursiveDeleteSubKey(const AnsiString Key)
  112. {
  113. if (OpenSubKey(Key, false))
  114. {
  115. ClearSubKeys();
  116. CloseSubKey();
  117. }
  118. DeleteSubKey(Key);
  119. }
  120. //---------------------------------------------------------------------------
  121. bool __fastcall THierarchicalStorage::HasSubKeys()
  122. {
  123. bool Result;
  124. TStrings * SubKeys = new TStringList();
  125. try
  126. {
  127. GetSubKeyNames(SubKeys);
  128. Result = (SubKeys->Count > 0);
  129. }
  130. __finally
  131. {
  132. delete SubKeys;
  133. }
  134. return Result;
  135. }
  136. //---------------------------------------------------------------------------
  137. void __fastcall THierarchicalStorage::ReadValues(Classes::TStrings* Strings,
  138. bool MaintainKeys)
  139. {
  140. TStrings * Names = new TStringList();
  141. try
  142. {
  143. GetValueNames(Names);
  144. for (int Index = 0; Index < Names->Count; Index++)
  145. {
  146. if (MaintainKeys)
  147. {
  148. Strings->Add(FORMAT("%s=%s", (Names->Strings[Index],
  149. ReadString(Names->Strings[Index], ""))));
  150. }
  151. else
  152. {
  153. Strings->Add(ReadString(Names->Strings[Index], ""));
  154. }
  155. }
  156. }
  157. __finally
  158. {
  159. delete Names;
  160. }
  161. }
  162. //---------------------------------------------------------------------------
  163. void __fastcall THierarchicalStorage::ClearValues()
  164. {
  165. TStrings * Names = new TStringList();
  166. try
  167. {
  168. GetValueNames(Names);
  169. for (int Index = 0; Index < Names->Count; Index++)
  170. {
  171. DeleteValue(Names->Strings[Index]);
  172. }
  173. }
  174. __finally
  175. {
  176. delete Names;
  177. }
  178. }
  179. //---------------------------------------------------------------------------
  180. void __fastcall THierarchicalStorage::WriteValues(Classes::TStrings * Strings,
  181. bool MaintainKeys)
  182. {
  183. ClearValues();
  184. if (Strings)
  185. {
  186. for (int Index = 0; Index < Strings->Count; Index++)
  187. {
  188. if (MaintainKeys)
  189. {
  190. assert(Strings->Strings[Index].Pos("=") > 1);
  191. WriteString(Strings->Names[Index], Strings->Values[Strings->Names[Index]]);
  192. }
  193. else
  194. {
  195. WriteString(IntToStr(Index), Strings->Strings[Index]);
  196. }
  197. }
  198. }
  199. }
  200. //---------------------------------------------------------------------------
  201. AnsiString __fastcall THierarchicalStorage::ReadString(const AnsiString Name, const AnsiString Default)
  202. {
  203. return UnMungeStr(ReadStringRaw(Name, Default));
  204. }
  205. //---------------------------------------------------------------------------
  206. AnsiString __fastcall THierarchicalStorage::ReadBinaryData(const AnsiString Name)
  207. {
  208. int Size = BinaryDataSize(Name);
  209. AnsiString Value;
  210. Value.SetLength(Size);
  211. ReadBinaryData(Name, Value.c_str(), Size);
  212. return Value;
  213. }
  214. //---------------------------------------------------------------------------
  215. void __fastcall THierarchicalStorage::WriteString(const AnsiString Name, const AnsiString Value)
  216. {
  217. WriteStringRaw(Name, MungeStr(Value));
  218. }
  219. //---------------------------------------------------------------------------
  220. void __fastcall THierarchicalStorage::WriteBinaryData(const AnsiString Name,
  221. const AnsiString Value)
  222. {
  223. WriteBinaryData(Name, Value.c_str(), Value.Length());
  224. }
  225. //---------------------------------------------------------------------------
  226. AnsiString __fastcall THierarchicalStorage::IncludeTrailingBackslash(const AnsiString S) const
  227. {
  228. return S.IsEmpty() ? S : ::IncludeTrailingBackslash(S);
  229. }
  230. //---------------------------------------------------------------------------
  231. AnsiString __fastcall THierarchicalStorage::ExcludeTrailingBackslash(const AnsiString S) const
  232. {
  233. return S.IsEmpty() ? S : ::ExcludeTrailingBackslash(S);
  234. }
  235. //===========================================================================
  236. __fastcall TRegistryStorage::TRegistryStorage(const AnsiString AStorage):
  237. THierarchicalStorage(IncludeTrailingBackslash(AStorage))
  238. {
  239. Init();
  240. };
  241. //---------------------------------------------------------------------------
  242. __fastcall TRegistryStorage::TRegistryStorage(const AnsiString AStorage, HKEY ARootKey):
  243. THierarchicalStorage(IncludeTrailingBackslash(AStorage))
  244. {
  245. Init();
  246. FRegistry->RootKey = ARootKey;
  247. }
  248. //---------------------------------------------------------------------------
  249. void __fastcall TRegistryStorage::Init()
  250. {
  251. FFailed = 0;
  252. FRegistry = new TRegistry();
  253. FRegistry->Access = KEY_READ;
  254. }
  255. //---------------------------------------------------------------------------
  256. __fastcall TRegistryStorage::~TRegistryStorage()
  257. {
  258. delete FRegistry;
  259. };
  260. //---------------------------------------------------------------------------
  261. void __fastcall TRegistryStorage::SetAccessMode(TStorageAccessMode value)
  262. {
  263. THierarchicalStorage::SetAccessMode(value);
  264. if (FRegistry)
  265. {
  266. switch (AccessMode) {
  267. case smRead:
  268. FRegistry->Access = KEY_READ;
  269. break;
  270. case smReadWrite:
  271. default:
  272. FRegistry->Access = KEY_READ | KEY_WRITE;
  273. break;
  274. }
  275. }
  276. }
  277. //---------------------------------------------------------------------------
  278. bool __fastcall TRegistryStorage::OpenSubKey(const AnsiString SubKey, bool CanCreate)
  279. {
  280. bool Result;
  281. if (FKeyHistory->Count > 0) FRegistry->CloseKey();
  282. Result = FRegistry->OpenKey(
  283. ExcludeTrailingBackslash(Storage + CurrentSubKey + SubKey), CanCreate);
  284. if (Result) Result = THierarchicalStorage::OpenSubKey(SubKey, CanCreate);
  285. return Result;
  286. }
  287. //---------------------------------------------------------------------------
  288. bool __fastcall TRegistryStorage::CreateSubKey(const AnsiString SubKey)
  289. {
  290. bool Result;
  291. if (FKeyHistory->Count) FRegistry->CloseKey();
  292. Result = FRegistry->CreateKey(ExcludeTrailingBackslash(Storage + CurrentSubKey + SubKey));
  293. if (Result) Result = THierarchicalStorage::CreateSubKey(CurrentSubKey + SubKey);
  294. return Result;
  295. }
  296. //---------------------------------------------------------------------------
  297. void __fastcall TRegistryStorage::CloseSubKey()
  298. {
  299. FRegistry->CloseKey();
  300. THierarchicalStorage::CloseSubKey();
  301. if (FKeyHistory->Count)
  302. {
  303. FRegistry->OpenKey(Storage + CurrentSubKey, True);
  304. }
  305. }
  306. //---------------------------------------------------------------------------
  307. bool __fastcall TRegistryStorage::DeleteSubKey(const AnsiString SubKey)
  308. {
  309. AnsiString K;
  310. if (FKeyHistory->Count == 0) K = Storage + CurrentSubKey;
  311. K += SubKey;
  312. return FRegistry->DeleteKey(K);
  313. }
  314. //---------------------------------------------------------------------------
  315. void __fastcall TRegistryStorage::GetSubKeyNames(Classes::TStrings* Strings)
  316. {
  317. FRegistry->GetKeyNames(Strings);
  318. }
  319. //---------------------------------------------------------------------------
  320. void __fastcall TRegistryStorage::GetValueNames(Classes::TStrings* Strings)
  321. {
  322. FRegistry->GetValueNames(Strings);
  323. }
  324. //---------------------------------------------------------------------------
  325. bool __fastcall TRegistryStorage::DeleteValue(const AnsiString Name)
  326. {
  327. return FRegistry->DeleteValue(Name);
  328. }
  329. //---------------------------------------------------------------------------
  330. bool __fastcall TRegistryStorage::KeyExists(const AnsiString SubKey)
  331. {
  332. return FRegistry->KeyExists(SubKey);
  333. }
  334. //---------------------------------------------------------------------------
  335. bool __fastcall TRegistryStorage::ValueExists(const AnsiString Value)
  336. {
  337. return FRegistry->ValueExists(Value);
  338. }
  339. //---------------------------------------------------------------------------
  340. int __fastcall TRegistryStorage::BinaryDataSize(const AnsiString Name)
  341. {
  342. return FRegistry->GetDataSize(Name);
  343. }
  344. //---------------------------------------------------------------------------
  345. bool __fastcall TRegistryStorage::ReadBool(const AnsiString Name, bool Default)
  346. {
  347. READ_REGISTRY(ReadBool);
  348. }
  349. //---------------------------------------------------------------------------
  350. TDateTime __fastcall TRegistryStorage::ReadDateTime(const AnsiString Name, TDateTime Default)
  351. {
  352. READ_REGISTRY(ReadDateTime);
  353. }
  354. //---------------------------------------------------------------------------
  355. double __fastcall TRegistryStorage::ReadFloat(const AnsiString Name, double Default)
  356. {
  357. READ_REGISTRY(ReadFloat);
  358. }
  359. //---------------------------------------------------------------------------
  360. int __fastcall TRegistryStorage::ReadInteger(const AnsiString Name, int Default)
  361. {
  362. READ_REGISTRY(ReadInteger);
  363. }
  364. //---------------------------------------------------------------------------
  365. __int64 __fastcall TRegistryStorage::ReadInt64(const AnsiString Name, __int64 Default)
  366. {
  367. __int64 Result = Default;
  368. if (FRegistry->ValueExists(Name))
  369. {
  370. try
  371. {
  372. FRegistry->ReadBinaryData(Name, &Result, sizeof(Result));
  373. }
  374. catch(...)
  375. {
  376. FFailed++;
  377. }
  378. }
  379. return Result;
  380. }
  381. //---------------------------------------------------------------------------
  382. AnsiString __fastcall TRegistryStorage::ReadStringRaw(const AnsiString Name, const AnsiString Default)
  383. {
  384. READ_REGISTRY(ReadString);
  385. }
  386. //---------------------------------------------------------------------------
  387. int __fastcall TRegistryStorage::ReadBinaryData(const AnsiString Name,
  388. void * Buffer, int Size)
  389. {
  390. int Result;
  391. if (FRegistry->ValueExists(Name))
  392. {
  393. try
  394. {
  395. Result = FRegistry->ReadBinaryData(Name, Buffer, Size);
  396. }
  397. catch(...)
  398. {
  399. Result = 0;
  400. FFailed++;
  401. }
  402. }
  403. else
  404. {
  405. Result = 0;
  406. }
  407. return Result;
  408. }
  409. //---------------------------------------------------------------------------
  410. void __fastcall TRegistryStorage::WriteBool(const AnsiString Name, bool Value)
  411. {
  412. WRITE_REGISTRY(WriteBool);
  413. }
  414. //---------------------------------------------------------------------------
  415. void __fastcall TRegistryStorage::WriteDateTime(const AnsiString Name, TDateTime Value)
  416. {
  417. WRITE_REGISTRY(WriteDateTime);
  418. }
  419. //---------------------------------------------------------------------------
  420. void __fastcall TRegistryStorage::WriteFloat(const AnsiString Name, double Value)
  421. {
  422. WRITE_REGISTRY(WriteFloat);
  423. }
  424. //---------------------------------------------------------------------------
  425. void __fastcall TRegistryStorage::WriteStringRaw(const AnsiString Name, const AnsiString Value)
  426. {
  427. WRITE_REGISTRY(WriteString);
  428. }
  429. //---------------------------------------------------------------------------
  430. void __fastcall TRegistryStorage::WriteInteger(const AnsiString Name, int Value)
  431. {
  432. WRITE_REGISTRY(WriteInteger);
  433. }
  434. //---------------------------------------------------------------------------
  435. void __fastcall TRegistryStorage::WriteInt64(const AnsiString Name, __int64 Value)
  436. {
  437. try
  438. {
  439. FRegistry->WriteBinaryData(Name, &Value, sizeof(Value));
  440. }
  441. catch(...)
  442. {
  443. FFailed++;
  444. }
  445. }
  446. //---------------------------------------------------------------------------
  447. void __fastcall TRegistryStorage::WriteBinaryData(const AnsiString Name,
  448. void * Buffer, int Size)
  449. {
  450. try
  451. {
  452. FRegistry->WriteBinaryData(Name, Buffer, Size);
  453. }
  454. catch(...)
  455. {
  456. FFailed++;
  457. }
  458. }
  459. //---------------------------------------------------------------------------
  460. int __fastcall TRegistryStorage::GetFailed()
  461. {
  462. int Result = FFailed;
  463. FFailed = 0;
  464. return Result;
  465. }
  466. //===========================================================================
  467. __fastcall TIniFileStorage::TIniFileStorage(const AnsiString AStorage):
  468. THierarchicalStorage(AStorage)
  469. {
  470. FIniFile = new TIniFile(Storage);
  471. }
  472. //---------------------------------------------------------------------------
  473. __fastcall TIniFileStorage::~TIniFileStorage()
  474. {
  475. delete FIniFile;
  476. }
  477. //---------------------------------------------------------------------------
  478. AnsiString __fastcall TIniFileStorage::GetCurrentSection()
  479. {
  480. return ExcludeTrailingBackslash(CurrentSubKey);
  481. }
  482. //---------------------------------------------------------------------------
  483. bool __fastcall TIniFileStorage::OpenSubKey(const AnsiString SubKey, bool CanCreate)
  484. {
  485. bool Result = CanCreate;
  486. if (!Result)
  487. {
  488. TStringList * Sections = new TStringList();
  489. try
  490. {
  491. Sections->Sorted = true;
  492. FIniFile->ReadSections(Sections);
  493. AnsiString NewKey = ExcludeTrailingBackslash(CurrentSubKey+SubKey);
  494. int Index = -1;
  495. if (Sections->Count)
  496. {
  497. Result = Sections->Find(NewKey, Index);
  498. if (!Result && Index < Sections->Count &&
  499. Sections->Strings[Index].SubString(1, NewKey.Length()+1) == NewKey + "\\")
  500. {
  501. Result = true;
  502. }
  503. }
  504. }
  505. __finally
  506. {
  507. delete Sections;
  508. }
  509. }
  510. if (Result)
  511. {
  512. Result = THierarchicalStorage::OpenSubKey(SubKey, CanCreate);
  513. }
  514. return Result;
  515. }
  516. //---------------------------------------------------------------------------
  517. bool __fastcall TIniFileStorage::DeleteSubKey(const AnsiString SubKey)
  518. {
  519. bool Result;
  520. try
  521. {
  522. FIniFile->EraseSection(CurrentSubKey + SubKey);
  523. Result = true;
  524. }
  525. catch (...)
  526. {
  527. Result = false;
  528. }
  529. return Result;
  530. }
  531. //---------------------------------------------------------------------------
  532. void __fastcall TIniFileStorage::GetSubKeyNames(Classes::TStrings* Strings)
  533. {
  534. TStrings * Sections = new TStringList();
  535. try
  536. {
  537. Strings->Clear();
  538. FIniFile->ReadSections(Sections);
  539. for (int i = 0; i < Sections->Count; i++)
  540. {
  541. AnsiString Section = Sections->Strings[i];
  542. if (AnsiCompareText(CurrentSubKey,
  543. Section.SubString(1, CurrentSubKey.Length())) == 0)
  544. {
  545. AnsiString SubSection = Section.SubString(CurrentSubKey.Length() + 1,
  546. Section.Length() - CurrentSubKey.Length());
  547. int P = SubSection.Pos("\\");
  548. if (P)
  549. {
  550. SubSection.SetLength(P - 1);
  551. }
  552. if (Strings->IndexOf(SubSection) < 0)
  553. {
  554. Strings->Add(SubSection);
  555. }
  556. }
  557. }
  558. }
  559. __finally
  560. {
  561. delete Sections;
  562. }
  563. }
  564. //---------------------------------------------------------------------------
  565. void __fastcall TIniFileStorage::GetValueNames(Classes::TStrings* Strings)
  566. {
  567. return FIniFile->ReadSection(CurrentSection, Strings);
  568. }
  569. //---------------------------------------------------------------------------
  570. bool __fastcall TIniFileStorage::KeyExists(const AnsiString SubKey)
  571. {
  572. return FIniFile->SectionExists(CurrentSubKey + SubKey);
  573. }
  574. //---------------------------------------------------------------------------
  575. bool __fastcall TIniFileStorage::ValueExists(const AnsiString Value)
  576. {
  577. return FIniFile->ValueExists(CurrentSection, Value);
  578. }
  579. //---------------------------------------------------------------------------
  580. bool __fastcall TIniFileStorage::DeleteValue(const AnsiString Name)
  581. {
  582. FIniFile->DeleteKey(CurrentSection, Name);
  583. return true;
  584. }
  585. //---------------------------------------------------------------------------
  586. int __fastcall TIniFileStorage::BinaryDataSize(const AnsiString Name)
  587. {
  588. return FIniFile->ReadString(CurrentSection, Name, "").Length() / 2;
  589. }
  590. //---------------------------------------------------------------------------
  591. bool __fastcall TIniFileStorage::ReadBool(const AnsiString Name, bool Default)
  592. {
  593. return FIniFile->ReadBool(CurrentSection, Name, Default);
  594. }
  595. //---------------------------------------------------------------------------
  596. int __fastcall TIniFileStorage::ReadInteger(const AnsiString Name, int Default)
  597. {
  598. return FIniFile->ReadInteger(CurrentSection, Name, Default);
  599. }
  600. //---------------------------------------------------------------------------
  601. __int64 __fastcall TIniFileStorage::ReadInt64(const AnsiString Name, __int64 Default)
  602. {
  603. __int64 Result = Default;
  604. AnsiString Str;
  605. Str = ReadStringRaw(Name, "");
  606. if (!Str.IsEmpty())
  607. {
  608. Result = StrToInt64Def(Str, Default);
  609. }
  610. return Result;
  611. }
  612. //---------------------------------------------------------------------------
  613. TDateTime __fastcall TIniFileStorage::ReadDateTime(const AnsiString Name, TDateTime Default)
  614. {
  615. return FIniFile->ReadDateTime(CurrentSection, Name, Default);
  616. }
  617. //---------------------------------------------------------------------------
  618. double __fastcall TIniFileStorage::ReadFloat(const AnsiString Name, double Default)
  619. {
  620. return FIniFile->ReadFloat(CurrentSection, Name, Default);
  621. }
  622. //---------------------------------------------------------------------------
  623. AnsiString __fastcall TIniFileStorage::ReadStringRaw(const AnsiString Name, AnsiString Default)
  624. {
  625. AnsiString Section = CurrentSection;
  626. AnsiString Result;
  627. Result = FIniFile->ReadString(Section, Name, Default);
  628. // TIniFile::ReadString has limit of 2 kB.
  629. // We could straithly use our routine, but call to legacy code is preserved
  630. // until ours it proved to work and also to save memory overhead
  631. if (Result.Length() == 2047)
  632. {
  633. char Buffer[10240];
  634. GetPrivateProfileString(Section.c_str(), Name.c_str(), Default.c_str(),
  635. Buffer, sizeof(Buffer), FIniFile->FileName.c_str());
  636. Result = Buffer;
  637. }
  638. return Result;
  639. }
  640. //---------------------------------------------------------------------------
  641. int __fastcall TIniFileStorage::ReadBinaryData(const AnsiString Name,
  642. void * Buffer, int Size)
  643. {
  644. AnsiString Value = HexToStr(ReadStringRaw(Name, ""));
  645. int Len = Value.Length();
  646. if (Size > Len)
  647. {
  648. Size = Len;
  649. }
  650. assert(Buffer);
  651. memcpy(Buffer, Value.c_str(), Size);
  652. return Size;
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TIniFileStorage::WriteBool(const AnsiString Name, bool Value)
  656. {
  657. FIniFile->WriteBool(CurrentSection, Name, Value);
  658. }
  659. //---------------------------------------------------------------------------
  660. void __fastcall TIniFileStorage::WriteInteger(const AnsiString Name, int Value)
  661. {
  662. FIniFile->WriteInteger(CurrentSection, Name, Value);
  663. }
  664. //---------------------------------------------------------------------------
  665. void __fastcall TIniFileStorage::WriteInt64(const AnsiString Name, __int64 Value)
  666. {
  667. WriteStringRaw(Name, IntToStr(Value));
  668. }
  669. //---------------------------------------------------------------------------
  670. void __fastcall TIniFileStorage::WriteDateTime(const AnsiString Name, TDateTime Value)
  671. {
  672. FIniFile->WriteDateTime(CurrentSection, Name, Value);
  673. }
  674. //---------------------------------------------------------------------------
  675. void __fastcall TIniFileStorage::WriteFloat(const AnsiString Name, double Value)
  676. {
  677. FIniFile->WriteFloat(CurrentSection, Name, Value);
  678. }
  679. //---------------------------------------------------------------------------
  680. void __fastcall TIniFileStorage::WriteStringRaw(const AnsiString Name, const AnsiString Value)
  681. {
  682. if ((Value.Length() >= 2) && (Value[1] == '"') && (Value[Value.Length()] == '"'))
  683. {
  684. FIniFile->WriteString(CurrentSection, Name, "\"" + Value + "\"");
  685. }
  686. else
  687. {
  688. FIniFile->WriteString(CurrentSection, Name, Value);
  689. }
  690. }
  691. //---------------------------------------------------------------------------
  692. void __fastcall TIniFileStorage::WriteBinaryData(const AnsiString Name,
  693. void * Buffer, int Size)
  694. {
  695. WriteStringRaw(Name, StrToHex(AnsiString(static_cast<char*>(Buffer), Size)));
  696. }