home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / FILEX.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  9.1 KB  |  327 lines

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