| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559 | //---------------------------------------------------------------------------#include <vcl.h>#pragma hdrstop#include "Common.h"#include "Exceptions.h"#include "PuttyIntf.h"#include "HierarchicalStorage.h"#include <Interface.h>#include <TextsCore.h>#include <StrUtils.hpp>#include <vector>//---------------------------------------------------------------------------#pragma package(smart_init)//---------------------------------------------------------------------------#define READ_REGISTRY(Method) \  if (FRegistry->ValueExists(Name)) \  try { return FRegistry->Method(Name); } catch(...) { FFailed++; return Default; } \  else return Default;#define WRITE_REGISTRY(Method) \  try { FRegistry->Method(Name, Value); } catch(...) { FFailed++; }//---------------------------------------------------------------------------UnicodeString __fastcall MungeStr(const UnicodeString & Str, bool ForceAnsi, bool Value){  RawByteString Source;  if (ForceAnsi)  {    Source = RawByteString(AnsiString(Str));  }  else  {    Source = RawByteString(UTF8String(Str));    if (Source.Length() > Str.Length())    {      Source.Insert(Bom, 1);    }  }  // should contain ASCII characters only  RawByteString Dest;  Dest.SetLength(Source.Length() * 3 + 1);  putty_mungestr(Source.c_str(), Dest.c_str());  PackStr(Dest);  if (Value)  {    // We do not want to munge * in PasswordMask    Dest = ReplaceStr(Dest, L"%2A", L"*");  }  return UnicodeString(Dest.c_str(), Dest.Length());}//---------------------------------------------------------------------------UnicodeString __fastcall UnMungeStr(const UnicodeString & Str){  // Str should contain ASCII characters only  RawByteString Source = AnsiString(Str);  RawByteString Dest;  Dest.SetLength(Source.Length() + 1);  putty_unmungestr(Source.c_str(), Dest.c_str(), Dest.Length());  // Cut the string at null character  PackStr(Dest);  UnicodeString Result;  if (Dest.SubString(1, LENOF(Bom)) == Bom)  {    Dest.Delete(1, LENOF(Bom));    Result = UTF8ToString(Dest);  }  else  {    Result = AnsiToString(Dest);  }  return Result;}//---------------------------------------------------------------------------UnicodeString __fastcall PuttyMungeStr(const UnicodeString Str){  return MungeStr(Str, false, false);}//---------------------------------------------------------------------------UnicodeString __fastcall MungeIniName(const UnicodeString & Str){  int P = Str.Pos(L"=");  // make this fast for now  if (P > 0)  {    return ReplaceStr(Str, L"=", L"%3D");  }  else  {    return Str;  }}//---------------------------------------------------------------------------UnicodeString __fastcall UnMungeIniName(const UnicodeString & Str){  int P = Str.Pos(L"%3D");  // make this fast for now  if (P > 0)  {    return ReplaceStr(Str, L"%3D", L"=");  }  else  {    return Str;  }}//===========================================================================__fastcall THierarchicalStorage::THierarchicalStorage(const UnicodeString AStorage){  FStorage = AStorage;  FKeyHistory = new TStringList();  AccessMode = smRead;  Explicit = false;  ForceSave = false;  // While this was implemented in 5.0 already, for some reason  // it was disabled (by mistake?). So although enabled for 5.6.1 only,  // data written in Unicode/UTF8 can be read by all versions back to 5.0.  ForceAnsi = false;  MungeStringValues = true;}//---------------------------------------------------------------------------__fastcall THierarchicalStorage::~THierarchicalStorage(){  delete FKeyHistory;}//---------------------------------------------------------------------------void __fastcall THierarchicalStorage::Flush(){}//---------------------------------------------------------------------------void __fastcall THierarchicalStorage::SetAccessMode(TStorageAccessMode value){  FAccessMode = value;}//---------------------------------------------------------------------------UnicodeString __fastcall THierarchicalStorage::GetCurrentSubKeyMunged(){  if (FKeyHistory->Count) return FKeyHistory->Strings[FKeyHistory->Count-1];    else return L"";}//---------------------------------------------------------------------------UnicodeString __fastcall THierarchicalStorage::GetCurrentSubKey(){  return UnMungeStr(GetCurrentSubKeyMunged());}//---------------------------------------------------------------------------bool __fastcall THierarchicalStorage::OpenRootKey(bool CanCreate){  return OpenSubKey(L"", CanCreate);}//---------------------------------------------------------------------------UnicodeString __fastcall THierarchicalStorage::MungeKeyName(UnicodeString Key){  UnicodeString Result = MungeStr(Key, ForceAnsi, false);  // if there's already ANSI-munged subkey, keep ANSI munging  if ((Result != Key) && !ForceAnsi && DoKeyExists(Key, true))  {    Result = MungeStr(Key, true, false);  }  return Result;}//---------------------------------------------------------------------------bool __fastcall THierarchicalStorage::OpenSubKey(UnicodeString Key, bool CanCreate, bool Path){  UnicodeString MungedKey;  if (Path)  {    DebugAssert(Key.IsEmpty() || (Key[Key.Length()] != L'\\'));    while (!Key.IsEmpty())    {      if (!MungedKey.IsEmpty())      {        MungedKey += L'\\';      }      MungedKey += MungeKeyName(CutToChar(Key, L'\\', false));    }  }  else  {    MungedKey = MungeKeyName(Key);  }  bool Result = DoOpenSubKey(MungedKey, CanCreate);  if (Result)  {    FKeyHistory->Add(IncludeTrailingBackslash(CurrentSubKey+MungedKey));  }  return Result;}//---------------------------------------------------------------------------void __fastcall THierarchicalStorage::CloseSubKey(){  if (FKeyHistory->Count == 0) throw Exception(L"");    else FKeyHistory->Delete(FKeyHistory->Count-1);}//---------------------------------------------------------------------------void __fastcall THierarchicalStorage::CloseAll(){  while (!CurrentSubKey.IsEmpty())  {    CloseSubKey();  }}//---------------------------------------------------------------------------void __fastcall THierarchicalStorage::ClearSubKeys(){  TStringList *SubKeys = new TStringList();  try  {    GetSubKeyNames(SubKeys);    for (int Index = 0; Index < SubKeys->Count; Index++)    {      RecursiveDeleteSubKey(SubKeys->Strings[Index]);    }  }  __finally  {    delete SubKeys;  }}//---------------------------------------------------------------------------void __fastcall THierarchicalStorage::RecursiveDeleteSubKey(const UnicodeString Key){  if (OpenSubKey(Key, false))  {    ClearSubKeys();    CloseSubKey();  }  DeleteSubKey(Key);}//---------------------------------------------------------------------------bool __fastcall THierarchicalStorage::HasSubKeys(){  bool Result;  TStrings * SubKeys = new TStringList();  try  {    GetSubKeyNames(SubKeys);    Result = (SubKeys->Count > 0);  }  __finally  {    delete SubKeys;  }  return Result;}//---------------------------------------------------------------------------bool __fastcall THierarchicalStorage::HasSubKey(const UnicodeString SubKey){  bool Result = OpenSubKey(SubKey, false);  if (Result)  {    CloseSubKey();  }  return Result;}//---------------------------------------------------------------------------bool __fastcall THierarchicalStorage::KeyExists(const UnicodeString SubKey){  return DoKeyExists(SubKey, ForceAnsi);}//---------------------------------------------------------------------------void __fastcall THierarchicalStorage::ReadValues(Classes::TStrings* Strings,  bool MaintainKeys){  TStrings * Names = new TStringList();  try  {    GetValueNames(Names);    for (int Index = 0; Index < Names->Count; Index++)    {      if (MaintainKeys)      {        Strings->Add(FORMAT(L"%s=%s", (Names->Strings[Index],          ReadString(Names->Strings[Index], L""))));      }      else      {        Strings->Add(ReadString(Names->Strings[Index], L""));      }    }  }  __finally  {    delete Names;  }}//---------------------------------------------------------------------------void __fastcall THierarchicalStorage::ClearValues(){  TStrings * Names = new TStringList();  try  {    GetValueNames(Names);    for (int Index = 0; Index < Names->Count; Index++)    {      DeleteValue(Names->Strings[Index]);    }  }  __finally  {    delete Names;  }}//---------------------------------------------------------------------------void __fastcall THierarchicalStorage::WriteValues(Classes::TStrings * Strings,  bool MaintainKeys){  ClearValues();  if (Strings)  {    for (int Index = 0; Index < Strings->Count; Index++)    {      if (MaintainKeys)      {        DebugAssert(Strings->Strings[Index].Pos(L"=") > 1);        WriteString(Strings->Names[Index], Strings->Values[Strings->Names[Index]]);      }      else      {        WriteString(IntToStr(Index), Strings->Strings[Index]);      }    }  }}//---------------------------------------------------------------------------UnicodeString __fastcall THierarchicalStorage::ReadString(const UnicodeString Name, const UnicodeString Default){  UnicodeString Result;  if (MungeStringValues)  {    Result = UnMungeStr(ReadStringRaw(Name, MungeStr(Default, ForceAnsi, true)));  }  else  {    Result = ReadStringRaw(Name, Default);  }  return Result;}//---------------------------------------------------------------------------RawByteString __fastcall THierarchicalStorage::ReadBinaryData(const UnicodeString Name){  size_t Size = BinaryDataSize(Name);  RawByteString Value;  Value.SetLength(Size);  ReadBinaryData(Name, Value.c_str(), Size);  return Value;}//---------------------------------------------------------------------------RawByteString __fastcall THierarchicalStorage::ReadStringAsBinaryData(const UnicodeString Name, const RawByteString Default){  UnicodeString UnicodeDefault = AnsiToString(Default);  // This should be exactly the same operation as calling ReadString in  // C++Builder 6 (non-Unicode) on Unicode-based OS  // (conversion is done by Ansi layer of the OS)  UnicodeString String = ReadString(Name, UnicodeDefault);  AnsiString Ansi = AnsiString(String);  RawByteString Result = RawByteString(Ansi.c_str(), Ansi.Length());  return Result;}//---------------------------------------------------------------------------void __fastcall THierarchicalStorage::WriteString(const UnicodeString Name, const UnicodeString Value){  if (MungeStringValues)  {    WriteStringRaw(Name, MungeStr(Value, ForceAnsi, true));  }  else  {    WriteStringRaw(Name, Value);  }}//---------------------------------------------------------------------------void __fastcall THierarchicalStorage::WriteBinaryData(const UnicodeString Name,  const RawByteString Value){  WriteBinaryData(Name, Value.c_str(), Value.Length());}//---------------------------------------------------------------------------void __fastcall THierarchicalStorage::WriteBinaryDataAsString(const UnicodeString Name, const RawByteString Value){  // This should be exactly the same operation as calling WriteString in  // C++Builder 6 (non-Unicode) on Unicode-based OS  // (conversion is done by Ansi layer of the OS)  WriteString(Name, AnsiToString(Value));}//---------------------------------------------------------------------------UnicodeString __fastcall THierarchicalStorage::IncludeTrailingBackslash(const UnicodeString & S){  // expanded from ?: as it caused memory leaks  if (S.IsEmpty())  {    return S;  }  else  {    return ::IncludeTrailingBackslash(S);  }}//---------------------------------------------------------------------------UnicodeString __fastcall THierarchicalStorage::ExcludeTrailingBackslash(const UnicodeString & S){  // expanded from ?: as it caused memory leaks  if (S.IsEmpty())  {    return S;  }  else  {    return ::ExcludeTrailingBackslash(S);  }}//---------------------------------------------------------------------------bool __fastcall THierarchicalStorage::GetTemporary(){  return false;}//===========================================================================__fastcall TRegistryStorage::TRegistryStorage(const UnicodeString AStorage):  THierarchicalStorage(IncludeTrailingBackslash(AStorage)){  FWowMode = 0;  Init();};//---------------------------------------------------------------------------__fastcall TRegistryStorage::TRegistryStorage(const UnicodeString AStorage, HKEY ARootKey, REGSAM WowMode):  THierarchicalStorage(IncludeTrailingBackslash(AStorage)){  FWowMode = WowMode;  Init();  FRegistry->RootKey = ARootKey;}//---------------------------------------------------------------------------void __fastcall TRegistryStorage::Init(){  FFailed = 0;  FRegistry = new TRegistry();  FRegistry->Access = KEY_READ | FWowMode;}//---------------------------------------------------------------------------__fastcall TRegistryStorage::~TRegistryStorage(){  delete FRegistry;};//---------------------------------------------------------------------------bool __fastcall TRegistryStorage::Copy(TRegistryStorage * Storage){  TRegistry * Registry = Storage->FRegistry;  bool Result = true;  TStrings * Names = new TStringList();  try  {    Registry->GetValueNames(Names);    std::vector<unsigned char> Buffer(1024, 0);    int Index = 0;    while ((Index < Names->Count) && Result)    {      UnicodeString Name = MungeStr(Names->Strings[Index], ForceAnsi, false);      unsigned long Size = Buffer.size();      unsigned long Type;      int RegResult;      do      {        RegResult = RegQueryValueEx(Registry->CurrentKey, Name.c_str(), NULL,          &Type, &Buffer[0], &Size);        if (RegResult == ERROR_MORE_DATA)        {          Buffer.resize(Size);        }      } while (RegResult == ERROR_MORE_DATA);      Result = (RegResult == ERROR_SUCCESS);      if (Result)      {        RegResult = RegSetValueEx(FRegistry->CurrentKey, Name.c_str(), NULL, Type,          &Buffer[0], Size);        Result = (RegResult == ERROR_SUCCESS);      }      ++Index;    }  }  __finally  {    delete Names;  }  return Result;}//---------------------------------------------------------------------------UnicodeString __fastcall TRegistryStorage::GetSource(){  return RootKeyToStr(FRegistry->RootKey) + L"\\" + Storage;}//---------------------------------------------------------------------------void __fastcall TRegistryStorage::SetAccessMode(TStorageAccessMode value){  THierarchicalStorage::SetAccessMode(value);  if (FRegistry)  {    switch (AccessMode) {      case smRead:        FRegistry->Access = KEY_READ | FWowMode;        break;      case smReadWrite:      default:        FRegistry->Access = KEY_READ | KEY_WRITE | FWowMode;        break;    }  }}//---------------------------------------------------------------------------bool __fastcall TRegistryStorage::DoOpenSubKey(const UnicodeString SubKey, bool CanCreate){  UnicodeString PrevPath;  bool WasOpened = (FRegistry->CurrentKey != NULL);  if (WasOpened)  {    PrevPath = FRegistry->CurrentPath;    DebugAssert(SamePaths(PrevPath, Storage + GetCurrentSubKeyMunged()));    FRegistry->CloseKey();  }  UnicodeString K = ExcludeTrailingBackslash(Storage + CurrentSubKey + SubKey);  bool Result = FRegistry->OpenKey(K, CanCreate);  if (!Result && WasOpened)  {    FRegistry->OpenKey(PrevPath, false);  }  return Result;}//---------------------------------------------------------------------------void __fastcall TRegistryStorage::CloseSubKey(){  FRegistry->CloseKey();  THierarchicalStorage::CloseSubKey();  if (FKeyHistory->Count)  {    FRegistry->OpenKey(Storage + GetCurrentSubKeyMunged(), True);  }}//---------------------------------------------------------------------------bool __fastcall TRegistryStorage::DeleteSubKey(const UnicodeString SubKey){  UnicodeString K;  if (FKeyHistory->Count == 0) K = Storage + CurrentSubKey;  K += MungeKeyName(SubKey);  return FRegistry->DeleteKey(K);}//---------------------------------------------------------------------------void __fastcall TRegistryStorage::GetSubKeyNames(Classes::TStrings* Strings){  FRegistry->GetKeyNames(Strings);  for (int Index = 0; Index < Strings->Count; Index++)  {    Strings->Strings[Index] = UnMungeStr(Strings->Strings[Index]);  }}//---------------------------------------------------------------------------void __fastcall TRegistryStorage::GetValueNames(Classes::TStrings* Strings){  FRegistry->GetValueNames(Strings);}//---------------------------------------------------------------------------bool __fastcall TRegistryStorage::DeleteValue(const UnicodeString Name){  return FRegistry->DeleteValue(Name);}//---------------------------------------------------------------------------bool __fastcall TRegistryStorage::DoKeyExists(const UnicodeString SubKey, bool AForceAnsi){  UnicodeString K = MungeStr(SubKey, AForceAnsi, false);  bool Result = FRegistry->KeyExists(K);  return Result;}//---------------------------------------------------------------------------bool __fastcall TRegistryStorage::ValueExists(const UnicodeString Value){  bool Result = FRegistry->ValueExists(Value);  return Result;}//---------------------------------------------------------------------------size_t __fastcall TRegistryStorage::BinaryDataSize(const UnicodeString Name){  size_t Result = FRegistry->GetDataSize(Name);  return Result;}//---------------------------------------------------------------------------bool __fastcall TRegistryStorage::ReadBool(const UnicodeString Name, bool Default){  READ_REGISTRY(ReadBool);}//---------------------------------------------------------------------------TDateTime __fastcall TRegistryStorage::ReadDateTime(const UnicodeString Name, TDateTime Default){  READ_REGISTRY(ReadDateTime);}//---------------------------------------------------------------------------double __fastcall TRegistryStorage::ReadFloat(const UnicodeString Name, double Default){  READ_REGISTRY(ReadFloat);}//---------------------------------------------------------------------------int __fastcall TRegistryStorage::ReadInteger(const UnicodeString Name, int Default){  READ_REGISTRY(ReadInteger);}//---------------------------------------------------------------------------__int64 __fastcall TRegistryStorage::ReadInt64(const UnicodeString Name, __int64 Default){  __int64 Result = Default;  if (FRegistry->ValueExists(Name))  {    try    {      FRegistry->ReadBinaryData(Name, &Result, sizeof(Result));    }    catch(...)    {      FFailed++;    }  }  return Result;}//---------------------------------------------------------------------------UnicodeString __fastcall TRegistryStorage::ReadStringRaw(const UnicodeString Name, const UnicodeString Default){  READ_REGISTRY(ReadString);}//---------------------------------------------------------------------------size_t __fastcall TRegistryStorage::ReadBinaryData(const UnicodeString Name,  void * Buffer, size_t Size){  size_t Result;  if (FRegistry->ValueExists(Name))  {    try    {      Result = FRegistry->ReadBinaryData(Name, Buffer, Size);    }    catch(...)    {      Result = 0;      FFailed++;    }  }  else  {    Result = 0;  }  return Result;}//---------------------------------------------------------------------------void __fastcall TRegistryStorage::WriteBool(const UnicodeString Name, bool Value){  WRITE_REGISTRY(WriteBool);}//---------------------------------------------------------------------------void __fastcall TRegistryStorage::WriteDateTime(const UnicodeString Name, TDateTime Value){  WRITE_REGISTRY(WriteDateTime);}//---------------------------------------------------------------------------void __fastcall TRegistryStorage::WriteFloat(const UnicodeString Name, double Value){  WRITE_REGISTRY(WriteFloat);}//---------------------------------------------------------------------------void __fastcall TRegistryStorage::WriteStringRaw(const UnicodeString Name, const UnicodeString Value){  WRITE_REGISTRY(WriteString);}//---------------------------------------------------------------------------void __fastcall TRegistryStorage::WriteInteger(const UnicodeString Name, int Value){  WRITE_REGISTRY(WriteInteger);}//---------------------------------------------------------------------------void __fastcall TRegistryStorage::WriteInt64(const UnicodeString Name, __int64 Value){  try  {    FRegistry->WriteBinaryData(Name, &Value, sizeof(Value));  }  catch(...)  {    FFailed++;  }}//---------------------------------------------------------------------------void __fastcall TRegistryStorage::WriteBinaryData(const UnicodeString Name,  const void * Buffer, int Size){  try  {    FRegistry->WriteBinaryData(Name, const_cast<void *>(Buffer), Size);  }  catch(...)  {    FFailed++;  }}//---------------------------------------------------------------------------int __fastcall TRegistryStorage::GetFailed(){  int Result = FFailed;  FFailed = 0;  return Result;}//===========================================================================__fastcall TCustomIniFileStorage::TCustomIniFileStorage(const UnicodeString Storage, TCustomIniFile * IniFile) :  THierarchicalStorage(Storage),  FIniFile(IniFile),  FMasterStorageOpenFailures(0),  FOpeningSubKey(false){}//---------------------------------------------------------------------------__fastcall TCustomIniFileStorage::~TCustomIniFileStorage(){  delete FIniFile;}//---------------------------------------------------------------------------UnicodeString __fastcall TCustomIniFileStorage::GetSource(){  return Storage;}//---------------------------------------------------------------------------UnicodeString __fastcall TCustomIniFileStorage::GetCurrentSection(){  return ExcludeTrailingBackslash(GetCurrentSubKeyMunged());}//---------------------------------------------------------------------------void __fastcall TCustomIniFileStorage::CacheSections(){  if (FSections.get() == NULL)  {    FSections.reset(new TStringList());    FIniFile->ReadSections(FSections.get());    FSections->Sorted = true; // has to set only after reading as ReadSections reset it to false  }}//---------------------------------------------------------------------------void __fastcall TCustomIniFileStorage::ResetCache(){  FSections.reset(NULL);}//---------------------------------------------------------------------------void __fastcall TCustomIniFileStorage::SetAccessMode(TStorageAccessMode value){  if (FMasterStorage.get() != NULL)  {    FMasterStorage->AccessMode = value;  }  THierarchicalStorage::SetAccessMode(value);}//---------------------------------------------------------------------------bool __fastcall TCustomIniFileStorage::DoOpenSubKey(const UnicodeString SubKey, bool CanCreate){  bool Result = CanCreate;  if (!Result)  {    CacheSections();    UnicodeString NewKey = ExcludeTrailingBackslash(CurrentSubKey+SubKey);    if (FSections->Count > 0)    {      int Index = -1;      Result = FSections->Find(NewKey, Index);      if (!Result &&          (Index < FSections->Count) &&          (FSections->Strings[Index].SubString(1, NewKey.Length()+1) == NewKey + L"\\"))      {        Result = true;      }    }  }  return Result;}//---------------------------------------------------------------------------bool __fastcall TCustomIniFileStorage::OpenRootKey(bool CanCreate){  // Not supported with master storage.  // Actually currently, we use OpenRootKey with TRegistryStorage only.  DebugAssert(FMasterStorage.get() == NULL);  return THierarchicalStorage::OpenRootKey(CanCreate);}//---------------------------------------------------------------------------bool __fastcall TCustomIniFileStorage::OpenSubKey(UnicodeString Key, bool CanCreate, bool Path){  bool Result;  {    TAutoFlag Flag(FOpeningSubKey);    Result = THierarchicalStorage::OpenSubKey(Key, CanCreate, Path);  }  if (FMasterStorage.get() != NULL)  {    if (FMasterStorageOpenFailures > 0)    {      FMasterStorageOpenFailures++;    }    else    {      bool MasterResult = FMasterStorage->OpenSubKey(Key, CanCreate, Path);      if (!Result && MasterResult)      {        Result = THierarchicalStorage::OpenSubKey(Key, true, Path);        DebugAssert(Result);      }      else if (Result && !MasterResult)      {        FMasterStorageOpenFailures++;      }    }  }  return Result;}//---------------------------------------------------------------------------void __fastcall TCustomIniFileStorage::CloseSubKey(){  THierarchicalStorage::CloseSubKey();  // What we are called to restore previous key from OpenSubKey,  // when opening path component fails, the master storage was not involved yet  if (!FOpeningSubKey && (FMasterStorage.get() != NULL))  {    if (FMasterStorageOpenFailures > 0)    {      FMasterStorageOpenFailures--;    }    else    {      FMasterStorage->CloseSubKey();    }  }}//---------------------------------------------------------------------------bool __fastcall TCustomIniFileStorage::DeleteSubKey(const UnicodeString SubKey){  bool Result;  try  {    ResetCache();    FIniFile->EraseSection(CurrentSubKey + MungeKeyName(SubKey));    Result = true;  }  catch (...)  {    Result = false;  }  if (HandleByMasterStorage())  {    Result = FMasterStorage->DeleteSubKey(SubKey);  }  return Result;}//---------------------------------------------------------------------------void __fastcall TCustomIniFileStorage::GetSubKeyNames(Classes::TStrings* Strings){  Strings->Clear();  if (HandleByMasterStorage())  {    FMasterStorage->GetSubKeyNames(Strings);  }  CacheSections();  for (int i = 0; i < FSections->Count; i++)  {    UnicodeString Section = FSections->Strings[i];    if (AnsiCompareText(CurrentSubKey,        Section.SubString(1, CurrentSubKey.Length())) == 0)    {      UnicodeString SubSection = Section.SubString(CurrentSubKey.Length() + 1,        Section.Length() - CurrentSubKey.Length());      int P = SubSection.Pos(L"\\");      if (P)      {        SubSection.SetLength(P - 1);      }      if (Strings->IndexOf(SubSection) < 0)      {        Strings->Add(UnMungeStr(SubSection));      }    }  }}//---------------------------------------------------------------------------void __fastcall TCustomIniFileStorage::GetValueNames(Classes::TStrings* Strings){  if (HandleByMasterStorage())  {    FMasterStorage->GetValueNames(Strings);  }  FIniFile->ReadSection(CurrentSection, Strings);  for (int Index = 0; Index < Strings->Count; Index++)  {    Strings->Strings[Index] = UnMungeIniName(Strings->Strings[Index]);  }}//---------------------------------------------------------------------------bool __fastcall TCustomIniFileStorage::DoKeyExists(const UnicodeString SubKey, bool AForceAnsi){  return    (HandleByMasterStorage() && FMasterStorage->DoKeyExists(SubKey, AForceAnsi)) ||    FIniFile->SectionExists(CurrentSubKey + MungeStr(SubKey, AForceAnsi, false));}//---------------------------------------------------------------------------bool __fastcall TCustomIniFileStorage::DoValueExists(const UnicodeString & Value){  return FIniFile->ValueExists(CurrentSection, MungeIniName(Value));}//---------------------------------------------------------------------------bool __fastcall TCustomIniFileStorage::ValueExists(const UnicodeString Value){  return    (HandleByMasterStorage() && FMasterStorage->ValueExists(Value)) ||    DoValueExists(Value);}//---------------------------------------------------------------------------bool __fastcall TCustomIniFileStorage::DeleteValue(const UnicodeString Name){  bool Result = true;  if (HandleByMasterStorage())  {    Result = FMasterStorage->DeleteValue(Name);  }  ResetCache();  FIniFile->DeleteKey(CurrentSection, MungeIniName(Name));  return Result;}//---------------------------------------------------------------------------bool __fastcall TCustomIniFileStorage::HandleByMasterStorage(){  return    (FMasterStorage.get() != NULL) &&    (FMasterStorageOpenFailures == 0);}//---------------------------------------------------------------------------bool __fastcall TCustomIniFileStorage::HandleReadByMasterStorage(const UnicodeString & Name){  return HandleByMasterStorage() && !DoValueExists(Name);}//---------------------------------------------------------------------------size_t __fastcall TCustomIniFileStorage::BinaryDataSize(const UnicodeString Name){  if (HandleReadByMasterStorage(Name))  {    return FMasterStorage->BinaryDataSize(Name);  }  else  {    return ReadStringRaw(Name, L"").Length() / 2;  }}//---------------------------------------------------------------------------bool __fastcall TCustomIniFileStorage::ReadBool(const UnicodeString Name, bool Default){  if (HandleReadByMasterStorage(Name))  {    return FMasterStorage->ReadBool(Name, Default);  }  else  {    return FIniFile->ReadBool(CurrentSection, MungeIniName(Name), Default);  }}//---------------------------------------------------------------------------int __fastcall TCustomIniFileStorage::ReadInteger(const UnicodeString Name, int Default){  int Result;  if (HandleReadByMasterStorage(Name))  {    Result = FMasterStorage->ReadInteger(Name, Default);  }  else  {    Result = FIniFile->ReadInteger(CurrentSection, MungeIniName(Name), Default);  }  return Result;}//---------------------------------------------------------------------------__int64 __fastcall TCustomIniFileStorage::ReadInt64(const UnicodeString Name, __int64 Default){  __int64 Result;  if (HandleReadByMasterStorage(Name))  {    Result = FMasterStorage->ReadInt64(Name, Default);  }  else  {    Result = Default;    UnicodeString Str;    Str = ReadStringRaw(Name, L"");    if (!Str.IsEmpty())    {      Result = StrToInt64Def(Str, Default);    }  }  return Result;}//---------------------------------------------------------------------------TDateTime __fastcall TCustomIniFileStorage::ReadDateTime(const UnicodeString Name, TDateTime Default){  TDateTime Result;  if (HandleReadByMasterStorage(Name))  {    Result = FMasterStorage->ReadDateTime(Name, Default);  }  else  {    UnicodeString Value = FIniFile->ReadString(CurrentSection, MungeIniName(Name), L"");    if (Value.IsEmpty())    {      Result = Default;    }    else    {      try      {        RawByteString Raw = HexToBytes(Value);        if (static_cast<size_t>(Raw.Length()) == sizeof(Result))        {          memcpy(&Result, Raw.c_str(), sizeof(Result));        }        else        {          Result = StrToDateTime(Value);        }      }      catch(...)      {        Result = Default;      }    }  }  return Result;}//---------------------------------------------------------------------------double __fastcall TCustomIniFileStorage::ReadFloat(const UnicodeString Name, double Default){  double Result;  if (HandleReadByMasterStorage(Name))  {    Result = FMasterStorage->ReadFloat(Name, Default);  }  else  {    UnicodeString Value = FIniFile->ReadString(CurrentSection, MungeIniName(Name), L"");    if (Value.IsEmpty())    {      Result = Default;    }    else    {      try      {        RawByteString Raw = HexToBytes(Value);        if (static_cast<size_t>(Raw.Length()) == sizeof(Result))        {          memcpy(&Result, Raw.c_str(), sizeof(Result));        }        else        {          Result = static_cast<double>(StrToFloat(Value));        }      }      catch(...)      {        Result = Default;      }    }  }  return Result;}//---------------------------------------------------------------------------UnicodeString __fastcall TCustomIniFileStorage::ReadStringRaw(const UnicodeString Name, UnicodeString Default){  UnicodeString Result;  if (HandleReadByMasterStorage(Name))  {    Result = FMasterStorage->ReadStringRaw(Name, Default);  }  else  {    Result = FIniFile->ReadString(CurrentSection, MungeIniName(Name), Default);  }  return Result;}//---------------------------------------------------------------------------size_t __fastcall TCustomIniFileStorage::ReadBinaryData(const UnicodeString Name,  void * Buffer, size_t Size){  size_t Len;  if (HandleReadByMasterStorage(Name))  {    FMasterStorage->ReadBinaryData(Name, Buffer, Size);  }  else  {    RawByteString Value = HexToBytes(ReadStringRaw(Name, L""));    Len = Value.Length();    if (Size > Len)    {      Size = Len;    }    DebugAssert(Buffer);    memcpy(Buffer, Value.c_str(), Size);  }  return Size;}//---------------------------------------------------------------------------void __fastcall TCustomIniFileStorage::WriteBool(const UnicodeString Name, bool Value){  if (HandleByMasterStorage())  {    FMasterStorage->WriteBool(Name, Value);  }  ResetCache();  FIniFile->WriteBool(CurrentSection, MungeIniName(Name), Value);}//---------------------------------------------------------------------------void __fastcall TCustomIniFileStorage::WriteInteger(const UnicodeString Name, int Value){  if (HandleByMasterStorage())  {    FMasterStorage->WriteInteger(Name, Value);  }  ResetCache();  FIniFile->WriteInteger(CurrentSection, MungeIniName(Name), Value);}//---------------------------------------------------------------------------void __fastcall TCustomIniFileStorage::WriteInt64(const UnicodeString Name, __int64 Value){  if (HandleByMasterStorage())  {    FMasterStorage->WriteInt64(Name, Value);  }  DoWriteStringRaw(Name, IntToStr(Value));}//---------------------------------------------------------------------------void __fastcall TCustomIniFileStorage::WriteDateTime(const UnicodeString Name, TDateTime Value){  if (HandleByMasterStorage())  {    FMasterStorage->WriteDateTime(Name, Value);  }  DoWriteBinaryData(Name, &Value, sizeof(Value));}//---------------------------------------------------------------------------void __fastcall TCustomIniFileStorage::WriteFloat(const UnicodeString Name, double Value){  if (HandleByMasterStorage())  {    FMasterStorage->WriteFloat(Name, Value);  }  DoWriteBinaryData(Name, &Value, sizeof(Value));}//---------------------------------------------------------------------------void __fastcall TCustomIniFileStorage::DoWriteStringRaw(const UnicodeString & Name, const UnicodeString & Value){  ResetCache();  FIniFile->WriteString(CurrentSection, MungeIniName(Name), Value);}//---------------------------------------------------------------------------void __fastcall TCustomIniFileStorage::WriteStringRaw(const UnicodeString Name, const UnicodeString Value){  if (HandleByMasterStorage())  {    FMasterStorage->WriteStringRaw(Name, Value);  }  DoWriteStringRaw(Name, Value);}//---------------------------------------------------------------------------void __fastcall TCustomIniFileStorage::DoWriteBinaryData(const UnicodeString & Name,  const void * Buffer, int Size){  DoWriteStringRaw(Name, BytesToHex(RawByteString(static_cast<const char*>(Buffer), Size)));}//---------------------------------------------------------------------------void __fastcall TCustomIniFileStorage::WriteBinaryData(const UnicodeString Name,  const void * Buffer, int Size){  if (HandleByMasterStorage())  {    FMasterStorage->WriteBinaryData(Name, Buffer, Size);  }  DoWriteBinaryData(Name, Buffer, Size);}//===========================================================================TIniFileStorage * __fastcall TIniFileStorage::CreateFromPath(const UnicodeString AStorage){  // The code was originally inline in the parent contructor call in the TIniFileStorage::TIniFileStorage [public originally].  // But if the TMemIniFile contructor throws (e.g. because the INI file is locked), the exception causes access violation.  // Moving the code to a factory solves this.  TMemIniFile * IniFile = new TMemIniFile(AStorage);  return new TIniFileStorage(AStorage, IniFile);}//---------------------------------------------------------------------------__fastcall TIniFileStorage::TIniFileStorage(const UnicodeString AStorage, TCustomIniFile * IniFile):  TCustomIniFileStorage(AStorage, IniFile){  FOriginal = new TStringList();  dynamic_cast<TMemIniFile *>(FIniFile)->GetStrings(FOriginal);  ApplyOverrides();}//---------------------------------------------------------------------------void __fastcall TIniFileStorage::Flush(){  if (FMasterStorage.get() != NULL)  {    FMasterStorage->Flush();  }  if (FOriginal != NULL)  {    TStrings * Strings = new TStringList;    try    {      dynamic_cast<TMemIniFile *>(FIniFile)->GetStrings(Strings);      if (!Strings->Equals(FOriginal))      {        int Attr;        // preserve attributes (especially hidden)        bool Exists = FileExists(ApiPath(Storage));        if (Exists)        {          Attr = GetFileAttributes(ApiPath(Storage).c_str());        }        else        {          Attr = FILE_ATTRIBUTE_NORMAL;        }        if (FLAGSET(Attr, FILE_ATTRIBUTE_READONLY) && ForceSave)        {          SetFileAttributes(ApiPath(Storage).c_str(), Attr & ~FILE_ATTRIBUTE_READONLY);        }        HANDLE Handle = CreateFile(ApiPath(Storage).c_str(), GENERIC_READ | GENERIC_WRITE,          0, NULL, CREATE_ALWAYS, Attr, 0);        if (Handle == INVALID_HANDLE_VALUE)        {          // "access denied" errors upon implicit saves to existing file are ignored          if (Explicit || !Exists || (GetLastError() != ERROR_ACCESS_DENIED))          {            throw EOSExtException(FMTLOAD((Exists ? WRITE_ERROR : CREATE_FILE_ERROR), (Storage)));          }        }        else        {          TStream * Stream = new THandleStream(int(Handle));          try          {            Strings->SaveToStream(Stream);          }          __finally          {            CloseHandle(Handle);            delete Stream;          }        }      }    }    __finally    {      delete FOriginal;      FOriginal = NULL;      delete Strings;    }  }}//---------------------------------------------------------------------------__fastcall TIniFileStorage::~TIniFileStorage(){  Flush();}//---------------------------------------------------------------------------void __fastcall TIniFileStorage::ApplyOverrides(){  UnicodeString OverridesKey = IncludeTrailingBackslash(L"Override");  CacheSections();  for (int i = 0; i < FSections->Count; i++)  {    UnicodeString Section = FSections->Strings[i];    if (SameText(OverridesKey,          Section.SubString(1, OverridesKey.Length())))    {      UnicodeString SubKey = Section.SubString(OverridesKey.Length() + 1,        Section.Length() - OverridesKey.Length());      // this all uses raw names (munged)      TStrings * Names = new TStringList;      try      {        FIniFile->ReadSection(Section, Names);        for (int ii = 0; ii < Names->Count; ii++)        {          UnicodeString Name = Names->Strings[ii];          UnicodeString Value = FIniFile->ReadString(Section, Name, L"");          FIniFile->WriteString(SubKey, Name, Value);        }      }      __finally      {        delete Names;      }      FIniFile->EraseSection(Section);      ResetCache();    }  }}//===========================================================================enum TWriteMode { wmAllow, wmFail, wmIgnore };//---------------------------------------------------------------------------class TOptionsIniFile : public TCustomIniFile{public:  __fastcall TOptionsIniFile(TStrings * Options, TWriteMode WriteMode, const UnicodeString & RootKey);  virtual UnicodeString __fastcall ReadString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Default);  virtual void __fastcall WriteString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Value);  virtual void __fastcall ReadSection(const UnicodeString Section, TStrings * Strings);  virtual void __fastcall ReadSections(TStrings* Strings);  virtual void __fastcall ReadSectionValues(const UnicodeString Section, TStrings* Strings);  virtual void __fastcall EraseSection(const UnicodeString Section);  virtual void __fastcall DeleteKey(const UnicodeString Section, const UnicodeString Ident);  virtual void __fastcall UpdateFile();  // Hoisted overload  void __fastcall ReadSections(const UnicodeString Section, TStrings* Strings);  // Ntb, we can implement ValueExists more efficiently than the TCustomIniFile.ValueExistsprivate:  TStrings * FOptions;  TWriteMode FWriteMode;  UnicodeString FRootKey;  bool __fastcall AllowWrite();  void __fastcall NotImplemented();  bool __fastcall AllowSection(const UnicodeString & Section);  UnicodeString __fastcall FormatKey(const UnicodeString & Section, const UnicodeString & Ident);};//---------------------------------------------------------------------------__fastcall TOptionsIniFile::TOptionsIniFile(TStrings * Options, TWriteMode WriteMode, const UnicodeString & RootKey) :  TCustomIniFile(UnicodeString()){  FOptions = Options;  FWriteMode = WriteMode;  FRootKey = RootKey;  if (!FRootKey.IsEmpty())  {    FRootKey += PathDelim;  }}//---------------------------------------------------------------------------void __fastcall TOptionsIniFile::NotImplemented(){  throw Exception(L"Not implemented");}//---------------------------------------------------------------------------bool __fastcall TOptionsIniFile::AllowWrite(){  switch (FWriteMode)  {    case wmAllow:      return true;    case wmFail:      NotImplemented();      return false; // never gets here    case wmIgnore:      return false;    default:      DebugFail();      return false;  }}//---------------------------------------------------------------------------bool __fastcall TOptionsIniFile::AllowSection(const UnicodeString & Section){  UnicodeString Name = Section;  if (!Name.IsEmpty())  {    Name += PathDelim;  }  bool Result = SameText(Name.SubString(1, FRootKey.Length()), FRootKey);  return Result;}//---------------------------------------------------------------------------UnicodeString __fastcall TOptionsIniFile::FormatKey(const UnicodeString & Section, const UnicodeString & Ident){  UnicodeString Result = Section;  if (!Result.IsEmpty())  {    Result += PathDelim;  }  Result += Ident; // Can be empty, when called from a contructor, AllowSection or ReadSection  if (DebugAlwaysTrue(AllowSection(Section)))  {    Result.Delete(1, FRootKey.Length());  }  return Result;}//---------------------------------------------------------------------------UnicodeString __fastcall TOptionsIniFile::ReadString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Default){  UnicodeString Value;  if (!AllowSection(Section))  {    Value = Default;  }  else  {    UnicodeString Name = FormatKey(Section, Ident);    int Index = FOptions->IndexOfName(Name);    if (Index >= 0)    {      Value = FOptions->ValueFromIndex[Index];    }    else    {      Value = Default;    }  }  return Value;}//---------------------------------------------------------------------------void __fastcall TOptionsIniFile::WriteString(const UnicodeString Section, const UnicodeString Ident, const UnicodeString Value){  if (AllowWrite() &&      DebugAlwaysTrue(AllowSection(Section)))  {    UnicodeString Name = FormatKey(Section, Ident);    FOptions->Values[Name] = Value;  }}//---------------------------------------------------------------------------void __fastcall TOptionsIniFile::ReadSection(const UnicodeString Section, TStrings * Strings){  if (AllowSection(Section))  {    UnicodeString SectionPrefix = FormatKey(Section, UnicodeString());    Strings->BeginUpdate();    try    {      for (int Index = 0; Index < FOptions->Count; Index++)      {        UnicodeString Name = FOptions->Names[Index];        if (SameText(Name.SubString(1, SectionPrefix.Length()), SectionPrefix) &&            (LastDelimiter(PathDelim, Name) <= SectionPrefix.Length()))        {          Strings->Add(Name.SubString(SectionPrefix.Length() + 1, Name.Length() - SectionPrefix.Length()));        }      }    }    __finally    {      Strings->EndUpdate();    }  }}//---------------------------------------------------------------------------void __fastcall TOptionsIniFile::ReadSections(TStrings * Strings){  std::unique_ptr<TStringList> Sections(CreateSortedStringList());  for (int Index = 0; Index < FOptions->Count; Index++)  {    UnicodeString Name = FOptions->Names[Index];    int P = LastDelimiter(PathDelim, Name);    if (P > 0)    {      UnicodeString Section = Name.SubString(1, P - 1);      if (Sections->IndexOf(Section) < 0)      {        Sections->Add(Section);      }    }  }  for (int Index = 0; Index < Sections->Count; Index++)  {    Strings->Add(FRootKey + Sections->Strings[Index]);  }}//---------------------------------------------------------------------------void __fastcall TOptionsIniFile::ReadSectionValues(const UnicodeString Section, TStrings * /*Strings*/){  NotImplemented();}//---------------------------------------------------------------------------void __fastcall TOptionsIniFile::EraseSection(const UnicodeString Section){  if (AllowWrite())  {    NotImplemented();  }}//---------------------------------------------------------------------------void __fastcall TOptionsIniFile::DeleteKey(const UnicodeString Section, const UnicodeString Ident){  if (AllowWrite() &&      DebugAlwaysTrue(AllowSection(Section)))  {    UnicodeString Name = FormatKey(Section, Ident);    int Index = FOptions->IndexOfName(Name);    if (Index >= 0)    {      FOptions->Delete(Index);    }  }}//---------------------------------------------------------------------------void __fastcall TOptionsIniFile::UpdateFile(){  if (AllowWrite())  {    // noop  }}//---------------------------------------------------------------------------void __fastcall TOptionsIniFile::ReadSections(const UnicodeString /*Section*/, TStrings * /*Strings*/){  NotImplemented();}//===========================================================================__fastcall TOptionsStorage::TOptionsStorage(TStrings * Options, bool AllowWrite):  TCustomIniFileStorage(    UnicodeString(L"Command-line options"),    new TOptionsIniFile(Options, (AllowWrite ? wmAllow : wmFail), UnicodeString())){}//---------------------------------------------------------------------------__fastcall TOptionsStorage::TOptionsStorage(TStrings * Options, const UnicodeString & RootKey, THierarchicalStorage * MasterStorage) :  TCustomIniFileStorage(    UnicodeString(L"Command-line options overriding " + MasterStorage->Source),    new TOptionsIniFile(Options, wmIgnore, RootKey)){  FMasterStorage.reset(MasterStorage);}//---------------------------------------------------------------------------bool __fastcall TOptionsStorage::GetTemporary(){  return true;}
 |