| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "Common.h"
- #include "Exceptions.h"
- #include "TextsCore.h"
- #include "Interface.h"
- #include <StrUtils.hpp>
- #include <math.h>
- #include <shellapi.h>
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- #ifdef _DEBUG
- #include <stdio.h>
- void __fastcall Trace(const AnsiString SourceFile, const AnsiString Func,
- int Line, const AnsiString Message)
- {
- const char * FileName = getenv(TRACEENV);
- if (FileName == NULL)
- {
- FileName = "C:\\winscptrace.log";
- }
- FILE * File = fopen(FileName, "a");
- if (File != NULL)
- {
- fprintf(File, "[%s] %s:%d:%s\n %s\n",
- Now().TimeString().c_str(),
- ExtractFileName(SourceFile).c_str(), Line, Func.c_str(), Message.c_str());
- fclose(File);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall DoAssert(char * Message, char * Filename, int LineNumber)
- {
- Trace(Filename, "assert", LineNumber, Message);
- _assert(Message, Filename, LineNumber);
- }
- #endif // ifdef _DEBUG
- //---------------------------------------------------------------------------
- // TCriticalSection
- //---------------------------------------------------------------------------
- __fastcall TCriticalSection::TCriticalSection()
- {
- FAcquired = 0;
- InitializeCriticalSection(&FSection);
- }
- //---------------------------------------------------------------------------
- __fastcall TCriticalSection::~TCriticalSection()
- {
- assert(FAcquired == 0);
- DeleteCriticalSection(&FSection);
- }
- //---------------------------------------------------------------------------
- void __fastcall TCriticalSection::Enter()
- {
- EnterCriticalSection(&FSection);
- FAcquired++;
- }
- //---------------------------------------------------------------------------
- void __fastcall TCriticalSection::Leave()
- {
- FAcquired--;
- LeaveCriticalSection(&FSection);
- }
- //---------------------------------------------------------------------------
- // TGuard
- //---------------------------------------------------------------------------
- __fastcall TGuard::TGuard(TCriticalSection * ACriticalSection) :
- FCriticalSection(ACriticalSection)
- {
- assert(ACriticalSection != NULL);
- FCriticalSection->Enter();
- }
- //---------------------------------------------------------------------------
- __fastcall TGuard::~TGuard()
- {
- FCriticalSection->Leave();
- }
- //---------------------------------------------------------------------------
- // TUnguard
- //---------------------------------------------------------------------------
- __fastcall TUnguard::TUnguard(TCriticalSection * ACriticalSection) :
- FCriticalSection(ACriticalSection)
- {
- assert(ACriticalSection != NULL);
- FCriticalSection->Leave();
- }
- //---------------------------------------------------------------------------
- __fastcall TUnguard::~TUnguard()
- {
- FCriticalSection->Enter();
- }
- //---------------------------------------------------------------------------
- //---------------------------------------------------------------------------
- const char EngShortMonthNames[12][4] =
- {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
- //---------------------------------------------------------------------------
- AnsiString ReplaceChar(AnsiString Str, Char A, Char B)
- {
- for (Integer Index = 0; Index < Str.Length(); Index++)
- if (Str[Index+1] == A) Str[Index+1] = B;
- return Str;
- }
- //---------------------------------------------------------------------------
- AnsiString DeleteChar(AnsiString Str, Char C)
- {
- int P;
- while ((P = Str.Pos(C)) > 0)
- {
- Str.Delete(P, 1);
- }
- return Str;
- }
- //---------------------------------------------------------------------------
- void PackStr(AnsiString &Str)
- {
- // Following will free unnecessary bytes
- Str = Str.c_str();
- }
- //---------------------------------------------------------------------------
- AnsiString MakeValidFileName(AnsiString FileName)
- {
- AnsiString IllegalChars = ";,=+<>|\"[] \\/?*";
- for (int Index = 0; Index < IllegalChars.Length(); Index++)
- {
- FileName = ReplaceChar(FileName, IllegalChars[Index+1], '-');
- }
- return FileName;
- }
- //---------------------------------------------------------------------------
- AnsiString RootKeyToStr(HKEY RootKey)
- {
- if (RootKey == HKEY_USERS) return "HKEY_USERS";
- else
- if (RootKey == HKEY_LOCAL_MACHINE) return "HKEY_LOCAL_MACHINE";
- else
- if (RootKey == HKEY_CURRENT_USER) return "HKEY_CURRENT_USER";
- else
- if (RootKey == HKEY_CLASSES_ROOT) return "HKEY_CLASSES_ROOT";
- else
- if (RootKey == HKEY_CURRENT_CONFIG) return "HKEY_CURRENT_CONFIG";
- else
- if (RootKey == HKEY_DYN_DATA) return "HKEY_DYN_DATA";
- else
- { Abort(); return ""; };
- }
- //---------------------------------------------------------------------------
- AnsiString BooleanToEngStr(bool B)
- {
- return B ? "Yes" : "No";
- }
- //---------------------------------------------------------------------------
- AnsiString BooleanToStr(bool B)
- {
- return B ? LoadStr(YES_STR) : LoadStr(NO_STR);
- }
- //---------------------------------------------------------------------------
- AnsiString CutToChar(AnsiString &Str, Char Ch, bool Trim)
- {
- Integer P = Str.Pos(Ch);
- AnsiString Result;
- if (P)
- {
- Result = Str.SubString(1, P-1);
- Str.Delete(1, P);
- }
- else
- {
- Result = Str;
- Str = "";
- }
- if (Trim)
- {
- Result = Result.TrimRight();
- Str = Str.TrimLeft();
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- AnsiString CutToChars(AnsiString & Str, AnsiString Chs, bool Trim)
- {
- int P;
- for (P = 1; P <= Str.Length(); P++)
- {
- if (IsDelimiter(Chs, Str, P))
- {
- break;
- }
- }
- AnsiString Result;
- if (P <= Str.Length())
- {
- Result = Str.SubString(1, P-1);
- Str.Delete(1, P);
- }
- else
- {
- Result = Str;
- Str = "";
- }
- if (Trim)
- {
- Result = Result.TrimRight();
- Str = Str.TrimLeft();
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- AnsiString DelimitStr(AnsiString Str, AnsiString Chars)
- {
- for (int i = 1; i <= Str.Length(); i++)
- {
- if (Str.IsDelimiter(Chars, i))
- {
- Str.Insert("\\", i);
- i++;
- }
- }
- return Str;
- }
- //---------------------------------------------------------------------------
- AnsiString ShellDelimitStr(AnsiString Str, char Quote)
- {
- AnsiString Chars = "$\\";
- if (Quote == '"')
- {
- Chars += "`\"";
- }
- return DelimitStr(Str, Chars);
- }
- //---------------------------------------------------------------------------
- AnsiString ExceptionLogString(Exception *E)
- {
- assert(E);
- if (E->InheritsFrom(__classid(Exception)))
- {
- AnsiString Msg;
- Msg = FORMAT("(%s) %s", (E->ClassName(), E->Message));
- if (E->InheritsFrom(__classid(ExtException)))
- {
- TStrings * MoreMessages = ((ExtException*)E)->MoreMessages;
- if (MoreMessages)
- {
- Msg += "\n" +
- StringReplace(MoreMessages->Text, "\r", "", TReplaceFlags() << rfReplaceAll);
- }
- }
- return Msg;
- }
- else
- {
- char Buffer[1024];
- ExceptionErrorMessage(ExceptObject(), ExceptAddr(), Buffer, sizeof(Buffer));
- return AnsiString(Buffer);
- }
- }
- //---------------------------------------------------------------------------
- bool IsDots(const AnsiString Str)
- {
- char * str = Str.c_str();
- return (str[strspn(str, ".")] == '\0');
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall SystemTemporaryDirectory()
- {
- AnsiString TempDir;
- TempDir.SetLength(MAX_PATH);
- TempDir.SetLength(GetTempPath(MAX_PATH, TempDir.c_str()));
- return TempDir;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall StripPathQuotes(const AnsiString Path)
- {
- if ((Path.Length() >= 2) &&
- (Path[1] == '\"') && (Path[Path.Length()] == '\"'))
- {
- return Path.SubString(2, Path.Length() - 2);
- }
- else
- {
- return Path;
- }
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall AddPathQuotes(AnsiString Path)
- {
- Path = StripPathQuotes(Path);
- if (Path.Pos(" "))
- {
- Path = "\"" + Path + "\"";
- }
- return Path;
- }
- //---------------------------------------------------------------------------
- void __fastcall SplitCommand(AnsiString Command, AnsiString &Program,
- AnsiString & Params, AnsiString & Dir)
- {
- Command = Command.Trim();
- Params = "";
- Dir = "";
- if (!Command.IsEmpty() && (Command[1] == '\"'))
- {
- Command.Delete(1, 1);
- int P = Command.Pos('"');
- if (P)
- {
- Program = Command.SubString(1, P-1).Trim();
- Params = Command.SubString(P + 1, Command.Length() - P).Trim();
- }
- else
- {
- throw Exception(FMTLOAD(INVALID_SHELL_COMMAND, ("\"" + Command)));
- }
- }
- else
- {
- int P = Command.Pos(" ");
- if (P)
- {
- Program = Command.SubString(1, P).Trim();
- Params = Command.SubString(P + 1, Command.Length() - P).Trim();
- }
- else
- {
- Program = Command;
- }
- }
- int B = Program.LastDelimiter("\\/");
- if (B)
- {
- Dir = Program.SubString(1, B).Trim();
- }
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall ExtractProgram(AnsiString Command)
- {
- AnsiString Program;
- AnsiString Params;
- AnsiString Dir;
- SplitCommand(Command, Program, Params, Dir);
- return Program;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall FormatCommand(AnsiString Program, AnsiString Params)
- {
- Program = Program.Trim();
- Params = Params.Trim();
- if (!Params.IsEmpty()) Params = " " + Params;
- if (Program.Pos(" ")) Program = "\"" + Program + "\"";
- return Program + Params;
- }
- //---------------------------------------------------------------------------
- const char ShellCommandFileNamePattern[] = "!.!";
- //---------------------------------------------------------------------------
- void __fastcall ReformatFileNameCommand(AnsiString & Command)
- {
- AnsiString Program, Params, Dir;
- SplitCommand(Command, Program, Params, Dir);
- if (Params.Pos(ShellCommandFileNamePattern) == 0)
- {
- Params = Params + (Params.IsEmpty() ? "" : " ") + ShellCommandFileNamePattern;
- }
- Command = FormatCommand(Program, Params);
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall ExpandFileNameCommand(const AnsiString Command,
- const AnsiString FileName)
- {
- return AnsiReplaceStr(Command, ShellCommandFileNamePattern,
- AddPathQuotes(FileName));
- }
- //---------------------------------------------------------------------------
- bool __fastcall IsDisplayableStr(const AnsiString Str)
- {
- bool Displayable = true;
- int Index = 1;
- while ((Index <= Str.Length()) && Displayable)
- {
- if (Str[Index] < '\32')
- {
- Displayable = false;
- }
- Index++;
- }
- return Displayable;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall CharToHex(char Ch)
- {
- return IntToHex((unsigned char)Ch, 2);
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall StrToHex(const AnsiString Str)
- {
- AnsiString Result;
- for (int i = 1; i <= Str.Length(); i++)
- {
- Result += CharToHex(Str[i]);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall HexToStr(const AnsiString Hex)
- {
- static AnsiString Digits = "0123456789ABCDEF";
- AnsiString Result;
- int L, P1, P2;
- L = Hex.Length();
- if (L % 2 == 0)
- {
- for (int i = 1; i <= Hex.Length(); i += 2)
- {
- P1 = Digits.Pos(Hex[i]);
- P2 = Digits.Pos(Hex[i + 1]);
- if (P1 <= 0 || P2 <= 0)
- {
- break;
- }
- else
- {
- Result += static_cast<char>((P1 - 1) * 16 + P2 - 1);
- }
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- unsigned int __fastcall HexToInt(const AnsiString Hex, int MinChars)
- {
- static AnsiString Digits = "0123456789ABCDEF";
- int Result = 0;
- int I = 1;
- while (I <= Hex.Length())
- {
- int A = Digits.Pos(Hex[I]);
- if (A <= 0)
- {
- if ((MinChars < 0) || (I <= MinChars))
- {
- Result = 0;
- }
- break;
- }
- Result = (Result * 16) + (A - 1);
- I++;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall FileSearchRec(const AnsiString FileName, TSearchRec & Rec)
- {
- int FindAttrs = faReadOnly | faHidden | faSysFile | faDirectory | faArchive;
- bool Result = (FindFirst(FileName, FindAttrs, Rec) == 0);
- if (Result)
- {
- FindClose(Rec);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall ProcessLocalDirectory(AnsiString DirName,
- TProcessLocalFileEvent CallBackFunc, void * Param,
- int FindAttrs)
- {
- assert(CallBackFunc);
- if (FindAttrs < 0)
- {
- FindAttrs = faReadOnly | faHidden | faSysFile | faDirectory | faArchive;
- }
- TSearchRec SearchRec;
- DirName = IncludeTrailingBackslash(DirName);
- if (FindFirst(DirName + "*.*", FindAttrs, SearchRec) == 0)
- {
- try
- {
- do
- {
- if ((SearchRec.Name != ".") && (SearchRec.Name != ".."))
- {
- CallBackFunc(DirName + SearchRec.Name, SearchRec, Param);
- }
- } while (FindNext(SearchRec) == 0);
- }
- __finally
- {
- FindClose(SearchRec);
- }
- }
- }
- //---------------------------------------------------------------------------
- struct TDateTimeParams
- {
- TDateTime UnixEpoch;
- double BaseDifference;
- long BaseDifferenceSec;
- double CurrentDaylightDifference;
- long CurrentDaylightDifferenceSec;
- double CurrentDifference;
- long CurrentDifferenceSec;
- double StandardDifference;
- long StandardDifferenceSec;
- double DaylightDifference;
- long DaylightDifferenceSec;
- SYSTEMTIME StandardDate;
- SYSTEMTIME DaylightDate;
- };
- static bool DateTimeParamsInitialized = false;
- static TDateTimeParams DateTimeParams;
- static TCriticalSection DateTimeParamsSection;
- //---------------------------------------------------------------------------
- static TDateTimeParams * __fastcall GetDateTimeParams()
- {
- if (!DateTimeParamsInitialized)
- {
- TGuard Guard(&DateTimeParamsSection);
- if (!DateTimeParamsInitialized)
- {
- TIME_ZONE_INFORMATION TZI;
- unsigned long GTZI;
- GTZI = GetTimeZoneInformation(&TZI);
- switch (GTZI)
- {
- case TIME_ZONE_ID_UNKNOWN:
- DateTimeParams.CurrentDifferenceSec = 0;
- DateTimeParams.CurrentDaylightDifferenceSec = 0;
- break;
- case TIME_ZONE_ID_STANDARD:
- DateTimeParams.CurrentDaylightDifferenceSec = TZI.StandardBias;
- break;
- case TIME_ZONE_ID_DAYLIGHT:
- DateTimeParams.CurrentDaylightDifferenceSec = TZI.DaylightBias;
- break;
- case TIME_ZONE_ID_INVALID:
- default:
- throw Exception(TIMEZONE_ERROR);
- }
- // Is it same as SysUtils::UnixDateDelta = 25569 ??
- DateTimeParams.UnixEpoch = EncodeDate(1970, 1, 1);
- DateTimeParams.BaseDifferenceSec = TZI.Bias;
- DateTimeParams.BaseDifference = double(TZI.Bias) / 1440;
- DateTimeParams.BaseDifferenceSec *= 60;
- DateTimeParams.CurrentDifferenceSec = TZI.Bias +
- DateTimeParams.CurrentDaylightDifferenceSec;
- DateTimeParams.CurrentDifference =
- double(DateTimeParams.CurrentDifferenceSec) / 1440;
- DateTimeParams.CurrentDifferenceSec *= 60;
- DateTimeParams.CurrentDaylightDifference =
- double(DateTimeParams.CurrentDaylightDifferenceSec) / 1440;
- DateTimeParams.CurrentDaylightDifferenceSec *= 60;
- DateTimeParams.DaylightDifferenceSec = TZI.DaylightBias * 60;
- DateTimeParams.DaylightDifference = double(TZI.DaylightBias) / 1440;
- DateTimeParams.StandardDifferenceSec = TZI.StandardBias * 60;
- DateTimeParams.StandardDifference = double(TZI.StandardBias) / 1440;
- DateTimeParams.StandardDate = TZI.StandardDate;
- DateTimeParams.DaylightDate = TZI.DaylightDate;
- DateTimeParamsInitialized = true;
- }
- }
- return &DateTimeParams;
- }
- //---------------------------------------------------------------------------
- static void __fastcall EncodeDSTMargin(const SYSTEMTIME & Date, unsigned short Year,
- TDateTime & Result)
- {
- if (Date.wYear == 0)
- {
- TDateTime Temp = EncodeDate(Year, Date.wMonth, 1);
- Result = Temp + ((Date.wDayOfWeek - DayOfWeek(Temp) + 8) % 7) +
- (7 * (Date.wDay - 1));
- if (Date.wDay == 5)
- {
- unsigned short Month = static_cast<unsigned short>(Date.wMonth + 1);
- if (Month > 12)
- {
- Month = static_cast<unsigned short>(Month - 12);
- Year++;
- }
- if (Result > EncodeDate(Year, Month, 1))
- {
- Result -= 7;
- }
- }
- Result += EncodeTime(Date.wHour, Date.wMinute, Date.wSecond,
- Date.wMilliseconds);
- }
- else
- {
- Result = EncodeDate(Year, Date.wMonth, Date.wDay) +
- EncodeTime(Date.wHour, Date.wMinute, Date.wSecond, Date.wMilliseconds);
- }
- }
- //---------------------------------------------------------------------------
- static bool __fastcall IsDateInDST(const TDateTime & DateTime)
- {
- struct TDSTCache
- {
- bool Filled;
- unsigned short Year;
- TDateTime StandardDate;
- TDateTime DaylightDate;
- };
- static TDSTCache DSTCache[10];
- static int DSTCacheCount = 0;
- static TCriticalSection Section;
- TDateTimeParams * Params = GetDateTimeParams();
- bool Result;
- if (Params->StandardDate.wMonth == 0)
- {
- Result = false;
- }
- else
- {
- unsigned short Year, Month, Day;
- DecodeDate(DateTime, Year, Month, Day);
- TDSTCache * CurrentCache = &DSTCache[0];
- int CacheIndex = 0;
- while ((CacheIndex < DSTCacheCount) && (CacheIndex < LENOF(DSTCache)) &&
- CurrentCache->Filled && (CurrentCache->Year != Year))
- {
- CacheIndex++;
- CurrentCache++;
- }
- if ((CacheIndex < DSTCacheCount) && (CacheIndex < LENOF(DSTCache)) &&
- CurrentCache->Filled)
- {
- assert(CurrentCache->Year == Year);
- Result = (DateTime >= CurrentCache->DaylightDate) &&
- (DateTime < CurrentCache->StandardDate);
- }
- else
- {
- TDSTCache NewCache;
- EncodeDSTMargin(Params->StandardDate, Year, NewCache.StandardDate);
- EncodeDSTMargin(Params->DaylightDate, Year, NewCache.DaylightDate);
- if (DSTCacheCount < LENOF(DSTCache))
- {
- TGuard Guard(&Section);
- if (DSTCacheCount < LENOF(DSTCache))
- {
- NewCache.Year = Year;
- DSTCache[DSTCacheCount] = NewCache;
- DSTCache[DSTCacheCount].Filled = true;
- DSTCacheCount++;
- }
- }
- Result = (DateTime >= NewCache.DaylightDate) &&
- (DateTime < NewCache.StandardDate);
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- TDateTime __fastcall UnixToDateTime(__int64 TimeStamp, bool ConsiderDST)
- {
- TDateTimeParams * Params = GetDateTimeParams();
- TDateTime Result;
- Result = Params->UnixEpoch + (double(TimeStamp) / 86400) - Params->CurrentDifference;
- if (ConsiderDST)
- {
- Result -= (IsDateInDST(Result) ?
- Params->DaylightDifference : Params->StandardDifference);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- inline __int64 __fastcall Round(double Number)
- {
- double Floor = floor(Number);
- double Ceil = ceil(Number);
- return ((Number - Floor) > (Ceil - Number)) ? Ceil : Floor;
- }
- //---------------------------------------------------------------------------
- #define TIME_POSIX_TO_WIN(t, ft) (*(LONGLONG*)&(ft) = \
- ((LONGLONG) (t) + (LONGLONG) 11644473600) * (LONGLONG) 10000000)
- #define TIME_WIN_TO_POSIX(ft, t) ((t) = (__int64) \
- ((*(LONGLONG*)&(ft)) / (LONGLONG) 10000000 - (LONGLONG) 11644473600))
- //---------------------------------------------------------------------------
- FILETIME __fastcall DateTimeToFileTime(const TDateTime DateTime,
- bool /*ConsiderDST*/)
- {
- TDateTimeParams * Params = GetDateTimeParams();
- __int64 UnixTimeStamp;
- FILETIME Result;
- UnixTimeStamp = Round(double(DateTime - Params->UnixEpoch) * 86400) +
- Params->CurrentDifferenceSec;
- TIME_POSIX_TO_WIN(UnixTimeStamp, Result);
- return Result;
- }
- //---------------------------------------------------------------------------
- __int64 __fastcall ConvertTimestampToUnix(const FILETIME & FileTime,
- bool ConsiderDST)
- {
- __int64 Result;
- TIME_WIN_TO_POSIX(FileTime, Result);
- if (ConsiderDST)
- {
- FILETIME LocalFileTime;
- SYSTEMTIME SystemTime;
- TDateTime DateTime;
- FileTimeToLocalFileTime(&FileTime, &LocalFileTime);
- FileTimeToSystemTime(&LocalFileTime, &SystemTime);
- DateTime = SystemTimeToDateTime(SystemTime);
- TDateTimeParams * Params = GetDateTimeParams();
- Result += (IsDateInDST(DateTime) ?
- Params->DaylightDifferenceSec : Params->StandardDifferenceSec);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- TDateTime __fastcall AdjustDateTimeFromUnix(TDateTime DateTime, bool ConsiderDST)
- {
- TDateTimeParams * Params = GetDateTimeParams();
- DateTime = DateTime - Params->CurrentDaylightDifference;
- bool IsInDST = IsDateInDST(DateTime);
- // shift always, even with ConsiderDST == false
- DateTime = DateTime - (!IsInDST ? Params->DaylightDifference :
- Params->StandardDifference);
- if (ConsiderDST)
- {
- if (IsInDST)
- {
- //DateTime = DateTime - Params->DaylightDifference;
- }
- else
- {
- DateTime = DateTime + Params->DaylightDifference;
- }
- }
- return DateTime;
- }
- //---------------------------------------------------------------------------
- __inline static bool __fastcall UnifySignificance(unsigned short & V1,
- unsigned short & V2)
- {
- bool Result = (V1 == 0) || (V2 == 0);
- if (Result)
- {
- V1 = 0;
- V2 = 0;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall UnifyDateTimePrecision(TDateTime & DateTime1, TDateTime & DateTime2)
- {
- unsigned short Y1, M1, D1, H1, N1, S1, MS1;
- unsigned short Y2, M2, D2, H2, N2, S2, MS2;
- bool Changed;
- if (DateTime1 != DateTime2)
- {
- DateTime1.DecodeDate(&Y1, &M1, &D1);
- DateTime1.DecodeTime(&H1, &N1, &S1, &MS1);
- DateTime2.DecodeDate(&Y2, &M2, &D2);
- DateTime2.DecodeTime(&H2, &N2, &S2, &MS2);
- Changed = UnifySignificance(MS1, MS2);
- if (Changed && UnifySignificance(S1, S2) && UnifySignificance(N1, N2) &&
- UnifySignificance(H1, H2) && UnifySignificance(D1, D2) &&
- UnifySignificance(M1, M2))
- {
- UnifySignificance(Y1, Y2);
- }
- if (Changed)
- {
- DateTime1 = EncodeDate(Y1, M1, D1) + EncodeTime(H1, N1, S1, MS1);
- DateTime2 = EncodeDate(Y2, M2, D2) + EncodeTime(H2, N2, S2, MS2);
- }
- }
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall FixedLenDateTimeFormat(const AnsiString & Format)
- {
- AnsiString Result = Format;
- bool AsIs = false;
- int Index = 1;
- while (Index <= Result.Length())
- {
- char F = Result[Index];
- if ((F == '\'') || (F == '\"'))
- {
- AsIs = !AsIs;
- Index++;
- }
- else if (!AsIs && ((F == 'a') || (F == 'A')))
- {
- if (Result.SubString(Index, 5).LowerCase() == "am/pm")
- {
- Index += 5;
- }
- else if (Result.SubString(Index, 3).LowerCase() == "a/p")
- {
- Index += 3;
- }
- else if (Result.SubString(Index, 4).LowerCase() == "ampm")
- {
- Index += 4;
- }
- else
- {
- Index++;
- }
- }
- else
- {
- if (!AsIs && (strchr("dDeEmMhHnNsS", F) != NULL) &&
- ((Index == Result.Length()) || (Result[Index + 1] != F)))
- {
- Result.Insert(F, Index);
- }
-
- while ((Index <= Result.Length()) && (F == Result[Index]))
- {
- Index++;
- }
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- int __fastcall CompareFileTime(TDateTime T1, TDateTime T2)
- {
- // "FAT" time precision
- // 1 ms more solves the rounding issues (see also CustomDirView.pas)
- static TDateTime Second(0, 0, 1, 1);
- int Result;
- if (T1 == T2)
- {
- // just optimalisation
- Result = 0;
- }
- else if ((T1 < T2) && (T2 - T1 > Second))
- {
- Result = -1;
- }
- else if ((T1 > T2) && (T1 - T2 > Second))
- {
- Result = 1;
- }
- else
- {
- Result = 0;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall RecursiveDeleteFile(const AnsiString FileName, bool ToRecycleBin)
- {
- SHFILEOPSTRUCT Data;
- memset(&Data, 0, sizeof(Data));
- Data.hwnd = NULL;
- Data.wFunc = FO_DELETE;
- AnsiString FileList(FileName);
- FileList.SetLength(FileList.Length() + 2);
- FileList[FileList.Length() - 1] = '\0';
- FileList[FileList.Length()] = '\0';
- Data.pFrom = FileList.c_str();
- Data.pTo = "";
- Data.fFlags = FOF_NOCONFIRMATION | FOF_RENAMEONCOLLISION | FOF_NOCONFIRMMKDIR |
- FOF_NOERRORUI | FOF_SILENT;
- if (ToRecycleBin)
- {
- Data.fFlags |= FOF_ALLOWUNDO;
- }
- int Result = SHFileOperation(&Data);
- return (Result == 0);
- }
- //---------------------------------------------------------------------------
- int __fastcall CancelAnswer(int Answers)
- {
- int Result;
- if ((Answers & qaCancel) != 0)
- {
- Result = qaCancel;
- }
- else if ((Answers & qaNo) != 0)
- {
- Result = qaNo;
- }
- else if ((Answers & qaAbort) != 0)
- {
- Result = qaAbort;
- }
- else if ((Answers & qaOK) != 0)
- {
- Result = qaOK;
- }
- else
- {
- assert(false);
- Result = qaCancel;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- int __fastcall AbortAnswer(int Answers)
- {
- int Result;
- if (FLAGSET(Answers, qaAbort))
- {
- Result = qaAbort;
- }
- else
- {
- Result = CancelAnswer(Answers);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- TPasLibModule * __fastcall FindModule(void * Instance)
- {
- TPasLibModule * CurModule;
- CurModule = reinterpret_cast<TPasLibModule*>(LibModuleList);
- while (CurModule)
- {
- if (CurModule->Instance == Instance)
- {
- break;
- }
- else
- {
- CurModule = CurModule->Next;
- }
- }
- return CurModule;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall LoadStr(int Ident, unsigned int MaxLength)
- {
- TPasLibModule * MainModule = FindModule(HInstance);
- assert(MainModule != NULL);
- AnsiString Result;
- Result.SetLength(MaxLength);
- int Length = LoadString(MainModule->ResInstance, Ident, Result.c_str(), MaxLength);
- Result.SetLength(Length);
- return Result;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall LoadStrPart(int Ident, int Part)
- {
- AnsiString Result;
- AnsiString Str = LoadStr(Ident);
- while (Part > 0)
- {
- Result = CutToChar(Str, '|', false);
- Part--;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall DecodeUrlChars(AnsiString S)
- {
- int i = 1;
- while (i <= S.Length())
- {
- switch (S[i])
- {
- case '+':
- S[i] = ' ';
- break;
- case '%':
- if (i <= S.Length() - 2)
- {
- S[i] = HexToStr(S.SubString(i + 1, 2))[1];
- S.Delete(i + 1, 2);
- }
- break;
- }
- i++;
- }
- return S;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall EncodeUrlChars(AnsiString S, AnsiString Ignore)
- {
- AnsiString Encode = "/ ";
- int i = 1;
- while (i <= S.Length())
- {
- if ((Encode.Pos(S[i]) > 0) &&
- (Ignore.Pos(S[i]) == 0))
- {
- AnsiString H = CharToHex(S[i]);
- S.Insert(H, i + 1);
- S[i] = '%';
- i += H.Length();
- }
- i++;
- }
- return S;
- }
- //---------------------------------------------------------------------------
- void __fastcall OemToAnsi(AnsiString & Str)
- {
- if (!Str.IsEmpty())
- {
- Str.Unique();
- OemToChar(Str.c_str(), Str.c_str());
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall AnsiToOem(AnsiString & Str)
- {
- if (!Str.IsEmpty())
- {
- Str.Unique();
- CharToOem(Str.c_str(), Str.c_str());
- }
- }
|