home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / FILEST.CP_ / FILEST.CP
Encoding:
Text File  |  1993-02-08  |  5.1 KB  |  190 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. #include <io.h>
  14. #include <limits.h>
  15. #include <malloc.h>
  16.  
  17. #include <sys\types.h>
  18. #include <sys\stat.h>
  19. #include <dos.h>
  20.  
  21. #ifdef AFX_AUX_SEG
  22. #pragma code_seg(AFX_AUX_SEG)
  23. #endif
  24.  
  25. #ifdef _DEBUG
  26. #undef THIS_FILE
  27. static char BASED_CODE THIS_FILE[] = __FILE__;
  28. #endif
  29.  
  30. #define new DEBUG_NEW
  31.  
  32. ////////////////////////////////////////////////////////////////////////////
  33. // Status information for all file classes 
  34. // In this file so everyone doesn't get the CTime package
  35.  
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CFileStatus implementation
  38. #ifdef _DEBUG
  39. void
  40. CFileStatus::Dump(CDumpContext& dc) const
  41. {           
  42.     AFX_DUMP0(dc, "file status information:");
  43.     AFX_DUMP1(dc, "\nm_ctime = ", m_ctime);
  44.     AFX_DUMP1(dc, "\nm_mtime = ", m_mtime);
  45.     AFX_DUMP1(dc, "\nm_atime = ", m_atime);
  46.     AFX_DUMP1(dc, "\nm_size = ", m_size);
  47.     AFX_DUMP1(dc, "\nm_attribute = ", m_attribute);
  48.     AFX_DUMP1(dc, "\nm_szFullName = ", m_szFullName);
  49. }
  50. #endif
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CFile Status implementation
  54. BOOL
  55. CFile::GetStatus(CFileStatus& rStatus) const
  56. {
  57.     ASSERT_VALID(this);
  58.     ASSERT(m_hFile != (UINT)hFileNull);
  59.  
  60.     //NOTE: cannot return name of file from handle
  61.     rStatus.m_szFullName[0] = '\0';
  62.  
  63.     struct _stat s;
  64.  
  65.     if (_fstat(m_hFile, &s) == 0)
  66.     {
  67.         rStatus.m_ctime = CTime(s.st_atime);
  68.         rStatus.m_atime = rStatus.m_ctime;
  69.         rStatus.m_mtime = rStatus.m_ctime;
  70.         rStatus.m_size = s.st_size;
  71.         rStatus.m_attribute = 0;    // dos won't give us this from
  72.                                     // just a fd, need the path name
  73.         return TRUE;
  74.     }   
  75.     return FALSE;
  76. }
  77.  
  78. BOOL PASCAL
  79. CFile::GetStatus(const char* pszFileName, CFileStatus& rStatus)
  80. {
  81.     // first fill in the full name of the file, undefined if we return FALSE
  82. #ifdef _WINDOWS
  83.     // use the canonicalize helper function
  84.     if (!_AfxFullPath(rStatus.m_szFullName, pszFileName))
  85.     {
  86.         rStatus.m_szFullName[0] = '\0';
  87.         return FALSE;
  88.     }
  89. #else
  90.     if (_fullpath(rStatus.m_szFullName, pszFileName, _MAX_PATH) == NULL)
  91.     {
  92.         rStatus.m_szFullName[0] = '\0';
  93.         return FALSE;
  94.     }
  95. #endif
  96.  
  97.     // finish filling in the structure
  98.     WORD wAttr = CFile::normal | CFile::readOnly |
  99.                 CFile::hidden | CFile::system |
  100.                 CFile::directory | CFile::archive;
  101.  
  102.     struct _find_t find;
  103.     char sz[_MAX_PATH];
  104.     AnsiToOem(pszFileName, sz);
  105.  
  106.     if (_dos_findfirst(sz, wAttr, &find) == 0)
  107.     {
  108.         rStatus.m_mtime = CTime((WORD)find.wr_date, (WORD)find.wr_time);
  109.         rStatus.m_ctime = rStatus.m_mtime;
  110.         rStatus.m_atime = rStatus.m_mtime;
  111.  
  112.         rStatus.m_size = find.size;
  113.         rStatus.m_attribute = find.attrib;
  114.         return TRUE;
  115.     }
  116.     else
  117.         return FALSE;
  118. }
  119.  
  120.  
  121. void PASCAL
  122. CFile::SetStatus(const char* pszFileName, const CFileStatus& status)
  123. {
  124.     UINT nErr;
  125.     UINT wAttr;
  126.     char sz[_MAX_PATH];
  127.  
  128.     AnsiToOem(pszFileName, sz);
  129.  
  130.     if ((nErr = _dos_getfileattr(sz, &wAttr)) != 0)
  131.         CFileException::ThrowOsError(nErr);
  132.  
  133.     if (status.m_attribute != wAttr && (wAttr & CFile::readOnly))
  134.     {
  135.         // Set file attribute, only if currently readonly.
  136.         // This way we will be able to modify the time assuming the
  137.         // caller changed the file from readonly.
  138.         if ((nErr = _dos_setfileattr(sz, status.m_attribute)) != 0)
  139.             CFileException::ThrowOsError(nErr);
  140.     }
  141.  
  142.     if (status.m_mtime.GetTime() != 0)
  143.     {
  144.         WORD wDate, wTime;
  145.         int handle;
  146.  
  147.         // set the file date/time
  148.         if ((nErr = _dos_open(sz, CFile::modeReadWrite, &handle)) != 0)
  149.             CFileException::ThrowOsError(nErr);
  150.  
  151.         wDate = (WORD)(((UINT)status.m_mtime.GetYear() - 1980) << 9);
  152.         wDate += (WORD)(((UINT)status.m_mtime.GetMonth()) << 5);
  153.         wDate += (WORD)((UINT)status.m_mtime.GetDay());
  154.  
  155.         wTime = (WORD)((UINT)(status.m_mtime.GetHour()) << 11);
  156.         wTime += (WORD)((UINT)status.m_mtime.GetMinute() << 5);
  157.         wTime += (WORD)((UINT)status.m_mtime.GetSecond() >> 1);
  158.  
  159.         if ((nErr = _dos_setftime(handle, wDate, wTime)) != 0)
  160.             CFileException::ThrowOsError(nErr);
  161.  
  162.         if ((nErr = _dos_close(handle)) != 0)
  163.             CFileException::ThrowOsError(nErr);
  164.     }
  165.  
  166.     if (status.m_attribute != wAttr && !(wAttr & CFile::readOnly))
  167.     {
  168.         // Set file attribute, only if currently not readonly.
  169.         if ((nErr = _dos_setfileattr(sz, status.m_attribute)) != 0)
  170.             CFileException::ThrowOsError(nErr);
  171.     }
  172.  
  173. }
  174.  
  175. ///////////////////////////////////////////////////////////////////////////////
  176. // CMemFile::GetStatus implementation
  177. BOOL
  178. CMemFile::GetStatus(CFileStatus& rStatus) const
  179. {
  180.     ASSERT_VALID(this);
  181.  
  182.     rStatus.m_ctime = 0;
  183.     rStatus.m_mtime = 0;
  184.     rStatus.m_atime = 0;
  185.     rStatus.m_size = m_nFileSize;
  186.     rStatus.m_attribute = CFile::normal;
  187.     rStatus.m_szFullName[0] = '\0';
  188.     return TRUE;
  189. }
  190.