Common.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. bool __fastcall RecursiveDeleteFile(const UnicodeString FileName, bool ToRecycleBin);
  90. void __fastcall DeleteFileChecked(const UnicodeString & FileName);
  91. unsigned int __fastcall CancelAnswer(unsigned int Answers);
  92. unsigned int __fastcall AbortAnswer(unsigned int Answers);
  93. unsigned int __fastcall ContinueAnswer(unsigned int Answers);
  94. UnicodeString __fastcall LoadStr(int Ident, unsigned int MaxLength);
  95. UnicodeString __fastcall LoadStrPart(int Ident, int Part);
  96. UnicodeString __fastcall EscapeHotkey(const UnicodeString & Caption);
  97. bool __fastcall CutToken(UnicodeString & Str, UnicodeString & Token,
  98. UnicodeString * RawToken = NULL);
  99. void __fastcall AddToList(UnicodeString & List, const UnicodeString & Value, const UnicodeString & Delimiter);
  100. bool __fastcall IsWinVista();
  101. bool __fastcall IsWin7();
  102. TLibModule * __fastcall FindModule(void * Instance);
  103. __int64 __fastcall Round(double Number);
  104. bool __fastcall TryRelativeStrToDateTime(UnicodeString S, TDateTime & DateTime);
  105. LCID __fastcall GetDefaultLCID();
  106. UnicodeString __fastcall DefaultEncodingName();
  107. UnicodeString __fastcall WindowsProductName();
  108. bool _fastcall GetWindowsProductType(DWORD & Type);
  109. bool __fastcall IsDirectoryWriteable(const UnicodeString & Path);
  110. UnicodeString __fastcall FormatNumber(__int64 Size);
  111. UnicodeString __fastcall FormatSize(__int64 Size);
  112. UnicodeString __fastcall ExtractFileBaseName(const UnicodeString & Path);
  113. //---------------------------------------------------------------------------
  114. typedef void __fastcall (__closure* TProcessLocalFileEvent)
  115. (const UnicodeString FileName, const TSearchRec Rec, void * Param);
  116. bool __fastcall FileSearchRec(const UnicodeString FileName, TSearchRec & Rec);
  117. struct TSearchRecChecked : public TSearchRec
  118. {
  119. UnicodeString Path;
  120. };
  121. int __fastcall FindCheck(int Result, const UnicodeString & Path);
  122. int __fastcall FindFirstUnchecked(const UnicodeString & Path, int Attr, TSearchRecChecked & F);
  123. int __fastcall FindFirstChecked(const UnicodeString & Path, int Attr, TSearchRecChecked & F);
  124. int __fastcall FindNextChecked(TSearchRecChecked & F);
  125. void __fastcall ProcessLocalDirectory(UnicodeString DirName,
  126. TProcessLocalFileEvent CallBackFunc, void * Param = NULL, int FindAttrs = -1);
  127. //---------------------------------------------------------------------------
  128. enum TDSTMode
  129. {
  130. dstmWin = 0, //
  131. dstmUnix = 1, // adjust UTC time to Windows "bug"
  132. dstmKeep = 2
  133. };
  134. bool __fastcall UsesDaylightHack();
  135. TDateTime __fastcall EncodeDateVerbose(Word Year, Word Month, Word Day);
  136. TDateTime __fastcall EncodeTimeVerbose(Word Hour, Word Min, Word Sec, Word MSec);
  137. TDateTime __fastcall SystemTimeToDateTimeVerbose(const SYSTEMTIME & SystemTime);
  138. TDateTime __fastcall UnixToDateTime(__int64 TimeStamp, TDSTMode DSTMode);
  139. TDateTime __fastcall ConvertTimestampToUTC(TDateTime DateTime);
  140. TDateTime __fastcall ConvertTimestampFromUTC(TDateTime DateTime);
  141. FILETIME __fastcall DateTimeToFileTime(const TDateTime DateTime, TDSTMode DSTMode);
  142. TDateTime __fastcall AdjustDateTimeFromUnix(TDateTime DateTime, TDSTMode DSTMode);
  143. void __fastcall UnifyDateTimePrecision(TDateTime & DateTime1, TDateTime & DateTime2);
  144. TDateTime __fastcall FileTimeToDateTime(const FILETIME & FileTime);
  145. __int64 __fastcall ConvertTimestampToUnix(const FILETIME & FileTime,
  146. TDSTMode DSTMode);
  147. __int64 __fastcall ConvertTimestampToUnixSafe(const FILETIME & FileTime,
  148. TDSTMode DSTMode);
  149. UnicodeString __fastcall FixedLenDateTimeFormat(const UnicodeString & Format);
  150. UnicodeString __fastcall StandardTimestamp(const TDateTime & DateTime);
  151. UnicodeString __fastcall StandardTimestamp();
  152. UnicodeString __fastcall GetTimeZoneLogString();
  153. bool __fastcall AdjustClockForDSTEnabled();
  154. int __fastcall CompareFileTime(TDateTime T1, TDateTime T2);
  155. int __fastcall TimeToMSec(TDateTime T);
  156. int __fastcall TimeToSeconds(TDateTime T);
  157. int __fastcall TimeToMinutes(TDateTime T);
  158. //---------------------------------------------------------------------------
  159. template<class MethodT>
  160. MethodT __fastcall MakeMethod(void * Data, void * Code)
  161. {
  162. MethodT Method;
  163. ((TMethod*)&Method)->Data = Data;
  164. ((TMethod*)&Method)->Code = Code;
  165. return Method;
  166. }
  167. //---------------------------------------------------------------------------
  168. class TGuard
  169. {
  170. public:
  171. __fastcall TGuard(TCriticalSection * ACriticalSection);
  172. __fastcall ~TGuard();
  173. private:
  174. TCriticalSection * FCriticalSection;
  175. };
  176. //---------------------------------------------------------------------------
  177. class TUnguard
  178. {
  179. public:
  180. __fastcall TUnguard(TCriticalSection * ACriticalSection);
  181. __fastcall ~TUnguard();
  182. private:
  183. TCriticalSection * FCriticalSection;
  184. };
  185. //---------------------------------------------------------------------------
  186. //---------------------------------------------------------------------------
  187. #include <assert.h>
  188. #define ACCESS_VIOLATION_TEST { (*((int*)NULL)) = 0; }
  189. #ifndef _DEBUG
  190. #undef assert
  191. #define assert(p) ((void)0)
  192. #define CHECK(p) p
  193. #define FAIL
  194. #define ALWAYS_TRUE(p) p
  195. #define ALWAYS_FALSE(p) p
  196. #define NOT_NULL(P) P
  197. #else
  198. #define CHECK(p) { bool __CHECK_RESULT__ = (p); assert(__CHECK_RESULT__); }
  199. #define FAIL assert(false)
  200. #define ALWAYS_TRUE(p) p
  201. #define ALWAYS_FALSE(p) p
  202. #define NOT_NULL(P) P
  203. #endif
  204. #define USEDPARAM(p) ((&p) == (&p))
  205. //---------------------------------------------------------------------------
  206. template<class T>
  207. class TValueRestorer
  208. {
  209. public:
  210. __fastcall TValueRestorer(T & Target, const T & Value) :
  211. FTarget(Target),
  212. FValue(Value)
  213. {
  214. }
  215. __fastcall TValueRestorer(T & Target) :
  216. FTarget(Target),
  217. FValue(Target)
  218. {
  219. }
  220. __fastcall ~TValueRestorer()
  221. {
  222. FTarget = FValue;
  223. }
  224. protected:
  225. T & FTarget;
  226. T FValue;
  227. };
  228. //---------------------------------------------------------------------------
  229. class TAutoNestingCounter : TValueRestorer<int>
  230. {
  231. public:
  232. __fastcall TAutoNestingCounter(int & Target) :
  233. TValueRestorer<int>(Target)
  234. {
  235. assert(Target >= 0);
  236. ++Target;
  237. }
  238. __fastcall ~TAutoNestingCounter()
  239. {
  240. assert(FTarget == (FValue + 1));
  241. }
  242. };
  243. //---------------------------------------------------------------------------
  244. #endif