home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK10 / MFC / INCLUDE / AFX.H$ / afx
Encoding:
Text File  |  1992-03-08  |  34.5 KB  |  1,197 lines

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