home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK10 / MFC / SRC / FILEX.CP$ / filex
Encoding:
Text File  |  1992-03-08  |  4.9 KB  |  187 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and Microsoft
  7. // QuickHelp documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "afx.h"
  12.  
  13. #pragma hdrstop
  14.  
  15. #include <errno.h>
  16.  
  17. #ifdef AFX_AUX_SEG
  18. #pragma code_seg(AFX_AUX_SEG)
  19. #endif
  20.  
  21. #ifdef _DEBUG
  22. #undef THIS_FILE
  23. static char BASED_CODE THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. #ifdef _DEBUG
  27.  
  28. // character strings to use for dumping CFileException
  29. static char BASED_CODE szNone[] = "none";
  30. static char BASED_CODE szGeneric[] = "generic";
  31. static char BASED_CODE szFileNotFound[] = "fileNotFound";
  32. static char BASED_CODE szBadPath[] = "badPath";
  33. static char BASED_CODE szTooManyOpenFiles[] = "tooManyOpenFiles";
  34. static char BASED_CODE szAccessDenied[] = "accessDenied";
  35. static char BASED_CODE szInvalidFile[] = "invalidFile";
  36. static char BASED_CODE szRemoveCurrentDir[] = "removeCurrentDir";
  37. static char BASED_CODE szDirectoryFull[] = "directoryFull";
  38. static char BASED_CODE szBadSeek[] = "badSeek";
  39. static char BASED_CODE szHardIO[] = "hardIO";
  40. static char BASED_CODE szSharingViolation[] = "sharingViolation";
  41. static char BASED_CODE szLockViolation[] = "lockViolation";
  42. static char BASED_CODE szDiskFull[] = "diskFull";
  43. static char BASED_CODE szEndOfFile[] = "endOfFile";
  44.  
  45. static char FAR* BASED_CODE rgszCFileExceptionCause[] =
  46. {
  47.     szNone,
  48.     szGeneric,
  49.     szFileNotFound,
  50.     szBadPath,
  51.     szTooManyOpenFiles,
  52.     szAccessDenied,
  53.     szInvalidFile,
  54.     szRemoveCurrentDir,
  55.     szDirectoryFull,
  56.     szBadSeek,
  57.     szHardIO,
  58.     szSharingViolation,
  59.     szLockViolation,
  60.     szDiskFull,
  61.     szEndOfFile,
  62. };
  63.  
  64. static char BASED_CODE szUnknown[] = "unknown";
  65. #endif
  66.  
  67. /////////////////////////////////////////////////////////////////////////////
  68. // CFileException
  69.  
  70. IMPLEMENT_DYNAMIC(CFileException, CException)
  71.  
  72. void
  73. CFileException::ThrowOsError(LONG lOsError)
  74. {
  75.     if (lOsError != 0)
  76.         AfxThrowFileException(CFileException::OsErrorToException(lOsError),
  77.             lOsError);
  78. }
  79.  
  80. void
  81. CFileException::ThrowErrno(int nErrno)
  82. {
  83.     if (nErrno != 0)
  84.         AfxThrowFileException(CFileException::ErrnoToException(nErrno),
  85.             _doserrno);
  86. }
  87.  
  88.  
  89. #ifdef _DEBUG
  90. void
  91. CFileException::Dump(CDumpContext& dc) const
  92. {
  93.     CObject::Dump(dc);
  94.     dc << " m_cause = ";
  95.  
  96.     if (m_cause >= 0 &&
  97.         m_cause < sizeof(rgszCFileExceptionCause) / sizeof(char FAR*))
  98.     {
  99.         dc << rgszCFileExceptionCause[m_cause];
  100.     }
  101.     else
  102.     {
  103.         dc << szUnknown;
  104.     }
  105.     dc << ", lOsError = " << m_lOsError;
  106. }
  107. #endif
  108.  
  109. void
  110. PASCAL AfxThrowFileException(int cause, LONG lOsError)
  111. {
  112. #ifdef _DEBUG
  113.     TRACE("CFile exception: ");
  114.     if (cause >= 0 &&
  115.         cause < sizeof(rgszCFileExceptionCause) / sizeof(char FAR*))
  116.     {
  117.         afxDump << (char FAR*)rgszCFileExceptionCause[cause];
  118.     }
  119.     else
  120.     {
  121.         afxDump << (char FAR*)szUnknown;
  122.     }
  123.     afxDump << ", OS error information = " << (void FAR*)lOsError << "\n";
  124. #endif
  125.     THROW(new CFileException(cause, lOsError));
  126. }
  127.  
  128.  
  129. int
  130. CFileException::ErrnoToException(int nErrno)
  131. {
  132.     switch(nErrno)
  133.     {
  134.         case EPERM:
  135.         case EACCES:
  136.                         return CFileException::accessDenied;
  137.         case EBADF:     return CFileException::invalidFile;
  138.         case EDEADLOCK: return CFileException::sharingViolation;
  139.         case EMFILE:    return CFileException::tooManyOpenFiles;
  140.         case ENOENT:
  141.         case ENFILE:
  142.                         return CFileException::fileNotFound;
  143.         case ENOSPC:    return CFileException::diskFull;
  144.         case EINVAL:
  145.         case EIO:       return CFileException::hardIO;
  146.         default:
  147.                         return CFileException::generic;
  148.     }
  149. }
  150.  
  151.  
  152. #ifdef _DOSWIN
  153. int
  154. CFileException::OsErrorToException(LONG lOsErr)
  155. {
  156.     switch ((UINT)lOsErr)
  157.     {
  158.         // DOS Error codes
  159.         case 0x1: return CFileException::generic;
  160.         case 0x2: return CFileException::fileNotFound;
  161.         case 0x3: return CFileException::badPath;
  162.         case 0x4: return CFileException::tooManyOpenFiles;
  163.         case 0x5: return CFileException::accessDenied;
  164.         case 0x6: return CFileException::invalidFile;
  165.         case 0xf: return CFileException::badPath;
  166.         case 0x10: return CFileException::removeCurrentDir;
  167.         case 0x12: return CFileException::directoryFull;
  168.         case 0x19: return CFileException::badSeek;
  169.         case 0x1d: return CFileException::hardIO;
  170.         case 0x1e: return CFileException::hardIO;
  171.         case 0x1f: return CFileException::hardIO;
  172.         case 0x58: return CFileException::hardIO;
  173.         case 0x20: return CFileException::sharingViolation;
  174.         case 0x21: return CFileException::lockViolation;
  175.         case 0x23: return CFileException::tooManyOpenFiles;
  176.         case 0x24: return CFileException::sharingViolation;
  177.         case 0x41: return CFileException::accessDenied;
  178.         case 0x43: return CFileException::fileNotFound;
  179.         case 0x52: return CFileException::accessDenied;
  180.         default: return CFileException::generic;
  181.     }
  182. }
  183. #endif // _WINDOWS
  184.  
  185.  
  186.  
  187.