Common.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //---------------------------------------------------------------------------
  2. #ifndef CommonH
  3. #define CommonH
  4. #ifndef C_ONLY
  5. //---------------------------------------------------------------------------
  6. #define EXCEPTION throw ExtException(NULL, "")
  7. #define THROWIFFALSE(C) if (!(C)) EXCEPTION
  8. #define SCOPY(dest, source) \
  9. strncpy(dest, source, sizeof(dest)); \
  10. dest[sizeof(dest)-1] = '\0'
  11. #define SAFE_DESTROY(OBJ) { TObject * PObj = OBJ; OBJ = NULL; delete PObj; }
  12. #define ASCOPY(dest, source) SCOPY(dest, source.c_str())
  13. #define FORMAT(S, F) Format(S, ARRAYOFCONST(F))
  14. #define FMTLOAD(I, F) FmtLoadStr(I, ARRAYOFCONST(F))
  15. #define LENOF(x) ( (sizeof((x))) / (sizeof(*(x))))
  16. #define FLAGSET(SET, FLAG) (((SET) & (FLAG)) == (FLAG))
  17. #define FLAGCLEAR(SET, FLAG) (((SET) & (FLAG)) == 0)
  18. #define FLAGMASK(ENABLE, FLAG) ((ENABLE) ? (FLAG) : 0)
  19. #define SWAP(TYPE, FIRST, SECOND) \
  20. { TYPE __Backup = FIRST; FIRST = SECOND; SECOND = __Backup; }
  21. //---------------------------------------------------------------------------
  22. extern const char EngShortMonthNames[12][4];
  23. //---------------------------------------------------------------------------
  24. AnsiString ReplaceChar(AnsiString Str, Char A, Char B);
  25. AnsiString DeleteChar(AnsiString Str, Char C);
  26. void PackStr(AnsiString &Str);
  27. AnsiString MakeValidFileName(AnsiString FileName);
  28. AnsiString RootKeyToStr(HKEY RootKey);
  29. AnsiString BooleanToStr(bool B);
  30. AnsiString BooleanToEngStr(bool B);
  31. AnsiString CutToChar(AnsiString &Str, Char Ch, bool Trim);
  32. AnsiString CutToChars(AnsiString &Str, AnsiString Chs, bool Trim);
  33. AnsiString DelimitStr(AnsiString Str, AnsiString Chars);
  34. AnsiString ShellDelimitStr(AnsiString Str, char Quote);
  35. void __fastcall OemToAnsi(AnsiString & Str);
  36. void __fastcall AnsiToOem(AnsiString & Str);
  37. AnsiString ExceptionLogString(Exception *E);
  38. bool IsDots(const AnsiString Str);
  39. AnsiString __fastcall SystemTemporaryDirectory();
  40. AnsiString __fastcall StripPathQuotes(const AnsiString Path);
  41. AnsiString __fastcall AddPathQuotes(AnsiString Path);
  42. void __fastcall SplitCommand(AnsiString Command, AnsiString &Program,
  43. AnsiString & Params, AnsiString & Dir);
  44. AnsiString __fastcall ExtractProgram(AnsiString Command);
  45. AnsiString __fastcall FormatCommand(AnsiString Program, AnsiString Params);
  46. bool __fastcall IsDisplayableStr(const AnsiString Str);
  47. AnsiString __fastcall CharToHex(char Ch);
  48. AnsiString __fastcall StrToHex(const AnsiString Str);
  49. AnsiString __fastcall HexToStr(const AnsiString Hex);
  50. unsigned int __fastcall HexToInt(const AnsiString Hex, int MinChars = 0);
  51. AnsiString __fastcall DecodeUrlChars(AnsiString S);
  52. bool __fastcall RecursiveDeleteFile(const AnsiString FileName, bool ToRecycleBin);
  53. int __fastcall CancelAnswer(int Answers);
  54. int __fastcall AbortAnswer(int Answers);
  55. AnsiString __fastcall LoadStr(int Ident, unsigned int MaxLength);
  56. AnsiString __fastcall LoadStrPart(int Ident, int Part);
  57. struct TPasLibModule;
  58. TPasLibModule * __fastcall FindModule(void * Instance);
  59. //---------------------------------------------------------------------------
  60. typedef void __fastcall (__closure* TProcessLocalFileEvent)
  61. (const AnsiString FileName, const TSearchRec Rec, void * Param);
  62. bool __fastcall FileSearchRec(const AnsiString FileName, TSearchRec & Rec);
  63. void __fastcall ProcessLocalDirectory(AnsiString DirName,
  64. TProcessLocalFileEvent CallBackFunc, void * Param = NULL, int FindAttrs = -1);
  65. //---------------------------------------------------------------------------
  66. TDateTime __fastcall UnixToDateTime(__int64 TimeStamp, bool ConsiderDST);
  67. FILETIME __fastcall DateTimeToFileTime(const TDateTime DateTime, bool ConsiderDST);
  68. TDateTime __fastcall AdjustDateTimeFromUnix(TDateTime DateTime, bool ConsiderDST);
  69. void __fastcall UnifyDateTimePrecision(TDateTime & DateTime1, TDateTime & DateTime2);
  70. __int64 __fastcall ConvertTimestampToUnix(const FILETIME & FileTime,
  71. bool ConsiderDST);
  72. AnsiString __fastcall FixedLenDateTimeFormat(const AnsiString & Format);
  73. int __fastcall CompareFileTime(TDateTime T1, TDateTime T2);
  74. //---------------------------------------------------------------------------
  75. class TCriticalSection
  76. {
  77. public:
  78. __fastcall TCriticalSection();
  79. __fastcall ~TCriticalSection();
  80. void __fastcall Enter();
  81. void __fastcall Leave();
  82. __property int Acquired = { read = FAcquired };
  83. private:
  84. TRTLCriticalSection FSection;
  85. int FAcquired;
  86. };
  87. //---------------------------------------------------------------------------
  88. class TGuard
  89. {
  90. public:
  91. __fastcall TGuard(TCriticalSection * ACriticalSection);
  92. __fastcall ~TGuard();
  93. private:
  94. TCriticalSection * FCriticalSection;
  95. };
  96. //---------------------------------------------------------------------------
  97. class TUnguard
  98. {
  99. public:
  100. __fastcall TUnguard(TCriticalSection * ACriticalSection);
  101. __fastcall ~TUnguard();
  102. private:
  103. TCriticalSection * FCriticalSection;
  104. };
  105. //---------------------------------------------------------------------------
  106. // C++B TLibModule is invalid (differs from PAS definition)
  107. struct TPasLibModule
  108. {
  109. TPasLibModule * Next;
  110. void * Instance;
  111. void * CodeInstance;
  112. void * DataInstance;
  113. void * ResInstance;
  114. };
  115. //---------------------------------------------------------------------------
  116. #ifdef _DEBUG
  117. #define TRACEENV "WINSCPTRACE"
  118. void __fastcall Trace(const AnsiString SourceFile, const AnsiString Func,
  119. int Line, const AnsiString Message);
  120. #define TRACE(MESSAGE) Trace(__FILE__, __FUNC__, __LINE__, MESSAGE)
  121. #define TRACEFMT(MESSAGE, PARAMS) Trace(__FILE__, __FUNC__, __LINE__, FORMAT(MESSAGE, PARAMS))
  122. class Callstack
  123. {
  124. public:
  125. inline Callstack(const char * File, const char * Func, unsigned int Line, AnsiString Message) :
  126. FFile(File), FFunc(Func), FLine(Line), FMessage(Message)
  127. {
  128. Trace(FFile, FFunc, FLine, AnsiString("Entry: ") + FMessage);
  129. }
  130. inline ~Callstack()
  131. {
  132. Trace(FFile, FFunc, FLine, AnsiString("Exit ") + FMessage);
  133. }
  134. private:
  135. const char * FFile;
  136. const char * FFunc;
  137. unsigned int FLine;
  138. AnsiString FMessage;
  139. };
  140. #define CALLSTACKIMPL(X) Callstack X(__FILE__, __FUNC__, __LINE__, "")
  141. #else // ifdef _DEBUG
  142. #define TRACE(PARAMS)
  143. #define TRACEFMT(MESSAGE, PARAMS)
  144. #define CALLSTACKIMPL(X)
  145. #endif // ifdef _DEBUG
  146. #define CALLSTACK CALLSTACKIMPL(__callstack)
  147. #define CALLSTACK1 CALLSTACKIMPL(__callstack1)
  148. //---------------------------------------------------------------------------
  149. #endif
  150. //---------------------------------------------------------------------------
  151. #include <assert.h>
  152. #ifndef _DEBUG
  153. #undef assert
  154. #define assert(p) ((void)0)
  155. #define CHECK(p) p
  156. #else
  157. #ifndef C_ONLY
  158. #ifndef DESIGN_ONLY
  159. #undef assert
  160. void __fastcall DoAssert(char * Message, char * Filename, int LineNumber);
  161. #define assert(p) ((p) ? (void)0 : DoAssert(#p, __FILE__, __LINE__))
  162. #endif // ifndef DESIGN_ONLY
  163. #endif // ifndef C_ONLY
  164. #define CHECK(p) { bool __CHECK_RESULT__ = (p); assert(__CHECK_RESULT__); }
  165. #endif
  166. #define USEDPARAM(p) ((p) == (p))
  167. //---------------------------------------------------------------------------
  168. #endif