Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1276 lines
38 KiB

  1. // Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1993 Microsoft Corporation,
  3. // All rights reserved.
  4. // This source code is only intended as a supplement to the
  5. // Microsoft Foundation Classes Reference and Microsoft
  6. // QuickHelp and/or WinHelp documentation provided with the library.
  7. // See these sources for detailed information regarding the
  8. // Microsoft Foundation Classes product.
  9. #ifndef __AFX_H__
  10. #define __AFX_H__
  11. #ifndef __cplusplus
  12. #error Microsoft Foundation Classes require C++ compilation (use a .cpp suffix)
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. #include <afxver_.h> // Target version control
  16. /////////////////////////////////////////////////////////////////////////////
  17. // Classes declared in this file
  18. // in addition to standard primitive data types and various helper macros
  19. struct CRuntimeClass; // object type information
  20. class CObject; // the root of all objects classes
  21. class CException; // the root of all exceptions
  22. class CMemoryException; // out-of-memory exception
  23. class CNotSupportedException; // feature not supported exception
  24. class CArchiveException;// archive exception
  25. class CFileException; // file exception
  26. class CFile; // raw binary file
  27. class CStdioFile; // buffered stdio text/binary file
  28. class CMemFile; // memory based file
  29. // Non CObject classes
  30. class CString; // growable string type
  31. class CTimeSpan; // time/date difference
  32. class CTime; // absolute time/date
  33. struct CFileStatus; // file status information
  34. struct CMemoryState; // diagnostic memory support
  35. class CArchive; // object persistence tool
  36. class CDumpContext; // object diagnostic dumping
  37. /////////////////////////////////////////////////////////////////////////////
  38. // Other includes from standard "C" runtimes
  39. #ifndef NOSTRICT
  40. #define STRICT // default is to use STRICT interfaces
  41. #endif
  42. #include <string.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <time.h>
  46. /////////////////////////////////////////////////////////////////////////////
  47. // Basic types (from Windows)
  48. typedef unsigned char BYTE; // 8-bit unsigned entity
  49. typedef unsigned short WORD; // 16-bit unsigned number
  50. typedef unsigned int UINT; // machine sized unsigned number (preferred)
  51. typedef long LONG; // 32-bit signed number
  52. typedef unsigned long DWORD; // 32-bit unsigned number
  53. typedef int BOOL; // BOOLean (0 or !=0)
  54. typedef char FAR* LPSTR; // far pointer to a string
  55. typedef const char FAR* LPCSTR; // far pointer to a read-only string
  56. typedef void* POSITION; // abstract iteration position
  57. // Standard constants
  58. #undef FALSE
  59. #undef TRUE
  60. #undef NULL
  61. #define FALSE 0
  62. #define TRUE 1
  63. #define NULL 0
  64. /////////////////////////////////////////////////////////////////////////////
  65. // Diagnostic support
  66. #ifdef _DEBUG
  67. extern "C"
  68. {
  69. void CDECL AfxTrace(LPCSTR pszFormat, ...);
  70. void AFXAPI AfxAssertFailedLine(LPCSTR lpszFileName, int nLine);
  71. void AFXAPI AfxAssertValidObject(const CObject* pOb,
  72. LPCSTR lpszFileName, int nLine);
  73. void AFXAPI AfxDump(const CObject* pOb); // Dump an object from CodeView
  74. }
  75. #define TRACE ::AfxTrace
  76. #define THIS_FILE __FILE__
  77. #define ASSERT(f) ((f) ? (void)0 : \
  78. ::AfxAssertFailedLine(THIS_FILE, __LINE__))
  79. #define VERIFY(f) ASSERT(f)
  80. #define ASSERT_VALID(pOb) (::AfxAssertValidObject(pOb, THIS_FILE, __LINE__))
  81. // The following trace macros put the trace string in a code segment
  82. // so that it will not impact DGROUP
  83. #define TRACE0(sz) \
  84. do { \
  85. static char BASED_DEBUG _sz[] = sz; \
  86. ::AfxTrace(_sz); \
  87. } while (0)
  88. #define TRACE1(sz, p1) \
  89. do { \
  90. static char BASED_DEBUG _sz[] = sz; \
  91. ::AfxTrace(_sz, p1); \
  92. } while (0)
  93. #define TRACE2(sz, p1, p2) \
  94. do { \
  95. static char BASED_DEBUG _sz[] = sz; \
  96. ::AfxTrace(_sz, p1, p2); \
  97. } while (0)
  98. #define TRACE3(sz, p1, p2, p3) \
  99. do { \
  100. static char BASED_DEBUG _sz[] = sz; \
  101. ::AfxTrace(_sz, p1, p2, p3); \
  102. } while (0)
  103. // Use this in Dump to put the string literals in a code segment and
  104. // out of DGROUP.
  105. #define AFX_DUMP0(dc, sz) \
  106. do { \
  107. static char BASED_DEBUG _sz[] = sz; \
  108. dc << _sz; \
  109. } while (0)
  110. #define AFX_DUMP1(dc, sz, p1) \
  111. do { \
  112. static char BASED_DEBUG _sz[] = sz; \
  113. dc << _sz << p1; \
  114. } while (0)
  115. #else
  116. #define ASSERT(f) ((void)0)
  117. #define VERIFY(f) ((void)(f))
  118. #define ASSERT_VALID(pOb) ((void)0)
  119. inline void CDECL AfxTrace(LPCSTR /* pszFormat */, ...) { }
  120. #define TRACE 1 ? (void)0 : ::AfxTrace
  121. #define TRACE0 1 ? (void)0 : ::AfxTrace
  122. #define TRACE1 1 ? (void)0 : ::AfxTrace
  123. #define TRACE2 1 ? (void)0 : ::AfxTrace
  124. #define TRACE3 1 ? (void)0 : ::AfxTrace
  125. #endif // _DEBUG
  126. /////////////////////////////////////////////////////////////////////////////
  127. // Turn off warnings for /W4
  128. // To resume any of these warning: #pragma warning(default: 4xxx)
  129. // which should be placed after the AFX include files
  130. #ifndef ALL_WARNINGS
  131. #pragma warning(disable: 4001) // nameless unions are part of C++
  132. #pragma warning(disable: 4061) // allow enums in switch with default
  133. #pragma warning(disable: 4127) // constant expression for TRACE/ASSERT
  134. #pragma warning(disable: 4134) // message map member fxn casts
  135. #pragma warning(disable: 4505) // optimize away locals
  136. #pragma warning(disable: 4510) // default constructors are bad to have
  137. #pragma warning(disable: 4511) // private copy constructors are good to have
  138. #pragma warning(disable: 4512) // private operator= are good to have
  139. #ifdef STRICT
  140. #pragma warning(disable: 4305) // STRICT handles are near*, integer truncation
  141. #endif
  142. #if (_MSC_VER >= 800)
  143. // turn off code generator warnings for information lost in normal optimizations
  144. #pragma warning(disable: 4705) // TRACE turned into statement with no effect
  145. #pragma warning(disable: 4710) // private constructors are disallowed
  146. #pragma warning(disable: 4791) // loss of debugging info in retail version
  147. #endif
  148. #endif //ALL_WARNINGS
  149. /////////////////////////////////////////////////////////////////////////////
  150. // Other implementation helpers
  151. #define BEFORE_START_POSITION ((void*)-1L)
  152. #define _AFX_FP_OFF(thing) (*((UINT*)&(thing)))
  153. #define _AFX_FP_SEG(lp) (*((UINT*)&(lp)+1))
  154. /////////////////////////////////////////////////////////////////////////////
  155. // Explicit extern for version API/Windows 3.0 loader problem
  156. #ifdef _WINDOWS
  157. extern "C" int AFXAPI _export _afx_version();
  158. #else
  159. extern "C" int AFXAPI _afx_version();
  160. #endif
  161. /////////////////////////////////////////////////////////////////////////////
  162. // Basic object model
  163. struct CRuntimeClass
  164. {
  165. // Attributes
  166. LPCSTR m_lpszClassName;
  167. int m_nObjectSize;
  168. UINT m_wSchema; // schema number of the loaded class
  169. void (PASCAL* m_pfnConstruct)(void* p); // NULL => abstract class
  170. CRuntimeClass* m_pBaseClass;
  171. // Operations
  172. CObject* CreateObject();
  173. // Implementation
  174. BOOL ConstructObject(void* pThis);
  175. void Store(CArchive& ar);
  176. static CRuntimeClass* PASCAL Load(CArchive& ar, UINT* pwSchemaNum);
  177. // CRuntimeClass objects linked together in simple list
  178. static CRuntimeClass* AFXAPI_DATA pFirstClass; // start of class list
  179. CRuntimeClass* m_pNextClass; // linked list of registered classes
  180. };
  181. /////////////////////////////////////////////////////////////////////////////
  182. // class CObject is the root of all compliant objects
  183. #if defined(_M_I86MM) && !defined(_PORTABLE)
  184. // force vtables to be in far code segments for medium model
  185. class FAR CObjectRoot
  186. {
  187. protected:
  188. virtual CRuntimeClass* GetRuntimeClass() NEAR const = 0;
  189. };
  190. #pragma warning(disable: 4149) // don't warn for medium model change
  191. class NEAR CObject : public CObjectRoot
  192. #else
  193. class CObject
  194. #endif
  195. {
  196. public:
  197. // Object model (types, destruction, allocation)
  198. virtual CRuntimeClass* GetRuntimeClass() const;
  199. virtual ~CObject(); // virtual destructors are necessary
  200. // Diagnostic allocations
  201. void* operator new(size_t, void* p);
  202. void* operator new(size_t nSize);
  203. void operator delete(void* p);
  204. #ifdef _DEBUG
  205. // for file name/line number tracking using DEBUG_NEW
  206. void* operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
  207. #endif
  208. // Disable the copy constructor and assignment by default so you will get
  209. // compiler errors instead of unexpected behaviour if you pass objects
  210. // by value or assign objects.
  211. protected:
  212. CObject();
  213. private:
  214. CObject(const CObject& objectSrc); // no implementation
  215. void operator=(const CObject& objectSrc); // no implementation
  216. // Attributes
  217. public:
  218. BOOL IsSerializable() const;
  219. BOOL IsKindOf(const CRuntimeClass* pClass) const;
  220. // Overridables
  221. virtual void Serialize(CArchive& ar);
  222. // Diagnostic Support
  223. virtual void AssertValid() const;
  224. virtual void Dump(CDumpContext& dc) const;
  225. // Implementation
  226. public:
  227. static CRuntimeClass AFXAPI_DATA classCObject;
  228. };
  229. #if defined(_M_I86MM)
  230. #pragma warning(default: 4149) // base class now ambient
  231. #endif
  232. // CObject::Serialize is always inline to avoid duplicate definitions
  233. inline void CObject::Serialize(CArchive&)
  234. { /* CObject does not serialize anything by default */ }
  235. // Helper macros
  236. #define RUNTIME_CLASS(class_name) \
  237. (&class_name::class##class_name)
  238. //////////////////////////////////////////////////////////////////////////////
  239. // Helper macros for declaring compliant classes
  240. // AFXAPP_DATA will be NEAR for app classes but AFXAPI_DATA for library classes
  241. #define AFXAPP_DATA AFXAPI_DATA
  242. #define DECLARE_DYNAMIC(class_name) \
  243. public: \
  244. static CRuntimeClass AFXAPP_DATA class##class_name; \
  245. virtual CRuntimeClass* GetRuntimeClass() const;
  246. // not serializable, but dynamically constructable
  247. #define DECLARE_DYNCREATE(class_name) \
  248. DECLARE_DYNAMIC(class_name) \
  249. static void PASCAL Construct(void* p);
  250. #define DECLARE_SERIAL(class_name) \
  251. DECLARE_DYNCREATE(class_name) \
  252. friend CArchive& AFXAPI operator>>(CArchive& ar, class_name* &pOb);
  253. // generate static object constructor for class registration
  254. #ifdef AFX_CLASS_MODEL
  255. struct NEAR AFX_CLASSINIT
  256. #else
  257. struct AFX_CLASSINIT
  258. #endif
  259. { AFX_CLASSINIT(CRuntimeClass* pNewClass); };
  260. #define _IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, wSchema, pfnNew) \
  261. static char BASED_CODE _lpsz##class_name[] = #class_name; \
  262. CRuntimeClass AFXAPP_DATA class_name::class##class_name = { \
  263. _lpsz##class_name, sizeof(class_name), wSchema, pfnNew, \
  264. RUNTIME_CLASS(base_class_name), NULL }; \
  265. static AFX_CLASSINIT _init_##class_name(&class_name::class##class_name); \
  266. CRuntimeClass* class_name::GetRuntimeClass() const \
  267. { return &class_name::class##class_name; } \
  268. // end of _IMPLEMENT_RUNTIMECLASS
  269. #define IMPLEMENT_DYNAMIC(class_name, base_class_name) \
  270. _IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, NULL)
  271. #define IMPLEMENT_DYNCREATE(class_name, base_class_name) \
  272. void PASCAL class_name::Construct(void* p) \
  273. { new(p) class_name; } \
  274. _IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, \
  275. class_name::Construct)
  276. #define IMPLEMENT_SERIAL(class_name, base_class_name, wSchema) \
  277. void PASCAL class_name::Construct(void* p) \
  278. { new(p) class_name; } \
  279. _IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, wSchema, \
  280. class_name::Construct) \
  281. CArchive& AFXAPI operator>>(CArchive& ar, class_name* &pOb) \
  282. { pOb = (class_name*) ar.ReadObject(RUNTIME_CLASS(class_name)); \
  283. return ar; } \
  284. // end of IMPLEMENT_SERIAL
  285. /////////////////////////////////////////////////////////////////////////////
  286. // other helpers
  287. // zero fill everything after the VTable pointer
  288. #define AFX_ZERO_INIT_OBJECT(base_class) \
  289. memset(((base_class*)this)+1, 0, sizeof(*this) - sizeof(base_class));
  290. // Windows compatible setjmp for C++
  291. #ifndef _AFX_JBLEN
  292. // use default Window C++ calling convention
  293. #define _AFX_JBLEN 9
  294. extern "C" int FAR PASCAL Catch(int FAR*);
  295. #endif
  296. /////////////////////////////////////////////////////////////////////////////
  297. // Exceptions
  298. class CException : public CObject
  299. {
  300. // abstract class for dynamic type checking
  301. DECLARE_DYNAMIC(CException)
  302. };
  303. // out-of-line routines for smaller code
  304. BOOL AFXAPI AfxCatchProc(CRuntimeClass* pClass);
  305. void AFXAPI AfxThrow(CException* pException, BOOL bShared);
  306. void AFXAPI AfxThrowLast();
  307. void AFXAPI AfxTryCleanupProc();
  308. // Placed on frame for EXCEPTION linkage
  309. struct AFX_STACK_DATA AFX_EXCEPTION_LINK
  310. {
  311. AFX_EXCEPTION_LINK* m_pLinkPrev; // previous top, next in handler chain
  312. CException* m_pException; // current exception (NULL in TRY block)
  313. BOOL m_bAutoDelete; // m_pException is "auto-delete", if TRUE
  314. UINT m_nType; // 0 for setjmp, !=0 for user extension
  315. union
  316. {
  317. int m_jumpBuf[_AFX_JBLEN]; // arg for Catch/Throw (nType = 0)
  318. struct
  319. {
  320. void (PASCAL* pfnCleanup)(AFX_EXCEPTION_LINK* pLink);
  321. void* pvData; // extra data follows
  322. } m_callback; // callback for cleanup (nType != 0)
  323. };
  324. AFX_EXCEPTION_LINK(); // for initialization and linking
  325. ~AFX_EXCEPTION_LINK() // for cleanup and unlinking
  326. { AfxTryCleanupProc(); };
  327. };
  328. // Exception global state - never access directly
  329. struct AFX_EXCEPTION_CONTEXT
  330. {
  331. AFX_EXCEPTION_LINK* m_pLinkTop;
  332. // Note: most of the exception context is now in the AFX_EXCEPTION_LINK
  333. };
  334. void AFXAPI AfxAbort();
  335. // Obsolete and non-portable: setting terminate handler
  336. // use CWinApp::ProcessWndProcException for Windows apps instead
  337. void AFXAPI AfxTerminate();
  338. #ifndef _AFXDLL
  339. typedef void (AFXAPI* AFX_TERM_PROC)();
  340. AFX_TERM_PROC AFXAPI AfxSetTerminate(AFX_TERM_PROC);
  341. #endif //!_AFXDLL
  342. /////////////////////////////////////////////////////////////////////////////
  343. // Exception helper macros
  344. #define TRY \
  345. { AFX_EXCEPTION_LINK _afxExceptionLink; \
  346. if (::Catch(_afxExceptionLink.m_jumpBuf) == 0)
  347. #define CATCH(class, e) \
  348. else if (::AfxCatchProc(RUNTIME_CLASS(class))) \
  349. { class* e = (class*)_afxExceptionLink.m_pException;
  350. #define AND_CATCH(class, e) \
  351. } else if (::AfxCatchProc(RUNTIME_CLASS(class))) \
  352. { class* e = (class*)_afxExceptionLink.m_pException;
  353. #define END_CATCH \
  354. } else { ::AfxThrowLast(); } }
  355. #define THROW(e) ::AfxThrow(e, FALSE)
  356. #define THROW_LAST() ::AfxThrowLast()
  357. // Advanced macros for smaller code
  358. #define CATCH_ALL(e) \
  359. else { CException* e = _afxExceptionLink.m_pException;
  360. #define AND_CATCH_ALL(e) \
  361. } else { CException* e = _afxExceptionLink.m_pException;
  362. #define END_CATCH_ALL } }
  363. #define END_TRY }
  364. /////////////////////////////////////////////////////////////////////////////
  365. // Standard Exception classes
  366. class CMemoryException : public CException
  367. {
  368. DECLARE_DYNAMIC(CMemoryException)
  369. public:
  370. CMemoryException();
  371. };
  372. class CNotSupportedException : public CException
  373. {
  374. DECLARE_DYNAMIC(CNotSupportedException)
  375. public:
  376. CNotSupportedException();
  377. };
  378. class CArchiveException : public CException
  379. {
  380. DECLARE_DYNAMIC(CArchiveException)
  381. public:
  382. enum {
  383. none,
  384. generic,
  385. readOnly,
  386. endOfFile,
  387. writeOnly,
  388. badIndex,
  389. badClass,
  390. badSchema
  391. };
  392. // Constructor
  393. CArchiveException(int cause = CArchiveException::none);
  394. // Attributes
  395. int m_cause;
  396. #ifdef _DEBUG
  397. virtual void Dump(CDumpContext& dc) const;
  398. #endif
  399. };
  400. class CFileException : public CException
  401. {
  402. DECLARE_DYNAMIC(CFileException)
  403. public:
  404. enum {
  405. none,
  406. generic,
  407. fileNotFound,
  408. badPath,
  409. tooManyOpenFiles,
  410. accessDenied,
  411. invalidFile,
  412. removeCurrentDir,
  413. directoryFull,
  414. badSeek,
  415. hardIO,
  416. sharingViolation,
  417. lockViolation,
  418. diskFull,
  419. endOfFile
  420. };
  421. // Constructors
  422. CFileException(int cause = CFileException::none, LONG lOsError = -1);
  423. // Attributes
  424. int m_cause;
  425. LONG m_lOsError;
  426. // Operations
  427. // convert a OS dependent error code to a Cause
  428. static int PASCAL OsErrorToException(LONG lOsError);
  429. static int PASCAL ErrnoToException(int nErrno);
  430. // helper functions to throw exception after converting to a Cause
  431. static void PASCAL ThrowOsError(LONG lOsError);
  432. static void PASCAL ThrowErrno(int nErrno);
  433. #ifdef _DEBUG
  434. virtual void Dump(CDumpContext&) const;
  435. #endif
  436. };
  437. /////////////////////////////////////////////////////////////////////////////
  438. // Standard exception throws
  439. void AFXAPI AfxThrowMemoryException();
  440. void AFXAPI AfxThrowNotSupportedException();
  441. void AFXAPI AfxThrowArchiveException(int cause);
  442. void AFXAPI AfxThrowFileException(int cause, LONG lOsError = -1);
  443. /////////////////////////////////////////////////////////////////////////////
  444. // File - raw unbuffered disk file I/O
  445. class CFile : public CObject
  446. {
  447. DECLARE_DYNAMIC(CFile)
  448. public:
  449. // Flag values
  450. enum OpenFlags {
  451. modeRead = 0x0000,
  452. modeWrite = 0x0001,
  453. modeReadWrite = 0x0002,
  454. shareCompat = 0x0000,
  455. shareExclusive = 0x0010,
  456. shareDenyWrite = 0x0020,
  457. shareDenyRead = 0x0030,
  458. shareDenyNone = 0x0040,
  459. modeNoInherit = 0x0080,
  460. modeCreate = 0x1000,
  461. typeText = 0x4000, // typeText and typeBinary are used in
  462. typeBinary = (int)0x8000 // derived classes only
  463. };
  464. enum Attribute {
  465. normal = 0x00,
  466. readOnly = 0x01,
  467. hidden = 0x02,
  468. system = 0x04,
  469. volume = 0x08,
  470. directory = 0x10,
  471. archive = 0x20
  472. };
  473. enum SeekPosition { begin = 0x0, current = 0x1, end = 0x2 };
  474. enum {hFileNull = -1};
  475. // Constructors
  476. CFile();
  477. CFile(int hFile);
  478. CFile(const char* pszFileName, UINT nOpenFlags);
  479. // Attributes
  480. UINT m_hFile;
  481. virtual DWORD GetPosition() const;
  482. BOOL GetStatus(CFileStatus& rStatus) const;
  483. // Operations
  484. virtual BOOL Open(const char* pszFileName, UINT nOpenFlags,
  485. CFileException* pError = NULL);
  486. static void PASCAL Rename(const char* pszOldName,
  487. const char* pszNewName);
  488. static void PASCAL Remove(const char* pszFileName);
  489. static BOOL PASCAL GetStatus(const char* pszFileName,
  490. CFileStatus& rStatus);
  491. static void PASCAL SetStatus(const char* pszFileName,
  492. const CFileStatus& status);
  493. DWORD SeekToEnd();
  494. void SeekToBegin();
  495. // Helpers for > 32K read/write operations. Use for any CFile derived class.
  496. DWORD ReadHuge(void FAR* lpBuffer, DWORD dwCount);
  497. void WriteHuge(const void FAR* lpBuffer, DWORD dwCount);
  498. // Overridables
  499. virtual CFile* Duplicate() const;
  500. virtual LONG Seek(LONG lOff, UINT nFrom);
  501. virtual void SetLength(DWORD dwNewLen);
  502. virtual DWORD GetLength() const;
  503. virtual UINT Read(void FAR* lpBuf, UINT nCount);
  504. virtual void Write(const void FAR* lpBuf, UINT nCount);
  505. virtual void LockRange(DWORD dwPos, DWORD dwCount);
  506. virtual void UnlockRange(DWORD dwPos, DWORD dwCount);
  507. virtual void Abort();
  508. virtual void Flush();
  509. virtual void Close();
  510. // Implementation
  511. public:
  512. virtual ~CFile();
  513. #ifdef _DEBUG
  514. virtual void AssertValid() const;
  515. virtual void Dump(CDumpContext& dc) const;
  516. #endif
  517. protected:
  518. BOOL m_bCloseOnDelete;
  519. };
  520. /////////////////////////////////////////////////////////////////////////////
  521. // STDIO file implementation
  522. class CStdioFile : public CFile
  523. {
  524. DECLARE_DYNAMIC(CStdioFile)
  525. public:
  526. // Constructors
  527. CStdioFile();
  528. CStdioFile(FILE* pOpenStream);
  529. CStdioFile(const char* pszFileName, UINT nOpenFlags);
  530. // Attributes
  531. FILE* m_pStream; // stdio FILE
  532. // m_hFile from base class is _fileno(m_pStream)
  533. // Operations
  534. virtual void WriteString(LPCSTR lpsz);
  535. // write a string, like "C" fputs
  536. virtual LPSTR ReadString(LPSTR lpsz, UINT nMax);
  537. // like "C" fgets
  538. // Implementation
  539. public:
  540. virtual ~CStdioFile();
  541. #ifdef _DEBUG
  542. void Dump(CDumpContext& dc) const;
  543. #endif
  544. virtual DWORD GetPosition() const;
  545. virtual BOOL Open(const char* pszFileName, UINT nOpenFlags,
  546. CFileException* pError = NULL);
  547. virtual UINT Read(void FAR* lpBuf, UINT nCount);
  548. virtual void Write(const void FAR* lpBuf, UINT nCount);
  549. virtual LONG Seek(LONG lOff, UINT nFrom);
  550. virtual void Abort();
  551. virtual void Flush();
  552. virtual void Close();
  553. // Unsupported APIs
  554. virtual CFile* Duplicate() const;
  555. virtual void LockRange(DWORD dwPos, DWORD dwCount);
  556. virtual void UnlockRange(DWORD dwPos, DWORD dwCount);
  557. };
  558. ////////////////////////////////////////////////////////////////////////////
  559. // Memory based file implementation
  560. class CMemFile : public CFile
  561. {
  562. DECLARE_DYNAMIC(CMemFile)
  563. public:
  564. // Constructors
  565. CMemFile(UINT nGrowBytes = 1024);
  566. // Advanced Overridables
  567. protected:
  568. virtual BYTE FAR* Alloc(DWORD nBytes);
  569. virtual BYTE FAR* Realloc(BYTE FAR* lpMem, DWORD nBytes);
  570. virtual BYTE FAR* Memcpy(BYTE FAR* lpMemTarget, const BYTE FAR* lpMemSource, UINT nBytes);
  571. virtual void Free(BYTE FAR* lpMem);
  572. virtual void GrowFile(DWORD dwNewLen);
  573. // Implementation
  574. protected:
  575. UINT m_nGrowBytes;
  576. DWORD m_nPosition;
  577. DWORD m_nBufferSize;
  578. DWORD m_nFileSize;
  579. BYTE FAR* m_lpBuffer;
  580. public:
  581. virtual ~CMemFile();
  582. #ifdef _DEBUG
  583. virtual void Dump(CDumpContext& dc) const;
  584. virtual void AssertValid() const;
  585. #endif
  586. virtual DWORD GetPosition() const;
  587. BOOL GetStatus(CFileStatus& rStatus) const;
  588. virtual LONG Seek(LONG lOff, UINT nFrom);
  589. virtual void SetLength(DWORD dwNewLen);
  590. virtual UINT Read(void FAR* lpBuf, UINT nCount);
  591. virtual void Write(const void FAR* lpBuf, UINT nCount);
  592. virtual void Abort();
  593. virtual void Flush();
  594. virtual void Close();
  595. // Unsupported APIs
  596. virtual CFile* Duplicate() const;
  597. virtual void LockRange(DWORD dwPos, DWORD dwCount);
  598. virtual void UnlockRange(DWORD dwPos, DWORD dwCount);
  599. };
  600. /////////////////////////////////////////////////////////////////////////////
  601. // Strings
  602. typedef char FAR* BSTR; // must match typedef in dispatch.h
  603. class CString
  604. {
  605. public:
  606. // Constructors
  607. CString();
  608. CString(const CString& stringSrc);
  609. CString(char ch, int nRepeat = 1);
  610. CString(const char* psz);
  611. CString(const char* pch, int nLength);
  612. #ifdef _NEARDATA
  613. // Additional versions for far string data
  614. CString(LPCSTR lpsz);
  615. CString(LPCSTR lpch, int nLength);
  616. #endif
  617. ~CString();
  618. // Attributes & Operations
  619. // as an array of characters
  620. int GetLength() const;
  621. BOOL IsEmpty() const;
  622. void Empty(); // free up the data
  623. char GetAt(int nIndex) const; // 0 based
  624. char operator[](int nIndex) const; // same as GetAt
  625. void SetAt(int nIndex, char ch);
  626. operator const char*() const; // as a C string
  627. // overloaded assignment
  628. const CString& operator=(const CString& stringSrc);
  629. const CString& operator=(char ch);
  630. const CString& operator=(const char* psz);
  631. // string concatenation
  632. const CString& operator+=(const CString& string);
  633. const CString& operator+=(char ch);
  634. const CString& operator+=(const char* psz);
  635. friend CString AFXAPI operator+(const CString& string1,
  636. const CString& string2);
  637. friend CString AFXAPI operator+(const CString& string, char ch);
  638. friend CString AFXAPI operator+(char ch, const CString& string);
  639. friend CString AFXAPI operator+(const CString& string, const char* psz);
  640. friend CString AFXAPI operator+(const char* psz, const CString& string);
  641. // string comparison
  642. int Compare(const char* psz) const; // straight character
  643. int CompareNoCase(const char* psz) const; // ignore case
  644. int Collate(const char* psz) const; // NLS aware
  645. // simple sub-string extraction
  646. CString Mid(int nFirst, int nCount) const;
  647. CString Mid(int nFirst) const;
  648. CString Left(int nCount) const;
  649. CString Right(int nCount) const;
  650. CString SpanIncluding(const char* pszCharSet) const;
  651. CString SpanExcluding(const char* pszCharSet) const;
  652. // upper/lower/reverse conversion
  653. void MakeUpper();
  654. void MakeLower();
  655. void MakeReverse();
  656. // searching (return starting index, or -1 if not found)
  657. // look for a single character match
  658. int Find(char ch) const; // like "C" strchr
  659. int ReverseFind(char ch) const;
  660. int FindOneOf(const char* pszCharSet) const;
  661. // look for a specific sub-string
  662. int Find(const char* pszSub) const; // like "C" strstr
  663. // input and output
  664. #ifdef _DEBUG
  665. friend CDumpContext& AFXAPI operator<<(CDumpContext& dc,
  666. const CString& string);
  667. #endif
  668. friend CArchive& AFXAPI operator<<(CArchive& ar, const CString& string);
  669. friend CArchive& AFXAPI operator>>(CArchive& ar, CString& string);
  670. // Windows support
  671. #ifdef _WINDOWS
  672. BOOL LoadString(UINT nID); // load from string resource
  673. // 255 chars max
  674. // ANSI<->OEM support (convert string in place)
  675. void AnsiToOem();
  676. void OemToAnsi();
  677. // OLE 2.0 BSTR support (use for OLE automation)
  678. BSTR AllocSysString();
  679. BSTR SetSysString(BSTR FAR* pbstr);
  680. #endif //_WINDOWS
  681. // Access to string implementation buffer as "C" character array
  682. char* GetBuffer(int nMinBufLength);
  683. void ReleaseBuffer(int nNewLength = -1);
  684. char* GetBufferSetLength(int nNewLength);
  685. // Implementation
  686. public:
  687. int GetAllocLength() const;
  688. protected:
  689. // lengths/sizes in characters
  690. // (note: an extra character is always allocated)
  691. char* m_pchData; // actual string (zero terminated)
  692. int m_nDataLength; // does not include terminating 0
  693. int m_nAllocLength; // does not include terminating 0
  694. // implementation helpers
  695. void Init();
  696. void AllocCopy(CString& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;
  697. void AllocBuffer(int nLen);
  698. void AssignCopy(int nSrcLen, const char* pszSrcData);
  699. void ConcatCopy(int nSrc1Len, const char* pszSrc1Data, int nSrc2Len, const char* pszSrc2Data);
  700. void ConcatInPlace(int nSrcLen, const char* pszSrcData);
  701. static void SafeDelete(char* pch);
  702. static int SafeStrlen(const char* psz);
  703. };
  704. // Compare helpers
  705. BOOL AFXAPI operator==(const CString& s1, const CString& s2);
  706. BOOL AFXAPI operator==(const CString& s1, const char* s2);
  707. BOOL AFXAPI operator==(const char* s1, const CString& s2);
  708. BOOL AFXAPI operator!=(const CString& s1, const CString& s2);
  709. BOOL AFXAPI operator!=(const CString& s1, const char* s2);
  710. BOOL AFXAPI operator!=(const char* s1, const CString& s2);
  711. BOOL AFXAPI operator<(const CString& s1, const CString& s2);
  712. BOOL AFXAPI operator<(const CString& s1, const char* s2);
  713. BOOL AFXAPI operator<(const char* s1, const CString& s2);
  714. BOOL AFXAPI operator>(const CString& s1, const CString& s2);
  715. BOOL AFXAPI operator>(const CString& s1, const char* s2);
  716. BOOL AFXAPI operator>(const char* s1, const CString& s2);
  717. BOOL AFXAPI operator<=(const CString& s1, const CString& s2);
  718. BOOL AFXAPI operator<=(const CString& s1, const char* s2);
  719. BOOL AFXAPI operator<=(const char* s1, const CString& s2);
  720. BOOL AFXAPI operator>=(const CString& s1, const CString& s2);
  721. BOOL AFXAPI operator>=(const CString& s1, const char* s2);
  722. BOOL AFXAPI operator>=(const char* s1, const CString& s2);
  723. /////////////////////////////////////////////////////////////////////////////
  724. // CTimeSpan and CTime
  725. class CTimeSpan
  726. {
  727. public:
  728. // Constructors
  729. CTimeSpan();
  730. CTimeSpan(time_t time);
  731. CTimeSpan(LONG lDays, int nHours, int nMins, int nSecs);
  732. CTimeSpan(const CTimeSpan& timeSpanSrc);
  733. const CTimeSpan& operator=(const CTimeSpan& timeSpanSrc);
  734. // Attributes
  735. // extract parts
  736. LONG GetDays() const; // total # of days
  737. LONG GetTotalHours() const;
  738. int GetHours() const;
  739. LONG GetTotalMinutes() const;
  740. int GetMinutes() const;
  741. LONG GetTotalSeconds() const;
  742. int GetSeconds() const;
  743. // Operations
  744. // time math
  745. CTimeSpan operator-(CTimeSpan timeSpan) const;
  746. CTimeSpan operator+(CTimeSpan timeSpan) const;
  747. const CTimeSpan& operator+=(CTimeSpan timeSpan);
  748. const CTimeSpan& operator-=(CTimeSpan timeSpan);
  749. BOOL operator==(CTimeSpan timeSpan) const;
  750. BOOL operator!=(CTimeSpan timeSpan) const;
  751. BOOL operator<(CTimeSpan timeSpan) const;
  752. BOOL operator>(CTimeSpan timeSpan) const;
  753. BOOL operator<=(CTimeSpan timeSpan) const;
  754. BOOL operator>=(CTimeSpan timeSpan) const;
  755. #if !defined(_AFXDLL) && !defined(_USRDLL)
  756. CString Format(const char* pFormat) const;
  757. #endif //! DLL variant
  758. // serialization
  759. #ifdef _DEBUG
  760. friend CDumpContext& AFXAPI operator<<(CDumpContext& dc,CTimeSpan timeSpan);
  761. #endif
  762. friend CArchive& AFXAPI operator<<(CArchive& ar, CTimeSpan timeSpan);
  763. friend CArchive& AFXAPI operator>>(CArchive& ar, CTimeSpan& rtimeSpan);
  764. private:
  765. time_t m_timeSpan;
  766. friend class CTime;
  767. };
  768. class CTime
  769. {
  770. public:
  771. // Constructors
  772. static CTime PASCAL GetCurrentTime();
  773. CTime();
  774. CTime(time_t time);
  775. CTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec);
  776. CTime(WORD wDosDate, WORD wDosTime);
  777. CTime(const CTime& timeSrc);
  778. const CTime& operator=(const CTime& timeSrc);
  779. const CTime& operator=(time_t t);
  780. // Attributes
  781. struct tm* GetGmtTm(struct tm* ptm = NULL) const;
  782. struct tm* GetLocalTm(struct tm* ptm = NULL) const;
  783. time_t GetTime() const;
  784. int GetYear() const;
  785. int GetMonth() const; // month of year (1 = Jan)
  786. int GetDay() const; // day of month
  787. int GetHour() const;
  788. int GetMinute() const;
  789. int GetSecond() const;
  790. int GetDayOfWeek() const; // 1=Sun, 2=Mon, ..., 7=Sat
  791. // Operations
  792. // time math
  793. CTimeSpan operator-(CTime time) const;
  794. CTime operator-(CTimeSpan timeSpan) const;
  795. CTime operator+(CTimeSpan timeSpan) const;
  796. const CTime& operator+=(CTimeSpan timeSpan);
  797. const CTime& operator-=(CTimeSpan timeSpan);
  798. BOOL operator==(CTime time) const;
  799. BOOL operator!=(CTime time) const;
  800. BOOL operator<(CTime time) const;
  801. BOOL operator>(CTime time) const;
  802. BOOL operator<=(CTime time) const;
  803. BOOL operator>=(CTime time) const;
  804. // formatting using "C" strftime
  805. #if !defined(_AFXDLL) && !defined(_USRDLL)
  806. CString Format(const char* pFormat) const;
  807. CString FormatGmt(const char* pFormat) const;
  808. #endif //! DLL variant
  809. // serialization
  810. #ifdef _DEBUG
  811. friend CDumpContext& AFXAPI operator<<(CDumpContext& dc, CTime time);
  812. #endif
  813. friend CArchive& AFXAPI operator<<(CArchive& ar, CTime time);
  814. friend CArchive& AFXAPI operator>>(CArchive& ar, CTime& rtime);
  815. private:
  816. time_t m_time;
  817. };
  818. /////////////////////////////////////////////////////////////////////////////
  819. // File status
  820. struct CFileStatus
  821. {
  822. CTime m_ctime; // creation date/time of file
  823. CTime m_mtime; // last modification date/time of file
  824. CTime m_atime; // last access date/time of file
  825. LONG m_size; // logical size of file in bytes
  826. BYTE m_attribute; // logical OR of CFile::Attribute enum values
  827. BYTE _m_padding; // pad the structure to a WORD
  828. char m_szFullName[_MAX_PATH]; // absolute path name
  829. #ifdef _DEBUG
  830. void Dump(CDumpContext& dc) const;
  831. #endif
  832. };
  833. /////////////////////////////////////////////////////////////////////////////
  834. // Diagnostic memory management routines
  835. // Low level sanity checks for memory blocks
  836. extern "C" BOOL AFXAPI AfxIsValidAddress(const void FAR* lp,
  837. UINT nBytes, BOOL bReadWrite = TRUE);
  838. #ifdef _NEARDATA
  839. BOOL AFXAPI AfxIsValidAddress(const void NEAR* np, UINT nBytes,
  840. BOOL bReadWrite = TRUE);
  841. #endif
  842. extern "C" BOOL AFXAPI AfxIsValidString(LPCSTR lpsz, int nLength = -1);
  843. #ifdef _NEARDATA
  844. BOOL AFXAPI AfxIsValidString(const char*psz, int nLength = -1);
  845. #endif
  846. #ifdef _DEBUG
  847. // Memory tracking allocation
  848. void* operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
  849. #define DEBUG_NEW new(THIS_FILE, __LINE__)
  850. // Return TRUE if valid memory block of nBytes
  851. BOOL AFXAPI AfxIsMemoryBlock(const void* p, UINT nBytes,
  852. LONG* plRequestNumber = NULL);
  853. // Return TRUE if memory is sane or print out what is wrong
  854. BOOL AFXAPI AfxCheckMemory();
  855. // Options for tuning the allocation diagnostics
  856. extern "C" { extern int NEAR afxMemDF; }
  857. enum AfxMemDF // memory debug/diagnostic flags
  858. {
  859. allocMemDF = 0x01, // turn on debugging allocator
  860. delayFreeMemDF = 0x02, // delay freeing memory
  861. checkAlwaysMemDF = 0x04 // AfxCheckMemory on every alloc/free
  862. };
  863. // Advanced initialization: for overriding default diagnostics
  864. extern "C" BOOL AFXAPI AfxDiagnosticInit(void);
  865. // turn on/off tracking for a short while
  866. BOOL AFXAPI AfxEnableMemoryTracking(BOOL bTrack);
  867. // Memory allocator failure simulation and control (_DEBUG only)
  868. // A failure hook returns whether to permit allocation
  869. typedef BOOL (AFXAPI* AFX_ALLOC_HOOK)(size_t nSize, BOOL bObject, LONG lRequestNumber);
  870. // Set new hook, return old (never NULL)
  871. AFX_ALLOC_HOOK AFXAPI AfxSetAllocHook(AFX_ALLOC_HOOK pfnAllocHook);
  872. #ifndef _PORTABLE
  873. // Debugger hook on specified allocation request - Obsolete
  874. void AFXAPI AfxSetAllocStop(LONG lRequestNumber);
  875. #endif
  876. struct CBlockHeader;
  877. // Memory state for snapshots/leak detection
  878. struct CMemoryState
  879. {
  880. // Attributes
  881. enum blockUsage
  882. {
  883. freeBlock, // memory not used
  884. objectBlock, // contains a CObject derived class object
  885. bitBlock, // contains ::operator new data
  886. nBlockUseMax // total number of usages
  887. };
  888. struct CBlockHeader* m_pBlockHeader;
  889. LONG m_lCounts[nBlockUseMax];
  890. LONG m_lSizes[nBlockUseMax];
  891. LONG m_lHighWaterCount;
  892. LONG m_lTotalCount;
  893. CMemoryState();
  894. // Operations
  895. void Checkpoint(); // fill with current state
  896. BOOL Difference(const CMemoryState& oldState,
  897. const CMemoryState& newState); // fill with difference
  898. // Output to afxDump
  899. void DumpStatistics() const;
  900. void DumpAllObjectsSince() const;
  901. };
  902. // Enumerate allocated objects or runtime classes
  903. void AFXAPI AfxDoForAllObjects(void (*pfn)(CObject* pObject,
  904. void* pContext), void* pContext);
  905. void AFXAPI AfxDoForAllClasses(void (*pfn)(const CRuntimeClass* pClass,
  906. void* pContext), void* pContext);
  907. #else
  908. // NonDebug version that assume everything is OK
  909. #define DEBUG_NEW new
  910. #define AfxCheckMemory() TRUE
  911. #define AfxIsMemoryBlock(p, nBytes) TRUE
  912. #endif // _DEBUG
  913. /////////////////////////////////////////////////////////////////////////////
  914. // Archives for serializing CObject data
  915. // needed for implementation
  916. class CPtrArray;
  917. class CMapPtrToWord;
  918. class CDocument;
  919. class CArchive
  920. {
  921. public:
  922. // Flag values
  923. enum Mode { store = 0, load = 1, bNoFlushOnDelete = 2 };
  924. CArchive(CFile* pFile, UINT nMode, int nBufSize = 512, void FAR* lpBuf = NULL);
  925. ~CArchive();
  926. // Attributes
  927. BOOL IsLoading() const;
  928. BOOL IsStoring() const;
  929. BOOL IsBufferEmpty() const;
  930. CFile* GetFile() const;
  931. CDocument* m_pDocument; // pointer to document being serialized
  932. // must set to serialize COleClientItems
  933. // in a document!
  934. // Operations
  935. UINT Read(void FAR* lpBuf, UINT nMax);
  936. void Write(const void FAR* lpBuf, UINT nMax);
  937. void Flush();
  938. void Close();
  939. void Abort();
  940. public:
  941. // Object I/O is pointer based to avoid added construction overhead.
  942. // Use the Serialize member function directly for embedded objects.
  943. friend CArchive& AFXAPI operator<<(CArchive& ar, const CObject* pOb);
  944. friend CArchive& AFXAPI operator>>(CArchive& ar, CObject*& pOb);
  945. friend CArchive& AFXAPI operator>>(CArchive& ar, const CObject*& pOb);
  946. // insertion operations
  947. // NOTE: operators available only for fixed size types for portability
  948. CArchive& operator<<(BYTE by);
  949. CArchive& operator<<(WORD w);
  950. CArchive& operator<<(LONG l);
  951. CArchive& operator<<(DWORD dw);
  952. CArchive& operator<<(float f);
  953. CArchive& operator<<(double d);
  954. // extraction operations
  955. // NOTE: operators available only for fixed size types for portability
  956. CArchive& operator>>(BYTE& by);
  957. CArchive& operator>>(WORD& w);
  958. CArchive& operator>>(DWORD& dw);
  959. CArchive& operator>>(LONG& l);
  960. CArchive& operator>>(float& f);
  961. CArchive& operator>>(double& d);
  962. CObject* ReadObject(const CRuntimeClass* pClass);
  963. void WriteObject(const CObject* pOb);
  964. // Implementation
  965. public:
  966. BOOL m_bForceFlat; // for COleClientItem implementation (default TRUE)
  967. void FillBuffer(UINT nBytesNeeded);
  968. protected:
  969. // archive objects cannot be copied or assigned
  970. CArchive(const CArchive& arSrc);
  971. void operator=(const CArchive& arSrc);
  972. BOOL m_nMode;
  973. BOOL m_bUserBuf;
  974. int m_nBufSize;
  975. CFile* m_pFile;
  976. BYTE FAR* m_lpBufCur;
  977. BYTE FAR* m_lpBufMax;
  978. BYTE FAR* m_lpBufStart;
  979. UINT m_nMapCount; // count and map used when storing
  980. union
  981. {
  982. CPtrArray* m_pLoadArray;
  983. CMapPtrToWord* m_pStoreMap;
  984. };
  985. };
  986. /////////////////////////////////////////////////////////////////////////////
  987. // Diagnostic dumping
  988. class CDumpContext
  989. {
  990. public:
  991. CDumpContext(CFile* pFile);
  992. // Attributes
  993. int GetDepth() const; // 0 => this object, 1 => children objects
  994. void SetDepth(int nNewDepth);
  995. // Operations
  996. CDumpContext& operator<<(LPCSTR lpsz);
  997. CDumpContext& operator<<(const void FAR* lp);
  998. #ifdef _NEARDATA
  999. CDumpContext& operator<<(const void NEAR* np);
  1000. #endif
  1001. CDumpContext& operator<<(const CObject* pOb);
  1002. CDumpContext& operator<<(const CObject& ob);
  1003. CDumpContext& operator<<(BYTE by);
  1004. CDumpContext& operator<<(WORD w);
  1005. CDumpContext& operator<<(UINT u);
  1006. CDumpContext& operator<<(LONG l);
  1007. CDumpContext& operator<<(DWORD dw);
  1008. CDumpContext& operator<<(float f);
  1009. CDumpContext& operator<<(double d);
  1010. CDumpContext& operator<<(int n);
  1011. void HexDump(const char* pszLine, BYTE* pby, int nBytes, int nWidth);
  1012. void Flush();
  1013. // Implementation
  1014. protected:
  1015. // dump context objects cannot be copied or assigned
  1016. CDumpContext(const CDumpContext& dcSrc);
  1017. void operator=(const CDumpContext& dcSrc);
  1018. void OutputString(LPCSTR lpsz);
  1019. int m_nDepth;
  1020. public:
  1021. CFile* m_pFile;
  1022. };
  1023. #ifdef _DEBUG
  1024. #ifdef _AFXCTL
  1025. extern CDumpContext& AFXAPP_DATA afxDump;
  1026. #else
  1027. extern CDumpContext& NEAR afxDump;
  1028. #endif
  1029. extern "C" { extern BOOL NEAR afxTraceEnabled; }
  1030. #endif
  1031. /////////////////////////////////////////////////////////////////////////////
  1032. // Inline function declarations
  1033. // macro to get ambient pointer from far pointer
  1034. // (original pointer must have been ambient)
  1035. #define _AfxGetPtrFromFarPtr(p) ((void*)(DWORD)(LPVOID)(p))
  1036. #ifdef _AFX_ENABLE_INLINES
  1037. #define _AFX_INLINE inline
  1038. #include <afx.inl>
  1039. #endif
  1040. #undef AFXAPP_DATA
  1041. #define AFXAPP_DATA NEAR
  1042. #endif // __AFX_H__