home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / include / afx.h next >
C/C++ Source or Header  |  1998-06-16  |  60KB  |  1,987 lines

  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.  
  11. #ifndef __AFX_H__
  12. #define __AFX_H__
  13.  
  14. #ifndef __cplusplus
  15.     #error MFC requires C++ compilation (use a .cpp suffix)
  16. #endif
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19.  
  20. #ifdef _AFX_MINREBUILD
  21. #pragma component(minrebuild, off)
  22. #endif
  23. #ifndef _AFX_FULLTYPEINFO
  24. #pragma component(mintypeinfo, on)
  25. #endif
  26.  
  27. #include <afxver_.h>        // Target version control
  28.  
  29. #ifndef _AFX_NOFORCE_LIBS
  30.  
  31. /////////////////////////////////////////////////////////////////////////////
  32. // Win32 libraries
  33.  
  34. #ifndef _AFXDLL
  35.     #ifndef _UNICODE
  36.         #ifdef _DEBUG
  37.             #pragma comment(lib, "nafxcwd.lib")
  38.         #else
  39.             #pragma comment(lib, "nafxcw.lib")
  40.         #endif
  41.     #else
  42.         #ifdef _DEBUG
  43.             #pragma comment(lib, "uafxcwd.lib")
  44.         #else
  45.             #pragma comment(lib, "uafxcw.lib")
  46.         #endif
  47.     #endif
  48. #else
  49.     #ifndef _UNICODE
  50.         #ifdef _DEBUG
  51.             #pragma comment(lib, "mfc42d.lib")
  52.             #pragma comment(lib, "mfcs42d.lib")
  53.         #else
  54.             #pragma comment(lib, "mfc42.lib")
  55.             #pragma comment(lib, "mfcs42.lib")
  56.         #endif
  57.     #else
  58.         #ifdef _DEBUG
  59.             #pragma comment(lib, "mfc42ud.lib")
  60.             #pragma comment(lib, "mfcs42ud.lib")
  61.         #else
  62.             #pragma comment(lib, "mfc42u.lib")
  63.             #pragma comment(lib, "mfcs42u.lib")
  64.         #endif
  65.     #endif
  66. #endif
  67.  
  68. #ifdef _DLL
  69.     #if !defined(_AFX_NO_DEBUG_CRT) && defined(_DEBUG)
  70.         #pragma comment(lib, "msvcrtd.lib")
  71.     #else
  72.         #pragma comment(lib, "msvcrt.lib")
  73.     #endif
  74. #else
  75. #ifdef _MT
  76.     #if !defined(_AFX_NO_DEBUG_CRT) && defined(_DEBUG)
  77.         #pragma comment(lib, "libcmtd.lib")
  78.     #else
  79.         #pragma comment(lib, "libcmt.lib")
  80.     #endif
  81. #else
  82.     #if !defined(_AFX_NO_DEBUG_CRT) && defined(_DEBUG)
  83.         #pragma comment(lib, "libcd.lib")
  84.     #else
  85.         #pragma comment(lib, "libc.lib")
  86.     #endif
  87. #endif
  88. #endif
  89.  
  90. #pragma comment(lib, "kernel32.lib")
  91. #pragma comment(lib, "user32.lib")
  92. #pragma comment(lib, "gdi32.lib")
  93. #pragma comment(lib, "comdlg32.lib")
  94. #pragma comment(lib, "winspool.lib")
  95. #pragma comment(lib, "advapi32.lib")
  96. #pragma comment(lib, "shell32.lib")
  97. #pragma comment(lib, "comctl32.lib")
  98.  
  99. // force inclusion of NOLIB.OBJ for /disallowlib directives
  100. #pragma comment(linker, "/include:__afxForceEXCLUDE")
  101.  
  102. // force inclusion of DLLMODUL.OBJ for _USRDLL
  103. #ifdef _USRDLL
  104. #pragma comment(linker, "/include:__afxForceUSRDLL")
  105. #endif
  106.  
  107. // force inclusion of STDAFX.OBJ for precompiled types
  108. #ifdef _AFXDLL
  109. #pragma comment(linker, "/include:__afxForceSTDAFX")
  110. #endif
  111.  
  112. #endif //!_AFX_NOFORCE_LIBS
  113.  
  114. /////////////////////////////////////////////////////////////////////////////
  115. // Classes declared in this file
  116. //   in addition to standard primitive data types and various helper macros
  117.  
  118. struct CRuntimeClass;          // object type information
  119.  
  120. class CObject;                        // the root of all objects classes
  121.  
  122.     class CException;                 // the root of all exceptions
  123.         class CArchiveException;      // archive exception
  124.         class CFileException;         // file exception
  125.         class CSimpleException;
  126.             class CMemoryException;       // out-of-memory exception
  127.             class CNotSupportedException; // feature not supported exception
  128.  
  129.     class CFile;                      // raw binary file
  130.         class CStdioFile;             // buffered stdio text/binary file
  131.         class CMemFile;               // memory based file
  132.  
  133. // Non CObject classes
  134. class CString;                        // growable string type
  135. class CTimeSpan;                      // time/date difference
  136. class CTime;                          // absolute time/date
  137. struct CFileStatus;                   // file status information
  138. struct CMemoryState;                  // diagnostic memory support
  139.  
  140. class CArchive;                       // object persistence tool
  141. class CDumpContext;                   // object diagnostic dumping
  142.  
  143. /////////////////////////////////////////////////////////////////////////////
  144. // Other includes from standard "C" runtimes
  145.  
  146. #ifndef _INC_STRING
  147.     #include <string.h>
  148. #endif
  149. #ifndef _INC_STDIO
  150.     #include <stdio.h>
  151. #endif
  152. #ifndef _INC_STDLIB
  153.     #include <stdlib.h>
  154. #endif
  155. #ifndef _INC_TIME
  156.     #include <time.h>
  157. #endif
  158. #ifndef _INC_LIMITS
  159.     #include <limits.h>
  160. #endif
  161. #ifndef _INC_STDDEF
  162.     #include <stddef.h>
  163. #endif
  164. #ifndef _INC_STDARG
  165.     #include <stdarg.h>
  166. #endif
  167.  
  168. #ifndef _AFX_NO_DEBUG_CRT
  169. #ifndef _INC_CRTDBG
  170.     #include <crtdbg.h>
  171. #endif
  172. #endif // _AFX_NO_DEBUG_CRT
  173.  
  174. #ifdef _AFX_OLD_EXCEPTIONS
  175. // use setjmp and helper functions instead of C++ keywords
  176. #ifndef _INC_SETJMP
  177.     #pragma warning(disable: 4611)
  178.     #include <setjmp.h>
  179. #endif
  180. #endif
  181.  
  182. #ifdef _AFX_PACKING
  183. #pragma pack(push, _AFX_PACKING)
  184. #endif
  185.  
  186. /////////////////////////////////////////////////////////////////////////////
  187. // Basic types
  188.  
  189. // abstract iteration position
  190. struct __POSITION { };
  191. typedef __POSITION* POSITION;
  192.  
  193. struct _AFX_DOUBLE  { BYTE doubleBits[sizeof(double)]; };
  194. struct _AFX_FLOAT   { BYTE floatBits[sizeof(float)]; };
  195.  
  196. // Standard constants
  197. #undef FALSE
  198. #undef TRUE
  199. #undef NULL
  200.  
  201. #define FALSE   0
  202. #define TRUE    1
  203. #define NULL    0
  204.  
  205. /////////////////////////////////////////////////////////////////////////////
  206. // Diagnostic support
  207.  
  208. #ifdef _DEBUG
  209.  
  210. BOOL AFXAPI AfxAssertFailedLine(LPCSTR lpszFileName, int nLine);
  211.  
  212. void AFX_CDECL AfxTrace(LPCTSTR lpszFormat, ...);
  213. // Note: file names are still ANSI strings (filenames rarely need UNICODE)
  214. void AFXAPI AfxAssertValidObject(const CObject* pOb,
  215.                 LPCSTR lpszFileName, int nLine);
  216. void AFXAPI AfxDump(const CObject* pOb); // Dump an object from CodeView
  217.  
  218. #define TRACE              ::AfxTrace
  219. #define THIS_FILE          __FILE__
  220. #define ASSERT(f) \
  221.     do \
  222.     { \
  223.     if (!(f) && AfxAssertFailedLine(THIS_FILE, __LINE__)) \
  224.         AfxDebugBreak(); \
  225.     } while (0) \
  226.  
  227. #define VERIFY(f)          ASSERT(f)
  228. #define ASSERT_VALID(pOb)  (::AfxAssertValidObject(pOb, THIS_FILE, __LINE__))
  229. #define DEBUG_ONLY(f)      (f)
  230.  
  231. // The following trace macros are provided for backward compatiblity
  232. //  (they also take a fixed number of parameters which provides
  233. //   some amount of extra error checking)
  234. #define TRACE0(sz)              ::AfxTrace(_T("%s"), _T(sz))
  235. #define TRACE1(sz, p1)          ::AfxTrace(_T(sz), p1)
  236. #define TRACE2(sz, p1, p2)      ::AfxTrace(_T(sz), p1, p2)
  237. #define TRACE3(sz, p1, p2, p3)  ::AfxTrace(_T(sz), p1, p2, p3)
  238.  
  239. // These AFX_DUMP macros also provided for backward compatibility
  240. #define AFX_DUMP0(dc, sz)   dc << _T(sz)
  241. #define AFX_DUMP1(dc, sz, p1) dc << _T(sz) << p1
  242.  
  243. #else   // _DEBUG
  244.  
  245. #define ASSERT(f)          ((void)0)
  246. #define VERIFY(f)          ((void)(f))
  247. #define ASSERT_VALID(pOb)  ((void)0)
  248. #define DEBUG_ONLY(f)      ((void)0)
  249. inline void AFX_CDECL AfxTrace(LPCTSTR, ...) { }
  250. #define TRACE              1 ? (void)0 : ::AfxTrace
  251. #define TRACE0(sz)
  252. #define TRACE1(sz, p1)
  253. #define TRACE2(sz, p1, p2)
  254. #define TRACE3(sz, p1, p2, p3)
  255.  
  256. #endif // !_DEBUG
  257.  
  258. #define ASSERT_POINTER(p, type) \
  259.     ASSERT(((p) != NULL) && AfxIsValidAddress((p), sizeof(type), FALSE))
  260.  
  261. #define ASSERT_NULL_OR_POINTER(p, type) \
  262.     ASSERT(((p) == NULL) || AfxIsValidAddress((p), sizeof(type), FALSE))
  263.  
  264. /////////////////////////////////////////////////////////////////////////////
  265. // Turn off warnings for /W4
  266. // To resume any of these warning: #pragma warning(default: 4xxx)
  267. // which should be placed after the AFX include files
  268. #ifndef ALL_WARNINGS
  269. // warnings generated with common MFC/Windows code
  270. #pragma warning(disable: 4127)  // constant expression for TRACE/ASSERT
  271. #pragma warning(disable: 4134)  // message map member fxn casts
  272. #pragma warning(disable: 4201)  // nameless unions are part of C++
  273. #pragma warning(disable: 4511)  // private copy constructors are good to have
  274. #pragma warning(disable: 4512)  // private operator= are good to have
  275. #pragma warning(disable: 4514)  // unreferenced inlines are common
  276. #pragma warning(disable: 4710)  // private constructors are disallowed
  277. #pragma warning(disable: 4705)  // statement has no effect in optimized code
  278. #pragma warning(disable: 4191)  // pointer-to-function casting
  279. // warnings caused by normal optimizations
  280. #ifndef _DEBUG
  281. #pragma warning(disable: 4701)  // local variable *may* be used without init
  282. #pragma warning(disable: 4702)  // unreachable code caused by optimizations
  283. #pragma warning(disable: 4791)  // loss of debugging info in release version
  284. #pragma warning(disable: 4189)  // initialized but unused variable
  285. #pragma warning(disable: 4390)  // empty controlled statement
  286. #endif
  287. // warnings specific to _AFXDLL version
  288. #ifdef _AFXDLL
  289. #pragma warning(disable: 4204)  // non-constant aggregate initializer
  290. #endif
  291. #ifdef _AFXDLL
  292. #pragma warning(disable: 4275)  // deriving exported class from non-exported
  293. #pragma warning(disable: 4251)  // using non-exported as public in exported
  294. #endif
  295. #endif //!ALL_WARNINGS
  296.  
  297. #ifdef _DEBUG
  298. #define UNUSED(x)
  299. #else
  300. #define UNUSED(x) x
  301. #endif
  302. #define UNUSED_ALWAYS(x) x
  303.  
  304. /////////////////////////////////////////////////////////////////////////////
  305. // Other implementation helpers
  306.  
  307. #define BEFORE_START_POSITION ((POSITION)-1L)
  308.  
  309. /////////////////////////////////////////////////////////////////////////////
  310. // explicit initialization for general purpose classes
  311.  
  312. BOOL AFXAPI AfxInitialize(BOOL bDLL = FALSE, DWORD dwVersion = _MFC_VER);
  313.  
  314. #undef AFX_DATA
  315. #define AFX_DATA AFX_CORE_DATA
  316.  
  317. /////////////////////////////////////////////////////////////////////////////
  318. // Basic object model
  319.  
  320. struct CRuntimeClass
  321. {
  322. // Attributes
  323.     LPCSTR m_lpszClassName;
  324.     int m_nObjectSize;
  325.     UINT m_wSchema; // schema number of the loaded class
  326.     CObject* (PASCAL* m_pfnCreateObject)(); // NULL => abstract class
  327. #ifdef _AFXDLL
  328.     CRuntimeClass* (PASCAL* m_pfnGetBaseClass)();
  329. #else
  330.     CRuntimeClass* m_pBaseClass;
  331. #endif
  332.  
  333. // Operations
  334.     CObject* CreateObject();
  335.     BOOL IsDerivedFrom(const CRuntimeClass* pBaseClass) const;
  336.  
  337. // Implementation
  338.     void Store(CArchive& ar) const;
  339.     static CRuntimeClass* PASCAL Load(CArchive& ar, UINT* pwSchemaNum);
  340.  
  341.     // CRuntimeClass objects linked together in simple list
  342.     CRuntimeClass* m_pNextClass;       // linked list of registered classes
  343. };
  344.  
  345. /////////////////////////////////////////////////////////////////////////////
  346. // Strings
  347.  
  348. #ifndef _OLEAUTO_H_
  349. #ifdef OLE2ANSI
  350.     typedef LPSTR BSTR;
  351. #else
  352.     typedef LPWSTR BSTR;// must (semantically) match typedef in oleauto.h
  353. #endif
  354. #endif
  355.  
  356. struct CStringData
  357. {
  358.     long nRefs;             // reference count
  359.     int nDataLength;        // length of data (including terminator)
  360.     int nAllocLength;       // length of allocation
  361.     // TCHAR data[nAllocLength]
  362.  
  363.     TCHAR* data()           // TCHAR* to managed data
  364.         { return (TCHAR*)(this+1); }
  365. };
  366.  
  367. class CString
  368. {
  369. public:
  370. // Constructors
  371.  
  372.     // constructs empty CString
  373.     CString();
  374.     // copy constructor
  375.     CString(const CString& stringSrc);
  376.     // from a single character
  377.     CString(TCHAR ch, int nRepeat = 1);
  378.     // from an ANSI string (converts to TCHAR)
  379.     CString(LPCSTR lpsz);
  380.     // from a UNICODE string (converts to TCHAR)
  381.     CString(LPCWSTR lpsz);
  382.     // subset of characters from an ANSI string (converts to TCHAR)
  383.     CString(LPCSTR lpch, int nLength);
  384.     // subset of characters from a UNICODE string (converts to TCHAR)
  385.     CString(LPCWSTR lpch, int nLength);
  386.     // from unsigned characters
  387.     CString(const unsigned char* psz);
  388.  
  389. // Attributes & Operations
  390.  
  391.     // get data length
  392.     int GetLength() const;
  393.     // TRUE if zero length
  394.     BOOL IsEmpty() const;
  395.     // clear contents to empty
  396.     void Empty();
  397.  
  398.     // return single character at zero-based index
  399.     TCHAR GetAt(int nIndex) const;
  400.     // return single character at zero-based index
  401.     TCHAR operator[](int nIndex) const;
  402.     // set a single character at zero-based index
  403.     void SetAt(int nIndex, TCHAR ch);
  404.     // return pointer to const string
  405.     operator LPCTSTR() const;
  406.  
  407.     // overloaded assignment
  408.  
  409.     // ref-counted copy from another CString
  410.     const CString& operator=(const CString& stringSrc);
  411.     // set string content to single character
  412.     const CString& operator=(TCHAR ch);
  413. #ifdef _UNICODE
  414.     const CString& operator=(char ch);
  415. #endif
  416.     // copy string content from ANSI string (converts to TCHAR)
  417.     const CString& operator=(LPCSTR lpsz);
  418.     // copy string content from UNICODE string (converts to TCHAR)
  419.     const CString& operator=(LPCWSTR lpsz);
  420.     // copy string content from unsigned chars
  421.     const CString& operator=(const unsigned char* psz);
  422.  
  423.     // string concatenation
  424.  
  425.     // concatenate from another CString
  426.     const CString& operator+=(const CString& string);
  427.  
  428.     // concatenate a single character
  429.     const CString& operator+=(TCHAR ch);
  430. #ifdef _UNICODE
  431.     // concatenate an ANSI character after converting it to TCHAR
  432.     const CString& operator+=(char ch);
  433. #endif
  434.     // concatenate a UNICODE character after converting it to TCHAR
  435.     const CString& operator+=(LPCTSTR lpsz);
  436.  
  437.     friend CString AFXAPI operator+(const CString& string1,
  438.             const CString& string2);
  439.     friend CString AFXAPI operator+(const CString& string, TCHAR ch);
  440.     friend CString AFXAPI operator+(TCHAR ch, const CString& string);
  441. #ifdef _UNICODE
  442.     friend CString AFXAPI operator+(const CString& string, char ch);
  443.     friend CString AFXAPI operator+(char ch, const CString& string);
  444. #endif
  445.     friend CString AFXAPI operator+(const CString& string, LPCTSTR lpsz);
  446.     friend CString AFXAPI operator+(LPCTSTR lpsz, const CString& string);
  447.  
  448.     // string comparison
  449.  
  450.     // straight character comparison
  451.     int Compare(LPCTSTR lpsz) const;
  452.     // compare ignoring case
  453.     int CompareNoCase(LPCTSTR lpsz) const;
  454.     // NLS aware comparison, case sensitive
  455.     int Collate(LPCTSTR lpsz) const;
  456.     // NLS aware comparison, case insensitive
  457.     int CollateNoCase(LPCTSTR lpsz) const;
  458.  
  459.     // simple sub-string extraction
  460.  
  461.     // return nCount characters starting at zero-based nFirst
  462.     CString Mid(int nFirst, int nCount) const;
  463.     // return all characters starting at zero-based nFirst
  464.     CString Mid(int nFirst) const;
  465.     // return first nCount characters in string
  466.     CString Left(int nCount) const;
  467.     // return nCount characters from end of string
  468.     CString Right(int nCount) const;
  469.  
  470.     //  characters from beginning that are also in passed string
  471.     CString SpanIncluding(LPCTSTR lpszCharSet) const;
  472.     // characters from beginning that are not also in passed string
  473.     CString SpanExcluding(LPCTSTR lpszCharSet) const;
  474.  
  475.     // upper/lower/reverse conversion
  476.  
  477.     // NLS aware conversion to uppercase
  478.     void MakeUpper();
  479.     // NLS aware conversion to lowercase
  480.     void MakeLower();
  481.     // reverse string right-to-left
  482.     void MakeReverse();
  483.  
  484.     // trimming whitespace (either side)
  485.  
  486.     // remove whitespace starting from right edge
  487.     void TrimRight();
  488.     // remove whitespace starting from left side
  489.     void TrimLeft();
  490.  
  491.     // trimming anything (either side)
  492.  
  493.     // remove continuous occurrences of chTarget starting from right
  494.     void TrimRight(TCHAR chTarget);
  495.     // remove continuous occcurrences of characters in passed string,
  496.     // starting from right
  497.     void TrimRight(LPCTSTR lpszTargets);
  498.     // remove continuous occurrences of chTarget starting from left
  499.     void TrimLeft(TCHAR chTarget);
  500.     // remove continuous occcurrences of characters in
  501.     // passed string, starting from left
  502.     void TrimLeft(LPCTSTR lpszTargets);
  503.  
  504.     // advanced manipulation
  505.  
  506.     // replace occurrences of chOld with chNew
  507.     int Replace(TCHAR chOld, TCHAR chNew);
  508.     // replace occurrences of substring lpszOld with lpszNew;
  509.     // empty lpszNew removes instances of lpszOld
  510.     int Replace(LPCTSTR lpszOld, LPCTSTR lpszNew);
  511.     // remove occurrences of chRemove
  512.     int Remove(TCHAR chRemove);
  513.     // insert character at zero-based index; concatenates
  514.     // if index is past end of string
  515.     int Insert(int nIndex, TCHAR ch);
  516.     // insert substring at zero-based index; concatenates
  517.     // if index is past end of string
  518.     int Insert(int nIndex, LPCTSTR pstr);
  519.     // delete nCount characters starting at zero-based index
  520.     int Delete(int nIndex, int nCount = 1);
  521.  
  522.     // searching
  523.  
  524.     // find character starting at left, -1 if not found
  525.     int Find(TCHAR ch) const;
  526.     // find character starting at right
  527.     int ReverseFind(TCHAR ch) const;
  528.     // find character starting at zero-based index and going right
  529.     int Find(TCHAR ch, int nStart) const;
  530.     // find first instance of any character in passed string
  531.     int FindOneOf(LPCTSTR lpszCharSet) const;
  532.     // find first instance of substring
  533.     int Find(LPCTSTR lpszSub) const;
  534.     // find first instance of substring starting at zero-based index
  535.     int Find(LPCTSTR lpszSub, int nStart) const;
  536.  
  537.     // simple formatting
  538.  
  539.     // printf-like formatting using passed string
  540.     void AFX_CDECL Format(LPCTSTR lpszFormat, ...);
  541.     // printf-like formatting using referenced string resource
  542.     void AFX_CDECL Format(UINT nFormatID, ...);
  543.     // printf-like formatting using variable arguments parameter
  544.     void FormatV(LPCTSTR lpszFormat, va_list argList);
  545.  
  546.     // formatting for localization (uses FormatMessage API)
  547.  
  548.     // format using FormatMessage API on passed string
  549.     void AFX_CDECL FormatMessage(LPCTSTR lpszFormat, ...);
  550.     // format using FormatMessage API on referenced string resource
  551.     void AFX_CDECL FormatMessage(UINT nFormatID, ...);
  552.  
  553.     // input and output
  554. #ifdef _DEBUG
  555.     friend CDumpContext& AFXAPI operator<<(CDumpContext& dc,
  556.                 const CString& string);
  557. #endif
  558.     friend CArchive& AFXAPI operator<<(CArchive& ar, const CString& string);
  559.     friend CArchive& AFXAPI operator>>(CArchive& ar, CString& string);
  560.  
  561.     // load from string resource
  562.     BOOL LoadString(UINT nID);
  563.  
  564. #ifndef _UNICODE
  565.     // ANSI <-> OEM support (convert string in place)
  566.  
  567.     // convert string from ANSI to OEM in-place
  568.     void AnsiToOem();
  569.     // convert string from OEM to ANSI in-place
  570.     void OemToAnsi();
  571. #endif
  572.  
  573. #ifndef _AFX_NO_BSTR_SUPPORT
  574.     // OLE BSTR support (use for OLE automation)
  575.  
  576.     // return a BSTR initialized with this CString's data
  577.     BSTR AllocSysString() const;
  578.     // reallocates the passed BSTR, copies content of this CString to it
  579.     BSTR SetSysString(BSTR* pbstr) const;
  580. #endif
  581.  
  582.     // Access to string implementation buffer as "C" character array
  583.  
  584.     // get pointer to modifiable buffer at least as long as nMinBufLength
  585.     LPTSTR GetBuffer(int nMinBufLength);
  586.     // release buffer, setting length to nNewLength (or to first nul if -1)
  587.     void ReleaseBuffer(int nNewLength = -1);
  588.     // get pointer to modifiable buffer exactly as long as nNewLength
  589.     LPTSTR GetBufferSetLength(int nNewLength);
  590.     // release memory allocated to but unused by string
  591.     void FreeExtra();
  592.  
  593.     // Use LockBuffer/UnlockBuffer to turn refcounting off
  594.  
  595.     // turn refcounting back on
  596.     LPTSTR LockBuffer();
  597.     // turn refcounting off
  598.     void UnlockBuffer();
  599.  
  600. // Implementation
  601. public:
  602.     ~CString();
  603.     int GetAllocLength() const;
  604.  
  605. protected:
  606.     LPTSTR m_pchData;   // pointer to ref counted string data
  607.  
  608.     // implementation helpers
  609.     CStringData* GetData() const;
  610.     void Init();
  611.     void AllocCopy(CString& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;
  612.     void AllocBuffer(int nLen);
  613.     void AssignCopy(int nSrcLen, LPCTSTR lpszSrcData);
  614.     void ConcatCopy(int nSrc1Len, LPCTSTR lpszSrc1Data, int nSrc2Len, LPCTSTR lpszSrc2Data);
  615.     void ConcatInPlace(int nSrcLen, LPCTSTR lpszSrcData);
  616.     void CopyBeforeWrite();
  617.     void AllocBeforeWrite(int nLen);
  618.     void Release();
  619.     static void PASCAL Release(CStringData* pData);
  620.     static int PASCAL SafeStrlen(LPCTSTR lpsz);
  621.     static void FASTCALL FreeData(CStringData* pData);
  622. };
  623.  
  624. // Compare helpers
  625. bool AFXAPI operator==(const CString& s1, const CString& s2);
  626. bool AFXAPI operator==(const CString& s1, LPCTSTR s2);
  627. bool AFXAPI operator==(LPCTSTR s1, const CString& s2);
  628. bool AFXAPI operator!=(const CString& s1, const CString& s2);
  629. bool AFXAPI operator!=(const CString& s1, LPCTSTR s2);
  630. bool AFXAPI operator!=(LPCTSTR s1, const CString& s2);
  631. bool AFXAPI operator<(const CString& s1, const CString& s2);
  632. bool AFXAPI operator<(const CString& s1, LPCTSTR s2);
  633. bool AFXAPI operator<(LPCTSTR s1, const CString& s2);
  634. bool AFXAPI operator>(const CString& s1, const CString& s2);
  635. bool AFXAPI operator>(const CString& s1, LPCTSTR s2);
  636. bool AFXAPI operator>(LPCTSTR s1, const CString& s2);
  637. bool AFXAPI operator<=(const CString& s1, const CString& s2);
  638. bool AFXAPI operator<=(const CString& s1, LPCTSTR s2);
  639. bool AFXAPI operator<=(LPCTSTR s1, const CString& s2);
  640. bool AFXAPI operator>=(const CString& s1, const CString& s2);
  641. bool AFXAPI operator>=(const CString& s1, LPCTSTR s2);
  642. bool AFXAPI operator>=(LPCTSTR s1, const CString& s2);
  643.  
  644. // conversion helpers
  645. int AFX_CDECL _wcstombsz(char* mbstr, const wchar_t* wcstr, size_t count);
  646. int AFX_CDECL _mbstowcsz(wchar_t* wcstr, const char* mbstr, size_t count);
  647.  
  648. // Globals
  649. extern AFX_DATA TCHAR afxChNil;
  650. #ifdef _AFXDLL
  651. const CString& AFXAPI AfxGetEmptyString();
  652. #define afxEmptyString AfxGetEmptyString()
  653. #else
  654. extern LPCTSTR _afxPchNil;
  655. #define afxEmptyString ((CString&)*(CString*)&_afxPchNil)
  656. #endif
  657.  
  658. /////////////////////////////////////////////////////////////////////////////
  659. // class CObject is the root of all compliant objects
  660.  
  661. #ifdef _AFXDLL
  662. class CObject
  663. #else
  664. class AFX_NOVTABLE CObject
  665. #endif
  666. {
  667. public:
  668.  
  669. // Object model (types, destruction, allocation)
  670.     virtual CRuntimeClass* GetRuntimeClass() const;
  671.     virtual ~CObject();  // virtual destructors are necessary
  672.  
  673.     // Diagnostic allocations
  674.     void* PASCAL operator new(size_t nSize);
  675.     void* PASCAL operator new(size_t, void* p);
  676.     void PASCAL operator delete(void* p);
  677. #if _MSC_VER >= 1200
  678.     void PASCAL operator delete(void* p, void* pPlace);
  679. #endif
  680.  
  681. #if defined(_DEBUG) && !defined(_AFX_NO_DEBUG_CRT)
  682.     // for file name/line number tracking using DEBUG_NEW
  683.     void* PASCAL operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
  684. #if _MSC_VER >= 1200
  685.     void PASCAL operator delete(void *p, LPCSTR lpszFileName, int nLine);
  686. #endif
  687. #endif
  688.  
  689.     // Disable the copy constructor and assignment by default so you will get
  690.     //   compiler errors instead of unexpected behaviour if you pass objects
  691.     //   by value or assign objects.
  692. protected:
  693.     CObject();
  694. private:
  695.     CObject(const CObject& objectSrc);              // no implementation
  696.     void operator=(const CObject& objectSrc);       // no implementation
  697.  
  698. // Attributes
  699. public:
  700.     BOOL IsSerializable() const;
  701.     BOOL IsKindOf(const CRuntimeClass* pClass) const;
  702.  
  703. // Overridables
  704.     virtual void Serialize(CArchive& ar);
  705.  
  706. #if defined(_DEBUG) || defined(_AFXDLL)
  707.     // Diagnostic Support
  708.     virtual void AssertValid() const;
  709.     virtual void Dump(CDumpContext& dc) const;
  710. #endif
  711.  
  712. // Implementation
  713. public:
  714.     static const AFX_DATA CRuntimeClass classCObject;
  715. #ifdef _AFXDLL
  716.     static CRuntimeClass* PASCAL _GetBaseClass();
  717. #endif
  718. };
  719.  
  720. // Helper macros
  721. #define RUNTIME_CLASS(class_name) ((CRuntimeClass*)(&class_name::class##class_name))
  722. #define ASSERT_KINDOF(class_name, object) \
  723.     ASSERT((object)->IsKindOf(RUNTIME_CLASS(class_name)))
  724.  
  725. // RTTI helper macros/functions
  726. const CObject* AFX_CDECL AfxDynamicDownCast(CRuntimeClass* pClass, const CObject* pObject);
  727. CObject* AFX_CDECL AfxDynamicDownCast(CRuntimeClass* pClass, CObject* pObject);
  728. #define DYNAMIC_DOWNCAST(class_name, object) \
  729.     (class_name*)AfxDynamicDownCast(RUNTIME_CLASS(class_name), object)
  730.  
  731. #ifdef _DEBUG
  732. const CObject* AFX_CDECL AfxStaticDownCast(CRuntimeClass* pClass, const CObject* pObject);
  733. CObject* AFX_CDECL AfxStaticDownCast(CRuntimeClass* pClass, CObject* pObject);
  734. #define STATIC_DOWNCAST(class_name, object) \
  735.     ((class_name*)AfxStaticDownCast(RUNTIME_CLASS(class_name), object))
  736. #else
  737. #define STATIC_DOWNCAST(class_name, object) ((class_name*)object)
  738. #endif
  739.  
  740. //////////////////////////////////////////////////////////////////////////////
  741. // Helper macros for declaring CRuntimeClass compatible classes
  742.  
  743. #ifdef _AFXDLL
  744. #define DECLARE_DYNAMIC(class_name) \
  745. protected: \
  746.     static CRuntimeClass* PASCAL _GetBaseClass(); \
  747. public: \
  748.     static const AFX_DATA CRuntimeClass class##class_name; \
  749.     virtual CRuntimeClass* GetRuntimeClass() const; \
  750.  
  751. #define _DECLARE_DYNAMIC(class_name) \
  752. protected: \
  753.     static CRuntimeClass* PASCAL _GetBaseClass(); \
  754. public: \
  755.     static AFX_DATA CRuntimeClass class##class_name; \
  756.     virtual CRuntimeClass* GetRuntimeClass() const; \
  757.  
  758. #else
  759. #define DECLARE_DYNAMIC(class_name) \
  760. public: \
  761.     static const AFX_DATA CRuntimeClass class##class_name; \
  762.     virtual CRuntimeClass* GetRuntimeClass() const; \
  763.  
  764. #define _DECLARE_DYNAMIC(class_name) \
  765. public: \
  766.     static AFX_DATA CRuntimeClass class##class_name; \
  767.     virtual CRuntimeClass* GetRuntimeClass() const; \
  768.  
  769. #endif
  770.  
  771. // not serializable, but dynamically constructable
  772. #define DECLARE_DYNCREATE(class_name) \
  773.     DECLARE_DYNAMIC(class_name) \
  774.     static CObject* PASCAL CreateObject();
  775.  
  776. #define _DECLARE_DYNCREATE(class_name) \
  777.     _DECLARE_DYNAMIC(class_name) \
  778.     static CObject* PASCAL CreateObject();
  779.  
  780. #define DECLARE_SERIAL(class_name) \
  781.     _DECLARE_DYNCREATE(class_name) \
  782.     AFX_API friend CArchive& AFXAPI operator>>(CArchive& ar, class_name* &pOb);
  783.  
  784. // generate static object constructor for class registration
  785. void AFXAPI AfxClassInit(CRuntimeClass* pNewClass);
  786. struct AFX_CLASSINIT
  787.     { AFX_CLASSINIT(CRuntimeClass* pNewClass) { AfxClassInit(pNewClass); } };
  788. struct AFX_CLASSINIT_COMPAT
  789.     { AFX_CLASSINIT_COMPAT(CRuntimeClass* pNewClass); };
  790.  
  791. #ifdef _AFXDLL
  792. #define IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, wSchema, pfnNew) \
  793.     CRuntimeClass* PASCAL class_name::_GetBaseClass() \
  794.         { return RUNTIME_CLASS(base_class_name); } \
  795.     AFX_COMDAT const AFX_DATADEF CRuntimeClass class_name::class##class_name = { \
  796.         #class_name, sizeof(class class_name), wSchema, pfnNew, \
  797.             &class_name::_GetBaseClass, NULL }; \
  798.     CRuntimeClass* class_name::GetRuntimeClass() const \
  799.         { return RUNTIME_CLASS(class_name); } \
  800.  
  801. #define _IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, wSchema, pfnNew) \
  802.     CRuntimeClass* PASCAL class_name::_GetBaseClass() \
  803.         { return RUNTIME_CLASS(base_class_name); } \
  804.     AFX_COMDAT AFX_DATADEF CRuntimeClass class_name::class##class_name = { \
  805.         #class_name, sizeof(class class_name), wSchema, pfnNew, \
  806.             &class_name::_GetBaseClass, NULL }; \
  807.     CRuntimeClass* class_name::GetRuntimeClass() const \
  808.         { return RUNTIME_CLASS(class_name); } \
  809.  
  810. #else
  811. #define IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, wSchema, pfnNew) \
  812.     AFX_COMDAT const AFX_DATADEF CRuntimeClass class_name::class##class_name = { \
  813.         #class_name, sizeof(class class_name), wSchema, pfnNew, \
  814.             RUNTIME_CLASS(base_class_name), NULL }; \
  815.     CRuntimeClass* class_name::GetRuntimeClass() const \
  816.         { return RUNTIME_CLASS(class_name); } \
  817.  
  818. #define _IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, wSchema, pfnNew) \
  819.     AFX_DATADEF CRuntimeClass class_name::class##class_name = { \
  820.         #class_name, sizeof(class class_name), wSchema, pfnNew, \
  821.             RUNTIME_CLASS(base_class_name), NULL }; \
  822.     CRuntimeClass* class_name::GetRuntimeClass() const \
  823.         { return RUNTIME_CLASS(class_name); } \
  824.  
  825. #endif
  826.  
  827. #define IMPLEMENT_DYNAMIC(class_name, base_class_name) \
  828.     IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, NULL)
  829.  
  830. #define IMPLEMENT_DYNCREATE(class_name, base_class_name) \
  831.     CObject* PASCAL class_name::CreateObject() \
  832.         { return new class_name; } \
  833.     IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, \
  834.         class_name::CreateObject)
  835.  
  836. #define IMPLEMENT_SERIAL(class_name, base_class_name, wSchema) \
  837.     CObject* PASCAL class_name::CreateObject() \
  838.         { return new class_name; } \
  839.     _IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, wSchema, \
  840.         class_name::CreateObject) \
  841.     AFX_CLASSINIT _init_##class_name(RUNTIME_CLASS(class_name)); \
  842.     CArchive& AFXAPI operator>>(CArchive& ar, class_name* &pOb) \
  843.         { pOb = (class_name*) ar.ReadObject(RUNTIME_CLASS(class_name)); \
  844.             return ar; } \
  845.  
  846. // optional bit for schema number that enables object versioning
  847. #define VERSIONABLE_SCHEMA  (0x80000000)
  848.  
  849. /////////////////////////////////////////////////////////////////////////////
  850. // other helpers
  851.  
  852. // zero fill everything after the vtbl pointer
  853. #define AFX_ZERO_INIT_OBJECT(base_class) \
  854.     memset(((base_class*)this)+1, 0, sizeof(*this) - sizeof(class base_class));
  855.  
  856.  
  857. /////////////////////////////////////////////////////////////////////////////
  858. // Exceptions
  859.  
  860. #ifdef _AFXDLL
  861. class CException : public CObject
  862. #else
  863. class AFX_NOVTABLE CException : public CObject
  864. #endif
  865. {
  866.     // abstract class for dynamic type checking
  867.     DECLARE_DYNAMIC(CException)
  868.  
  869. public:
  870. // Constructors
  871.     CException();   // sets m_bAutoDelete = TRUE
  872.     CException(BOOL bAutoDelete);   // sets m_bAutoDelete = bAutoDelete
  873.  
  874. // Operations
  875.     void Delete();  // use to delete exception in 'catch' block
  876.  
  877.     virtual BOOL GetErrorMessage(LPTSTR lpszError, UINT nMaxError,
  878.         PUINT pnHelpContext = NULL);
  879.     virtual int ReportError(UINT nType = MB_OK, UINT nMessageID = 0);
  880.  
  881. // Implementation (setting m_bAutoDelete to FALSE is advanced)
  882. public:
  883.     virtual ~CException();
  884.     BOOL m_bAutoDelete;
  885. #ifdef _DEBUG
  886.     void PASCAL operator delete(void* pbData);
  887. #if _MSC_VER >= 1200
  888.     void PASCAL operator delete(void* pbData, LPCSTR lpszFileName, int nLine);
  889. #endif
  890. protected:
  891.     BOOL m_bReadyForDelete;
  892. #endif
  893. };
  894.  
  895. #ifdef _AFXDLL
  896. class CSimpleException : public CException
  897. #else
  898. class AFX_NOVTABLE CSimpleException : public CException
  899. #endif
  900. {
  901.     // base class for resource-critical MFC exceptions
  902.     // handles ownership and initialization of an error message
  903.  
  904. public:
  905. // Constructors
  906.     CSimpleException();
  907.     CSimpleException(BOOL bAutoDelete);
  908.  
  909. // Operations
  910.     virtual BOOL GetErrorMessage(LPTSTR lpszError, UINT nMaxError,
  911.         PUINT pnHelpContext = NULL);
  912.  
  913. // Implementation (setting m_bAutoDelete to FALSE is advanced)
  914. public:
  915.     virtual ~CSimpleException();
  916.     BOOL m_bAutoDelete;
  917.  
  918.     void InitString();      // used during MFC initialization
  919.  
  920. protected:
  921.     BOOL m_bInitialized;
  922.     BOOL m_bLoaded;
  923.     TCHAR m_szMessage[128];
  924.     UINT m_nResourceID;
  925.  
  926. #ifdef _DEBUG
  927.     BOOL m_bReadyForDelete;
  928. #endif
  929. };
  930.  
  931. // helper routines for non-C++ EH implementations
  932. #ifdef _AFX_OLD_EXCEPTIONS
  933.     BOOL AFXAPI AfxCatchProc(CRuntimeClass* pClass);
  934.     void AFXAPI AfxThrow(CException* pException);
  935. #else
  936.     // for THROW_LAST auto-delete backward compatiblity
  937.     void AFXAPI AfxThrowLastCleanup();
  938. #endif
  939.  
  940. // other out-of-line helper functions
  941. void AFXAPI AfxTryCleanup();
  942.  
  943. #ifndef _AFX_JUMPBUF
  944. // Use portable 'jmp_buf' defined by ANSI by default.
  945. #define _AFX_JUMPBUF jmp_buf
  946. #endif
  947.  
  948. // Placed on frame for EXCEPTION linkage, or CException cleanup
  949. struct AFX_EXCEPTION_LINK
  950. {
  951. #ifdef _AFX_OLD_EXCEPTIONS
  952.     union
  953.     {
  954.         _AFX_JUMPBUF m_jumpBuf;
  955.         struct
  956.         {
  957.             void (PASCAL* pfnCleanup)(AFX_EXCEPTION_LINK* pLink);
  958.             void* pvData;       // extra data follows
  959.         } m_callback;       // callback for cleanup (nType != 0)
  960.     };
  961.     UINT m_nType;               // 0 for setjmp, !=0 for user extension
  962. #endif //!_AFX_OLD_EXCEPTIONS
  963.  
  964.     AFX_EXCEPTION_LINK* m_pLinkPrev;    // previous top, next in handler chain
  965.     CException* m_pException;   // current exception (NULL in TRY block)
  966.  
  967.     AFX_EXCEPTION_LINK();       // for initialization and linking
  968.     ~AFX_EXCEPTION_LINK()       // for cleanup and unlinking
  969.         { AfxTryCleanup(); };
  970. };
  971.  
  972. // Exception global state - never access directly
  973. struct AFX_EXCEPTION_CONTEXT
  974. {
  975.     AFX_EXCEPTION_LINK* m_pLinkTop;
  976.  
  977.     // Note: most of the exception context is now in the AFX_EXCEPTION_LINK
  978. };
  979.  
  980. #ifndef _PNH_DEFINED
  981. typedef int (__cdecl * _PNH)( size_t );
  982. #define _PNH_DEFINED
  983. #endif
  984.  
  985. _PNH AFXAPI AfxGetNewHandler();
  986. _PNH AFXAPI AfxSetNewHandler(_PNH pfnNewHandler);
  987. int AFX_CDECL AfxNewHandler(size_t nSize);
  988.  
  989. void AFXAPI AfxAbort();
  990.  
  991. #ifdef _AFX_OLD_EXCEPTIONS
  992.  
  993. // Obsolete and non-portable: setting terminate handler
  994. //  use CWinApp::ProcessWndProcException for Windows apps instead
  995. //  can also use set_terminate which is part of C++ standard library
  996. //      (these are provided for backward compatibility)
  997. void AFXAPI AfxTerminate();
  998. typedef void (AFXAPI* AFX_TERM_PROC)();
  999. AFX_TERM_PROC AFXAPI AfxSetTerminate(AFX_TERM_PROC);
  1000.  
  1001. #endif
  1002.  
  1003. /////////////////////////////////////////////////////////////////////////////
  1004. // Exception macros using try, catch and throw
  1005. //  (for backward compatibility to previous versions of MFC)
  1006.  
  1007. #ifndef _AFX_OLD_EXCEPTIONS
  1008.  
  1009. #define TRY { AFX_EXCEPTION_LINK _afxExceptionLink; try {
  1010.  
  1011. #define CATCH(class, e) } catch (class* e) \
  1012.     { ASSERT(e->IsKindOf(RUNTIME_CLASS(class))); \
  1013.         _afxExceptionLink.m_pException = e;
  1014.  
  1015. #define AND_CATCH(class, e) } catch (class* e) \
  1016.     { ASSERT(e->IsKindOf(RUNTIME_CLASS(class))); \
  1017.         _afxExceptionLink.m_pException = e;
  1018.  
  1019. #define END_CATCH } }
  1020.  
  1021. #define THROW(e) throw e
  1022. #define THROW_LAST() (AfxThrowLastCleanup(), throw)
  1023.  
  1024. // Advanced macros for smaller code
  1025. #define CATCH_ALL(e) } catch (CException* e) \
  1026.     { { ASSERT(e->IsKindOf(RUNTIME_CLASS(CException))); \
  1027.         _afxExceptionLink.m_pException = e;
  1028.  
  1029. #define AND_CATCH_ALL(e) } catch (CException* e) \
  1030.     { { ASSERT(e->IsKindOf(RUNTIME_CLASS(CException))); \
  1031.         _afxExceptionLink.m_pException = e;
  1032.  
  1033. #define END_CATCH_ALL } } }
  1034.  
  1035. #define END_TRY } catch (CException* e) \
  1036.     { ASSERT(e->IsKindOf(RUNTIME_CLASS(CException))); \
  1037.         _afxExceptionLink.m_pException = e; } }
  1038.  
  1039. #else //_AFX_OLD_EXCEPTIONS
  1040.  
  1041. /////////////////////////////////////////////////////////////////////////////
  1042. // Exception macros using setjmp and longjmp
  1043. //  (for portability to compilers with no support for C++ exception handling)
  1044.  
  1045. #define TRY \
  1046.     { AFX_EXCEPTION_LINK _afxExceptionLink; \
  1047.     if (::setjmp(_afxExceptionLink.m_jumpBuf) == 0)
  1048.  
  1049. #define CATCH(class, e) \
  1050.     else if (::AfxCatchProc(RUNTIME_CLASS(class))) \
  1051.     { class* e = (class*)_afxExceptionLink.m_pException;
  1052.  
  1053. #define AND_CATCH(class, e) \
  1054.     } else if (::AfxCatchProc(RUNTIME_CLASS(class))) \
  1055.     { class* e = (class*)_afxExceptionLink.m_pException;
  1056.  
  1057. #define END_CATCH \
  1058.     } else { ::AfxThrow(NULL); } }
  1059.  
  1060. #define THROW(e) AfxThrow(e)
  1061. #define THROW_LAST() AfxThrow(NULL)
  1062.  
  1063. // Advanced macros for smaller code
  1064. #define CATCH_ALL(e) \
  1065.     else { CException* e = _afxExceptionLink.m_pException;
  1066.  
  1067. #define AND_CATCH_ALL(e) \
  1068.     } else { CException* e = _afxExceptionLink.m_pException;
  1069.  
  1070. #define END_CATCH_ALL } }
  1071.  
  1072. #define END_TRY }
  1073.  
  1074. #endif //_AFX_OLD_EXCEPTIONS
  1075.  
  1076. /////////////////////////////////////////////////////////////////////////////
  1077. // Standard Exception classes
  1078.  
  1079. class CMemoryException : public CSimpleException
  1080. {
  1081.     DECLARE_DYNAMIC(CMemoryException)
  1082. public:
  1083.     CMemoryException();
  1084.  
  1085. // Implementation
  1086. public:
  1087.     CMemoryException(BOOL bAutoDelete);
  1088.     CMemoryException(BOOL bAutoDelete, UINT nResourceID);
  1089.     virtual ~CMemoryException();
  1090. };
  1091.  
  1092. class CNotSupportedException : public CSimpleException
  1093. {
  1094.     DECLARE_DYNAMIC(CNotSupportedException)
  1095. public:
  1096.     CNotSupportedException();
  1097.  
  1098. // Implementation
  1099. public:
  1100.     CNotSupportedException(BOOL bAutoDelete);
  1101.     CNotSupportedException(BOOL bAutoDelete, UINT nResourceID);
  1102.     virtual ~CNotSupportedException();
  1103. };
  1104.  
  1105. class CArchiveException : public CException
  1106. {
  1107.     DECLARE_DYNAMIC(CArchiveException)
  1108. public:
  1109.     enum {
  1110.         none,
  1111.         generic,
  1112.         readOnly,
  1113.         endOfFile,
  1114.         writeOnly,
  1115.         badIndex,
  1116.         badClass,
  1117.         badSchema
  1118.     };
  1119.  
  1120. // Constructor
  1121.     CArchiveException(int cause = CArchiveException::none,
  1122.         LPCTSTR lpszArchiveName = NULL);
  1123.  
  1124. // Attributes
  1125.     int m_cause;
  1126.     CString m_strFileName;
  1127.  
  1128. // Implementation
  1129. public:
  1130.     virtual ~CArchiveException();
  1131. #ifdef _DEBUG
  1132.     virtual void Dump(CDumpContext& dc) const;
  1133. #endif
  1134.     virtual BOOL GetErrorMessage(LPTSTR lpszError, UINT nMaxError,
  1135.         PUINT pnHelpContext = NULL);
  1136. };
  1137.  
  1138. class CFileException : public CException
  1139. {
  1140.     DECLARE_DYNAMIC(CFileException)
  1141.  
  1142. public:
  1143.     enum {
  1144.         none,
  1145.         generic,
  1146.         fileNotFound,
  1147.         badPath,
  1148.         tooManyOpenFiles,
  1149.         accessDenied,
  1150.         invalidFile,
  1151.         removeCurrentDir,
  1152.         directoryFull,
  1153.         badSeek,
  1154.         hardIO,
  1155.         sharingViolation,
  1156.         lockViolation,
  1157.         diskFull,
  1158.         endOfFile
  1159.     };
  1160.  
  1161. // Constructor
  1162.     CFileException(int cause = CFileException::none, LONG lOsError = -1,
  1163.         LPCTSTR lpszArchiveName = NULL);
  1164.  
  1165. // Attributes
  1166.     int     m_cause;
  1167.     LONG    m_lOsError;
  1168.     CString m_strFileName;
  1169.  
  1170. // Operations
  1171.     // convert a OS dependent error code to a Cause
  1172.     static int PASCAL OsErrorToException(LONG lOsError);
  1173.     static int PASCAL ErrnoToException(int nErrno);
  1174.  
  1175.     // helper functions to throw exception after converting to a Cause
  1176.     static void PASCAL ThrowOsError(LONG lOsError, LPCTSTR lpszFileName = NULL);
  1177.     static void PASCAL ThrowErrno(int nErrno, LPCTSTR lpszFileName = NULL);
  1178.  
  1179. // Implementation
  1180. public:
  1181.     virtual ~CFileException();
  1182. #ifdef _DEBUG
  1183.     virtual void Dump(CDumpContext&) const;
  1184. #endif
  1185.     virtual BOOL GetErrorMessage(LPTSTR lpszError, UINT nMaxError,
  1186.         PUINT pnHelpContext = NULL);
  1187. };
  1188.  
  1189. /////////////////////////////////////////////////////////////////////////////
  1190. // Standard exception throws
  1191.  
  1192. void AFXAPI AfxThrowMemoryException();
  1193. void AFXAPI AfxThrowNotSupportedException();
  1194. void AFXAPI AfxThrowArchiveException(int cause,
  1195.     LPCTSTR lpszArchiveName = NULL);
  1196. void AFXAPI AfxThrowFileException(int cause, LONG lOsError = -1,
  1197.     LPCTSTR lpszFileName = NULL);
  1198.  
  1199. /////////////////////////////////////////////////////////////////////////////
  1200. // File - raw unbuffered disk file I/O
  1201.  
  1202. class CFile : public CObject
  1203. {
  1204.     DECLARE_DYNAMIC(CFile)
  1205.  
  1206. public:
  1207. // Flag values
  1208.     enum OpenFlags {
  1209.         modeRead =          0x0000,
  1210.         modeWrite =         0x0001,
  1211.         modeReadWrite =     0x0002,
  1212.         shareCompat =       0x0000,
  1213.         shareExclusive =    0x0010,
  1214.         shareDenyWrite =    0x0020,
  1215.         shareDenyRead =     0x0030,
  1216.         shareDenyNone =     0x0040,
  1217.         modeNoInherit =     0x0080,
  1218.         modeCreate =        0x1000,
  1219.         modeNoTruncate =    0x2000,
  1220.         typeText =          0x4000, // typeText and typeBinary are used in
  1221.         typeBinary =   (int)0x8000 // derived classes only
  1222.         };
  1223.  
  1224.     enum Attribute {
  1225.         normal =    0x00,
  1226.         readOnly =  0x01,
  1227.         hidden =    0x02,
  1228.         system =    0x04,
  1229.         volume =    0x08,
  1230.         directory = 0x10,
  1231.         archive =   0x20
  1232.         };
  1233.  
  1234.     enum SeekPosition { begin = 0x0, current = 0x1, end = 0x2 };
  1235.  
  1236.     enum { hFileNull = -1 };
  1237.  
  1238. // Constructors
  1239.     CFile();
  1240.     CFile(int hFile);
  1241.     CFile(LPCTSTR lpszFileName, UINT nOpenFlags);
  1242.  
  1243. // Attributes
  1244.     UINT m_hFile;
  1245.     operator HFILE() const;
  1246.  
  1247.     virtual DWORD GetPosition() const;
  1248.     BOOL GetStatus(CFileStatus& rStatus) const;
  1249.     virtual CString GetFileName() const;
  1250.     virtual CString GetFileTitle() const;
  1251.     virtual CString GetFilePath() const;
  1252.     virtual void SetFilePath(LPCTSTR lpszNewName);
  1253.  
  1254. // Operations
  1255.     virtual BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags,
  1256.         CFileException* pError = NULL);
  1257.  
  1258.     static void PASCAL Rename(LPCTSTR lpszOldName,
  1259.                 LPCTSTR lpszNewName);
  1260.     static void PASCAL Remove(LPCTSTR lpszFileName);
  1261.     static BOOL PASCAL GetStatus(LPCTSTR lpszFileName,
  1262.                 CFileStatus& rStatus);
  1263.     static void PASCAL SetStatus(LPCTSTR lpszFileName,
  1264.                 const CFileStatus& status);
  1265.  
  1266.     DWORD SeekToEnd();
  1267.     void SeekToBegin();
  1268.  
  1269.     // backward compatible ReadHuge and WriteHuge
  1270.     DWORD ReadHuge(void* lpBuffer, DWORD dwCount);
  1271.     void WriteHuge(const void* lpBuffer, DWORD dwCount);
  1272.  
  1273. // Overridables
  1274.     virtual CFile* Duplicate() const;
  1275.  
  1276.     virtual LONG Seek(LONG lOff, UINT nFrom);
  1277.     virtual void SetLength(DWORD dwNewLen);
  1278.     virtual DWORD GetLength() const;
  1279.  
  1280.     virtual UINT Read(void* lpBuf, UINT nCount);
  1281.     virtual void Write(const void* lpBuf, UINT nCount);
  1282.  
  1283.     virtual void LockRange(DWORD dwPos, DWORD dwCount);
  1284.     virtual void UnlockRange(DWORD dwPos, DWORD dwCount);
  1285.  
  1286.     virtual void Abort();
  1287.     virtual void Flush();
  1288.     virtual void Close();
  1289.  
  1290. // Implementation
  1291. public:
  1292.     virtual ~CFile();
  1293. #ifdef _DEBUG
  1294.     virtual void AssertValid() const;
  1295.     virtual void Dump(CDumpContext& dc) const;
  1296. #endif
  1297.     enum BufferCommand { bufferRead, bufferWrite, bufferCommit, bufferCheck };
  1298.     virtual UINT GetBufferPtr(UINT nCommand, UINT nCount = 0,
  1299.         void** ppBufStart = NULL, void** ppBufMax = NULL);
  1300.  
  1301. protected:
  1302.     BOOL m_bCloseOnDelete;
  1303.     CString m_strFileName;
  1304. };
  1305.  
  1306. /////////////////////////////////////////////////////////////////////////////
  1307. // STDIO file implementation
  1308.  
  1309. class CStdioFile : public CFile
  1310. {
  1311.     DECLARE_DYNAMIC(CStdioFile)
  1312.  
  1313. public:
  1314. // Constructors
  1315.     CStdioFile();
  1316.     CStdioFile(FILE* pOpenStream);
  1317.     CStdioFile(LPCTSTR lpszFileName, UINT nOpenFlags);
  1318.  
  1319. // Attributes
  1320.     FILE* m_pStream;    // stdio FILE
  1321.                         // m_hFile from base class is _fileno(m_pStream)
  1322.  
  1323. // Operations
  1324.     // reading and writing strings
  1325.     virtual void WriteString(LPCTSTR lpsz);
  1326.     virtual LPTSTR ReadString(LPTSTR lpsz, UINT nMax);
  1327.     virtual BOOL ReadString(CString& rString);
  1328.  
  1329. // Implementation
  1330. public:
  1331.     virtual ~CStdioFile();
  1332. #ifdef _DEBUG
  1333.     void Dump(CDumpContext& dc) const;
  1334. #endif
  1335.     virtual DWORD GetPosition() const;
  1336.     virtual BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags,
  1337.         CFileException* pError = NULL);
  1338.     virtual UINT Read(void* lpBuf, UINT nCount);
  1339.     virtual void Write(const void* lpBuf, UINT nCount);
  1340.     virtual LONG Seek(LONG lOff, UINT nFrom);
  1341.     virtual void Abort();
  1342.     virtual void Flush();
  1343.     virtual void Close();
  1344.  
  1345.     // Unsupported APIs
  1346.     virtual CFile* Duplicate() const;
  1347.     virtual void LockRange(DWORD dwPos, DWORD dwCount);
  1348.     virtual void UnlockRange(DWORD dwPos, DWORD dwCount);
  1349. };
  1350.  
  1351. ////////////////////////////////////////////////////////////////////////////
  1352. // Memory based file implementation
  1353.  
  1354. class CMemFile : public CFile
  1355. {
  1356.     DECLARE_DYNAMIC(CMemFile)
  1357.  
  1358. public:
  1359. // Constructors
  1360.     CMemFile(UINT nGrowBytes = 1024);
  1361.     CMemFile(BYTE* lpBuffer, UINT nBufferSize, UINT nGrowBytes = 0);
  1362.  
  1363. // Operations
  1364.     void Attach(BYTE* lpBuffer, UINT nBufferSize, UINT nGrowBytes = 0);
  1365.     BYTE* Detach();
  1366.  
  1367. // Advanced Overridables
  1368. protected:
  1369.     virtual BYTE* Alloc(DWORD nBytes);
  1370.     virtual BYTE* Realloc(BYTE* lpMem, DWORD nBytes);
  1371.     virtual BYTE* Memcpy(BYTE* lpMemTarget, const BYTE* lpMemSource, UINT nBytes);
  1372.     virtual void Free(BYTE* lpMem);
  1373.     virtual void GrowFile(DWORD dwNewLen);
  1374.  
  1375. // Implementation
  1376. protected:
  1377.     UINT m_nGrowBytes;
  1378.     DWORD m_nPosition;
  1379.     DWORD m_nBufferSize;
  1380.     DWORD m_nFileSize;
  1381.     BYTE* m_lpBuffer;
  1382.     BOOL m_bAutoDelete;
  1383.  
  1384. public:
  1385.     virtual ~CMemFile();
  1386. #ifdef _DEBUG
  1387.     virtual void Dump(CDumpContext& dc) const;
  1388.     virtual void AssertValid() const;
  1389. #endif
  1390.     virtual DWORD GetPosition() const;
  1391.     BOOL GetStatus(CFileStatus& rStatus) const;
  1392.     virtual LONG Seek(LONG lOff, UINT nFrom);
  1393.     virtual void SetLength(DWORD dwNewLen);
  1394.     virtual UINT Read(void* lpBuf, UINT nCount);
  1395.     virtual void Write(const void* lpBuf, UINT nCount);
  1396.     virtual void Abort();
  1397.     virtual void Flush();
  1398.     virtual void Close();
  1399.     virtual UINT GetBufferPtr(UINT nCommand, UINT nCount = 0,
  1400.         void** ppBufStart = NULL, void** ppBufMax = NULL);
  1401.  
  1402.     // Unsupported APIs
  1403.     virtual CFile* Duplicate() const;
  1404.     virtual void LockRange(DWORD dwPos, DWORD dwCount);
  1405.     virtual void UnlockRange(DWORD dwPos, DWORD dwCount);
  1406. };
  1407.  
  1408. ////////////////////////////////////////////////////////////////////////////
  1409. // Local file searches
  1410.  
  1411. class CFileFind : public CObject
  1412. {
  1413. public:
  1414.     CFileFind();
  1415.     virtual ~CFileFind();
  1416.  
  1417. // Attributes
  1418. public:
  1419.     DWORD GetLength() const;
  1420. #if defined(_X86_) || defined(_ALPHA_)
  1421.     __int64 GetLength64() const;
  1422. #endif
  1423.     virtual CString GetFileName() const;
  1424.     virtual CString GetFilePath() const;
  1425.     virtual CString GetFileTitle() const;
  1426.     virtual CString GetFileURL() const;
  1427.     virtual CString GetRoot() const;
  1428.  
  1429.     virtual BOOL GetLastWriteTime(FILETIME* pTimeStamp) const;
  1430.     virtual BOOL GetLastAccessTime(FILETIME* pTimeStamp) const;
  1431.     virtual BOOL GetCreationTime(FILETIME* pTimeStamp) const;
  1432.     virtual BOOL GetLastWriteTime(CTime& refTime) const;
  1433.     virtual BOOL GetLastAccessTime(CTime& refTime) const;
  1434.     virtual BOOL GetCreationTime(CTime& refTime) const;
  1435.  
  1436.     virtual BOOL MatchesMask(DWORD dwMask) const;
  1437.  
  1438.     virtual BOOL IsDots() const;
  1439.     // these aren't virtual because they all use MatchesMask(), which is
  1440.     BOOL IsReadOnly() const;
  1441.     BOOL IsDirectory() const;
  1442.     BOOL IsCompressed() const;
  1443.     BOOL IsSystem() const;
  1444.     BOOL IsHidden() const;
  1445.     BOOL IsTemporary() const;
  1446.     BOOL IsNormal() const;
  1447.     BOOL IsArchived() const;
  1448.  
  1449. // Operations
  1450.     void Close();
  1451.     virtual BOOL FindFile(LPCTSTR pstrName = NULL, DWORD dwUnused = 0);
  1452.     virtual BOOL FindNextFile();
  1453.  
  1454. protected:
  1455.     virtual void CloseContext();
  1456.  
  1457. // Implementation
  1458. protected:
  1459.     void* m_pFoundInfo;
  1460.     void* m_pNextInfo;
  1461.     HANDLE m_hContext;
  1462.     BOOL m_bGotLast;
  1463.     CString m_strRoot;
  1464.     TCHAR m_chDirSeparator;     // not '\\' for Internet classes
  1465.  
  1466. #ifdef _DEBUG
  1467.     void Dump(CDumpContext& dc) const;
  1468.     void AssertValid() const;
  1469. #endif
  1470.  
  1471.     DECLARE_DYNAMIC(CFileFind)
  1472. };
  1473.  
  1474. /////////////////////////////////////////////////////////////////////////////
  1475. // CTimeSpan and CTime
  1476.  
  1477. class CTimeSpan
  1478. {
  1479. public:
  1480.  
  1481. // Constructors
  1482.     CTimeSpan();
  1483.     CTimeSpan(time_t time);
  1484.     CTimeSpan(LONG lDays, int nHours, int nMins, int nSecs);
  1485.  
  1486.     CTimeSpan(const CTimeSpan& timeSpanSrc);
  1487.     const CTimeSpan& operator=(const CTimeSpan& timeSpanSrc);
  1488.  
  1489. // Attributes
  1490.     // extract parts
  1491.     LONG GetDays() const;   // total # of days
  1492.     LONG GetTotalHours() const;
  1493.     int GetHours() const;
  1494.     LONG GetTotalMinutes() const;
  1495.     int GetMinutes() const;
  1496.     LONG GetTotalSeconds() const;
  1497.     int GetSeconds() const;
  1498.  
  1499. // Operations
  1500.     // time math
  1501.     CTimeSpan operator-(CTimeSpan timeSpan) const;
  1502.     CTimeSpan operator+(CTimeSpan timeSpan) const;
  1503.     const CTimeSpan& operator+=(CTimeSpan timeSpan);
  1504.     const CTimeSpan& operator-=(CTimeSpan timeSpan);
  1505.     BOOL operator==(CTimeSpan timeSpan) const;
  1506.     BOOL operator!=(CTimeSpan timeSpan) const;
  1507.     BOOL operator<(CTimeSpan timeSpan) const;
  1508.     BOOL operator>(CTimeSpan timeSpan) const;
  1509.     BOOL operator<=(CTimeSpan timeSpan) const;
  1510.     BOOL operator>=(CTimeSpan timeSpan) const;
  1511.  
  1512. #ifdef _UNICODE
  1513.     // for compatibility with MFC 3.x
  1514.     CString Format(LPCSTR pFormat) const;
  1515. #endif
  1516.     CString Format(LPCTSTR pFormat) const;
  1517.     CString Format(UINT nID) const;
  1518.  
  1519.     // serialization
  1520. #ifdef _DEBUG
  1521.     friend CDumpContext& AFXAPI operator<<(CDumpContext& dc,CTimeSpan timeSpan);
  1522. #endif
  1523.     friend CArchive& AFXAPI operator<<(CArchive& ar, CTimeSpan timeSpan);
  1524.     friend CArchive& AFXAPI operator>>(CArchive& ar, CTimeSpan& rtimeSpan);
  1525.  
  1526. private:
  1527.     time_t m_timeSpan;
  1528.     friend class CTime;
  1529. };
  1530.  
  1531. class CTime
  1532. {
  1533. public:
  1534.  
  1535. // Constructors
  1536.     static CTime PASCAL GetCurrentTime();
  1537.  
  1538.     CTime();
  1539.     CTime(time_t time);
  1540.     CTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec,
  1541.         int nDST = -1);
  1542.     CTime(WORD wDosDate, WORD wDosTime, int nDST = -1);
  1543.     CTime(const CTime& timeSrc);
  1544.  
  1545.     CTime(const SYSTEMTIME& sysTime, int nDST = -1);
  1546.     CTime(const FILETIME& fileTime, int nDST = -1);
  1547.     const CTime& operator=(const CTime& timeSrc);
  1548.     const CTime& operator=(time_t t);
  1549.  
  1550. // Attributes
  1551.     struct tm* GetGmtTm(struct tm* ptm = NULL) const;
  1552.     struct tm* GetLocalTm(struct tm* ptm = NULL) const;
  1553.     BOOL GetAsSystemTime(SYSTEMTIME& timeDest) const;
  1554.  
  1555.     time_t GetTime() const;
  1556.     int GetYear() const;
  1557.     int GetMonth() const;       // month of year (1 = Jan)
  1558.     int GetDay() const;         // day of month
  1559.     int GetHour() const;
  1560.     int GetMinute() const;
  1561.     int GetSecond() const;
  1562.     int GetDayOfWeek() const;   // 1=Sun, 2=Mon, ..., 7=Sat
  1563.  
  1564. // Operations
  1565.     // time math
  1566.     CTimeSpan operator-(CTime time) const;
  1567.     CTime operator-(CTimeSpan timeSpan) const;
  1568.     CTime operator+(CTimeSpan timeSpan) const;
  1569.     const CTime& operator+=(CTimeSpan timeSpan);
  1570.     const CTime& operator-=(CTimeSpan timeSpan);
  1571.     BOOL operator==(CTime time) const;
  1572.     BOOL operator!=(CTime time) const;
  1573.     BOOL operator<(CTime time) const;
  1574.     BOOL operator>(CTime time) const;
  1575.     BOOL operator<=(CTime time) const;
  1576.     BOOL operator>=(CTime time) const;
  1577.  
  1578.     // formatting using "C" strftime
  1579.     CString Format(LPCTSTR pFormat) const;
  1580.     CString FormatGmt(LPCTSTR pFormat) const;
  1581.     CString Format(UINT nFormatID) const;
  1582.     CString FormatGmt(UINT nFormatID) const;
  1583.  
  1584. #ifdef _UNICODE
  1585.     // for compatibility with MFC 3.x
  1586.     CString Format(LPCSTR pFormat) const;
  1587.     CString FormatGmt(LPCSTR pFormat) const;
  1588. #endif
  1589.  
  1590.     // serialization
  1591. #ifdef _DEBUG
  1592.     friend CDumpContext& AFXAPI operator<<(CDumpContext& dc, CTime time);
  1593. #endif
  1594.     friend CArchive& AFXAPI operator<<(CArchive& ar, CTime time);
  1595.     friend CArchive& AFXAPI operator>>(CArchive& ar, CTime& rtime);
  1596.  
  1597. private:
  1598.     time_t m_time;
  1599. };
  1600.  
  1601. /////////////////////////////////////////////////////////////////////////////
  1602. // File status
  1603.  
  1604. struct CFileStatus
  1605. {
  1606.     CTime m_ctime;          // creation date/time of file
  1607.     CTime m_mtime;          // last modification date/time of file
  1608.     CTime m_atime;          // last access date/time of file
  1609.     LONG m_size;            // logical size of file in bytes
  1610.     BYTE m_attribute;       // logical OR of CFile::Attribute enum values
  1611.     BYTE _m_padding;        // pad the structure to a WORD
  1612.     TCHAR m_szFullName[_MAX_PATH]; // absolute path name
  1613.  
  1614. #ifdef _DEBUG
  1615.     void Dump(CDumpContext& dc) const;
  1616. #endif
  1617. };
  1618.  
  1619. /////////////////////////////////////////////////////////////////////////////
  1620. // Diagnostic memory management routines
  1621.  
  1622. // Low level sanity checks for memory blocks
  1623. BOOL AFXAPI AfxIsValidAddress(const void* lp,
  1624.             UINT nBytes, BOOL bReadWrite = TRUE);
  1625. BOOL AFXAPI AfxIsValidString(LPCWSTR lpsz, int nLength = -1);
  1626. BOOL AFXAPI AfxIsValidString(LPCSTR lpsz, int nLength = -1);
  1627.  
  1628. #if defined(_DEBUG) && !defined(_AFX_NO_DEBUG_CRT)
  1629.  
  1630. // Memory tracking allocation
  1631. void* AFX_CDECL operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
  1632. #define DEBUG_NEW new(THIS_FILE, __LINE__)
  1633. #if _MSC_VER >= 1200
  1634. void AFX_CDECL operator delete(void* p, LPCSTR lpszFileName, int nLine);
  1635. #endif
  1636.  
  1637. void* AFXAPI AfxAllocMemoryDebug(size_t nSize, BOOL bIsObject,
  1638.     LPCSTR lpszFileName, int nLine);
  1639. void AFXAPI AfxFreeMemoryDebug(void* pbData, BOOL bIsObject);
  1640.  
  1641. // Dump any memory leaks since program started
  1642. BOOL AFXAPI AfxDumpMemoryLeaks();
  1643.  
  1644. // Return TRUE if valid memory block of nBytes
  1645. BOOL AFXAPI AfxIsMemoryBlock(const void* p, UINT nBytes,
  1646.     LONG* plRequestNumber = NULL);
  1647.  
  1648. // Return TRUE if memory is sane or print out what is wrong
  1649. BOOL AFXAPI AfxCheckMemory();
  1650.  
  1651. #define afxMemDF _crtDbgFlag
  1652.  
  1653. enum AfxMemDF // memory debug/diagnostic flags
  1654. {
  1655.     allocMemDF          = 0x01,         // turn on debugging allocator
  1656.     delayFreeMemDF      = 0x02,         // delay freeing memory
  1657.     checkAlwaysMemDF    = 0x04          // AfxCheckMemory on every alloc/free
  1658. };
  1659.  
  1660. #ifdef _UNICODE
  1661. #define AfxOutputDebugString(lpsz) \
  1662.     do \
  1663.     { \
  1664.         USES_CONVERSION; \
  1665.         _RPT0(_CRT_WARN, W2CA(lpsz)); \
  1666.     } while (0)
  1667. #else
  1668. #define AfxOutputDebugString(lpsz) _RPT0(_CRT_WARN, lpsz)
  1669. #endif
  1670.  
  1671. // turn on/off tracking for a short while
  1672. BOOL AFXAPI AfxEnableMemoryTracking(BOOL bTrack);
  1673.  
  1674. // Advanced initialization: for overriding default diagnostics
  1675. BOOL AFXAPI AfxDiagnosticInit(void);
  1676.  
  1677. // A failure hook returns whether to permit allocation
  1678. typedef BOOL (AFXAPI* AFX_ALLOC_HOOK)(size_t nSize, BOOL bObject, LONG lRequestNumber);
  1679.  
  1680. // Set new hook, return old (never NULL)
  1681. AFX_ALLOC_HOOK AFXAPI AfxSetAllocHook(AFX_ALLOC_HOOK pfnAllocHook);
  1682.  
  1683. // Debugger hook on specified allocation request - Obsolete
  1684. void AFXAPI AfxSetAllocStop(LONG lRequestNumber);
  1685.  
  1686. // Memory state for snapshots/leak detection
  1687. struct CMemoryState
  1688. {
  1689. // Attributes
  1690.     enum blockUsage
  1691.     {
  1692.         freeBlock,    // memory not used
  1693.         objectBlock,  // contains a CObject derived class object
  1694.         bitBlock,     // contains ::operator new data
  1695.         crtBlock,
  1696.         ignoredBlock,
  1697.         nBlockUseMax  // total number of usages
  1698.     };
  1699.  
  1700.     _CrtMemState m_memState;
  1701.     LONG m_lCounts[nBlockUseMax];
  1702.     LONG m_lSizes[nBlockUseMax];
  1703.     LONG m_lHighWaterCount;
  1704.     LONG m_lTotalCount;
  1705.  
  1706.     CMemoryState();
  1707.  
  1708. // Operations
  1709.     void Checkpoint();  // fill with current state
  1710.     BOOL Difference(const CMemoryState& oldState,
  1711.                     const CMemoryState& newState);  // fill with difference
  1712.     void UpdateData();
  1713.  
  1714.     // Output to afxDump
  1715.     void DumpStatistics() const;
  1716.     void DumpAllObjectsSince() const;
  1717. };
  1718.  
  1719. // Enumerate allocated objects or runtime classes
  1720. void AFXAPI AfxDoForAllObjects(void (AFX_CDECL *pfn)(CObject* pObject, void* pContext),
  1721.     void* pContext);
  1722. void AFXAPI AfxDoForAllClasses(void (AFX_CDECL *pfn)(const CRuntimeClass* pClass,
  1723.     void* pContext), void* pContext);
  1724.  
  1725. #else
  1726.  
  1727. // non-_DEBUG_ALLOC version that assume everything is OK
  1728. #define DEBUG_NEW new
  1729. #define AfxCheckMemory() TRUE
  1730. #define AfxIsMemoryBlock(p, nBytes) TRUE
  1731. #define AfxEnableMemoryTracking(bTrack) FALSE
  1732. #define AfxOutputDebugString(lpsz) ::OutputDebugString(lpsz)
  1733.  
  1734. // diagnostic initialization
  1735. #ifndef _DEBUG
  1736. #define AfxDiagnosticInit() TRUE
  1737. #else
  1738. BOOL AFXAPI AfxDiagnosticInit(void);
  1739. #endif
  1740.  
  1741. #endif // _DEBUG
  1742.  
  1743. /////////////////////////////////////////////////////////////////////////////
  1744. // Archives for serializing CObject data
  1745.  
  1746. // needed for implementation
  1747. class CPtrArray;
  1748. class CMapPtrToPtr;
  1749. class CDocument;
  1750.  
  1751. class CArchive
  1752. {
  1753. public:
  1754. // Flag values
  1755.     enum Mode { store = 0, load = 1, bNoFlushOnDelete = 2, bNoByteSwap = 4 };
  1756.  
  1757.     CArchive(CFile* pFile, UINT nMode, int nBufSize = 4096, void* lpBuf = NULL);
  1758.     ~CArchive();
  1759.  
  1760. // Attributes
  1761.     BOOL IsLoading() const;
  1762.     BOOL IsStoring() const;
  1763.     BOOL IsByteSwapping() const;
  1764.     BOOL IsBufferEmpty() const;
  1765.  
  1766.     CFile* GetFile() const;
  1767.     UINT GetObjectSchema(); // only valid when reading a CObject*
  1768.     void SetObjectSchema(UINT nSchema);
  1769.  
  1770.     // pointer to document being serialized -- must set to serialize
  1771.     //  COleClientItems in a document!
  1772.     CDocument* m_pDocument;
  1773.  
  1774. // Operations
  1775.     UINT Read(void* lpBuf, UINT nMax);
  1776.     void Write(const void* lpBuf, UINT nMax);
  1777.     void Flush();
  1778.     void Close();
  1779.     void Abort();   // close and shutdown without exceptions
  1780.  
  1781.     // reading and writing strings
  1782.     void WriteString(LPCTSTR lpsz);
  1783.     LPTSTR ReadString(LPTSTR lpsz, UINT nMax);
  1784.     BOOL ReadString(CString& rString);
  1785.  
  1786. public:
  1787.     // Object I/O is pointer based to avoid added construction overhead.
  1788.     // Use the Serialize member function directly for embedded objects.
  1789.     friend CArchive& AFXAPI operator<<(CArchive& ar, const CObject* pOb);
  1790.  
  1791.     friend CArchive& AFXAPI operator>>(CArchive& ar, CObject*& pOb);
  1792.     friend CArchive& AFXAPI operator>>(CArchive& ar, const CObject*& pOb);
  1793.  
  1794.     // insertion operations
  1795.     CArchive& operator<<(BYTE by);
  1796.     CArchive& operator<<(WORD w);
  1797.     CArchive& operator<<(LONG l);
  1798.     CArchive& operator<<(DWORD dw);
  1799.     CArchive& operator<<(float f);
  1800.     CArchive& operator<<(double d);
  1801.  
  1802.     CArchive& operator<<(int i);
  1803.     CArchive& operator<<(short w);
  1804.     CArchive& operator<<(char ch);
  1805.     CArchive& operator<<(unsigned u);
  1806.  
  1807.     // extraction operations
  1808.     CArchive& operator>>(BYTE& by);
  1809.     CArchive& operator>>(WORD& w);
  1810.     CArchive& operator>>(DWORD& dw);
  1811.     CArchive& operator>>(LONG& l);
  1812.     CArchive& operator>>(float& f);
  1813.     CArchive& operator>>(double& d);
  1814.  
  1815.     CArchive& operator>>(int& i);
  1816.     CArchive& operator>>(short& w);
  1817.     CArchive& operator>>(char& ch);
  1818.     CArchive& operator>>(unsigned& u);
  1819.  
  1820.     // object read/write
  1821.     CObject* ReadObject(const CRuntimeClass* pClass);
  1822.     void WriteObject(const CObject* pOb);
  1823.     // advanced object mapping (used for forced references)
  1824.     void MapObject(const CObject* pOb);
  1825.  
  1826.     // advanced versioning support
  1827.     void WriteClass(const CRuntimeClass* pClassRef);
  1828.     CRuntimeClass* ReadClass(const CRuntimeClass* pClassRefRequested = NULL,
  1829.         UINT* pSchema = NULL, DWORD* pObTag = NULL);
  1830.     void SerializeClass(const CRuntimeClass* pClassRef);
  1831.  
  1832.     // advanced operations (used when storing/loading many objects)
  1833.     void SetStoreParams(UINT nHashSize = 2053, UINT nBlockSize = 128);
  1834.     void SetLoadParams(UINT nGrowBy = 1024);
  1835.  
  1836. // Implementation
  1837. public:
  1838.     BOOL m_bForceFlat;  // for COleClientItem implementation (default TRUE)
  1839.     BOOL m_bDirectBuffer;   // TRUE if m_pFile supports direct buffering
  1840.     void FillBuffer(UINT nBytesNeeded);
  1841.     void CheckCount();  // throw exception if m_nMapCount is too large
  1842.  
  1843.     // special functions for reading and writing (16-bit compatible) counts
  1844.     DWORD ReadCount();
  1845.     void WriteCount(DWORD dwCount);
  1846.  
  1847.     // public for advanced use
  1848.     UINT m_nObjectSchema;
  1849.     CString m_strFileName;
  1850.  
  1851. protected:
  1852.     // archive objects cannot be copied or assigned
  1853.     CArchive(const CArchive& arSrc);
  1854.     void operator=(const CArchive& arSrc);
  1855.  
  1856.     BOOL m_nMode;
  1857.     BOOL m_bUserBuf;
  1858.     int m_nBufSize;
  1859.     CFile* m_pFile;
  1860.     BYTE* m_lpBufCur;
  1861.     BYTE* m_lpBufMax;
  1862.     BYTE* m_lpBufStart;
  1863.  
  1864.     // array/map for CObject* and CRuntimeClass* load/store
  1865.     UINT m_nMapCount;
  1866.     union
  1867.     {
  1868.         CPtrArray* m_pLoadArray;
  1869.         CMapPtrToPtr* m_pStoreMap;
  1870.     };
  1871.     // map to keep track of mismatched schemas
  1872.     CMapPtrToPtr* m_pSchemaMap;
  1873.  
  1874.     // advanced parameters (controls performance with large archives)
  1875.     UINT m_nGrowSize;
  1876.     UINT m_nHashSize;
  1877. };
  1878.  
  1879. /////////////////////////////////////////////////////////////////////////////
  1880. // Diagnostic dumping
  1881.  
  1882. // Note: AfxDumpStack is available in release builds, although it is always
  1883. //  statically linked so as to not negatively affect the size of MFC42.DLL.
  1884.  
  1885. #define AFX_STACK_DUMP_TARGET_TRACE         0x0001
  1886. #define AFX_STACK_DUMP_TARGET_CLIPBOARD 0x0002
  1887. #define AFX_STACK_DUMP_TARGET_BOTH          0x0003
  1888. #define AFX_STACK_DUMP_TARGET_ODS           0x0004
  1889. #ifdef _DEBUG
  1890. #define AFX_STACK_DUMP_TARGET_DEFAULT       AFX_STACK_DUMP_TARGET_TRACE
  1891. #else
  1892. #define AFX_STACK_DUMP_TARGET_DEFAULT       AFX_STACK_DUMP_TARGET_CLIPBOARD
  1893. #endif
  1894.  
  1895. void AFXAPI AfxDumpStack(DWORD dwFlags = AFX_STACK_DUMP_TARGET_DEFAULT);
  1896.  
  1897. class CDumpContext
  1898. {
  1899. public:
  1900.     CDumpContext(CFile* pFile = NULL);
  1901.  
  1902. // Attributes
  1903.     int GetDepth() const;      // 0 => this object, 1 => children objects
  1904.     void SetDepth(int nNewDepth);
  1905.  
  1906. // Operations
  1907.     CDumpContext& operator<<(LPCTSTR lpsz);
  1908. #ifdef _UNICODE
  1909.     CDumpContext& operator<<(LPCSTR lpsz);  // automatically widened
  1910. #else
  1911.     CDumpContext& operator<<(LPCWSTR lpsz); // automatically thinned
  1912. #endif
  1913.     CDumpContext& operator<<(const void* lp);
  1914.     CDumpContext& operator<<(const CObject* pOb);
  1915.     CDumpContext& operator<<(const CObject& ob);
  1916.     CDumpContext& operator<<(BYTE by);
  1917.     CDumpContext& operator<<(WORD w);
  1918.     CDumpContext& operator<<(UINT u);
  1919.     CDumpContext& operator<<(LONG l);
  1920.     CDumpContext& operator<<(DWORD dw);
  1921.     CDumpContext& operator<<(float f);
  1922.     CDumpContext& operator<<(double d);
  1923.     CDumpContext& operator<<(int n);
  1924.     void HexDump(LPCTSTR lpszLine, BYTE* pby, int nBytes, int nWidth);
  1925.     void Flush();
  1926.  
  1927. // Implementation
  1928. protected:
  1929.     // dump context objects cannot be copied or assigned
  1930.     CDumpContext(const CDumpContext& dcSrc);
  1931.     void operator=(const CDumpContext& dcSrc);
  1932.     void OutputString(LPCTSTR lpsz);
  1933.  
  1934.     int m_nDepth;
  1935.  
  1936. public:
  1937.     CFile* m_pFile;
  1938. };
  1939.  
  1940. #ifdef _DEBUG
  1941. extern AFX_DATA CDumpContext afxDump;
  1942. extern AFX_DATA BOOL afxTraceEnabled;
  1943. #endif
  1944.  
  1945. /////////////////////////////////////////////////////////////////////////////
  1946. // Special include for Win32s compatibility
  1947.  
  1948. #ifdef _AFX_PACKING
  1949. #pragma pack(pop)
  1950. #endif
  1951.  
  1952. #ifndef __AFXCOLL_H__
  1953.     #include <afxcoll.h>
  1954.     #ifndef __AFXSTATE_H__
  1955.         #include <afxstat_.h> // for _AFX_APP_STATE and _AFX_THREAD_STATE
  1956.     #endif
  1957. #endif
  1958.  
  1959. /////////////////////////////////////////////////////////////////////////////
  1960. // Inline function declarations
  1961.  
  1962. #ifdef _AFX_ENABLE_INLINES
  1963. #define _AFX_INLINE AFX_INLINE
  1964.  
  1965. #if !defined(_AFX_CORE_IMPL) || !defined(_AFXDLL) || defined(_DEBUG)
  1966. #define _AFX_PUBLIC_INLINE AFX_INLINE
  1967. #else
  1968. #define _AFX_PUBLIC_INLINE
  1969. #endif
  1970.  
  1971. #include <afx.inl>
  1972. #endif
  1973.  
  1974. #undef AFX_DATA
  1975. #define AFX_DATA
  1976.  
  1977. #ifdef _AFX_MINREBUILD
  1978. #pragma component(minrebuild, on)
  1979. #endif
  1980. #ifndef _AFX_FULLTYPEINFO
  1981. #pragma component(mintypeinfo, off)
  1982. #endif
  1983.  
  1984. #endif // __AFX_H__
  1985.  
  1986. /////////////////////////////////////////////////////////////////////////////
  1987.