Common.h 11 KB

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