home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / filex.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-16  |  9.0 KB  |  321 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. #include "stdafx.h"
  12. #include <errno.h>
  13.  
  14. #ifdef AFX_CORE1_SEG
  15. #pragma code_seg(AFX_CORE1_SEG)
  16. #endif
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. #ifdef _DEBUG
  24. static const LPCSTR rgszCFileExceptionCause[] =
  25. {
  26.     "none",
  27.     "generic",
  28.     "fileNotFound",
  29.     "badPath",
  30.     "tooManyOpenFiles",
  31.     "accessDenied",
  32.     "invalidFile",
  33.     "removeCurrentDir",
  34.     "directoryFull",
  35.     "badSeek",
  36.     "hardIO",
  37.     "sharingViolation",
  38.     "lockViolation",
  39.     "diskFull",
  40.     "endOfFile",
  41. };
  42. static const char szUnknown[] = "unknown";
  43. #endif
  44.  
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CFileException
  47.  
  48. void PASCAL CFileException::ThrowOsError(LONG lOsError,
  49.     LPCTSTR lpszFileName /* = NULL */)
  50. {
  51.     if (lOsError != 0)
  52.         AfxThrowFileException(CFileException::OsErrorToException(lOsError),
  53.             lOsError, lpszFileName);
  54. }
  55.  
  56. void PASCAL CFileException::ThrowErrno(int nErrno,
  57.     LPCTSTR lpszFileName /* = NULL */)
  58. {
  59.     if (nErrno != 0)
  60.         AfxThrowFileException(CFileException::ErrnoToException(nErrno),
  61.             _doserrno, lpszFileName);
  62. }
  63.  
  64. BOOL CFileException::GetErrorMessage(LPTSTR lpszError, UINT nMaxError,
  65.     PUINT pnHelpContext)
  66. {
  67.     ASSERT(lpszError != NULL && AfxIsValidString(lpszError, nMaxError));
  68.  
  69.     if (pnHelpContext != NULL)
  70.         *pnHelpContext = m_cause + AFX_IDP_FILE_NONE;
  71.  
  72.     CString strMessage;
  73.     CString strFileName = m_strFileName;
  74.     if (strFileName.IsEmpty())
  75.         strFileName.LoadString(AFX_IDS_UNNAMED_FILE);
  76.     AfxFormatString1(strMessage,
  77.         m_cause + AFX_IDP_FILE_NONE, strFileName);
  78.     lstrcpyn(lpszError, strMessage, nMaxError);
  79.  
  80.     return TRUE;
  81. }
  82.  
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CFileException diagnostics
  85.  
  86. #ifdef _DEBUG
  87. void CFileException::Dump(CDumpContext& dc) const
  88. {
  89.     CObject::Dump(dc);
  90.  
  91.     dc << "m_cause = ";
  92.     if (m_cause >= 0 && m_cause < _countof(rgszCFileExceptionCause))
  93.         dc << rgszCFileExceptionCause[m_cause];
  94.     else
  95.         dc << szUnknown;
  96.     dc << "\nm_lOsError = " << (void*)m_lOsError;
  97.  
  98.     dc << "\n";
  99. }
  100. #endif
  101.  
  102. /////////////////////////////////////////////////////////////////////////////
  103. // CFileException helpers
  104.  
  105. void AFXAPI AfxThrowFileException(int cause, LONG lOsError,
  106.     LPCTSTR lpszFileName /* == NULL */)
  107. {
  108. #ifdef _DEBUG
  109.     LPCSTR lpsz;
  110.     if (cause >= 0 && cause < _countof(rgszCFileExceptionCause))
  111.         lpsz = rgszCFileExceptionCause[cause];
  112.     else
  113.         lpsz = szUnknown;
  114.     TRACE3("CFile exception: %hs, File %s, OS error information = %ld.\n",
  115.         lpsz, (lpszFileName == NULL) ? _T("Unknown") : lpszFileName, lOsError);
  116. #endif
  117.     THROW(new CFileException(cause, lOsError, lpszFileName));
  118. }
  119.  
  120. int PASCAL CFileException::ErrnoToException(int nErrno)
  121. {
  122.     switch(nErrno)
  123.     {
  124.     case EPERM:
  125.     case EACCES:
  126.         return CFileException::accessDenied;
  127.     case EBADF:
  128.         return CFileException::invalidFile;
  129.     case EDEADLOCK:
  130.         return CFileException::sharingViolation;
  131.     case EMFILE:
  132.         return CFileException::tooManyOpenFiles;
  133.     case ENOENT:
  134.     case ENFILE:
  135.         return CFileException::fileNotFound;
  136.     case ENOSPC:
  137.         return CFileException::diskFull;
  138.     case EINVAL:
  139.     case EIO:
  140.         return CFileException::hardIO;
  141.     default:
  142.         return CFileException::generic;
  143.     }
  144. }
  145.  
  146. int PASCAL CFileException::OsErrorToException(LONG lOsErr)
  147. {
  148.     // NT Error codes
  149.     switch ((UINT)lOsErr)
  150.     {
  151.     case NO_ERROR:
  152.         return CFileException::none;
  153.     case ERROR_FILE_NOT_FOUND:
  154.         return CFileException::fileNotFound;
  155.     case ERROR_PATH_NOT_FOUND:
  156.         return CFileException::badPath;
  157.     case ERROR_TOO_MANY_OPEN_FILES:
  158.         return CFileException::tooManyOpenFiles;
  159.     case ERROR_ACCESS_DENIED:
  160.         return CFileException::accessDenied;
  161.     case ERROR_INVALID_HANDLE:
  162.         return CFileException::fileNotFound;
  163.     case ERROR_BAD_FORMAT:
  164.         return CFileException::invalidFile;
  165.     case ERROR_INVALID_ACCESS:
  166.         return CFileException::accessDenied;
  167.     case ERROR_INVALID_DRIVE:
  168.         return CFileException::badPath;
  169.     case ERROR_CURRENT_DIRECTORY:
  170.         return CFileException::removeCurrentDir;
  171.     case ERROR_NOT_SAME_DEVICE:
  172.         return CFileException::badPath;
  173.     case ERROR_NO_MORE_FILES:
  174.         return CFileException::fileNotFound;
  175.     case ERROR_WRITE_PROTECT:
  176.         return CFileException::accessDenied;
  177.     case ERROR_BAD_UNIT:
  178.         return CFileException::hardIO;
  179.     case ERROR_NOT_READY:
  180.         return CFileException::hardIO;
  181.     case ERROR_BAD_COMMAND:
  182.         return CFileException::hardIO;
  183.     case ERROR_CRC:
  184.         return CFileException::hardIO;
  185.     case ERROR_BAD_LENGTH:
  186.         return CFileException::badSeek;
  187.     case ERROR_SEEK:
  188.         return CFileException::badSeek;
  189.     case ERROR_NOT_DOS_DISK:
  190.         return CFileException::invalidFile;
  191.     case ERROR_SECTOR_NOT_FOUND:
  192.         return CFileException::badSeek;
  193.     case ERROR_WRITE_FAULT:
  194.         return CFileException::accessDenied;
  195.     case ERROR_READ_FAULT:
  196.         return CFileException::badSeek;
  197.     case ERROR_SHARING_VIOLATION:
  198.         return CFileException::sharingViolation;
  199.     case ERROR_LOCK_VIOLATION:
  200.         return CFileException::lockViolation;
  201.     case ERROR_WRONG_DISK:
  202.         return CFileException::badPath;
  203.     case ERROR_SHARING_BUFFER_EXCEEDED:
  204.         return CFileException::tooManyOpenFiles;
  205.     case ERROR_HANDLE_EOF:
  206.         return CFileException::endOfFile;
  207.     case ERROR_HANDLE_DISK_FULL:
  208.         return CFileException::diskFull;
  209.     case ERROR_DUP_NAME:
  210.         return CFileException::badPath;
  211.     case ERROR_BAD_NETPATH:
  212.         return CFileException::badPath;
  213.     case ERROR_NETWORK_BUSY:
  214.         return CFileException::accessDenied;
  215.     case ERROR_DEV_NOT_EXIST:
  216.         return CFileException::badPath;
  217.     case ERROR_ADAP_HDW_ERR:
  218.         return CFileException::hardIO;
  219.     case ERROR_BAD_NET_RESP:
  220.         return CFileException::accessDenied;
  221.     case ERROR_UNEXP_NET_ERR:
  222.         return CFileException::hardIO;
  223.     case ERROR_BAD_REM_ADAP:
  224.         return CFileException::invalidFile;
  225.     case ERROR_NO_SPOOL_SPACE:
  226.         return CFileException::directoryFull;
  227.     case ERROR_NETNAME_DELETED:
  228.         return CFileException::accessDenied;
  229.     case ERROR_NETWORK_ACCESS_DENIED:
  230.         return CFileException::accessDenied;
  231.     case ERROR_BAD_DEV_TYPE:
  232.         return CFileException::invalidFile;
  233.     case ERROR_BAD_NET_NAME:
  234.         return CFileException::badPath;
  235.     case ERROR_TOO_MANY_NAMES:
  236.         return CFileException::tooManyOpenFiles;
  237.     case ERROR_SHARING_PAUSED:
  238.         return CFileException::badPath;
  239.     case ERROR_REQ_NOT_ACCEP:
  240.         return CFileException::accessDenied;
  241.     case ERROR_FILE_EXISTS:
  242.         return CFileException::accessDenied;
  243.     case ERROR_CANNOT_MAKE:
  244.         return CFileException::accessDenied;
  245.     case ERROR_ALREADY_ASSIGNED:
  246.         return CFileException::badPath;
  247.     case ERROR_INVALID_PASSWORD:
  248.         return CFileException::accessDenied;
  249.     case ERROR_NET_WRITE_FAULT:
  250.         return CFileException::hardIO;
  251.     case ERROR_DISK_CHANGE:
  252.         return CFileException::fileNotFound;
  253.     case ERROR_DRIVE_LOCKED:
  254.         return CFileException::lockViolation;
  255.     case ERROR_BUFFER_OVERFLOW:
  256.         return CFileException::badPath;
  257.     case ERROR_DISK_FULL:
  258.         return CFileException::diskFull;
  259.     case ERROR_NO_MORE_SEARCH_HANDLES:
  260.         return CFileException::tooManyOpenFiles;
  261.     case ERROR_INVALID_TARGET_HANDLE:
  262.         return CFileException::invalidFile;
  263.     case ERROR_INVALID_CATEGORY:
  264.         return CFileException::hardIO;
  265.     case ERROR_INVALID_NAME:
  266.         return CFileException::badPath;
  267.     case ERROR_INVALID_LEVEL:
  268.         return CFileException::badPath;
  269.     case ERROR_NO_VOLUME_LABEL:
  270.         return CFileException::badPath;
  271.     case ERROR_NEGATIVE_SEEK:
  272.         return CFileException::badSeek;
  273.     case ERROR_SEEK_ON_DEVICE:
  274.         return CFileException::badSeek;
  275.     case ERROR_DIR_NOT_ROOT:
  276.         return CFileException::badPath;
  277.     case ERROR_DIR_NOT_EMPTY:
  278.         return CFileException::removeCurrentDir;
  279.     case ERROR_LABEL_TOO_LONG:
  280.         return CFileException::badPath;
  281.     case ERROR_BAD_PATHNAME:
  282.         return CFileException::badPath;
  283.     case ERROR_LOCK_FAILED:
  284.         return CFileException::lockViolation;
  285.     case ERROR_BUSY:
  286.         return CFileException::accessDenied;
  287.     case ERROR_INVALID_ORDINAL:
  288.         return CFileException::invalidFile;
  289.     case ERROR_ALREADY_EXISTS:
  290.         return CFileException::accessDenied;
  291.     case ERROR_INVALID_EXE_SIGNATURE:
  292.         return CFileException::invalidFile;
  293.     case ERROR_BAD_EXE_FORMAT:
  294.         return CFileException::invalidFile;
  295.     case ERROR_FILENAME_EXCED_RANGE:
  296.         return CFileException::badPath;
  297.     case ERROR_META_EXPANSION_TOO_LONG:
  298.         return CFileException::badPath;
  299.     case ERROR_DIRECTORY:
  300.         return CFileException::badPath;
  301.     case ERROR_OPERATION_ABORTED:
  302.         return CFileException::hardIO;
  303.     case ERROR_IO_INCOMPLETE:
  304.         return CFileException::hardIO;
  305.     case ERROR_IO_PENDING:
  306.         return CFileException::hardIO;
  307.     case ERROR_SWAPERROR:
  308.         return CFileException::accessDenied;
  309.     default:
  310.         return CFileException::generic;
  311.     }
  312. }
  313.  
  314. #ifdef AFX_INIT_SEG
  315. #pragma code_seg(AFX_INIT_SEG)
  316. #endif
  317.  
  318. IMPLEMENT_DYNAMIC(CFileException, CException)
  319.  
  320. /////////////////////////////////////////////////////////////////////////////
  321.