home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / FILEX.CP_ / FILEX.CP
Encoding:
Text File  |  1993-02-08  |  5.9 KB  |  252 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 and/or WinHelp 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_AUX_SEG
  15. #pragma code_seg(AFX_AUX_SEG)
  16. #endif
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char BASED_CODE THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. // Global helpers to do huge file reading and writing
  24. // These are not needed for 32 bit implementations, but inline
  25. // delegation routines are in afx.inl
  26.  
  27. static UINT _AfxCalcSize(DWORD cbTotal, const void FAR* lpStart)
  28. {
  29.     // return size to read/write (16K max unless limited by segment bounds)
  30.     DWORD cb = 0x10000L - _AFX_FP_OFF(lpStart);
  31.     if (cb > cbTotal)
  32.         cb = cbTotal;
  33.     return (cb > 16384) ? 16384 : (UINT)cb;
  34. }
  35.  
  36. DWORD
  37. CFile::ReadHuge(void FAR* lpBuffer, DWORD dwCount)
  38. {
  39.     ASSERT_VALID(this);
  40.  
  41.     DWORD dwToRead = dwCount;
  42.     while (dwToRead > 0)
  43.     {
  44.         UINT nRead = _AfxCalcSize(dwToRead, lpBuffer);
  45.         if (Read(lpBuffer, nRead) < nRead)
  46.             return ((dwCount - dwToRead) + nRead);
  47.         dwToRead -= nRead;
  48.         lpBuffer = ((BYTE _huge*)lpBuffer) + nRead;
  49.     }
  50.     return dwCount;
  51. }
  52.  
  53. void
  54. CFile::WriteHuge(const void FAR* lpBuffer, DWORD dwCount)
  55. {
  56.     ASSERT_VALID(this);
  57.  
  58.     DWORD dwToWrite = dwCount;
  59.     while (dwToWrite > 0)
  60.     {
  61.         UINT nWrite = _AfxCalcSize(dwToWrite, lpBuffer);
  62.         Write(lpBuffer, nWrite);
  63.         dwToWrite -= nWrite;
  64.         lpBuffer = ((const BYTE _huge*)lpBuffer) + nWrite;
  65.     }
  66. }
  67.  
  68.  
  69. #ifdef _DEBUG
  70.  
  71. // character strings to use for dumping CFileException
  72. static char BASED_CODE szNone[] = "none";
  73. static char BASED_CODE szGeneric[] = "generic";
  74. static char BASED_CODE szFileNotFound[] = "fileNotFound";
  75. static char BASED_CODE szBadPath[] = "badPath";
  76. static char BASED_CODE szTooManyOpenFiles[] = "tooManyOpenFiles";
  77. static char BASED_CODE szAccessDenied[] = "accessDenied";
  78. static char BASED_CODE szInvalidFile[] = "invalidFile";
  79. static char BASED_CODE szRemoveCurrentDir[] = "removeCurrentDir";
  80. static char BASED_CODE szDirectoryFull[] = "directoryFull";
  81. static char BASED_CODE szBadSeek[] = "badSeek";
  82. static char BASED_CODE szHardIO[] = "hardIO";
  83. static char BASED_CODE szSharingViolation[] = "sharingViolation";
  84. static char BASED_CODE szLockViolation[] = "lockViolation";
  85. static char BASED_CODE szDiskFull[] = "diskFull";
  86. static char BASED_CODE szEndOfFile[] = "endOfFile";
  87.  
  88. static LPCSTR BASED_CODE rgszCFileExceptionCause[] =
  89. {
  90.     szNone,
  91.     szGeneric,
  92.     szFileNotFound,
  93.     szBadPath,
  94.     szTooManyOpenFiles,
  95.     szAccessDenied,
  96.     szInvalidFile,
  97.     szRemoveCurrentDir,
  98.     szDirectoryFull,
  99.     szBadSeek,
  100.     szHardIO,
  101.     szSharingViolation,
  102.     szLockViolation,
  103.     szDiskFull,
  104.     szEndOfFile,
  105. };
  106.  
  107. static char BASED_CODE szUnknown[] = "unknown";
  108. #endif
  109.  
  110. /////////////////////////////////////////////////////////////////////////////
  111. // CFileException
  112.  
  113. IMPLEMENT_DYNAMIC(CFileException, CException)
  114.  
  115. void PASCAL
  116. CFileException::ThrowOsError(LONG lOsError)
  117. {
  118.     if (lOsError != 0)
  119.         AfxThrowFileException(CFileException::OsErrorToException(lOsError),
  120.             lOsError);
  121. }
  122.  
  123. void PASCAL
  124. CFileException::ThrowErrno(int nErrno)
  125. {
  126.     if (nErrno != 0)
  127.         AfxThrowFileException(CFileException::ErrnoToException(nErrno),
  128.             _doserrno);
  129. }
  130.  
  131.  
  132. #ifdef _DEBUG
  133. void
  134. CFileException::Dump(CDumpContext& dc) const
  135. {
  136.     CObject::Dump(dc);
  137.     AFX_DUMP0(dc, " m_cause = ");
  138.  
  139.     if (m_cause >= 0 &&
  140.         m_cause < sizeof(rgszCFileExceptionCause) / sizeof(LPCSTR))
  141.     {
  142.         dc << rgszCFileExceptionCause[m_cause];
  143.     }
  144.     else
  145.     {
  146.         dc << szUnknown;
  147.     }
  148.     AFX_DUMP1(dc, ", lOsError = ", m_lOsError);
  149. }
  150. #endif
  151.  
  152. void AFXAPI
  153. AfxThrowFileException(int cause, LONG lOsError)
  154. {
  155. #ifdef _DEBUG
  156.     LPCSTR lpsz;
  157.     if (cause >= 0 &&
  158.         cause < sizeof(rgszCFileExceptionCause) / sizeof(LPCSTR))
  159.     {
  160.         lpsz = rgszCFileExceptionCause[cause];
  161.     }
  162.     else
  163.     {
  164.         lpsz = szUnknown;
  165.     }
  166.     TRACE2("CFile exception: %Fs, OS error information = %ld\n",
  167.         lpsz, lOsError);
  168. #endif
  169.     THROW(new CFileException(cause, lOsError));
  170. }
  171.  
  172.  
  173. int PASCAL
  174. CFileException::ErrnoToException(int nErrno)
  175. {
  176.     switch(nErrno)
  177.     {
  178.     case EPERM:
  179.     case EACCES:
  180.         return CFileException::accessDenied;
  181.     case EBADF:
  182.         return CFileException::invalidFile;
  183.     case EDEADLOCK:
  184.         return CFileException::sharingViolation;
  185.     case EMFILE:
  186.         return CFileException::tooManyOpenFiles;
  187.     case ENOENT:
  188.     case ENFILE:
  189.         return CFileException::fileNotFound;
  190.     case ENOSPC:
  191.         return CFileException::diskFull;
  192.     case EINVAL:
  193.     case EIO:
  194.         return CFileException::hardIO;
  195.     default:
  196.         return CFileException::generic;
  197.     }
  198. }
  199.  
  200.  
  201. int PASCAL CFileException::OsErrorToException(LONG lOsErr)
  202. {
  203.     switch ((UINT)lOsErr)
  204.     {
  205.     // DOS Error codes
  206.     case 0x1:
  207.         return CFileException::generic;
  208.     case 0x2:
  209.         return CFileException::fileNotFound;
  210.     case 0x3:
  211.         return CFileException::badPath;
  212.     case 0x4:
  213.         return CFileException::tooManyOpenFiles;
  214.     case 0x5:
  215.         return CFileException::accessDenied;
  216.     case 0x6:
  217.         return CFileException::invalidFile;
  218.     case 0xf:
  219.         return CFileException::badPath;
  220.     case 0x10:
  221.         return CFileException::removeCurrentDir;
  222.     case 0x12:
  223.         return CFileException::directoryFull;
  224.     case 0x19:
  225.         return CFileException::badSeek;
  226.     case 0x1d:
  227.         return CFileException::hardIO;
  228.     case 0x1e:
  229.         return CFileException::hardIO;
  230.     case 0x1f:
  231.         return CFileException::hardIO;
  232.     case 0x58:
  233.         return CFileException::hardIO;
  234.     case 0x20:
  235.         return CFileException::sharingViolation;
  236.     case 0x21:
  237.         return CFileException::lockViolation;
  238.     case 0x23:
  239.         return CFileException::tooManyOpenFiles;
  240.     case 0x24:
  241.         return CFileException::sharingViolation;
  242.     case 0x41:
  243.         return CFileException::accessDenied;
  244.     case 0x43:
  245.         return CFileException::fileNotFound;
  246.     case 0x52:
  247.         return CFileException::accessDenied;
  248.     default:
  249.         return CFileException::generic;
  250.     }
  251. }
  252.