afx.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #ifndef __AFX_H__
  11. #define __AFX_H__
  12. #ifndef __cplusplus
  13. #error MFC requires C++ compilation (use a .cpp suffix)
  14. #endif
  15. /////////////////////////////////////////////////////////////////////////////
  16. #include <afxver_.h> // Target version control
  17. /////////////////////////////////////////////////////////////////////////////
  18. // Other includes from standard "C" runtimes
  19. #include <System.hpp>
  20. /////////////////////////////////////////////////////////////////////////////
  21. // Diagnostic support
  22. #define ASSERT(f) ((void)0)
  23. #define VERIFY(f) ((void)(f))
  24. #define ASSERT_VALID(pOb) ((void)0)
  25. #define TRACE0(sz)
  26. #define TRACE1(sz, p1)
  27. #define TRACE2(sz, p1, p2)
  28. #define TRACE3(sz, p1, p2, p3)
  29. /////////////////////////////////////////////////////////////////////////////
  30. // Turn off warnings for /W4
  31. // To resume any of these warning: #pragma warning(default: 4xxx)
  32. // which should be placed after the AFX include files
  33. #ifndef ALL_WARNINGS
  34. // warnings generated with common MFC/Windows code
  35. #pragma warning(disable: 4127) // constant expression for TRACE/ASSERT
  36. #pragma warning(disable: 4134) // message map member fxn casts
  37. #pragma warning(disable: 4201) // nameless unions are part of C++
  38. #pragma warning(disable: 4511) // private copy constructors are good to have
  39. #pragma warning(disable: 4512) // private operator= are good to have
  40. #pragma warning(disable: 4514) // unreferenced inlines are common
  41. #pragma warning(disable: 4710) // private constructors are disallowed
  42. #pragma warning(disable: 4705) // statement has no effect in optimized code
  43. #pragma warning(disable: 4191) // pointer-to-function casting
  44. // warnings caused by normal optimizations
  45. #pragma warning(disable: 4701) // local variable *may* be used without init
  46. #pragma warning(disable: 4702) // unreachable code caused by optimizations
  47. #pragma warning(disable: 4791) // loss of debugging info in release version
  48. #pragma warning(disable: 4189) // initialized but unused variable
  49. #pragma warning(disable: 4390) // empty controlled statement
  50. #endif //!ALL_WARNINGS
  51. #define UNUSED(x) (static_cast<const void *>(&x) == static_cast<const void *>(&x))
  52. /////////////////////////////////////////////////////////////////////////////
  53. // Basic object model
  54. /////////////////////////////////////////////////////////////////////////////
  55. // Strings
  56. class CString
  57. {
  58. public:
  59. // Constructors
  60. // constructs empty CString
  61. CString();
  62. // copy constructor
  63. CString(const CString& stringSrc);
  64. // copy constructor
  65. explicit CString(const UnicodeString& str);
  66. // from an ANSI string (converts to TCHAR)
  67. CString(LPCSTR lpsz);
  68. // from a UNICODE string (converts to TCHAR)
  69. CString(LPCWSTR lpsz);
  70. // subset of characters from an ANSI string (converts to TCHAR)
  71. CString(LPCSTR lpch, int nLength);
  72. // subset of characters from a UNICODE string (converts to TCHAR)
  73. CString(LPCWSTR lpch, int nLength);
  74. // from unsigned characters
  75. CString(const unsigned char* psz);
  76. // Attributes & Operations
  77. // get data length
  78. int GetLength() const;
  79. // TRUE if zero length
  80. BOOL IsEmpty() const;
  81. // clear contents to empty
  82. void Empty();
  83. // return single character at zero-based index
  84. TCHAR GetAt(int nIndex) const;
  85. // return single character at zero-based index
  86. TCHAR operator[](int nIndex) const;
  87. // set a single character at zero-based index
  88. void SetAt(int nIndex, TCHAR ch);
  89. // return pointer to const string
  90. operator LPCTSTR() const;
  91. // overloaded assignment
  92. // ref-counted copy from another CString
  93. const CString& operator=(const CString& stringSrc);
  94. // set string content to single character
  95. const CString& operator=(TCHAR ch);
  96. const CString& operator=(char ch);
  97. // copy string content from ANSI string (converts to TCHAR)
  98. const CString& operator=(LPCSTR lpsz);
  99. // copy string content from UNICODE string (converts to TCHAR)
  100. const CString& operator=(LPCWSTR lpsz);
  101. // copy string content from unsigned chars
  102. const CString& operator=(const unsigned char* psz);
  103. // string concatenation
  104. // concatenate from another CString
  105. const CString& operator+=(const CString& string);
  106. // concatenate a single character
  107. const CString& operator+=(TCHAR ch);
  108. // concatenate an ANSI character after converting it to TCHAR
  109. const CString& operator+=(char ch);
  110. // concatenate a UNICODE character after converting it to TCHAR
  111. const CString& operator+=(LPCTSTR lpsz);
  112. friend CString AFXAPI operator+(const CString& string1,
  113. const CString& string2);
  114. friend CString AFXAPI operator+(const CString& string, TCHAR ch);
  115. friend CString AFXAPI operator+(TCHAR ch, const CString& string);
  116. friend CString AFXAPI operator+(const CString& string, char ch);
  117. friend CString AFXAPI operator+(char ch, const CString& string);
  118. friend CString AFXAPI operator+(const CString& string, LPCTSTR lpsz);
  119. friend CString AFXAPI operator+(LPCTSTR lpsz, const CString& string);
  120. // string comparison
  121. // straight character comparison
  122. int Compare(LPCTSTR lpsz) const;
  123. // compare ignoring case
  124. int CompareNoCase(LPCTSTR lpsz) const;
  125. // simple sub-string extraction
  126. // return nCount characters starting at zero-based nFirst
  127. CString Mid(int nFirst, int nCount) const;
  128. // return all characters starting at zero-based nFirst
  129. CString Mid(int nFirst) const;
  130. // return first nCount characters in string
  131. CString Left(int nCount) const;
  132. // return nCount characters from end of string
  133. CString Right(int nCount) const;
  134. // NLS aware conversion to lowercase
  135. void MakeLower();
  136. // trimming anything (either side)
  137. // remove continuous occurrences of chTarget starting from right
  138. void TrimRight(TCHAR chTarget);
  139. // remove continuous occcurrences of characters in passed string,
  140. // starting from right
  141. void TrimRight(LPCTSTR lpszTargets);
  142. // remove continuous occurrences of chTarget starting from left
  143. void TrimLeft(TCHAR chTarget);
  144. // remove continuous occcurrences of characters in
  145. // passed string, starting from left
  146. void TrimLeft(LPCTSTR lpszTargets);
  147. // advanced manipulation
  148. // replace occurrences of chOld with chNew
  149. int Replace(TCHAR chOld, TCHAR chNew);
  150. // replace occurrences of substring lpszOld with lpszNew;
  151. // empty lpszNew removes instances of lpszOld
  152. BOOL Replace(LPCTSTR lpszOld, LPCTSTR lpszNew);
  153. // delete nCount characters starting at zero-based index
  154. int Delete(int nIndex, int nCount = 1);
  155. // searching
  156. // find character starting at left, -1 if not found
  157. int Find(TCHAR ch) const;
  158. // find character starting at right
  159. int ReverseFind(TCHAR ch) const;
  160. // find character starting at zero-based index and going right
  161. int Find(TCHAR ch, int nStart) const;
  162. // find first instance of any character in passed string
  163. int FindOneOf(LPCTSTR lpszCharSet) const;
  164. // find first instance of substring
  165. int Find(LPCTSTR lpszSub) const;
  166. // find first instance of substring starting at zero-based index
  167. int Find(LPCTSTR lpszSub, int nStart) const;
  168. // simple formatting
  169. // printf-like formatting using passed string
  170. void Format(LPCTSTR lpszFormat, ...);
  171. // printf-like formatting using referenced string resource
  172. void Format(UINT nFormatID, ...);
  173. // printf-like formatting using variable arguments parameter
  174. void FormatV(LPCTSTR lpszFormat, va_list argList);
  175. // load from string resource
  176. BOOL LoadString(UINT nID);
  177. // Implementation
  178. protected:
  179. UnicodeString m_Data;
  180. };
  181. // Compare helpers
  182. bool AFXAPI operator==(const CString& s1, const CString& s2);
  183. bool AFXAPI operator==(const CString& s1, LPCTSTR s2);
  184. bool AFXAPI operator==(LPCTSTR s1, const CString& s2);
  185. bool AFXAPI operator!=(const CString& s1, const CString& s2);
  186. bool AFXAPI operator!=(const CString& s1, LPCTSTR s2);
  187. bool AFXAPI operator!=(LPCTSTR s1, const CString& s2);
  188. bool AFXAPI operator<(const CString& s1, const CString& s2);
  189. bool AFXAPI operator<(const CString& s1, LPCTSTR s2);
  190. bool AFXAPI operator<(LPCTSTR s1, const CString& s2);
  191. /////////////////////////////////////////////////////////////////////////////
  192. // Standard Exception classes
  193. class CFileException
  194. {
  195. public:
  196. enum {
  197. none,
  198. generic,
  199. fileNotFound,
  200. badPath,
  201. tooManyOpenFiles,
  202. accessDenied,
  203. invalidFile,
  204. removeCurrentDir,
  205. directoryFull,
  206. badSeek,
  207. hardIO,
  208. sharingViolation,
  209. lockViolation,
  210. diskFull,
  211. endOfFile
  212. };
  213. // Constructor
  214. CFileException(int cause = CFileException::none, LONG lOsError = -1,
  215. LPCTSTR lpszArchiveName = NULL);
  216. // Attributes
  217. int m_cause;
  218. LONG m_lOsError;
  219. CString m_strFileName;
  220. // Operations
  221. // convert a OS dependent error code to a Cause
  222. static int OsErrorToException(LONG lOsError);
  223. // helper functions to throw exception after converting to a Cause
  224. static void ThrowOsError(LONG lOsError, LPCTSTR lpszFileName = NULL);
  225. // Implementation
  226. public:
  227. BOOL GetErrorMessage(LPTSTR lpszError, UINT nMaxError,
  228. PUINT pnHelpContext = NULL);
  229. };
  230. /////////////////////////////////////////////////////////////////////////////
  231. // Standard exception throws
  232. void AFXAPI AfxThrowFileException(int cause, LONG lOsError = -1,
  233. LPCTSTR lpszFileName = NULL);
  234. /////////////////////////////////////////////////////////////////////////////
  235. // File - raw unbuffered disk file I/O
  236. class CFile
  237. {
  238. public:
  239. // Flag values
  240. enum OpenFlags {
  241. modeRead = 0x0000,
  242. modeWrite = 0x0001,
  243. modeReadWrite = 0x0002,
  244. shareCompat = 0x0000,
  245. shareExclusive = 0x0010,
  246. shareDenyWrite = 0x0020,
  247. shareDenyRead = 0x0030,
  248. shareDenyNone = 0x0040,
  249. modeNoInherit = 0x0080,
  250. modeCreate = 0x1000,
  251. modeNoTruncate = 0x2000,
  252. typeText = 0x4000, // typeText and typeBinary are used in
  253. typeBinary = (int)0x8000 // derived classes only
  254. };
  255. enum { hFileNull = -1 };
  256. // Constructors
  257. CFile();
  258. // Attributes
  259. UINT m_hFile;
  260. // Operations
  261. BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags,
  262. CFileException* pError = NULL);
  263. static BOOL IsValid(LPCTSTR lpszFileName);
  264. // Overridables
  265. UINT Read(void* lpBuf, UINT nCount);
  266. void Write(const void* lpBuf, UINT nCount);
  267. void Close();
  268. // Implementation
  269. public:
  270. ~CFile();
  271. protected:
  272. BOOL m_bCloseOnDelete;
  273. CString m_strFileName;
  274. };
  275. /////////////////////////////////////////////////////////////////////////////
  276. // CTimeSpan and CTime
  277. class CTimeSpan
  278. {
  279. public:
  280. // Constructors
  281. CTimeSpan();
  282. CTimeSpan(time_t time);
  283. CTimeSpan(const CTimeSpan& timeSpanSrc);
  284. const CTimeSpan& operator=(const CTimeSpan& timeSpanSrc);
  285. // Attributes
  286. // extract parts
  287. LONG GetTotalSeconds() const;
  288. // Operations
  289. // time math
  290. BOOL operator==(CTimeSpan timeSpan) const;
  291. BOOL operator!=(CTimeSpan timeSpan) const;
  292. private:
  293. time_t m_timeSpan;
  294. };
  295. class CTime
  296. {
  297. public:
  298. // Constructors
  299. static CTime CreateForCurrentTime();
  300. CTime();
  301. CTime(time_t time);
  302. CTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec,
  303. int nDST = -1);
  304. CTime(const CTime& timeSrc);
  305. CTime(const SYSTEMTIME& sysTime, int nDST = -1);
  306. CTime(const FILETIME& fileTime, int nDST = -1);
  307. const CTime& operator=(const CTime& timeSrc);
  308. const CTime& operator=(time_t t);
  309. // Attributes
  310. struct tm* GetLocalTm(struct tm* ptm = NULL) const;
  311. time_t GetTime() const;
  312. int GetYear() const;
  313. int GetMonth() const; // month of year (1 = Jan)
  314. int GetDay() const; // day of month
  315. int GetHour() const;
  316. int GetMinute() const;
  317. // Operations
  318. // time math
  319. CTimeSpan operator-(CTime time) const;
  320. BOOL operator==(CTime time) const;
  321. BOOL operator!=(CTime time) const;
  322. private:
  323. time_t m_time;
  324. };
  325. /////////////////////////////////////////////////////////////////////////////
  326. // Inline function declarations
  327. #ifdef _AFX_ENABLE_INLINES
  328. #define _AFX_INLINE AFX_INLINE
  329. #include <afx.inl>
  330. #endif
  331. #ifdef WINSCP
  332. extern HINSTANCE afxCurrentResourceHandle;
  333. #endif
  334. #endif // __AFX_H__
  335. /////////////////////////////////////////////////////////////////////////////