UnicodeString FileSearch(const UnicodeString & FileName, const UnicodeString & DirectoryList) { UnicodeString Temp; UnicodeString Result; Temp = DirectoryList; UnicodeString PathSeparators = L"/\\"; do { intptr_t I = ::Pos(Temp, PathSeparators); while ((Temp.Length() > 0) && (I == 0)) { Temp.Delete(1, 1); I = ::Pos(Temp, PathSeparators); } I = ::Pos(Temp, PathSeparators); if (I > 0) { Result = Temp.SubString(1, I - 1); Temp.Delete(1, I); } else { Result = Temp; Temp = L""; } Result = ::IncludeTrailingBackslash(Result); Result = Result + FileName; if (!::FileExists(Result)) { Result = L""; } } while (!(Temp.Length() == 0) || (Result.Length() != 0)); return Result; }
//--------------------------------------------------------------------------- UnicodeString __fastcall TGenerateUrlDialog::GenerateUrl(UnicodeString Path) { UnicodeString Url = FData->GenerateSessionUrl( FLAGMASK(WinSCPSpecificCheck->Checked, sufSpecific) | FLAGMASK(UserNameCheck->Enabled && UserNameCheck->Checked, sufUserName) | FLAGMASK(PasswordCheck->Enabled && PasswordCheck->Checked, sufPassword) | FLAGMASK(HostKeyCheck->Enabled && HostKeyCheck->Checked, sufHostKey)); if ((RemoteDirectoryCheck->Enabled && RemoteDirectoryCheck->Checked) || (FPaths != NULL)) { if (StartsStr(L"/", Path)); { Path.Delete(1, 1); } Url += EncodeUrlPath(Path); } if (SaveExtensionCheck->Enabled && SaveExtensionCheck->Checked) { Url += UnicodeString(UrlParamSeparator) + UrlSaveParamName; } return Url; }
//--------------------------------------------------------------------------- void __fastcall TRightsFrame::SetText(UnicodeString value) { if (Text != value) { UnicodeString RightsStr = value; int P = RightsStr.LowerCase().Pos(FAddXToDirectoriesSuffix); bool AAddXToDirectories = (P > 0); if (AAddXToDirectories) { RightsStr.Delete(P, FAddXToDirectoriesSuffix.Length()); } RightsStr = DeleteChar(DeleteChar(RightsStr, L'('), L')').Trim(); TRights R = Rights; if (((RightsStr.Length() == 3) || (RightsStr.Length() == 4)) && IsNumber(RightsStr)) { R.Octal = RightsStr; } else { R.Text = RightsStr; } Rights = R; AddXToDirectories = AAddXToDirectories; } }
//--------------------------------------------------------------------------- UnicodeString UnixExcludeLeadingBackslash(const UnicodeString & Path) { UnicodeString Result = Path; while (!Result.IsEmpty() && Result[1] == L'/') { Result.Delete(1, 1); } return Result; }
UnicodeString TCopyParamType::RestoreChars(const UnicodeString & AFileName) const { UnicodeString FileName = AFileName; if (GetInvalidCharsReplacement() == TokenReplacement) { wchar_t * InvalidChar = const_cast<wchar_t *>(FileName.c_str()); while ((InvalidChar = wcschr(InvalidChar, TokenPrefix)) != nullptr) { intptr_t Index = InvalidChar - FileName.c_str() + 1; if (FileName.Length() >= Index + 2) { UnicodeString Hex = FileName.SubString(Index + 1, 2); wchar_t Char = static_cast<wchar_t>(HexToByte(Hex)); if ((Char != L'\0') && ((FTokenizibleChars.Pos(Char) > 0) || (((Char == L' ') || (Char == L'.')) && (Index == FileName.Length() - 2)))) { FileName[Index] = Char; FileName.Delete(Index + 1, 2); InvalidChar = const_cast<wchar_t *>(FileName.c_str() + Index); } else if ((Hex == L"00") && ((Index == FileName.Length() - 2) || (FileName[Index + 3] == L'.')) && IsReservedName(FileName.SubString(1, Index - 1) + FileName.SubString(Index + 3, FileName.Length() - Index - 3 + 1))) { FileName.Delete(Index, 3); InvalidChar = const_cast<wchar_t *>(FileName.c_str() + Index - 1); } else { InvalidChar++; } } else { InvalidChar++; } } } return FileName; }
bool TForm1::MakeBackup() { UnicodeString dir = ExtractFileDir(m_strFileName) + "\\backups"; if (!DirectoryExists(dir) && !CreateDir(dir)) { return false; } UnicodeString newfilename = ExtractFileName(m_strFileName); newfilename.Delete(newfilename.Length() - ExtractFileExt(m_strFileName).Length() + 1, ExtractFileExt(m_strFileName).Length()); newfilename = dir + L"\\" + newfilename + " (" + TDateTime::CurrentDateTime().FormatString("yyyy-mm-dd hh_nn") + ")" + ExtractFileExt(m_strFileName); Log->Lines->Add("Сохраняю резервную копию файла в " + newfilename); bool bResult = CopyFile(m_strFileName.c_str(),newfilename.c_str(),false); return bResult; }
static UnicodeString DoXmlEscape(const UnicodeString & Str, bool NewLine) { UnicodeString Result = Str; for (intptr_t Index = 1; Index <= Result.Length(); ++Index) { const wchar_t * Repl = nullptr; switch (Result[Index]) { case L'&': Repl = L"amp;"; break; case L'>': Repl = L"gt;"; break; case L'<': Repl = L"lt;"; break; case L'"': Repl = L"quot;"; break; case L'\n': if (NewLine) { Repl = L"#10;"; } break; case L'\r': Result.Delete(Index, 1); Index--; break; } if (Repl != nullptr) { Result[Index] = L'&'; Result.Insert(Repl, Index + 1); Index += wcslen(Repl); } } return Result; }
static UnicodeString DoXmlEscape(UnicodeString AStr, bool NewLine) { UnicodeString Str = AStr; for (intptr_t Index = 1; Index <= Str.Length(); ++Index) { const wchar_t *Repl = nullptr; switch (Str[Index]) { case L'&': Repl = L"amp;"; break; case L'>': Repl = L"gt;"; break; case L'<': Repl = L"lt;"; break; case L'"': Repl = L"quot;"; break; case L'\n': if (NewLine) { Repl = L"#10;"; } break; case L'\r': Str.Delete(Index, 1); --Index; break; } if (Repl != nullptr) { Str[Index] = L'&'; Str.Insert(Repl, Index + 1); Index += nb::StrLength(Repl); } } return Str; }
//--------------------------------------------------------------------------- UnicodeString __fastcall DoXmlEscape(UnicodeString Str, bool NewLine) { for (int i = 1; i <= Str.Length(); i++) { const wchar_t * Repl = NULL; switch (Str[i]) { case L'&': Repl = L"amp;"; break; case L'>': Repl = L"gt;"; break; case L'<': Repl = L"lt;"; break; case L'"': Repl = L"quot;"; break; case L'\n': if (NewLine) { Repl = L"#10;"; } break; case L'\r': Str.Delete(i, 1); i--; break; } if (Repl != NULL) { Str[i] = L'&'; Str.Insert(Repl, i + 1); i += wcslen(Repl); } } return Str; }
//--------------------------------------------------------------------------- static long OpenWinSCPKey(HKEY Key, const char * SubKey, HKEY * Result, bool CanCreate) { long R; assert(Configuration != NULL); assert(Key == HKEY_CURRENT_USER); USEDPARAM(Key); UnicodeString RegKey = SubKey; int PuttyKeyLen = OriginalPuttyRegistryStorageKey.Length(); assert(RegKey.SubString(1, PuttyKeyLen) == OriginalPuttyRegistryStorageKey); RegKey = RegKey.SubString(PuttyKeyLen + 1, RegKey.Length() - PuttyKeyLen); if (!RegKey.IsEmpty()) { assert(RegKey[1] == L'\\'); RegKey.Delete(1, 1); } if (RegKey.IsEmpty()) { *Result = static_cast<HKEY>(NULL); R = ERROR_SUCCESS; } else { // we expect this to be called only from verify_host_key() or store_host_key() assert(RegKey == L"SshHostKeys"); THierarchicalStorage * Storage = Configuration->CreateConfigStorage(); Storage->AccessMode = (CanCreate ? smReadWrite : smRead); if (Storage->OpenSubKey(RegKey, CanCreate)) { *Result = reinterpret_cast<HKEY>(Storage); R = ERROR_SUCCESS; } else { delete Storage; R = ERROR_CANTOPEN; } } return R; }
static long OpenWinSCPKey(HKEY Key, const char * SubKey, HKEY * Result, bool CanCreate) { long R; assert(GetConfiguration() != nullptr); assert(Key == HKEY_CURRENT_USER); USEDPARAM(Key); UnicodeString RegKey = SubKey; UnicodeString OriginalPuttyRegistryStorageKey(_T(PUTTY_REG_POS)); intptr_t PuttyKeyLen = OriginalPuttyRegistryStorageKey.Length(); assert(RegKey.SubString(1, PuttyKeyLen) == OriginalPuttyRegistryStorageKey); RegKey = RegKey.SubString(PuttyKeyLen + 1, RegKey.Length() - PuttyKeyLen); if (!RegKey.IsEmpty()) { assert(RegKey[1] == L'\\'); RegKey.Delete(1, 1); } if (RegKey.IsEmpty()) { *Result = static_cast<HKEY>(nullptr); R = ERROR_SUCCESS; } else { // we expect this to be called only from verify_host_key() or store_host_key() assert(RegKey == L"SshHostKeys"); std::unique_ptr<THierarchicalStorage> Storage(GetConfiguration()->CreateConfigStorage()); Storage->SetAccessMode((CanCreate ? smReadWrite : smRead)); if (Storage->OpenSubKey(RegKey, CanCreate)) { *Result = reinterpret_cast<HKEY>(Storage.release()); R = ERROR_SUCCESS; } else { R = ERROR_CANTOPEN; } } return R; }
//--------------------------------------------------------------------------- UnicodeString StripHotkey(const UnicodeString & AText) { UnicodeString Result = AText; intptr_t Len = Result.Length(); intptr_t Pos = 1; while (Pos <= Len) { if (Result[Pos] == L'&') { Result.Delete(Pos, 1); Len--; } else { Pos++; } } return Result; }
//--------------------------------------------------------------------------- void __fastcall TFileFindDialog::FileFound(TTerminal * /*Terminal*/, const UnicodeString FileName, const TRemoteFile * AFile, bool & Cancel) { TListItem * Item = FileView->Items->Add(); TRemoteFile * File = AFile->Duplicate(true); Item->Data = File; Item->ImageIndex = File->IconIndex; UnicodeString Caption = File->FileName; if (File->IsDirectory) { Caption = UnixIncludeTrailingBackslash(Caption); } Item->Caption = Caption; UnicodeString Directory = UnixExtractFilePath(File->FullFileName); if (AnsiSameText(FDirectory, Directory.SubString(1, FDirectory.Length()))) { Directory[1] = L'.'; Directory.Delete(2, FDirectory.Length() - 1); } else { assert(false); } Item->SubItems->Add(Directory); if (File->IsDirectory) { Item->SubItems->Add(L""); } else { Item->SubItems->Add( FormatBytes(File->Size, WinConfiguration->FormatSizeBytes, WinConfiguration->FormatSizeBytes)); } Item->SubItems->Add(UserModificationStr(File->Modification, File->ModificationFmt)); UpdateControls(); Cancel = (FState == ffAborting); Application->ProcessMessages(); }
//--------------------------------------------------------------------------- void TFileMasks::CreateMask( const UnicodeString & MaskStr, intptr_t MaskStart, intptr_t /*MaskEnd*/, bool Include) { bool Directory = false; // shut up TMask Mask; Mask.MaskStr = MaskStr; Mask.UserStr = MaskStr; Mask.FileNameMask.Kind = TMaskMask::Any; Mask.FileNameMask.Mask = nullptr; Mask.DirectoryMask.Kind = TMaskMask::Any; Mask.DirectoryMask.Mask = nullptr; Mask.HighSizeMask = TMask::None; Mask.LowSizeMask = TMask::None; Mask.HighModificationMask = TMask::None; Mask.LowModificationMask = TMask::None; wchar_t NextPartDelimiter = L'\0'; intptr_t NextPartFrom = 1; while (NextPartFrom <= MaskStr.Length()) { wchar_t PartDelimiter = NextPartDelimiter; intptr_t PartFrom = NextPartFrom; UnicodeString PartStr = CopyToChars(MaskStr, NextPartFrom, L"<>", false, &NextPartDelimiter, true); intptr_t PartStart = MaskStart + PartFrom - 1; intptr_t PartEnd = MaskStart + NextPartFrom - 1 - 2; TrimEx(PartStr, PartStart, PartEnd); if (PartDelimiter != L'\0') { bool Low = (PartDelimiter == L'>'); TMask::TMaskBoundary Boundary; if ((PartStr.Length() >= 1) && (PartStr[1] == L'=')) { Boundary = TMask::Close; PartStr.Delete(1, 1); } else { Boundary = TMask::Open; } TFormatSettings FormatSettings = TFormatSettings::Create(GetDefaultLCID()); FormatSettings.DateSeparator = L'-'; FormatSettings.TimeSeparator = L':'; FormatSettings.ShortDateFormat = L"yyyy/mm/dd"; FormatSettings.ShortTimeFormat = L"hh:nn:ss"; TDateTime Modification; if (TryStrToDateTime(PartStr, Modification, FormatSettings) || TryRelativeStrToDateTime(PartStr, Modification)) { TMask::TMaskBoundary & ModificationMask = (Low ? Mask.LowModificationMask : Mask.HighModificationMask); if ((ModificationMask != TMask::None) || Directory) { // include delimiter into size part ThrowError(PartStart - 1, PartEnd); } ModificationMask = Boundary; (Low ? Mask.LowModification : Mask.HighModification) = Modification; } else { TMask::TMaskBoundary & SizeMask = (Low ? Mask.LowSizeMask : Mask.HighSizeMask); __int64 & Size = (Low ? Mask.LowSize : Mask.HighSize); if ((SizeMask != TMask::None) || Directory) { // include delimiter into size part ThrowError(PartStart - 1, PartEnd); } SizeMask = Boundary; Size = ParseSize(PartStr); } } else if (!PartStr.IsEmpty()) { intptr_t D = PartStr.LastDelimiter(DirectoryMaskDelimiters); Directory = (D > 0) && (D == PartStr.Length()); if (Directory) { do { PartStr.SetLength(PartStr.Length() - 1); Mask.UserStr.Delete(PartStart - MaskStart + D, 1); D--; } while (PartStr.IsDelimiter(DirectoryMaskDelimiters, PartStr.Length())); D = PartStr.LastDelimiter(DirectoryMaskDelimiters); if (FForceDirectoryMasks == 0) { Directory = false; Mask.MaskStr = Mask.UserStr; } } else if (FForceDirectoryMasks > 0) { Directory = true; Mask.MaskStr.Insert(DirectoryMaskDelimiters[1], PartStart - MaskStart + PartStr.Length()); } if (D > 0) { // make sure sole "/" (root dir) is preserved as is CreateMaskMask( UnixExcludeTrailingBackslash(ToUnixPath(PartStr.SubString(1, D))), PartStart, PartStart + D - 1, false, Mask.DirectoryMask); CreateMaskMask( PartStr.SubString(D + 1, PartStr.Length() - D), PartStart + D, PartEnd, true, Mask.FileNameMask); } else { CreateMaskMask(PartStr, PartStart, PartEnd, true, Mask.FileNameMask); } } } FMasks[MASK_INDEX(Directory, Include)].push_back(Mask); }
//--------------------------------------------------------------------------- void __fastcall TSynchronizeChecklistDialog::LoadItem(TListItem * Item) { UnicodeString S; const TSynchronizeChecklist::TItem * ChecklistItem = FChecklist->Item[Item->Index]; Item->Data = const_cast<TSynchronizeChecklist::TItem *>(ChecklistItem); Item->Checked = ChecklistItem->Checked; if (ChecklistItem->ImageIndex >= 0) { Item->ImageIndex = ChecklistItem->ImageIndex; } else { Item->ImageIndex = FakeFileImageIndex(ChecklistItem->GetFileName(), FLAGMASK(ChecklistItem->IsDirectory, FILE_ATTRIBUTE_DIRECTORY)); } S = ChecklistItem->GetFileName(); if (ChecklistItem->IsDirectory) { S = IncludeTrailingBackslash(S); } Item->Caption = S; if (ChecklistItem->Action == TSynchronizeChecklist::saDeleteRemote) { Item->SubItems->Add(L""); Item->SubItems->Add(L""); Item->SubItems->Add(L""); } else { S = ChecklistItem->Local.Directory; if (AnsiSameText(FLocalDirectory, S.SubString(1, FLocalDirectory.Length()))) { S[1] = L'.'; S.Delete(2, FLocalDirectory.Length() - 1); } else { assert(false); } Item->SubItems->Add(S); if (ChecklistItem->Action == TSynchronizeChecklist::saDownloadNew) { Item->SubItems->Add(L""); Item->SubItems->Add(L""); } else { if (ChecklistItem->IsDirectory) { Item->SubItems->Add(L""); } else { Item->SubItems->Add( FormatBytes(ChecklistItem->Local.Size, WinConfiguration->FormatSizeBytes, WinConfiguration->FormatSizeBytes)); } Item->SubItems->Add(UserModificationStr(ChecklistItem->Local.Modification, ChecklistItem->Local.ModificationFmt)); } } Item->SubItems->Add(L""); if (ChecklistItem->Action == TSynchronizeChecklist::saDeleteLocal) { Item->SubItems->Add(L""); Item->SubItems->Add(L""); Item->SubItems->Add(L""); } else { S = ChecklistItem->Remote.Directory; if (AnsiSameText(FRemoteDirectory, S.SubString(1, FRemoteDirectory.Length()))) { S[1] = L'.'; S.Delete(2, FRemoteDirectory.Length() - 1); } else { assert(false); } Item->SubItems->Add(S); if (ChecklistItem->Action == TSynchronizeChecklist::saUploadNew) { Item->SubItems->Add(L""); Item->SubItems->Add(L""); } else { if (ChecklistItem->IsDirectory) { Item->SubItems->Add(L""); } else { Item->SubItems->Add( FormatBytes(ChecklistItem->Remote.Size, WinConfiguration->FormatSizeBytes, WinConfiguration->FormatSizeBytes)); } Item->SubItems->Add(UserModificationStr(ChecklistItem->Remote.Modification, ChecklistItem->Remote.ModificationFmt)); } } }