123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899 |
- //---------------------------------------------------------------------------
- #define NO_WIN32_LEAN_AND_MEAN
- #include <vcl.h>
- #pragma hdrstop
- #include "Common.h"
- #include "Exceptions.h"
- #include "TextsCore.h"
- #include "Interface.h"
- #include <StrUtils.hpp>
- #include <DateUtils.hpp>
- #include <math.h>
- #include <shlobj.h>
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- //---------------------------------------------------------------------------
- // 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 wchar_t EngShortMonthNames[12][4] =
- {L"Jan", L"Feb", L"Mar", L"Apr", L"May", L"Jun",
- L"Jul", L"Aug", L"Sep", L"Oct", L"Nov", L"Dec"};
- const char Bom[3] = "\xEF\xBB\xBF";
- const wchar_t TokenPrefix = L'%';
- const wchar_t NoReplacement = wchar_t(false);
- const wchar_t TokenReplacement = wchar_t(true);
- const UnicodeString LocalInvalidChars = L"/\\:*?\"<>|";
- //---------------------------------------------------------------------------
- UnicodeString ReplaceChar(UnicodeString Str, wchar_t A, wchar_t B)
- {
- for (Integer Index = 0; Index < Str.Length(); Index++)
- if (Str[Index+1] == A) Str[Index+1] = B;
- return Str;
- }
- //---------------------------------------------------------------------------
- UnicodeString DeleteChar(UnicodeString Str, wchar_t C)
- {
- int P;
- while ((P = Str.Pos(C)) > 0)
- {
- Str.Delete(P, 1);
- }
- return Str;
- }
- //---------------------------------------------------------------------------
- void PackStr(UnicodeString &Str)
- {
- // Following will free unnecessary bytes
- Str = Str.c_str();
- }
- //---------------------------------------------------------------------------
- void PackStr(RawByteString &Str)
- {
- // Following will free unnecessary bytes
- Str = Str.c_str();
- }
- //---------------------------------------------------------------------------
- void __fastcall Shred(UnicodeString & Str)
- {
- if (!Str.IsEmpty())
- {
- Str.Unique();
- memset(Str.c_str(), 0, Str.Length() * sizeof(*Str.c_str()));
- Str = L"";
- }
- }
- //---------------------------------------------------------------------------
- UnicodeString MakeValidFileName(UnicodeString FileName)
- {
- UnicodeString IllegalChars = L":;,=+<>|\"[] \\/?*";
- for (int Index = 0; Index < IllegalChars.Length(); Index++)
- {
- FileName = ReplaceChar(FileName, IllegalChars[Index+1], L'-');
- }
- return FileName;
- }
- //---------------------------------------------------------------------------
- UnicodeString RootKeyToStr(HKEY RootKey)
- {
- if (RootKey == HKEY_USERS) return L"HKEY_USERS";
- else
- if (RootKey == HKEY_LOCAL_MACHINE) return L"HKEY_LOCAL_MACHINE";
- else
- if (RootKey == HKEY_CURRENT_USER) return L"HKEY_CURRENT_USER";
- else
- if (RootKey == HKEY_CLASSES_ROOT) return L"HKEY_CLASSES_ROOT";
- else
- if (RootKey == HKEY_CURRENT_CONFIG) return L"HKEY_CURRENT_CONFIG";
- else
- if (RootKey == HKEY_DYN_DATA) return L"HKEY_DYN_DATA";
- else
- { Abort(); return L""; };
- }
- //---------------------------------------------------------------------------
- UnicodeString BooleanToEngStr(bool B)
- {
- if (B)
- {
- return L"Yes";
- }
- else
- {
- return L"No";
- }
- }
- //---------------------------------------------------------------------------
- UnicodeString BooleanToStr(bool B)
- {
- if (B)
- {
- return LoadStr(YES_STR);
- }
- else
- {
- return LoadStr(NO_STR);
- }
- }
- //---------------------------------------------------------------------------
- UnicodeString DefaultStr(const UnicodeString & Str, const UnicodeString & Default)
- {
- if (!Str.IsEmpty())
- {
- return Str;
- }
- else
- {
- return Default;
- }
- }
- //---------------------------------------------------------------------------
- UnicodeString CutToChar(UnicodeString &Str, wchar_t Ch, bool Trim)
- {
- Integer P = Str.Pos(Ch);
- UnicodeString Result;
- if (P)
- {
- Result = Str.SubString(1, P-1);
- Str.Delete(1, P);
- }
- else
- {
- Result = Str;
- Str = L"";
- }
- if (Trim)
- {
- Result = Result.TrimRight();
- Str = Str.TrimLeft();
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- UnicodeString CopyToChars(const UnicodeString & Str, int & From, UnicodeString Chs, bool Trim,
- wchar_t * Delimiter, bool DoubleDelimiterEscapes)
- {
- UnicodeString Result;
- int P;
- for (P = From; P <= Str.Length(); P++)
- {
- if (IsDelimiter(Chs, Str, P))
- {
- if (DoubleDelimiterEscapes &&
- (P < Str.Length()) &&
- IsDelimiter(Chs, Str, P + 1))
- {
- Result += Str[P];
- P++;
- }
- else
- {
- break;
- }
- }
- else
- {
- Result += Str[P];
- }
- }
- if (P <= Str.Length())
- {
- if (Delimiter != NULL)
- {
- *Delimiter = Str[P];
- }
- }
- else
- {
- if (Delimiter != NULL)
- {
- *Delimiter = L'\0';
- }
- }
- // even if we reached the end, return index, as if there were the delimiter,
- // so caller can easily find index of the end of the piece by subtracting
- // 2 from From (as long as he did not asked for trimming)
- From = P+1;
- if (Trim)
- {
- Result = Result.TrimRight();
- while ((From <= Str.Length()) && (Str[From] == L' '))
- {
- From++;
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- UnicodeString DelimitStr(UnicodeString Str, UnicodeString Chars)
- {
- for (int i = 1; i <= Str.Length(); i++)
- {
- if (Str.IsDelimiter(Chars, i))
- {
- Str.Insert(L"\\", i);
- i++;
- }
- }
- return Str;
- }
- //---------------------------------------------------------------------------
- UnicodeString ShellDelimitStr(UnicodeString Str, wchar_t Quote)
- {
- UnicodeString Chars = L"$\\";
- if (Quote == L'"')
- {
- Chars += L"`\"";
- }
- return DelimitStr(Str, Chars);
- }
- //---------------------------------------------------------------------------
- UnicodeString ExceptionLogString(Exception *E)
- {
- assert(E);
- if (E->InheritsFrom(__classid(Exception)))
- {
- UnicodeString Msg;
- Msg = FORMAT(L"(%s) %s", (E->ClassName(), E->Message));
- if (E->InheritsFrom(__classid(ExtException)))
- {
- TStrings * MoreMessages = ((ExtException*)E)->MoreMessages;
- if (MoreMessages)
- {
- Msg += L"\n" +
- StringReplace(MoreMessages->Text, L"\r", L"", TReplaceFlags() << rfReplaceAll);
- }
- }
- return Msg;
- }
- else
- {
- wchar_t Buffer[1024];
- ExceptionErrorMessage(ExceptObject(), ExceptAddr(), Buffer, LENOF(Buffer));
- return UnicodeString(Buffer);
- }
- }
- //---------------------------------------------------------------------------
- bool IsNumber(const UnicodeString Str)
- {
- int Value;
- return TryStrToInt(Str, Value);
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall SystemTemporaryDirectory()
- {
- UnicodeString TempDir;
- TempDir.SetLength(MAX_PATH);
- TempDir.SetLength(GetTempPath(MAX_PATH, TempDir.c_str()));
- return TempDir;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall GetShellFolderPath(int CSIdl)
- {
- UnicodeString Result;
- wchar_t Path[2 * MAX_PATH + 10] = L"\0";
- if (SUCCEEDED(SHGetFolderPath(NULL, CSIdl, NULL, SHGFP_TYPE_CURRENT, Path)))
- {
- Result = Path;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall StripPathQuotes(const UnicodeString Path)
- {
- if ((Path.Length() >= 2) &&
- (Path[1] == L'\"') && (Path[Path.Length()] == L'\"'))
- {
- return Path.SubString(2, Path.Length() - 2);
- }
- else
- {
- return Path;
- }
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall AddPathQuotes(UnicodeString Path)
- {
- Path = StripPathQuotes(Path);
- if (Path.Pos(L" "))
- {
- Path = L"\"" + Path + L"\"";
- }
- return Path;
- }
- //---------------------------------------------------------------------------
- static wchar_t * __fastcall ReplaceChar(
- UnicodeString & FileName, wchar_t * InvalidChar, wchar_t InvalidCharsReplacement)
- {
- int Index = InvalidChar - FileName.c_str() + 1;
- if (InvalidCharsReplacement == TokenReplacement)
- {
- // currently we do not support unicode chars replacement
- if (FileName[Index] > 0xFF)
- {
- EXCEPTION;
- }
- FileName.Insert(ByteToHex(static_cast<unsigned char>(FileName[Index])), Index + 1);
- FileName[Index] = TokenPrefix;
- InvalidChar = FileName.c_str() + Index + 2;
- }
- else
- {
- FileName[Index] = InvalidCharsReplacement;
- InvalidChar = FileName.c_str() + Index;
- }
- return InvalidChar;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall ValidLocalFileName(UnicodeString FileName)
- {
- return ValidLocalFileName(FileName, L'_', L"", LocalInvalidChars);
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall ValidLocalFileName(
- UnicodeString FileName, wchar_t InvalidCharsReplacement,
- const UnicodeString & TokenizibleChars, const UnicodeString & LocalInvalidChars)
- {
- if (InvalidCharsReplacement != NoReplacement)
- {
- bool ATokenReplacement = (InvalidCharsReplacement == TokenReplacement);
- const wchar_t * Chars =
- (ATokenReplacement ? TokenizibleChars : LocalInvalidChars).c_str();
- wchar_t * InvalidChar = FileName.c_str();
- while ((InvalidChar = wcspbrk(InvalidChar, Chars)) != NULL)
- {
- int Pos = (InvalidChar - FileName.c_str() + 1);
- wchar_t Char;
- if (ATokenReplacement &&
- (*InvalidChar == TokenPrefix) &&
- (((FileName.Length() - Pos) <= 1) ||
- (((Char = static_cast<wchar_t>(HexToByte(FileName.SubString(Pos + 1, 2)))) == L'\0') ||
- (TokenizibleChars.Pos(Char) == 0))))
- {
- InvalidChar++;
- }
- else
- {
- InvalidChar = ReplaceChar(FileName, InvalidChar, InvalidCharsReplacement);
- }
- }
- // Windows trim trailing space or dot, hence we must encode it to preserve it
- if (!FileName.IsEmpty() &&
- ((FileName[FileName.Length()] == L' ') ||
- (FileName[FileName.Length()] == L'.')))
- {
- ReplaceChar(FileName, FileName.c_str() + FileName.Length() - 1, InvalidCharsReplacement);
- }
- if (IsReservedName(FileName))
- {
- int P = FileName.Pos(".");
- if (P == 0)
- {
- P = FileName.Length() + 1;
- }
- FileName.Insert(L"%00", P);
- }
- }
- return FileName;
- }
- //---------------------------------------------------------------------------
- void __fastcall SplitCommand(UnicodeString Command, UnicodeString &Program,
- UnicodeString & Params, UnicodeString & Dir)
- {
- Command = Command.Trim();
- Params = L"";
- Dir = L"";
- if (!Command.IsEmpty() && (Command[1] == L'\"'))
- {
- Command.Delete(1, 1);
- int P = Command.Pos(L'"');
- 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, (L"\"" + Command)));
- }
- }
- else
- {
- int P = Command.Pos(L" ");
- if (P)
- {
- Program = Command.SubString(1, P).Trim();
- Params = Command.SubString(P + 1, Command.Length() - P).Trim();
- }
- else
- {
- Program = Command;
- }
- }
- int B = Program.LastDelimiter(L"\\/");
- if (B)
- {
- Dir = Program.SubString(1, B).Trim();
- }
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall ExtractProgram(UnicodeString Command)
- {
- UnicodeString Program;
- UnicodeString Params;
- UnicodeString Dir;
- SplitCommand(Command, Program, Params, Dir);
- return Program;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall FormatCommand(UnicodeString Program, UnicodeString Params)
- {
- Program = Program.Trim();
- Params = Params.Trim();
- if (!Params.IsEmpty()) Params = L" " + Params;
- if (Program.Pos(L" ")) Program = L"\"" + Program + L"\"";
- return Program + Params;
- }
- //---------------------------------------------------------------------------
- const wchar_t ShellCommandFileNamePattern[] = L"!.!";
- //---------------------------------------------------------------------------
- void __fastcall ReformatFileNameCommand(UnicodeString & Command)
- {
- if (!Command.IsEmpty())
- {
- UnicodeString Program, Params, Dir;
- SplitCommand(Command, Program, Params, Dir);
- if (Params.Pos(ShellCommandFileNamePattern) == 0)
- {
- Params = Params + (Params.IsEmpty() ? L"" : L" ") + ShellCommandFileNamePattern;
- }
- Command = FormatCommand(Program, Params);
- }
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall ExpandFileNameCommand(const UnicodeString Command,
- const UnicodeString FileName)
- {
- return AnsiReplaceStr(Command, ShellCommandFileNamePattern,
- AddPathQuotes(FileName));
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall EscapePuttyCommandParam(UnicodeString Param)
- {
- bool Space = false;
- for (int i = 1; i <= Param.Length(); i++)
- {
- switch (Param[i])
- {
- case L'"':
- Param.Insert(L"\\", i);
- i++;
- break;
- case L' ':
- Space = true;
- break;
- case L'\\':
- int i2 = i;
- while ((i2 <= Param.Length()) && (Param[i2] == L'\\'))
- {
- i2++;
- }
- if ((i2 <= Param.Length()) && (Param[i2] == L'"'))
- {
- while (Param[i] == L'\\')
- {
- Param.Insert(L"\\", i);
- i += 2;
- }
- i--;
- }
- break;
- }
- }
- if (Space)
- {
- Param = L"\"" + Param + L'"';
- }
- return Param;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall ExpandEnvironmentVariables(const UnicodeString & Str)
- {
- UnicodeString Buf;
- unsigned int Size = 1024;
- Buf.SetLength(Size);
- Buf.Unique();
- unsigned int Len = ExpandEnvironmentStrings(Str.c_str(), Buf.c_str(), Size);
- if (Len > Size)
- {
- Buf.SetLength(Len);
- Buf.Unique();
- ExpandEnvironmentStrings(Str.c_str(), Buf.c_str(), Len);
- }
- PackStr(Buf);
- return Buf;
- }
- //---------------------------------------------------------------------------
- bool __fastcall CompareFileName(const UnicodeString & Path1, const UnicodeString & Path2)
- {
- UnicodeString ShortPath1 = ExtractShortPathName(Path1);
- UnicodeString ShortPath2 = ExtractShortPathName(Path2);
- bool Result;
- // ExtractShortPathName returns empty string if file does not exist
- if (ShortPath1.IsEmpty() || ShortPath2.IsEmpty())
- {
- Result = AnsiSameText(Path1, Path2);
- }
- else
- {
- Result = AnsiSameText(ShortPath1, ShortPath2);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall ComparePaths(const UnicodeString & Path1, const UnicodeString & Path2)
- {
- // TODO: ExpandUNCFileName
- return AnsiSameText(IncludeTrailingBackslash(Path1), IncludeTrailingBackslash(Path2));
- }
- //---------------------------------------------------------------------------
- bool __fastcall IsReservedName(UnicodeString FileName)
- {
- int P = FileName.Pos(L".");
- int Len = (P > 0) ? P - 1 : FileName.Length();
- if ((Len == 3) || (Len == 4))
- {
- if (P > 0)
- {
- FileName.SetLength(P - 1);
- }
- static UnicodeString Reserved[] = {
- L"CON", L"PRN", L"AUX", L"NUL",
- L"COM1", L"COM2", L"COM3", L"COM4", L"COM5", L"COM6", L"COM7", L"COM8", L"COM9",
- L"LPT1", L"LPT2", L"LPT3", L"LPT4", L"LPT5", L"LPT6", L"LPT7", L"LPT8", L"LPT9" };
- for (unsigned int Index = 0; Index < LENOF(Reserved); Index++)
- {
- if (SameText(FileName, Reserved[Index]))
- {
- return true;
- }
- }
- }
- return false;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall DisplayableStr(const RawByteString & Str)
- {
- bool Displayable = true;
- int Index = 1;
- while ((Index <= Str.Length()) && Displayable)
- {
- if (((Str[Index] < '\x20') || (static_cast<unsigned char>(Str[Index]) >= static_cast<unsigned char>('\x80'))) &&
- (Str[Index] != '\n') && (Str[Index] != '\r') && (Str[Index] != '\t') && (Str[Index] != '\b'))
- {
- Displayable = false;
- }
- Index++;
- }
- UnicodeString Result;
- if (Displayable)
- {
- Result = L"\"";
- for (int Index = 1; Index <= Str.Length(); Index++)
- {
- switch (Str[Index])
- {
- case '\n':
- Result += L"\\n";
- break;
- case '\r':
- Result += L"\\r";
- break;
- case '\t':
- Result += L"\\t";
- break;
- case '\b':
- Result += L"\\b";
- break;
- case '\\':
- Result += L"\\\\";
- break;
- case '"':
- Result += L"\\\"";
- break;
- default:
- Result += wchar_t(Str[Index]);
- break;
- }
- }
- Result += L"\"";
- }
- else
- {
- Result = L"0x" + BytesToHex(Str);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall ByteToHex(unsigned char B, bool UpperCase)
- {
- static wchar_t UpperDigits[] = L"0123456789ABCDEF";
- static wchar_t LowerDigits[] = L"0123456789abcdef";
- const wchar_t * Digits = (UpperCase ? UpperDigits : LowerDigits);
- UnicodeString Result;
- Result.SetLength(2);
- Result[1] = Digits[(B & 0xF0) >> 4];
- Result[2] = Digits[(B & 0x0F) >> 0];
- return Result;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall BytesToHex(const unsigned char * B, size_t Length, bool UpperCase, wchar_t Separator)
- {
- UnicodeString Result;
- for (size_t i = 0; i < Length; i++)
- {
- Result += ByteToHex(B[i], UpperCase);
- if ((Separator != L'\0') && (i < Length - 1))
- {
- Result += Separator;
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall BytesToHex(RawByteString Str, bool UpperCase, wchar_t Separator)
- {
- return BytesToHex(reinterpret_cast<const unsigned char *>(Str.c_str()), Str.Length(), UpperCase, Separator);
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall CharToHex(wchar_t Ch, bool UpperCase)
- {
- return BytesToHex(reinterpret_cast<const unsigned char *>(&Ch), sizeof(Ch), UpperCase);
- }
- //---------------------------------------------------------------------------
- RawByteString __fastcall HexToBytes(const UnicodeString Hex)
- {
- static UnicodeString Digits = L"0123456789ABCDEF";
- RawByteString Result;
- int L, P1, P2;
- L = Hex.Length();
- if (L % 2 == 0)
- {
- for (int i = 1; i <= Hex.Length(); i += 2)
- {
- P1 = Digits.Pos((wchar_t)toupper(Hex[i]));
- P2 = Digits.Pos((wchar_t)toupper(Hex[i + 1]));
- if (P1 <= 0 || P2 <= 0)
- {
- Result = L"";
- break;
- }
- else
- {
- Result += static_cast<char>((P1 - 1) * 16 + P2 - 1);
- }
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- unsigned char __fastcall HexToByte(const UnicodeString Hex)
- {
- static UnicodeString Digits = L"0123456789ABCDEF";
- assert(Hex.Length() == 2);
- int P1 = Digits.Pos((wchar_t)toupper(Hex[1]));
- int P2 = Digits.Pos((wchar_t)toupper(Hex[2]));
- return
- static_cast<unsigned char>(((P1 <= 0) || (P2 <= 0)) ? 0 : (((P1 - 1) << 4) + (P2 - 1)));
- }
- //---------------------------------------------------------------------------
- int __fastcall FindCheck(int Result)
- {
- if ((Result != ERROR_SUCCESS) &&
- (Result != ERROR_FILE_NOT_FOUND) &&
- (Result != ERROR_NO_MORE_FILES))
- {
- RaiseLastOSError(Result);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- int __fastcall FindFirstChecked(const UnicodeString & Path, int Attr, TSearchRec & F)
- {
- return FindCheck(FindFirst(Path, Attr, F));
- }
- //---------------------------------------------------------------------------
- // It can make sense to use FindNextChecked, even if unchecked FindFirst is used.
- // I.e. even if we do not care that FindFirst failed, if FindNext
- // failes after successfull FindFirst, it mean some terrible problem
- int __fastcall FindNextChecked(TSearchRec & F)
- {
- return FindCheck(FindNext(F));
- }
- //---------------------------------------------------------------------------
- bool __fastcall FileSearchRec(const UnicodeString 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(UnicodeString DirName,
- TProcessLocalFileEvent CallBackFunc, void * Param,
- int FindAttrs)
- {
- assert(CallBackFunc);
- if (FindAttrs < 0)
- {
- FindAttrs = faReadOnly | faHidden | faSysFile | faDirectory | faArchive;
- }
- TSearchRec SearchRec;
- DirName = IncludeTrailingBackslash(DirName);
- if (FindFirstChecked(DirName + L"*.*", FindAttrs, SearchRec) == 0)
- {
- try
- {
- do
- {
- if ((SearchRec.Name != L".") && (SearchRec.Name != L".."))
- {
- CallBackFunc(DirName + SearchRec.Name, SearchRec, Param);
- }
- } while (FindNextChecked(SearchRec) == 0);
- }
- __finally
- {
- FindClose(SearchRec);
- }
- }
- }
- //---------------------------------------------------------------------------
- TDateTime __fastcall EncodeDateVerbose(Word Year, Word Month, Word Day)
- {
- try
- {
- TDateTime DateTime = EncodeDate(Year, Month, Day);
- return DateTime;
- }
- catch (EConvertError & E)
- {
- throw EConvertError(FORMAT(L"%s [%d-%d-%d]", (E.Message, int(Year), int(Month), int(Day))));
- }
- }
- //---------------------------------------------------------------------------
- TDateTime __fastcall EncodeTimeVerbose(Word Hour, Word Min, Word Sec, Word MSec)
- {
- try
- {
- TDateTime DateTime = EncodeTime(Hour, Min, Sec, MSec);
- return DateTime;
- }
- catch (EConvertError & E)
- {
- throw EConvertError(FORMAT(L"%s [%d:%d:%d.%d]", (E.Message, int(Hour), int(Min), int(Sec), int(MSec))));
- }
- }
- //---------------------------------------------------------------------------
- struct TDateTimeParams
- {
- TDateTime UnixEpoch;
- double BaseDifference;
- long BaseDifferenceSec;
- // All Current* are actually global, not per-year
- double CurrentDaylightDifference;
- long CurrentDaylightDifferenceSec;
- double CurrentDifference;
- long CurrentDifferenceSec;
- double StandardDifference;
- long StandardDifferenceSec;
- double DaylightDifference;
- long DaylightDifferenceSec;
- SYSTEMTIME SystemStandardDate;
- SYSTEMTIME SystemDaylightDate;
- TDateTime StandardDate;
- TDateTime DaylightDate;
- bool SummerDST;
- // This is actually global, not per-year
- bool DaylightHack;
- };
- typedef std::map<int, TDateTimeParams> TYearlyDateTimeParams;
- static TYearlyDateTimeParams YearlyDateTimeParams;
- static std::auto_ptr<TCriticalSection> DateTimeParamsSection(new TCriticalSection());
- static void __fastcall EncodeDSTMargin(const SYSTEMTIME & Date, unsigned short Year,
- TDateTime & Result);
- //---------------------------------------------------------------------------
- static unsigned short __fastcall DecodeYear(const TDateTime & DateTime)
- {
- unsigned short Year, Month, Day;
- DecodeDate(DateTime, Year, Month, Day);
- return Year;
- }
- //---------------------------------------------------------------------------
- static const TDateTimeParams * __fastcall GetDateTimeParams(unsigned short Year)
- {
- TGuard Guard(DateTimeParamsSection.get());
- TDateTimeParams * Result;
- TYearlyDateTimeParams::iterator i = YearlyDateTimeParams.find(Year);
- if (i != YearlyDateTimeParams.end())
- {
- Result = &(*i).second;
- }
- else
- {
- // creates new entry as a side effect
- Result = &YearlyDateTimeParams[Year];
- TIME_ZONE_INFORMATION TZI;
- unsigned long GTZI;
- HINSTANCE Kernel32 = GetModuleHandle(kernel32);
- typedef BOOL WINAPI (* TGetTimeZoneInformationForYear)(USHORT wYear, PDYNAMIC_TIME_ZONE_INFORMATION pdtzi, LPTIME_ZONE_INFORMATION ptzi);
- TGetTimeZoneInformationForYear GetTimeZoneInformationForYear =
- (TGetTimeZoneInformationForYear)GetProcAddress(Kernel32, "GetTimeZoneInformationForYear");
- if ((Year == 0) || (GetTimeZoneInformationForYear == NULL))
- {
- GTZI = GetTimeZoneInformation(&TZI);
- }
- else
- {
- GetTimeZoneInformationForYear(Year, NULL, &TZI);
- GTZI = TIME_ZONE_ID_UNKNOWN;
- }
- switch (GTZI)
- {
- case TIME_ZONE_ID_UNKNOWN:
- Result->CurrentDaylightDifferenceSec = 0;
- break;
- case TIME_ZONE_ID_STANDARD:
- Result->CurrentDaylightDifferenceSec = TZI.StandardBias;
- break;
- case TIME_ZONE_ID_DAYLIGHT:
- Result->CurrentDaylightDifferenceSec = TZI.DaylightBias;
- break;
- case TIME_ZONE_ID_INVALID:
- default:
- throw Exception(TIMEZONE_ERROR);
- }
- Result->BaseDifferenceSec = TZI.Bias;
- Result->BaseDifference = double(TZI.Bias) / MinsPerDay;
- Result->BaseDifferenceSec *= SecsPerMin;
- Result->CurrentDifferenceSec = TZI.Bias +
- Result->CurrentDaylightDifferenceSec;
- Result->CurrentDifference =
- double(Result->CurrentDifferenceSec) / MinsPerDay;
- Result->CurrentDifferenceSec *= SecsPerMin;
- Result->CurrentDaylightDifference =
- double(Result->CurrentDaylightDifferenceSec) / MinsPerDay;
- Result->CurrentDaylightDifferenceSec *= SecsPerMin;
- Result->DaylightDifferenceSec = TZI.DaylightBias * SecsPerMin;
- Result->DaylightDifference = double(TZI.DaylightBias) / MinsPerDay;
- Result->StandardDifferenceSec = TZI.StandardBias * SecsPerMin;
- Result->StandardDifference = double(TZI.StandardBias) / MinsPerDay;
- Result->SystemStandardDate = TZI.StandardDate;
- Result->SystemDaylightDate = TZI.DaylightDate;
- unsigned short AYear = (Year != 0) ? Year : DecodeYear(Now());
- if (Result->SystemStandardDate.wMonth != 0)
- {
- EncodeDSTMargin(Result->SystemStandardDate, AYear, Result->StandardDate);
- }
- if (Result->SystemDaylightDate.wMonth != 0)
- {
- EncodeDSTMargin(Result->SystemDaylightDate, AYear, Result->DaylightDate);
- }
- Result->SummerDST = (Result->DaylightDate < Result->StandardDate);
- Result->DaylightHack = !IsWin7() || IsExactly2008R2();
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- static void __fastcall EncodeDSTMargin(const SYSTEMTIME & Date, unsigned short Year,
- TDateTime & Result)
- {
- if (Date.wYear == 0)
- {
- TDateTime Temp = EncodeDateVerbose(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 >= EncodeDateVerbose(Year, Month, 1))
- {
- Result -= 7;
- }
- }
- Result += EncodeTimeVerbose(Date.wHour, Date.wMinute, Date.wSecond,
- Date.wMilliseconds);
- }
- else
- {
- Result = EncodeDateVerbose(Year, Date.wMonth, Date.wDay) +
- EncodeTimeVerbose(Date.wHour, Date.wMinute, Date.wSecond, Date.wMilliseconds);
- }
- }
- //---------------------------------------------------------------------------
- static bool __fastcall IsDateInDST(const TDateTime & DateTime)
- {
- const TDateTimeParams * Params = GetDateTimeParams(DecodeYear(DateTime));
- bool Result;
- // On some systems it occurs that StandardDate is unset, while
- // DaylightDate is set. MSDN states that this is invalid and
- // should be treated as if there is no daylight saving.
- // So check both.
- if ((Params->SystemStandardDate.wMonth == 0) ||
- (Params->SystemDaylightDate.wMonth == 0))
- {
- Result = false;
- }
- else
- {
- if (Params->SummerDST)
- {
- Result =
- (DateTime >= Params->DaylightDate) &&
- (DateTime < Params->StandardDate);
- }
- else
- {
- Result =
- (DateTime < Params->StandardDate) ||
- (DateTime >= Params->DaylightDate);
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall UsesDaylightHack()
- {
- return GetDateTimeParams(0)->DaylightHack;
- }
- //---------------------------------------------------------------------------
- TDateTime __fastcall UnixToDateTime(__int64 TimeStamp, TDSTMode DSTMode)
- {
- assert(int(EncodeDateVerbose(1970, 1, 1)) == UnixDateDelta);
- TDateTime Result = UnixDateDelta + (double(TimeStamp) / SecsPerDay);
- const TDateTimeParams * Params = GetDateTimeParams(DecodeYear(Result));
- if (Params->DaylightHack)
- {
- if ((DSTMode == dstmWin) || (DSTMode == dstmUnix))
- {
- const TDateTimeParams * CurrentParams = GetDateTimeParams(0);
- Result -= CurrentParams->CurrentDifference;
- }
- else if (DSTMode == dstmKeep)
- {
- Result -= Params->BaseDifference;
- }
- }
- else
- {
- Result -= Params->BaseDifference;
- }
- if ((DSTMode == dstmUnix) || (DSTMode == dstmKeep))
- {
- Result -= (IsDateInDST(Result) ?
- Params->DaylightDifference : Params->StandardDifference);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- __int64 __fastcall Round(double Number)
- {
- double Floor = floor(Number);
- double Ceil = ceil(Number);
- return static_cast<__int64>(((Number - Floor) > (Ceil - Number)) ? Ceil : Floor);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TryRelativeStrToDateTime(UnicodeString S, TDateTime & DateTime)
- {
- S = S.Trim();
- int Index = 1;
- while ((Index <= S.Length()) && (S[Index] >= '0') && (S[Index] <= '9'))
- {
- Index++;
- }
- UnicodeString NumberStr = S.SubString(1, Index - 1);
- int Number;
- bool Result = TryStrToInt(NumberStr, Number);
- if (Result)
- {
- S.Delete(1, Index - 1);
- S = S.Trim().UpperCase();
- DateTime = Now();
- // These may not overlap with ParseSize (K, M and G)
- if (S == "S")
- {
- DateTime = IncSecond(DateTime, -Number);
- }
- else if (S == "N")
- {
- DateTime = IncMinute(DateTime, -Number);
- }
- else if (S == "H")
- {
- DateTime = IncHour(DateTime, -Number);
- }
- else if (S == "D")
- {
- DateTime = IncDay(DateTime, -Number);
- }
- else if (S == "Y")
- {
- DateTime = IncYear(DateTime, -Number);
- }
- else
- {
- Result = false;
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- static __int64 __fastcall DateTimeToUnix(const TDateTime DateTime)
- {
- const TDateTimeParams * CurrentParams = GetDateTimeParams(0);
- assert(int(EncodeDateVerbose(1970, 1, 1)) == UnixDateDelta);
- return Round(double(DateTime - UnixDateDelta) * SecsPerDay) +
- CurrentParams->CurrentDifferenceSec;
- }
- //---------------------------------------------------------------------------
- FILETIME __fastcall DateTimeToFileTime(const TDateTime DateTime,
- TDSTMode /*DSTMode*/)
- {
- __int64 UnixTimeStamp = ::DateTimeToUnix(DateTime);
- const TDateTimeParams * Params = GetDateTimeParams(DecodeYear(DateTime));
- if (!Params->DaylightHack)
- {
- UnixTimeStamp += (IsDateInDST(DateTime) ?
- Params->DaylightDifferenceSec : Params->StandardDifferenceSec);
- const TDateTimeParams * CurrentParams = GetDateTimeParams(0);
- UnixTimeStamp -= CurrentParams->CurrentDaylightDifferenceSec;
- }
- FILETIME Result;
- (*(__int64*)&(Result) = (__int64(UnixTimeStamp) + 11644473600LL) * 10000000LL);
- return Result;
- }
- //---------------------------------------------------------------------------
- TDateTime __fastcall FileTimeToDateTime(const FILETIME & FileTime)
- {
- // duplicated in DirView.pas
- SYSTEMTIME SysTime;
- if (!UsesDaylightHack())
- {
- SYSTEMTIME UniverzalSysTime;
- FileTimeToSystemTime(&FileTime, &UniverzalSysTime);
- SystemTimeToTzSpecificLocalTime(NULL, &UniverzalSysTime, &SysTime);
- }
- else
- {
- FILETIME LocalFileTime;
- FileTimeToLocalFileTime(&FileTime, &LocalFileTime);
- FileTimeToSystemTime(&LocalFileTime, &SysTime);
- }
- TDateTime Result = SystemTimeToDateTime(SysTime);
- return Result;
- }
- //---------------------------------------------------------------------------
- __int64 __fastcall ConvertTimestampToUnix(const FILETIME & FileTime,
- TDSTMode DSTMode)
- {
- __int64 Result = ((*(__int64*)&(FileTime)) / 10000000LL - 11644473600LL);
- if (UsesDaylightHack())
- {
- if ((DSTMode == dstmUnix) || (DSTMode == dstmKeep))
- {
- FILETIME LocalFileTime;
- SYSTEMTIME SystemTime;
- FileTimeToLocalFileTime(&FileTime, &LocalFileTime);
- FileTimeToSystemTime(&LocalFileTime, &SystemTime);
- TDateTime DateTime = SystemTimeToDateTime(SystemTime);
- const TDateTimeParams * Params = GetDateTimeParams(DecodeYear(DateTime));
- Result += (IsDateInDST(DateTime) ?
- Params->DaylightDifferenceSec : Params->StandardDifferenceSec);
- if (DSTMode == dstmKeep)
- {
- const TDateTimeParams * CurrentParams = GetDateTimeParams(0);
- Result -= CurrentParams->CurrentDaylightDifferenceSec;
- }
- }
- }
- else
- {
- if (DSTMode == dstmWin)
- {
- FILETIME LocalFileTime;
- SYSTEMTIME SystemTime;
- FileTimeToLocalFileTime(&FileTime, &LocalFileTime);
- FileTimeToSystemTime(&LocalFileTime, &SystemTime);
- TDateTime DateTime = SystemTimeToDateTime(SystemTime);
- const TDateTimeParams * Params = GetDateTimeParams(DecodeYear(DateTime));
- Result -= (IsDateInDST(DateTime) ?
- Params->DaylightDifferenceSec : Params->StandardDifferenceSec);
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- TDateTime __fastcall ConvertTimestampToUTC(TDateTime DateTime)
- {
- const TDateTimeParams * Params = GetDateTimeParams(DecodeYear(DateTime));
- DateTime +=
- (IsDateInDST(DateTime) ?
- Params->DaylightDifference : Params->StandardDifference);
- DateTime += Params->BaseDifference;
- if (Params->DaylightHack)
- {
- const TDateTimeParams * CurrentParams = GetDateTimeParams(0);
- DateTime += CurrentParams->CurrentDaylightDifference;
- }
- return DateTime;
- }
- //---------------------------------------------------------------------------
- TDateTime __fastcall ConvertTimestampFromUTC(TDateTime DateTime)
- {
- const TDateTimeParams * Params = GetDateTimeParams(DecodeYear(DateTime));
- DateTime -=
- (IsDateInDST(DateTime) ?
- Params->DaylightDifference : Params->StandardDifference);
- DateTime -= Params->BaseDifference;
- if (Params->DaylightHack)
- {
- const TDateTimeParams * CurrentParams = GetDateTimeParams(0);
- DateTime -= CurrentParams->CurrentDaylightDifference;
- }
- return DateTime;
- }
- //---------------------------------------------------------------------------
- __int64 __fastcall ConvertTimestampToUnixSafe(const FILETIME & FileTime,
- TDSTMode DSTMode)
- {
- __int64 Result;
- if ((FileTime.dwLowDateTime == 0) &&
- (FileTime.dwHighDateTime == 0))
- {
- Result = ::DateTimeToUnix(Now());
- }
- else
- {
- Result = ConvertTimestampToUnix(FileTime, DSTMode);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- TDateTime __fastcall AdjustDateTimeFromUnix(TDateTime DateTime, TDSTMode DSTMode)
- {
- const TDateTimeParams * Params = GetDateTimeParams(DecodeYear(DateTime));
- if (Params->DaylightHack)
- {
- if ((DSTMode == dstmWin) || (DSTMode == dstmUnix))
- {
- const TDateTimeParams * CurrentParams = GetDateTimeParams(0);
- DateTime = DateTime - CurrentParams->CurrentDaylightDifference;
- }
- if (!IsDateInDST(DateTime))
- {
- if (DSTMode == dstmWin)
- {
- DateTime = DateTime - Params->DaylightDifference;
- }
- }
- else
- {
- DateTime = DateTime - Params->StandardDifference;
- }
- }
- else
- {
- if (DSTMode == dstmWin)
- {
- if (IsDateInDST(DateTime))
- {
- DateTime = DateTime + Params->DaylightDifference;
- }
- else
- {
- DateTime = DateTime + Params->StandardDifference;
- }
- }
- }
- return DateTime;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall FixedLenDateTimeFormat(const UnicodeString & Format)
- {
- UnicodeString Result = Format;
- bool AsIs = false;
- int Index = 1;
- while (Index <= Result.Length())
- {
- wchar_t F = Result[Index];
- if ((F == L'\'') || (F == L'\"'))
- {
- AsIs = !AsIs;
- Index++;
- }
- else if (!AsIs && ((F == L'a') || (F == L'A')))
- {
- if (Result.SubString(Index, 5).LowerCase() == L"am/pm")
- {
- Index += 5;
- }
- else if (Result.SubString(Index, 3).LowerCase() == L"a/p")
- {
- Index += 3;
- }
- else if (Result.SubString(Index, 4).LowerCase() == L"ampm")
- {
- Index += 4;
- }
- else
- {
- Index++;
- }
- }
- else
- {
- if (!AsIs && (wcschr(L"dDeEmMhHnNsS", F) != NULL) &&
- ((Index == Result.Length()) || (Result[Index + 1] != F)))
- {
- Result.Insert(F, Index);
- }
- while ((Index <= Result.Length()) && (F == Result[Index]))
- {
- Index++;
- }
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- static UnicodeString __fastcall FormatTimeZone(long Sec)
- {
- TTimeSpan Span = TTimeSpan::FromSeconds(Sec);
- UnicodeString Str;
- if ((Span.Seconds == 0) && (Span.Minutes == 0))
- {
- Str = FORMAT(L"%d", (-Span.Hours));
- }
- else if (Span.Seconds == 0)
- {
- Str = FORMAT(L"%d:%2.2d", (-Span.Hours, abs(Span.Minutes)));
- }
- else
- {
- Str = FORMAT(L"%d:%2.2d:%2.2d", (-Span.Hours, abs(Span.Minutes), abs(Span.Seconds)));
- }
- Str = ((Span <= TTimeSpan::Zero) ? L"+" : L"") + Str;
- return Str;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall GetTimeZoneLogString()
- {
- const TDateTimeParams * Params = GetDateTimeParams(0);
- UnicodeString Result =
- FORMAT("Current: GMT%s, Standard: GMT%s, DST: GMT%s, DST Start: %s, DST End: %s",
- (FormatTimeZone(Params->CurrentDifferenceSec),
- FormatTimeZone(Params->BaseDifferenceSec + Params->StandardDifferenceSec),
- FormatTimeZone(Params->BaseDifferenceSec + Params->DaylightDifferenceSec),
- Params->DaylightDate.DateString(),
- Params->StandardDate.DateString()));
- return Result;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall StandardTimestamp(const TDateTime & DateTime)
- {
- return FormatDateTime(L"yyyy'-'mm'-'dd'T'hh':'nn':'ss'.'zzz'Z'", ConvertTimestampToUTC(DateTime));
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall StandardTimestamp()
- {
- return StandardTimestamp(Now());
- }
- //---------------------------------------------------------------------------
- static TDateTime TwoSeconds(0, 0, 2, 0);
- int __fastcall CompareFileTime(TDateTime T1, TDateTime T2)
- {
- // "FAT" time precision
- // (when one time is seconds-precision and other is millisecond-precision,
- // we may have times like 12:00:00.000 and 12:00:01.999, which should
- // be treated the same)
- int Result;
- if (T1 == T2)
- {
- // just optimization
- Result = 0;
- }
- else if ((T1 < T2) && (T2 - T1 >= TwoSeconds))
- {
- Result = -1;
- }
- else if ((T1 > T2) && (T1 - T2 >= TwoSeconds))
- {
- Result = 1;
- }
- else
- {
- Result = 0;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- int __fastcall TimeToMSec(TDateTime T)
- {
- return int(Round(double(T) * double(MSecsPerDay)));
- }
- //---------------------------------------------------------------------------
- int __fastcall TimeToMinutes(TDateTime T)
- {
- return TimeToMSec(T) / MSecsPerSec / SecsPerMin;
- }
- //---------------------------------------------------------------------------
- bool __fastcall RecursiveDeleteFile(const UnicodeString FileName, bool ToRecycleBin)
- {
- SHFILEOPSTRUCT Data;
- memset(&Data, 0, sizeof(Data));
- Data.hwnd = NULL;
- Data.wFunc = FO_DELETE;
- UnicodeString FileList(FileName);
- FileList.SetLength(FileList.Length() + 2);
- FileList[FileList.Length() - 1] = L'\0';
- FileList[FileList.Length()] = L'\0';
- Data.pFrom = FileList.c_str();
- Data.pTo = L"";
- Data.fFlags = FOF_NOCONFIRMATION | FOF_RENAMEONCOLLISION | FOF_NOCONFIRMMKDIR |
- FOF_NOERRORUI | FOF_SILENT;
- if (ToRecycleBin)
- {
- Data.fFlags |= FOF_ALLOWUNDO;
- }
- int ErrorCode = SHFileOperation(&Data);
- bool Result = (ErrorCode == 0);
- if (!Result)
- {
- // according to MSDN, SHFileOperation may return following non-Win32
- // error codes
- if (((ErrorCode >= 0x71) && (ErrorCode <= 0x88)) ||
- (ErrorCode == 0xB7) || (ErrorCode == 0x402) || (ErrorCode == 0x10000) ||
- (ErrorCode == 0x10074))
- {
- ErrorCode = 0;
- }
- SetLastError(ErrorCode);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- unsigned int __fastcall CancelAnswer(unsigned int Answers)
- {
- unsigned 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;
- }
- //---------------------------------------------------------------------------
- unsigned int __fastcall AbortAnswer(unsigned int Answers)
- {
- unsigned int Result;
- if (FLAGSET(Answers, qaAbort))
- {
- Result = qaAbort;
- }
- else
- {
- Result = CancelAnswer(Answers);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- unsigned int __fastcall ContinueAnswer(unsigned int Answers)
- {
- unsigned int Result;
- if (FLAGSET(Answers, qaSkip))
- {
- Result = qaSkip;
- }
- else if (FLAGSET(Answers, qaIgnore))
- {
- Result = qaIgnore;
- }
- else if (FLAGSET(Answers, qaYes))
- {
- Result = qaYes;
- }
- else if (FLAGSET(Answers, qaOK))
- {
- Result = qaOK;
- }
- else if (FLAGSET(Answers, qaRetry))
- {
- Result = qaRetry;
- }
- else
- {
- Result = CancelAnswer(Answers);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- TLibModule * __fastcall FindModule(void * Instance)
- {
- TLibModule * CurModule;
- CurModule = reinterpret_cast<TLibModule*>(LibModuleList);
- while (CurModule)
- {
- if (CurModule->Instance == (unsigned)Instance)
- {
- break;
- }
- else
- {
- CurModule = CurModule->Next;
- }
- }
- return CurModule;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall LoadStr(int Ident, unsigned int MaxLength)
- {
- TLibModule * MainModule = FindModule(HInstance);
- assert(MainModule != NULL);
- UnicodeString Result;
- Result.SetLength(MaxLength);
- int Length = LoadString((HINSTANCE)MainModule->ResInstance, Ident, Result.c_str(), MaxLength);
- Result.SetLength(Length);
- return Result;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall LoadStrPart(int Ident, int Part)
- {
- UnicodeString Result;
- UnicodeString Str = LoadStr(Ident);
- while (Part > 0)
- {
- Result = CutToChar(Str, L'|', false);
- Part--;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall DecodeUrlChars(UnicodeString S)
- {
- int i = 1;
- while (i <= S.Length())
- {
- switch (S[i])
- {
- case L'+':
- S[i] = ' ';
- break;
- case L'%':
- if (i <= S.Length() - 2)
- {
- unsigned char B = HexToByte(S.SubString(i + 1, 2));
- if (B > 0)
- {
- S[i] = (wchar_t)B;
- S.Delete(i + 1, 2);
- }
- }
- break;
- }
- i++;
- }
- return S;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall DoEncodeUrl(UnicodeString S, UnicodeString Chars)
- {
- int i = 1;
- while (i <= S.Length())
- {
- if (Chars.Pos(S[i]) > 0)
- {
- UnicodeString H = ByteToHex(AnsiString(UnicodeString(S[i]))[1]);
- S.Insert(H, i + 1);
- S[i] = '%';
- i += H.Length();
- }
- i++;
- }
- return S;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall EncodeUrlChars(UnicodeString S, UnicodeString Ignore)
- {
- UnicodeString Chars;
- if (Ignore.Pos(L' ') == 0)
- {
- Chars += L' ';
- }
- if (Ignore.Pos(L'/') == 0)
- {
- Chars += L'/';
- }
- return DoEncodeUrl(S, Chars);
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall NonUrlChars()
- {
- UnicodeString S;
- for (unsigned int I = 0; I <= 127; I++)
- {
- wchar_t C = static_cast<wchar_t>(I);
- if (((C >= L'a') && (C <= L'z')) ||
- ((C >= L'A') && (C <= L'Z')) ||
- ((C >= L'0') && (C <= L'9')) ||
- (C == L'_') || (C == L'-') || (C == L'.'))
- {
- // noop
- }
- else
- {
- S += C;
- }
- }
- return S;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall EncodeUrlString(UnicodeString S)
- {
- return DoEncodeUrl(S, NonUrlChars());
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall EscapeHotkey(const UnicodeString & Caption)
- {
- return StringReplace(Caption, L"&", L"&&", TReplaceFlags() << rfReplaceAll);
- }
- //---------------------------------------------------------------------------
- // duplicated in console's Main.cpp
- bool __fastcall CutToken(UnicodeString & Str, UnicodeString & Token,
- UnicodeString * RawToken)
- {
- bool Result;
- Token = L"";
- // inspired by Putty's sftp_getcmd() from PSFTP.C
- int Index = 1;
- while ((Index <= Str.Length()) &&
- ((Str[Index] == L' ') || (Str[Index] == L'\t')))
- {
- Index++;
- }
- if (Index <= Str.Length())
- {
- bool Quoting = false;
- while (Index <= Str.Length())
- {
- if (!Quoting && ((Str[Index] == L' ') || (Str[Index] == L'\t')))
- {
- break;
- }
- else if ((Str[Index] == L'"') && (Index + 1 <= Str.Length()) &&
- (Str[Index + 1] == L'"'))
- {
- Index += 2;
- Token += L'"';
- }
- else if (Str[Index] == L'"')
- {
- Index++;
- Quoting = !Quoting;
- }
- else
- {
- Token += Str[Index];
- Index++;
- }
- }
- if (RawToken != NULL)
- {
- (*RawToken) = Str.SubString(1, Index - 1);
- }
- if (Index <= Str.Length())
- {
- Index++;
- }
- Str = Str.SubString(Index, Str.Length());
- Result = true;
- }
- else
- {
- Result = false;
- Str = L"";
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall AddToList(UnicodeString & List, const UnicodeString & Value, const UnicodeString & Delimiter)
- {
- if (!Value.IsEmpty())
- {
- if (!List.IsEmpty() &&
- ((List.Length() < Delimiter.Length()) ||
- (List.SubString(List.Length() - Delimiter.Length() + 1, Delimiter.Length()) != Delimiter)))
- {
- List += Delimiter;
- }
- List += Value;
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall Is2000()
- {
- return (Win32MajorVersion >= 5);
- }
- //---------------------------------------------------------------------------
- bool __fastcall IsWin7()
- {
- return
- (Win32MajorVersion > 6) ||
- ((Win32MajorVersion == 6) && (Win32MinorVersion >= 1));
- }
- //---------------------------------------------------------------------------
- bool __fastcall IsExactly2008R2()
- {
- HINSTANCE Kernel32 = GetModuleHandle(kernel32);
- typedef BOOL WINAPI (* TGetProductInfo)(DWORD, DWORD, DWORD, DWORD, PDWORD);
- TGetProductInfo GetProductInfo =
- (TGetProductInfo)GetProcAddress(Kernel32, "GetProductInfo");
- bool Result;
- if (GetProductInfo == NULL)
- {
- Result = false;
- }
- else
- {
- DWORD Type;
- GetProductInfo(Win32MajorVersion, Win32MinorVersion, 0, 0, &Type);
- switch (Type)
- {
- case 0x0008 /*PRODUCT_DATACENTER_SERVER*/:
- case 0x000C /*PRODUCT_DATACENTER_SERVER_CORE}*/:
- case 0x0027 /*PRODUCT_DATACENTER_SERVER_CORE_V*/:
- case 0x0025 /*PRODUCT_DATACENTER_SERVER_V*/:
- case 0x000A /*PRODUCT_ENTERPRISE_SERVE*/:
- case 0x000E /*PRODUCT_ENTERPRISE_SERVER_COR*/:
- case 0x0029 /*PRODUCT_ENTERPRISE_SERVER_CORE_*/:
- case 0x000F /*PRODUCT_ENTERPRISE_SERVER_IA6*/:
- case 0x0026 /*PRODUCT_ENTERPRISE_SERVER_*/:
- case 0x002A /*PRODUCT_HYPER*/:
- case 0x001E /*PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMEN*/:
- case 0x0020 /*PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGIN*/:
- case 0x001F /*PRODUCT_MEDIUMBUSINESS_SERVER_SECURIT*/:
- case 0x0018 /*PRODUCT_SERVER_FOR_SMALLBUSINES*/:
- case 0x0023 /*PRODUCT_SERVER_FOR_SMALLBUSINESS_*/:
- case 0x0021 /*PRODUCT_SERVER_FOUNDATIO*/:
- case 0x0009 /*PRODUCT_SMALLBUSINESS_SERVE*/:
- case 0x0038 /*PRODUCT_SOLUTION_EMBEDDEDSERVE*/:
- case 0x0007 /*PRODUCT_STANDARD_SERVE*/:
- case 0x000D /*PRODUCT_STANDARD_SERVER_COR*/:
- case 0x0028 /*PRODUCT_STANDARD_SERVER_CORE_*/:
- case 0x0024 /*PRODUCT_STANDARD_SERVER_*/:
- case 0x0017 /*PRODUCT_STORAGE_ENTERPRISE_SERVE*/:
- case 0x0014 /*PRODUCT_STORAGE_EXPRESS_SERVE*/:
- case 0x0015 /*PRODUCT_STORAGE_STANDARD_SERVE*/:
- case 0x0016 /*PRODUCT_STORAGE_WORKGROUP_SERVE*/:
- case 0x0011 /*PRODUCT_WEB_SERVE*/:
- case 0x001D /*PRODUCT_WEB_SERVER_COR*/:
- Result = true;
- break;
- default:
- Result = false;
- break;
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- LCID __fastcall GetDefaultLCID()
- {
- return Is2000() ? GetUserDefaultLCID() : GetThreadLocale();
- }
- //---------------------------------------------------------------------------
- static UnicodeString ADefaultEncodingName;
- UnicodeString __fastcall DefaultEncodingName()
- {
- if (ADefaultEncodingName.IsEmpty())
- {
- CPINFOEX Info;
- GetCPInfoEx(CP_ACP, 0, &Info);
- ADefaultEncodingName = Info.CodePageName;
- }
- return ADefaultEncodingName;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall WindowsProductName()
- {
- UnicodeString Result;
- TRegistry * Registry = new TRegistry(KEY_READ);
- try
- {
- Registry->RootKey = HKEY_LOCAL_MACHINE;
- if (Registry->OpenKey("SOFTWARE", false) &&
- Registry->OpenKey("Microsoft", false) &&
- Registry->OpenKey("Windows NT", false) &&
- Registry->OpenKey("CurrentVersion", false))
- {
- Result = Registry->ReadString("ProductName");
- }
- delete Registry;
- }
- catch(...)
- {
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall IsDirectoryWriteable(const UnicodeString & Path)
- {
- UnicodeString FileName =
- IncludeTrailingPathDelimiter(Path) +
- FORMAT("wscp_%s_%d.tmp", (FormatDateTime(L"nnzzz", Now()), int(GetCurrentProcessId())));
- HANDLE Handle = CreateFile(FileName.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL,
- CREATE_NEW, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, 0);
- bool Result = (Handle != INVALID_HANDLE_VALUE);
- if (Result)
- {
- CloseHandle(Handle);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall FormatNumber(__int64 Number)
- {
- return FormatFloat(L"#,##0", Number);
- }
- //---------------------------------------------------------------------------
- // simple alternative to FormatBytes
- UnicodeString __fastcall FormatSize(__int64 Size)
- {
- return FormatNumber(Size);
- }
- //---------------------------------------------------------------------------
- // Suppress warning about unused constants in DateUtils.hpp
- #pragma warn -8080
|