Common.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //---------------------------------------------------------------------------
  2. #ifndef CommonH
  3. #define CommonH
  4. //---------------------------------------------------------------------------
  5. #define EXCEPTION throw ExtException(NULL, L"")
  6. #define THROWOSIFFALSE(C) { if (!(C)) RaiseLastOSError(); }
  7. #define SAFE_DESTROY_EX(CLASS, OBJ) { CLASS * PObj = OBJ; OBJ = NULL; delete PObj; }
  8. #define SAFE_DESTROY(OBJ) SAFE_DESTROY_EX(TObject, OBJ)
  9. #define NULL_TERMINATE(S) S[LENOF(S) - 1] = L'\0'
  10. #define ASCOPY(dest, source) \
  11. { \
  12. AnsiString CopyBuf = source; \
  13. strncpy(dest, CopyBuf.c_str(), LENOF(dest)); \
  14. dest[LENOF(dest)-1] = '\0'; \
  15. }
  16. #define FORMAT(S, F) Format(S, ARRAYOFCONST(F))
  17. #define FMTLOAD(I, F) FmtLoadStr(I, ARRAYOFCONST(F))
  18. #define LENOF(x) ( (sizeof((x))) / (sizeof(*(x))))
  19. #define FLAGSET(SET, FLAG) (((SET) & (FLAG)) == (FLAG))
  20. #define FLAGCLEAR(SET, FLAG) (((SET) & (FLAG)) == 0)
  21. #define FLAGMASK(ENABLE, FLAG) ((ENABLE) ? (FLAG) : 0)
  22. #define SWAP(TYPE, FIRST, SECOND) \
  23. { TYPE __Backup = FIRST; FIRST = SECOND; SECOND = __Backup; }
  24. //---------------------------------------------------------------------------
  25. extern const wchar_t EngShortMonthNames[12][4];
  26. extern const char Bom[3];
  27. extern const wchar_t TokenPrefix;
  28. extern const wchar_t NoReplacement;
  29. extern const wchar_t TokenReplacement;
  30. extern const UnicodeString LocalInvalidChars;
  31. extern const UnicodeString PasswordMask;
  32. //---------------------------------------------------------------------------
  33. UnicodeString ReplaceChar(UnicodeString Str, wchar_t A, wchar_t B);
  34. UnicodeString DeleteChar(UnicodeString Str, wchar_t C);
  35. void PackStr(UnicodeString &Str);
  36. void PackStr(RawByteString &Str);
  37. void __fastcall Shred(UnicodeString & Str);
  38. UnicodeString MakeValidFileName(UnicodeString FileName);
  39. UnicodeString RootKeyToStr(HKEY RootKey);
  40. UnicodeString BooleanToStr(bool B);
  41. UnicodeString BooleanToEngStr(bool B);
  42. UnicodeString DefaultStr(const UnicodeString & Str, const UnicodeString & Default);
  43. UnicodeString CutToChar(UnicodeString &Str, wchar_t Ch, bool Trim);
  44. UnicodeString CopyToChars(const UnicodeString & Str, int & From, UnicodeString Chs, bool Trim,
  45. wchar_t * Delimiter = NULL, bool DoubleDelimiterEscapes = false);
  46. UnicodeString DelimitStr(UnicodeString Str, UnicodeString Chars);
  47. UnicodeString ShellDelimitStr(UnicodeString Str, wchar_t Quote);
  48. UnicodeString ExceptionLogString(Exception *E);
  49. UnicodeString __fastcall MainInstructions(const UnicodeString & S);
  50. UnicodeString __fastcall MainInstructionsFirstParagraph(const UnicodeString & S);
  51. bool ExtractMainInstructions(UnicodeString & S, UnicodeString & MainInstructions);
  52. UnicodeString UnformatMessage(UnicodeString S);
  53. UnicodeString RemoveInteractiveMsgTag(UnicodeString S);
  54. bool IsNumber(const UnicodeString Str);
  55. UnicodeString __fastcall SystemTemporaryDirectory();
  56. UnicodeString __fastcall GetShellFolderPath(int CSIdl);
  57. UnicodeString __fastcall StripPathQuotes(const UnicodeString Path);
  58. UnicodeString __fastcall AddPathQuotes(UnicodeString Path);
  59. void __fastcall SplitCommand(UnicodeString Command, UnicodeString &Program,
  60. UnicodeString & Params, UnicodeString & Dir);
  61. UnicodeString __fastcall ValidLocalFileName(UnicodeString FileName);
  62. UnicodeString __fastcall ValidLocalFileName(
  63. UnicodeString FileName, wchar_t InvalidCharsReplacement,
  64. const UnicodeString & TokenizibleChars, const UnicodeString & LocalInvalidChars);
  65. UnicodeString __fastcall ExtractProgram(UnicodeString Command);
  66. UnicodeString __fastcall ExtractProgramName(UnicodeString Command);
  67. UnicodeString __fastcall FormatCommand(UnicodeString Program, UnicodeString Params);
  68. UnicodeString __fastcall ExpandFileNameCommand(const UnicodeString Command,
  69. const UnicodeString FileName);
  70. void __fastcall ReformatFileNameCommand(UnicodeString & Command);
  71. UnicodeString __fastcall EscapePuttyCommandParam(UnicodeString Param);
  72. UnicodeString __fastcall ExpandEnvironmentVariables(const UnicodeString & Str);
  73. bool __fastcall ComparePaths(const UnicodeString & Path1, const UnicodeString & Path2);
  74. bool __fastcall CompareFileName(const UnicodeString & Path1, const UnicodeString & Path2);
  75. bool __fastcall IsReservedName(UnicodeString FileName);
  76. UnicodeString __fastcall ApiPath(UnicodeString Path);
  77. UnicodeString __fastcall DisplayableStr(const RawByteString & Str);
  78. UnicodeString __fastcall ByteToHex(unsigned char B, bool UpperCase = true);
  79. UnicodeString __fastcall BytesToHex(const unsigned char * B, size_t Length, bool UpperCase = true, wchar_t Separator = L'\0');
  80. UnicodeString __fastcall BytesToHex(RawByteString Str, bool UpperCase = true, wchar_t Separator = L'\0');
  81. UnicodeString __fastcall CharToHex(wchar_t Ch, bool UpperCase = true);
  82. RawByteString __fastcall HexToBytes(const UnicodeString Hex);
  83. unsigned char __fastcall HexToByte(const UnicodeString Hex);
  84. bool __fastcall IsLowerCaseLetter(wchar_t Ch);
  85. bool __fastcall IsUpperCaseLetter(wchar_t Ch);
  86. bool __fastcall IsLetter(wchar_t Ch);
  87. bool __fastcall IsDigit(wchar_t Ch);
  88. bool __fastcall IsHex(wchar_t Ch);
  89. UnicodeString __fastcall DecodeUrlChars(UnicodeString S);
  90. UnicodeString __fastcall EncodeUrlChars(UnicodeString S);
  91. UnicodeString __fastcall EncodeUrlString(UnicodeString S);
  92. UnicodeString __fastcall EncodeUrlPath(UnicodeString S);
  93. UnicodeString __fastcall AppendUrlParams(UnicodeString URL, UnicodeString Params);
  94. bool __fastcall RecursiveDeleteFile(const UnicodeString FileName, bool ToRecycleBin);
  95. void __fastcall DeleteFileChecked(const UnicodeString & FileName);
  96. unsigned int __fastcall CancelAnswer(unsigned int Answers);
  97. unsigned int __fastcall AbortAnswer(unsigned int Answers);
  98. unsigned int __fastcall ContinueAnswer(unsigned int Answers);
  99. UnicodeString __fastcall LoadStr(int Ident, unsigned int MaxLength);
  100. UnicodeString __fastcall LoadStrPart(int Ident, int Part);
  101. UnicodeString __fastcall EscapeHotkey(const UnicodeString & Caption);
  102. bool __fastcall CutToken(UnicodeString & Str, UnicodeString & Token,
  103. UnicodeString * RawToken = NULL);
  104. void __fastcall AddToList(UnicodeString & List, const UnicodeString & Value, const UnicodeString & Delimiter);
  105. bool __fastcall IsWinVista();
  106. bool __fastcall IsWin7();
  107. bool __fastcall IsWine();
  108. TLibModule * __fastcall FindModule(void * Instance);
  109. __int64 __fastcall Round(double Number);
  110. bool __fastcall TryRelativeStrToDateTime(UnicodeString S, TDateTime & DateTime);
  111. LCID __fastcall GetDefaultLCID();
  112. UnicodeString __fastcall DefaultEncodingName();
  113. UnicodeString __fastcall WindowsProductName();
  114. bool _fastcall GetWindowsProductType(DWORD & Type);
  115. bool __fastcall IsDirectoryWriteable(const UnicodeString & Path);
  116. UnicodeString __fastcall FormatNumber(__int64 Size);
  117. UnicodeString __fastcall FormatSize(__int64 Size);
  118. UnicodeString __fastcall ExtractFileBaseName(const UnicodeString & Path);
  119. TStringList * __fastcall TextToStringList(const UnicodeString & Text);
  120. UnicodeString __fastcall TrimVersion(UnicodeString Version);
  121. UnicodeString __fastcall FormatVersion(int MajovVersion, int MinorVersion, int Release);
  122. TFormatSettings __fastcall GetEngFormatSettings();
  123. int __fastcall ParseShortEngMonthName(const UnicodeString & MonthStr);
  124. //---------------------------------------------------------------------------
  125. typedef void __fastcall (__closure* TProcessLocalFileEvent)
  126. (const UnicodeString FileName, const TSearchRec Rec, void * Param);
  127. bool __fastcall FileSearchRec(const UnicodeString FileName, TSearchRec & Rec);
  128. struct TSearchRecChecked : public TSearchRec
  129. {
  130. UnicodeString Path;
  131. };
  132. int __fastcall FindCheck(int Result, const UnicodeString & Path);
  133. int __fastcall FindFirstUnchecked(const UnicodeString & Path, int Attr, TSearchRecChecked & F);
  134. int __fastcall FindFirstChecked(const UnicodeString & Path, int Attr, TSearchRecChecked & F);
  135. int __fastcall FindNextChecked(TSearchRecChecked & F);
  136. void __fastcall ProcessLocalDirectory(UnicodeString DirName,
  137. TProcessLocalFileEvent CallBackFunc, void * Param = NULL, int FindAttrs = -1);
  138. //---------------------------------------------------------------------------
  139. enum TDSTMode
  140. {
  141. dstmWin = 0, //
  142. dstmUnix = 1, // adjust UTC time to Windows "bug"
  143. dstmKeep = 2
  144. };
  145. bool __fastcall UsesDaylightHack();
  146. TDateTime __fastcall EncodeDateVerbose(Word Year, Word Month, Word Day);
  147. TDateTime __fastcall EncodeTimeVerbose(Word Hour, Word Min, Word Sec, Word MSec);
  148. TDateTime __fastcall SystemTimeToDateTimeVerbose(const SYSTEMTIME & SystemTime);
  149. TDateTime __fastcall UnixToDateTime(__int64 TimeStamp, TDSTMode DSTMode);
  150. TDateTime __fastcall ConvertTimestampToUTC(TDateTime DateTime);
  151. TDateTime __fastcall ConvertTimestampFromUTC(TDateTime DateTime);
  152. FILETIME __fastcall DateTimeToFileTime(const TDateTime DateTime, TDSTMode DSTMode);
  153. TDateTime __fastcall AdjustDateTimeFromUnix(TDateTime DateTime, TDSTMode DSTMode);
  154. void __fastcall UnifyDateTimePrecision(TDateTime & DateTime1, TDateTime & DateTime2);
  155. TDateTime __fastcall FileTimeToDateTime(const FILETIME & FileTime);
  156. __int64 __fastcall ConvertTimestampToUnix(const FILETIME & FileTime,
  157. TDSTMode DSTMode);
  158. __int64 __fastcall ConvertTimestampToUnixSafe(const FILETIME & FileTime,
  159. TDSTMode DSTMode);
  160. UnicodeString __fastcall FixedLenDateTimeFormat(const UnicodeString & Format);
  161. UnicodeString __fastcall StandardTimestamp(const TDateTime & DateTime);
  162. UnicodeString __fastcall StandardTimestamp();
  163. UnicodeString __fastcall StandardDatestamp();
  164. UnicodeString __fastcall GetTimeZoneLogString();
  165. bool __fastcall AdjustClockForDSTEnabled();
  166. int __fastcall CompareFileTime(TDateTime T1, TDateTime T2);
  167. int __fastcall TimeToMSec(TDateTime T);
  168. int __fastcall TimeToSeconds(TDateTime T);
  169. int __fastcall TimeToMinutes(TDateTime T);
  170. //---------------------------------------------------------------------------
  171. template<class MethodT>
  172. MethodT __fastcall MakeMethod(void * Data, void * Code)
  173. {
  174. MethodT Method;
  175. ((TMethod*)&Method)->Data = Data;
  176. ((TMethod*)&Method)->Code = Code;
  177. return Method;
  178. }
  179. //---------------------------------------------------------------------------
  180. class TGuard
  181. {
  182. public:
  183. __fastcall TGuard(TCriticalSection * ACriticalSection);
  184. __fastcall ~TGuard();
  185. private:
  186. TCriticalSection * FCriticalSection;
  187. };
  188. //---------------------------------------------------------------------------
  189. class TUnguard
  190. {
  191. public:
  192. __fastcall TUnguard(TCriticalSection * ACriticalSection);
  193. __fastcall ~TUnguard();
  194. private:
  195. TCriticalSection * FCriticalSection;
  196. };
  197. //---------------------------------------------------------------------------
  198. //---------------------------------------------------------------------------
  199. #include <assert.h>
  200. #define ACCESS_VIOLATION_TEST { (*((int*)NULL)) = 0; }
  201. #ifndef _DEBUG
  202. #undef assert
  203. #define assert(p) ((void)0)
  204. #define CHECK(p) p
  205. #define FAIL
  206. #define ALWAYS_TRUE(p) p
  207. #define ALWAYS_FALSE(p) p
  208. #define NOT_NULL(P) P
  209. #else
  210. #define CHECK(p) { bool __CHECK_RESULT__ = (p); assert(__CHECK_RESULT__); }
  211. #define FAIL assert(false)
  212. #define ALWAYS_TRUE(p) p
  213. #define ALWAYS_FALSE(p) p
  214. #define NOT_NULL(P) P
  215. #endif
  216. #define USEDPARAM(p) ((&p) == (&p))
  217. //---------------------------------------------------------------------------
  218. template<class T>
  219. class TValueRestorer
  220. {
  221. public:
  222. __fastcall TValueRestorer(T & Target, const T & Value) :
  223. FTarget(Target),
  224. FValue(Value)
  225. {
  226. }
  227. __fastcall TValueRestorer(T & Target) :
  228. FTarget(Target),
  229. FValue(Target)
  230. {
  231. }
  232. __fastcall ~TValueRestorer()
  233. {
  234. FTarget = FValue;
  235. }
  236. protected:
  237. T & FTarget;
  238. T FValue;
  239. };
  240. //---------------------------------------------------------------------------
  241. class TAutoNestingCounter : TValueRestorer<int>
  242. {
  243. public:
  244. __fastcall TAutoNestingCounter(int & Target) :
  245. TValueRestorer<int>(Target)
  246. {
  247. assert(Target >= 0);
  248. ++Target;
  249. }
  250. __fastcall ~TAutoNestingCounter()
  251. {
  252. assert(FTarget == (FValue + 1));
  253. }
  254. };
  255. //---------------------------------------------------------------------------
  256. #endif