home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / FILEST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-02  |  7.9 KB  |  289 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. #include <io.h>
  14. #include <sys\types.h>
  15. #include <sys\stat.h>
  16.  
  17. #ifdef AFX_CORE1_SEG
  18. #pragma code_seg(AFX_CORE1_SEG)
  19. #endif
  20.  
  21. #ifdef _DEBUG
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. #define new DEBUG_NEW
  27.  
  28. ////////////////////////////////////////////////////////////////////////////
  29. // Status information for all file classes
  30. // In this file so everyone doesn't get the CTime package
  31.  
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CFileStatus diagnostics
  34.  
  35. #ifdef _DEBUG
  36. void CFileStatus::Dump(CDumpContext& dc) const
  37. {
  38.     dc << "a CFileStatus at " << (void*)this;
  39.  
  40.     dc << "\nm_ctime = " << m_ctime;
  41.     dc << "\nm_mtime = " << m_mtime;
  42.     dc << "\nm_atime = " << m_atime;
  43.     dc << "\nm_size = " << m_size;
  44.     dc << "\nm_attribute = " << m_attribute;
  45.     dc << "\nm_szFullName = " << m_szFullName;
  46.  
  47.     dc << "\n";
  48. }
  49. #endif
  50.  
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CFile name handlers
  53.  
  54. CString CFile::GetFileName() const
  55. {
  56.     ASSERT_VALID(this);
  57.  
  58.     CFileStatus status;
  59.     GetStatus(status);
  60.     CString strResult;
  61.     AfxGetFileName(status.m_szFullName, strResult.GetBuffer(_MAX_FNAME),
  62.         _MAX_FNAME);
  63.     strResult.ReleaseBuffer();
  64.     return strResult;
  65. }
  66.  
  67. CString CFile::GetFileTitle() const
  68. {
  69.     ASSERT_VALID(this);
  70.  
  71.     CFileStatus status;
  72.     GetStatus(status);
  73.     CString strResult;
  74.     AfxGetFileTitle(status.m_szFullName, strResult.GetBuffer(_MAX_FNAME),
  75.         _MAX_FNAME);
  76.     strResult.ReleaseBuffer();
  77.     return strResult;
  78. }
  79.  
  80. CString CFile::GetFilePath() const
  81. {
  82.     ASSERT_VALID(this);
  83.  
  84.     CFileStatus status;
  85.     GetStatus(status);
  86.     return status.m_szFullName;
  87. }
  88.  
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CFile Status implementation
  91.  
  92. BOOL CFile::GetStatus(CFileStatus& rStatus) const
  93. {
  94.     ASSERT_VALID(this);
  95.  
  96.     memset(&rStatus, 0, sizeof(CFileStatus));
  97.  
  98.     // copy file name from cached m_strFileName
  99.     lstrcpyn(rStatus.m_szFullName, m_strFileName,
  100.         _countof(rStatus.m_szFullName));
  101.  
  102.     if (m_hFile != hFileNull)
  103.     {
  104.         // get time current file size
  105.         FILETIME ftCreate, ftAccess, ftModify;
  106.         if (!::GetFileTime((HANDLE)m_hFile, &ftCreate, &ftAccess, &ftModify))
  107.             return FALSE;
  108.  
  109.         if ((rStatus.m_size = ::GetFileSize((HANDLE)m_hFile, NULL)) == (DWORD)-1L)
  110.             return FALSE;
  111.  
  112. #ifdef _MAC
  113.         rStatus.m_attribute = 0;
  114. #else
  115.         if (m_strFileName.IsEmpty())
  116.             rStatus.m_attribute = 0;
  117.         else
  118.         {
  119.             DWORD dwAttribute = ::GetFileAttributes(m_strFileName);
  120.  
  121.             // don't return an error for this because previous versions of MFC didn't
  122.             if (dwAttribute == 0xFFFFFFFF)
  123.                 rStatus.m_attribute = 0;
  124.             else
  125.             {
  126.                 rStatus.m_attribute = (BYTE) dwAttribute;
  127. #ifdef _DEBUG
  128.                 // MFC BUG: m_attribute is only a BYTE wide
  129.                 if (dwAttribute & ~0xFF)
  130.                     TRACE0("Warning: CFile::GetStatus() returns m_attribute without high-order flags.\n");
  131. #endif
  132.             }
  133.         }
  134. #endif // _MAC
  135.  
  136.         // convert times as appropriate
  137.         rStatus.m_ctime = CTime(ftCreate);
  138.         rStatus.m_atime = CTime(ftAccess);
  139.         rStatus.m_mtime = CTime(ftModify);
  140.  
  141.         if (rStatus.m_ctime.GetTime() == 0)
  142.             rStatus.m_ctime = rStatus.m_mtime;
  143.  
  144.         if (rStatus.m_atime.GetTime() == 0)
  145.             rStatus.m_atime = rStatus.m_mtime;
  146.     }
  147.     return TRUE;
  148. }
  149.  
  150. BOOL PASCAL CFile::GetStatus(LPCTSTR lpszFileName, CFileStatus& rStatus)
  151. {
  152.     // attempt to fully qualify path first
  153.     if (!AfxFullPath(rStatus.m_szFullName, lpszFileName))
  154.     {
  155.         rStatus.m_szFullName[0] = '\0';
  156.         return FALSE;
  157.     }
  158.  
  159.     WIN32_FIND_DATA findFileData;
  160.     HANDLE hFind = FindFirstFile((LPTSTR)lpszFileName, &findFileData);
  161.     if (hFind == INVALID_HANDLE_VALUE)
  162.         return FALSE;
  163.     VERIFY(FindClose(hFind));
  164.  
  165.     // strip attribute of NORMAL bit, our API doesn't have a "normal" bit.
  166.     rStatus.m_attribute = (BYTE)
  167.         (findFileData.dwFileAttributes & ~FILE_ATTRIBUTE_NORMAL);
  168.  
  169.     // get just the low DWORD of the file size
  170.     ASSERT(findFileData.nFileSizeHigh == 0);
  171.     rStatus.m_size = (LONG)findFileData.nFileSizeLow;
  172.  
  173.     // convert times as appropriate
  174.     rStatus.m_ctime = CTime(findFileData.ftCreationTime);
  175.     rStatus.m_atime = CTime(findFileData.ftLastAccessTime);
  176.     rStatus.m_mtime = CTime(findFileData.ftLastWriteTime);
  177.  
  178.     if (rStatus.m_ctime.GetTime() == 0)
  179.         rStatus.m_ctime = rStatus.m_mtime;
  180.  
  181.     if (rStatus.m_atime.GetTime() == 0)
  182.         rStatus.m_atime = rStatus.m_mtime;
  183.  
  184.     return TRUE;
  185. }
  186.  
  187. void AFX_CDECL AfxTimeToFileTime(const CTime& time, LPFILETIME pFileTime)
  188. {
  189.     SYSTEMTIME sysTime;
  190.     sysTime.wYear = (WORD)time.GetYear();
  191.     sysTime.wMonth = (WORD)time.GetMonth();
  192.     sysTime.wDay = (WORD)time.GetDay();
  193.     sysTime.wHour = (WORD)time.GetHour();
  194.     sysTime.wMinute = (WORD)time.GetMinute();
  195.     sysTime.wSecond = (WORD)time.GetSecond();
  196.     sysTime.wMilliseconds = 0;
  197.  
  198.     // convert system time to local file time
  199.     FILETIME localTime;
  200.     if (!SystemTimeToFileTime((LPSYSTEMTIME)&sysTime, &localTime))
  201.         CFileException::ThrowOsError((LONG)::GetLastError());
  202.  
  203.     // convert local file time to UTC file time
  204.     if (!LocalFileTimeToFileTime(&localTime, pFileTime))
  205.         CFileException::ThrowOsError((LONG)::GetLastError());
  206. }
  207.  
  208. void PASCAL CFile::SetStatus(LPCTSTR lpszFileName, const CFileStatus& status)
  209. {
  210.     DWORD wAttr;
  211.     FILETIME creationTime;
  212.     FILETIME lastAccessTime;
  213.     FILETIME lastWriteTime;
  214.     LPFILETIME lpCreationTime = NULL;
  215.     LPFILETIME lpLastAccessTime = NULL;
  216.     LPFILETIME lpLastWriteTime = NULL;
  217.  
  218.     if ((wAttr = GetFileAttributes((LPTSTR)lpszFileName)) == (DWORD)-1L)
  219.         CFileException::ThrowOsError((LONG)GetLastError());
  220.  
  221.     if ((DWORD)status.m_attribute != wAttr && (wAttr & readOnly))
  222.     {
  223.         // Set file attribute, only if currently readonly.
  224.         // This way we will be able to modify the time assuming the
  225.         // caller changed the file from readonly.
  226.  
  227.         if (!SetFileAttributes((LPTSTR)lpszFileName, (DWORD)status.m_attribute))
  228.             CFileException::ThrowOsError((LONG)GetLastError());
  229.     }
  230.  
  231.     // last modification time
  232.     if (status.m_mtime.GetTime() != 0)
  233.     {
  234.         AfxTimeToFileTime(status.m_mtime, &lastWriteTime);
  235.         lpLastWriteTime = &lastWriteTime;
  236.  
  237.         // last access time
  238.         if (status.m_atime.GetTime() != 0)
  239.         {
  240.             AfxTimeToFileTime(status.m_atime, &lastAccessTime);
  241.             lpLastAccessTime = &lastAccessTime;
  242.         }
  243.  
  244.         // create time
  245.         if (status.m_ctime.GetTime() != 0)
  246.         {
  247.             AfxTimeToFileTime(status.m_ctime, &creationTime);
  248.             lpCreationTime = &creationTime;
  249.         }
  250.  
  251.         HANDLE hFile = ::CreateFile(lpszFileName, GENERIC_READ|GENERIC_WRITE,
  252.             FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  253.             NULL);
  254.  
  255.         if (hFile == INVALID_HANDLE_VALUE)
  256.             CFileException::ThrowOsError((LONG)::GetLastError());
  257.  
  258.         if (!SetFileTime((HANDLE)hFile, lpCreationTime, lpLastAccessTime, lpLastWriteTime))
  259.             CFileException::ThrowOsError((LONG)::GetLastError());
  260.  
  261.         if (!::CloseHandle(hFile))
  262.             CFileException::ThrowOsError((LONG)::GetLastError());
  263.     }
  264.  
  265.     if ((DWORD)status.m_attribute != wAttr && !(wAttr & readOnly))
  266.     {
  267.         if (!SetFileAttributes((LPTSTR)lpszFileName, (DWORD)status.m_attribute))
  268.             CFileException::ThrowOsError((LONG)GetLastError());
  269.     }
  270. }
  271.  
  272. ///////////////////////////////////////////////////////////////////////////////
  273. // CMemFile::GetStatus implementation
  274.  
  275. BOOL CMemFile::GetStatus(CFileStatus& rStatus) const
  276. {
  277.     ASSERT_VALID(this);
  278.  
  279.     rStatus.m_ctime = 0;
  280.     rStatus.m_mtime = 0;
  281.     rStatus.m_atime = 0;
  282.     rStatus.m_size = m_nFileSize;
  283.     rStatus.m_attribute = normal;
  284.     rStatus.m_szFullName[0] = '\0';
  285.     return TRUE;
  286. }
  287.  
  288. /////////////////////////////////////////////////////////////////////////////
  289.