Ejemplo n.º 1
0
void CDocument::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU)
{
	// store the path fully qualified
	TCHAR szFullPath[_MAX_PATH];
	if ( lstrlen(lpszPathName) >= _MAX_PATH )
	{
		ASSERT(FALSE);
		// MFC requires paths with length < _MAX_PATH
		// No other way to handle the error from a void function
		AfxThrowFileException(CFileException::badPath);
	}

	if( AfxFullPath(szFullPath, lpszPathName) == FALSE )
	{
		ASSERT(FALSE);
		// MFC requires paths with length < _MAX_PATH
		// No other way to handle the error from a void function
		AfxThrowFileException(CFileException::badPath);
	}

	m_strPathName = szFullPath;
	ASSERT(!m_strPathName.IsEmpty());       // must be set to something
	m_bEmbedded = FALSE;
	ASSERT_VALID(this);

	// set the document title based on path name
	TCHAR szTitle[_MAX_FNAME];
	if (AfxGetFileTitle(szFullPath, szTitle, _MAX_FNAME) == 0)
		SetTitle(szTitle);

	// add it to the file MRU list
	if (bAddToMRU)
		AfxGetApp()->AddToRecentFileList(m_strPathName);

	ASSERT_VALID(this);
}
Ejemplo n.º 2
0
BOOL CStkFile::OpenShare(LPCTSTR lpszFileName, UINT nOpenFlags, int nAddToFileEnd, CString sShareName, CFileException* pException)
{
	m_bShareMem = TRUE;

	ASSERT(AfxIsValidString(lpszFileName));
	ASSERT(pException == NULL || AfxIsValidAddress(pException, sizeof(CFileException)));


	nOpenFlags &= ~(UINT)typeBinary;

	m_bCloseOnDelete = FALSE;
	m_hFile = (HANDLE)hFileNull;
	m_strFileName.Empty();

	TCHAR szTemp[_MAX_PATH];
	AfxFullPath(szTemp, lpszFileName);
	m_strFileName = szTemp;


	ASSERT(sizeof(HANDLE) == sizeof(UINT));
	ASSERT(shareCompat == 0);


	CString sFileN = lpszFileName;
	int nFileLen = 0;
	if (sFileN != "")
	{
		CFile fl;
		if (fl.Open(m_strFileName, CFile::shareDenyNone | CFile::modeReadWrite) == FALSE)
		{
			return FALSE;
		}

		nFileLen = fl.GetLength();
		if (nFileLen <= 0)
		{
			nFileLen = 16;
		}

		fl.Close();
	}


	m_bCloseOnDelete = TRUE;
	m_nLenFile = nFileLen;


	DWORD flProtect = 0;
	flProtect = PAGE_READWRITE;

	m_sNameShareMem = sShareName;
	m_hFileMap = CreateFileMapping((HANDLE)m_hFile, NULL, flProtect, 0, m_nLenFile + nAddToFileEnd, m_sNameShareMem);
	if (m_hFileMap == NULL)
	{
		AfxMessageBox("Error! hFileMap is Null.");
		CloseHandle(m_hFileMap);
		return FALSE;
	}
	else if (GetLastError() == ERROR_ALREADY_EXISTS)
	{
	}


	m_lpvFileBegin = (BYTE*)MapViewOfFile(m_hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, m_nLenFile + nAddToFileEnd);
	ASSERT(m_lpvFileBegin != NULL);

	m_lpvFileCurrent = m_lpvFileBegin;

	DWORD dwError;
	if (m_nLenFile == -1 && (dwError = GetLastError()) != NO_ERROR)
	{
		AfxMessageBox("GetFileSize 出错!");
		m_nLenFile = 0;
		m_lpvFileEnd = m_lpvFileBegin;
		return FALSE;
	}

	m_lpvFileEnd = m_lpvFileBegin + m_nLenFile + nAddToFileEnd;

	if (sFileN == "")
	{
		m_nLenFile = nAddToFileEnd;
	}

	return TRUE;
}
Ejemplo n.º 3
0
BOOL CStkFile::Open(LPCTSTR lpszFileName, UINT nOpenFlags, int nAddToFileEnd, CFileException* pException)
{
	ASSERT(AfxIsValidString(lpszFileName));
	ASSERT(pException == NULL || AfxIsValidAddress(pException, sizeof(CFileException)));

	nOpenFlags &= ~(UINT)typeBinary;

	m_bCloseOnDelete = FALSE;
	m_hFile = (HANDLE)hFileNull;
	m_strFileName.Empty();

	TCHAR szTemp[_MAX_PATH];
	AfxFullPath(szTemp, lpszFileName);
	m_strFileName = szTemp;

	ASSERT(sizeof(HANDLE) == sizeof(UINT));
	ASSERT(shareCompat == 0);


	int nFileLen = 0;
	if (_access(lpszFileName, 06) == -1)
	{
		nFileLen = 16;
	}


	ASSERT((modeRead | modeWrite | modeReadWrite) == 3);

	HANDLE hFile = CreateFile(lpszFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
		OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		if (pException != NULL)
		{
			pException->m_lOsError = GetLastError();
			pException->m_cause = CFileException::OsErrorToException(pException->m_lOsError);
			pException->m_strFileName = lpszFileName;
		}

		return FALSE;
	}

	m_hFile = (HANDLE)hFile;
	m_bCloseOnDelete = TRUE;
	m_nLenFile = GetFileSize((HANDLE)m_hFile, NULL);


	DWORD flProtect = 0;
	flProtect = PAGE_READWRITE;

	if (m_sNameShareMem == "")
	{
		m_hFileMap = CreateFileMapping((HANDLE)m_hFile, NULL, flProtect, 0, nFileLen + m_nLenFile + nAddToFileEnd, NULL);
	}
	else
	{
		m_hFileMap = CreateFileMapping((HANDLE)m_hFile, NULL, flProtect, 0, nFileLen + m_nLenFile + nAddToFileEnd, m_sNameShareMem);
	}
	if (m_hFileMap == NULL)
	{
		CString ss = m_strFileName;
		ss = "文件" + ss;
		AfxMessageBox("遇到错误! hFileMap 是零。请关闭程序后删除此文件!");
		CloseHandle(m_hFileMap);
		return FALSE;
	}
	else if (GetLastError() == ERROR_ALREADY_EXISTS)
	{
	}


	m_lpvFileBegin = (BYTE*)MapViewOfFile(m_hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, nFileLen + m_nLenFile + nAddToFileEnd);
	ASSERT(m_lpvFileBegin != NULL);

	m_lpvFileCurrent = m_lpvFileBegin;

	DWORD dwError;
	if (m_nLenFile == -1 && (dwError = GetLastError()) != NO_ERROR)
	{
		AfxMessageBox("GetFileSize 出错!");
		m_nLenFile = 0;
		m_lpvFileEnd = m_lpvFileBegin;
		return FALSE;
	}

	m_lpvFileEnd = m_lpvFileBegin + m_nLenFile + nAddToFileEnd;

	return TRUE;
}
Ejemplo n.º 4
0
CStaticDoc* CStaticDoc::OpenDocumentFile( LPCTSTR lpszPathName )
{
	// Resolve File Name
	TCHAR szPath[_MAX_PATH];
	ASSERT(lstrlen(lpszPathName) < sizeof(szPath));
	if( NULL != lpszPathName )
	{
		TCHAR szTemp[_MAX_PATH];
		if (lpszPathName[0] == '\"')
			++lpszPathName;
		lstrcpyn(szTemp, lpszPathName, _MAX_PATH);
		LPTSTR lpszLast = _tcsrchr(szTemp, '\"');
		if (lpszLast != NULL)
			*lpszLast = 0;
		AfxFullPath(szPath, szTemp);
		TCHAR szLinkName[_MAX_PATH];
		if (AfxResolveShortcut(AfxGetMainWnd(), szPath, szLinkName, _MAX_PATH))
			lstrcpy(szPath, szLinkName);
	}

	// Create document
	CStaticDoc *pDocument = CreateNewDocument( );
	if( NULL == pDocument )
		return NULL;

	if (lpszPathName == NULL)
	{
		// create a new document
		if (!pDocument->OnNewDocument())
		{
			// user has been alerted to what failed in OnNewDocument
			TRACE0("CStaticDoc::OnNewDocument returned FALSE.\n");
			delete pDocument;
			return NULL;
		}
	}
	else
	{
		CWaitCursor wait;

		// open an existing document
		BOOL bWasModified = pDocument->IsModified();
		pDocument->SetModifiedFlag(FALSE);  // not dirty for open

		if (!pDocument->OnOpenDocument( szPath ))
		{
			// user has been alerted to what failed in OnOpenDocument
			TRACE0("CStaticDoc::OnOpenDocument returned FALSE.\n");
			if (!pDocument->IsModified())
			{
				// original document is untouched
				pDocument->SetModifiedFlag(bWasModified);
			}
			else
			{
				// we corrupted the original document
				if (!pDocument->OnNewDocument())
				{
					TRACE0("Error: OnNewDocument failed after trying to open a document - trying to continue.\n");
					// assume we can continue
				}
			}
			delete pDocument;
			return NULL;        // open failed
		}
		pDocument->SetPathName(szPath);
	}
	return pDocument;
}
Ejemplo n.º 5
0
void CWizardView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// TODO: Add your specialized code here and/or call the base class
	if( UPDATE_HINT_WIZARDVIEW != lHint )
	{
		if( ::IsWindow( m_listRecent.GetSafeHwnd() ) )
			m_listRecent.Invalidate( );
		return;
	}
	if( IsWindowVisible() )
		SetFocus( );

	m_listRecent.DeleteAllItems( );

	//	insert items
	CStkUIApp * pApp = AfxGetStkUIApp();
	CRecentFileList * pRecent = NULL;
	if( pApp )
		pRecent = pApp->GetRecentFileList( );

	CString	strOpened;
	strOpened.LoadString( IDS_STRATEGYOPENED );

	CStringArray	astrAdded;

	int	count	=	0;
	if( pRecent )
	{
		for( int i=pRecent->GetSize()-1; i>=0; i-- )
		{
			CString strPath = (*pRecent)[i];
			if( strPath.IsEmpty() )
				continue;
			if( 0 != access(strPath,0) )
			{
				pRecent->Remove(i);
				continue;
			}

			TCHAR	szFullPath[_MAX_PATH];
			AfxFullPath( szFullPath, strPath );
			strPath	=	szFullPath;

			CString strName	=	CStrategy::GetName( strPath );
			BOOL	bOpened	=	pApp->IsFileOpened( strPath );
			int nItem = m_listRecent.InsertItem( count, strPath, bOpened ? 1 : 0 );
			m_listRecent.SetItemText( nItem, 0, strName );
			m_listRecent.SetItemText( nItem, 1, strPath );
			if( bOpened )
				m_listRecent.SetItemText( nItem, 3, strOpened );
			astrAdded.Add( strPath );
			
			CString	strTime;
			CFileStatus	status;
			if( CFile::GetStatus( strPath, status ) )
			{
				strTime = status.m_mtime.Format( "%Y-%m-%d" );
				m_listRecent.SetItemText( nItem, 2, strTime );
			}
			count ++;
		}
	}

	CString strExt	=	AfxGetStrategyFileExt( );
	if( !strExt.IsEmpty())
	{
		CFileFind finder;
		BOOL	bWorking	=	finder.FindFile( AfxGetFilePath( (LPCTSTR)AfxGetProfile().GetProjectPath(), LPCTSTR("*" + strExt) ) );
		while( bWorking )
		{
			bWorking	=	finder.FindNextFile();
			CString	strPath	=	finder.GetFilePath();

			int i;
			for( i=0; i<astrAdded.GetSize(); i++ )
			{
				if( 0 == strPath.CompareNoCase( astrAdded.ElementAt(i) ) )
					break;
			}
			if( i < astrAdded.GetSize() )
				continue;

			CString strName	=	CStrategy::GetName( strPath );
			BOOL	bOpened	=	pApp->IsFileOpened( strPath );
			int nItem = m_listRecent.InsertItem( count, strPath, bOpened ? 1 : 0 );
			m_listRecent.SetItemText( nItem, 0, strName );
			m_listRecent.SetItemText( nItem, 1, strPath );
			if( bOpened )
				m_listRecent.SetItemText( nItem, 3, strOpened );

			CString	strTime;
			CFileStatus	status;
			if( CFile::GetStatus( strPath, status ) )
			{
				strTime = status.m_mtime.Format( "%Y-%m-%d" );
				m_listRecent.SetItemText( nItem, 2, strTime );
			}
			count ++;

			if( count >= 256 )
				break;
		}
		finder.Close();
	}

	m_listRecent.Invalidate( );
}
Ejemplo n.º 6
0
BOOL CFile::Open(LPCTSTR lpszFileName, UINT nOpenFlags,
	CFileException* pException)
{
	ASSERT_VALID(this);
	ASSERT(AfxIsValidString(lpszFileName));
	ASSERT(pException == NULL ||
		AfxIsValidAddress(pException, sizeof(CFileException)));
	ASSERT((nOpenFlags & typeText) == 0);   // text mode not supported

	// CFile objects are always binary and CreateFile does not need flag
	nOpenFlags &= ~(UINT)typeBinary;

	m_bCloseOnDelete = FALSE;
	m_hFile = (UINT)hFileNull;
	m_strFileName.Empty();

	TCHAR szTemp[_MAX_PATH];
	AfxFullPath(szTemp, lpszFileName);
	m_strFileName = szTemp;

	ASSERT(sizeof(HANDLE) == sizeof(UINT));
	ASSERT(shareCompat == 0);

	// map read/write mode
	ASSERT((modeRead|modeWrite|modeReadWrite) == 3);
	DWORD dwAccess;
	switch (nOpenFlags & 3)
	{
	case modeRead:
		dwAccess = GENERIC_READ;
		break;
	case modeWrite:
		dwAccess = GENERIC_WRITE;
		break;
	case modeReadWrite:
		dwAccess = GENERIC_READ|GENERIC_WRITE;
		break;
	default:
		ASSERT(FALSE);  // invalid share mode
	}

	// map share mode
	DWORD dwShareMode;
	switch (nOpenFlags & 0x70)
	{
	case shareCompat:       // map compatibility mode to exclusive
	case shareExclusive:
		dwShareMode = 0;
		break;
	case shareDenyWrite:
		dwShareMode = FILE_SHARE_READ;
		break;
	case shareDenyRead:
		dwShareMode = FILE_SHARE_WRITE;
		break;
	case shareDenyNone:
		dwShareMode = FILE_SHARE_WRITE|FILE_SHARE_READ;
		break;
	default:
		ASSERT(FALSE);  // invalid share mode?
	}

	// Note: typeText and typeBinary are used in derived classes only.

	// map modeNoInherit flag
	SECURITY_ATTRIBUTES sa;
	sa.nLength = sizeof(sa);
	sa.lpSecurityDescriptor = NULL;
	sa.bInheritHandle = (nOpenFlags & modeNoInherit) == 0;

	// map creation flags
	DWORD dwCreateFlag;
	if (nOpenFlags & modeCreate)
	{
		if (nOpenFlags & modeNoTruncate)
			dwCreateFlag = OPEN_ALWAYS;
		else
			dwCreateFlag = CREATE_ALWAYS;
	}
	else
		dwCreateFlag = OPEN_EXISTING;

	// attempt file creation
	HANDLE hFile = ::CreateFile(lpszFileName, dwAccess, dwShareMode, &sa,
		dwCreateFlag, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		if (pException != NULL)
		{
			pException->m_lOsError = ::GetLastError();
			pException->m_cause =
				CFileException::OsErrorToException(pException->m_lOsError);

			// use passed file name (not expanded vesion) when reporting
			// an error while opening

			pException->m_strFileName = lpszFileName;
		}
		return FALSE;
	}
	m_hFile = (HFILE)hFile;
	m_bCloseOnDelete = TRUE;

	return TRUE;
}
Ejemplo n.º 7
0
void CHexFileList::Add(LPCTSTR lpszPathName)
{
	ASSERT(lpszPathName != NULL);
	ASSERT(AfxIsValidString(lpszPathName));

	// Get the fully qualified path name
	TCHAR szTemp[_MAX_PATH];
	AfxFullPath(szTemp, lpszPathName);

	ASSERT(hash_.size() == name_.size());
	ASSERT(opened_.size() == name_.size());
	ASSERT(open_count_.size() == name_.size());
	ASSERT(data_.size() == name_.size());

	CString saved_data;

	// Get hash to speed search of the list
	CString ss(szTemp);
	ss.MakeUpper();
	unsigned long hash = str_hash(ss);

	int open_count = 0;

	// Find and remove any existing entry(s) for this file
	// Note: we use an index rather than iterator since an iterator is invalid after erase() is called
	for (int ii = name_.size()-1; ii >= 0; ii--)
	{
		// Using AfxComparePath is more reliable as it handles DBCS case-insensitive comparisons etc
		if (hash == hash_[ii] && AfxComparePath(name_[ii], szTemp))
		{
			// Remove the existing entry for the file
			name_.erase(name_.begin() + ii);
			hash_.erase(hash_.begin() + ii);
			opened_.erase(opened_.begin() + ii);
			open_count = open_count_[ii];          // save open count
			open_count_.erase(open_count_.begin() + ii);
			saved_data = data_[ii];                //  save data_ for when file read (below)
			data_.erase(data_.begin() + ii);

			break;   // A file should only appear in the list once (but we should have a way to remove duplicates?)
		}
	}

	// Add to the end of the vectors (so most recent files are at the end)
	name_.push_back(szTemp);
	hash_.push_back(hash);
	opened_.push_back(time(NULL));
	open_count_.push_back(open_count + 1);  // inc open count (do we need IncOpenCount()?)
	data_.push_back(saved_data);

	ASSERT(hash_.size() == name_.size());
	ASSERT(opened_.size() == name_.size());
	ASSERT(open_count_.size() == name_.size());
	ASSERT(data_.size() == name_.size());

	// Let base class also keep its silly little list
	CRecentFileList::Add(lpszPathName);

	((CMainFrame *)AfxGetMainWnd())->UpdateExplorer(szTemp);  // Let explorer update (last opened time)
	ASSERT(Check());
}
Ejemplo n.º 8
0
BOOL PASCAL GetStatus64(LPCTSTR lpszFileName, CFileStatus64& rStatus)
{
	// attempt to fully qualify path first
	if (!AfxFullPath(rStatus.m_szFullName, lpszFileName))
	{
		rStatus.m_szFullName[0] = _MPT('\0');
		return FALSE;
	}

	WIN32_FIND_DATA findFileData;
	HANDLE hFind = FindFirstFile((LPTSTR)lpszFileName, &findFileData);
	if (hFind == INVALID_HANDLE_VALUE)
		return FALSE;
	VERIFY(FindClose(hFind));

	// strip attribute of NORMAL bit, our API doesn't have a "normal" bit.
	rStatus.m_attribute = (BYTE)
		(findFileData.dwFileAttributes & ~FILE_ATTRIBUTE_NORMAL);

	rStatus.m_size = ((_int64)findFileData.nFileSizeHigh<<32)+findFileData.nFileSizeLow;

	// convert times as appropriate
	TRY
	{
		rStatus.m_ctime = CTime(findFileData.ftCreationTime);
		rStatus.m_has_ctime = true;
	}
	CATCH_ALL(e)
	{
		rStatus.m_has_ctime = false;
	}
	END_CATCH_ALL;

	TRY
	{
		rStatus.m_atime = CTime(findFileData.ftLastAccessTime);
		rStatus.m_has_atime = true;
	}
	CATCH_ALL(e)
	{
		rStatus.m_has_atime = false;
	}
	END_CATCH_ALL;

	TRY
	{
		rStatus.m_mtime = CTime(findFileData.ftLastWriteTime);
		rStatus.m_has_mtime = true;
	}
	CATCH_ALL(e)
	{
		rStatus.m_has_mtime = false;
	}
	END_CATCH_ALL;

	if (!rStatus.m_has_ctime || rStatus.m_ctime.GetTime() == 0)
	{
		if (rStatus.m_has_mtime)
		{
			rStatus.m_ctime = rStatus.m_mtime;
			rStatus.m_has_ctime = true;
		}
		else
			rStatus.m_has_ctime = false;
	}


	if (!rStatus.m_has_atime || rStatus.m_atime.GetTime() == 0)
	{
		if (rStatus.m_has_mtime)
		{
			rStatus.m_atime = rStatus.m_mtime;
			rStatus.m_has_atime = true;
		}
		else
			rStatus.m_has_atime = false;
	}

	if (!rStatus.m_has_mtime || rStatus.m_mtime.GetTime() == 0)
	{
		if (rStatus.m_has_ctime)
		{
			rStatus.m_mtime = rStatus.m_ctime;
			rStatus.m_has_mtime = true;
		}
		else
			rStatus.m_has_mtime = false;
	}

	return TRUE;
}
CXTPRecentFileListItem* CXTPRecentFileList::AddItem(LPCTSTR lpszPathName)
{
	ASSERT(m_arrNames != NULL);
	ASSERT(lpszPathName != NULL);
	ASSERT(AfxIsValidString(lpszPathName));

	// fully qualify the path name
	TCHAR szTemp[_MAX_PATH];
	AfxFullPath(szTemp, lpszPathName);

	int iMRU = 0;
	CXTPRecentFileListItem* pItem = NULL;

	// update the MRU list, if an existing MRU string matches file name
	for (; iMRU < m_nSize - 1; iMRU++)
	{
		if (AfxComparePath(m_arrNames[iMRU], szTemp))
		{
			pItem = m_pItems[iMRU];
			break;      // iMRU will point to matching entry
		}
	}

	if (iMRU == m_nSize - 1) // Not found
	{
		for (; iMRU >= 0; iMRU--)
		{
			if (!m_pItems[iMRU] || !m_pItems[iMRU]->IsPinned())
				break;
		}
	}

	if (iMRU < 0)
		return NULL;

	if (pItem == NULL && m_pItems[iMRU] != NULL)
	{
		CMDTARGET_RELEASE(m_pItems[iMRU]);
	}

	// move MRU strings before this one down
	for (; iMRU > 0; iMRU--)
	{
		ASSERT(iMRU > 0);
		ASSERT(iMRU < m_nSize);
		m_arrNames[iMRU] = m_arrNames[iMRU - 1];
		m_pItems[iMRU] = m_pItems[iMRU - 1];
	}
	// place this one at the beginning
	m_arrNames[0] = szTemp;

	if (pItem)
	{
		m_pItems[0] = pItem;
	}
	else
	{
		pItem = new CXTPRecentFileListItem(this);
		pItem->m_strPathName = m_arrNames[0];

		m_pItems[0] = pItem;

		OnNewItem(pItem);
	}
	return pItem;
}
Ejemplo n.º 10
0
BOOL PASCAL CFile::GetStatus(LPCTSTR lpszFileName, CFileStatus& rStatus)
{
	ASSERT( lpszFileName != NULL );

	if ( lpszFileName == NULL ) 
	{
		return FALSE;
	}

	if ( lstrlen(lpszFileName) >= _MAX_PATH )
	{
		ASSERT(FALSE); // MFC requires paths with length < _MAX_PATH
		return FALSE;
	}
	
	// attempt to fully qualify path first
	if (!AfxFullPath(rStatus.m_szFullName, lpszFileName))
	{
		rStatus.m_szFullName[0] = '\0';
		return FALSE;
	}

	WIN32_FIND_DATA findFileData;
	HANDLE hFind = FindFirstFile((LPTSTR)lpszFileName, &findFileData);
	if (hFind == INVALID_HANDLE_VALUE)
		return FALSE;
	VERIFY(FindClose(hFind));

	// strip attribute of NORMAL bit, our API doesn't have a "normal" bit.
	rStatus.m_attribute = (BYTE)
		(findFileData.dwFileAttributes & ~FILE_ATTRIBUTE_NORMAL);

	// get just the low DWORD of the file size
	ASSERT(findFileData.nFileSizeHigh == 0);
	rStatus.m_size = (LONG)findFileData.nFileSizeLow;

	// convert times as appropriate
	if (CTime::IsValidFILETIME(findFileData.ftCreationTime))
	{
		rStatus.m_ctime = CTime(findFileData.ftCreationTime);
	}
	else
	{
		rStatus.m_ctime = CTime();
	}

	if (CTime::IsValidFILETIME(findFileData.ftLastAccessTime))
	{
		rStatus.m_atime = CTime(findFileData.ftLastAccessTime);
	}
	else
	{
		rStatus.m_atime = CTime();
	}

	if (CTime::IsValidFILETIME(findFileData.ftLastWriteTime))
	{
		rStatus.m_mtime = CTime(findFileData.ftLastWriteTime);
	}
	else
	{
		rStatus.m_mtime = CTime();
	}

	if (rStatus.m_ctime.GetTime() == 0)
		rStatus.m_ctime = rStatus.m_mtime;

	if (rStatus.m_atime.GetTime() == 0)
		rStatus.m_atime = rStatus.m_mtime;

	return TRUE;
}
Ejemplo n.º 11
0
BOOL PASCAL CFile::GetStatus(LPCTSTR lpszFileName, CFileStatus& rStatus)
{
	ASSERT( lpszFileName != NULL );

	if ( lpszFileName == NULL ) 
	{
		return FALSE;
	}

	if ( lstrlen(lpszFileName) >= _MAX_PATH )
	{
		ASSERT(FALSE); // MFC requires paths with length < _MAX_PATH
		return FALSE;
	}
	
	// attempt to fully qualify path first
	if (!AfxFullPath(rStatus.m_szFullName, lpszFileName))
	{
		rStatus.m_szFullName[0] = '\0';
		return FALSE;
	}

	WIN32_FILE_ATTRIBUTE_DATA fileAttributeData;

	{
		if (!GetFileAttributesEx(lpszFileName, GetFileExInfoStandard, &fileAttributeData))
			return FALSE;
	}

	// strip attribute of NORMAL bit, our API doesn't have a "normal" bit.
	rStatus.m_attribute = (BYTE)
		(fileAttributeData.dwFileAttributes & ~FILE_ATTRIBUTE_NORMAL);

	rStatus.m_size = fileAttributeData.nFileSizeHigh;
	rStatus.m_size <<= 32;
	rStatus.m_size |= fileAttributeData.nFileSizeLow;

	// convert times as appropriate
	if (CTime::IsValidFILETIME(fileAttributeData.ftCreationTime))
	{
		rStatus.m_ctime = CTime(fileAttributeData.ftCreationTime);
	}
	else
	{
		rStatus.m_ctime = CTime();
	}

	if (CTime::IsValidFILETIME(fileAttributeData.ftLastAccessTime))
	{
		rStatus.m_atime = CTime(fileAttributeData.ftLastAccessTime);
	}
	else
	{
		rStatus.m_atime = CTime();
	}

	if (CTime::IsValidFILETIME(fileAttributeData.ftLastWriteTime))
	{
		rStatus.m_mtime = CTime(fileAttributeData.ftLastWriteTime);
	}
	else
	{
		rStatus.m_mtime = CTime();
	}

	if (rStatus.m_ctime.GetTime() == 0)
		rStatus.m_ctime = rStatus.m_mtime;

	if (rStatus.m_atime.GetTime() == 0)
		rStatus.m_atime = rStatus.m_mtime;

	return TRUE;
}
Ejemplo n.º 12
0
CDocument* CMyDocManager::OpenDocumentFile(LPCTSTR lpszFileName)
{
	// From MFC: CDocManager::OpenDocumentFile

	CString strFileName = lpszFileName;

	strFileName.TrimLeft();
	strFileName.TrimRight();
	if (strFileName[0] == '\"')
		strFileName.Delete(0);
	int nPos = strFileName.ReverseFind('\"');
	if (nPos != -1)
		strFileName.Delete(nPos);

	CString strQuery, strPage;
	nPos = strFileName.Find('?');
	if (nPos != -1)
	{
		strQuery = strFileName.Mid(nPos + 1);
		strFileName = strFileName.Left(nPos);
	}

	bool bPathTooLong = false;
	TCHAR szPath[_MAX_PATH];
	if (!AfxFullPath(szPath, strFileName))
		bPathTooLong = true;

	if (bPathTooLong || !PathFileExists(szPath))
	{
		// Try extracting page number
		nPos = strFileName.ReverseFind('#');
		if (nPos != -1)
		{
			strPage = strFileName.Mid(nPos + 1);
			strFileName = strFileName.Left(nPos);

			if (!AfxFullPath(szPath, strFileName))
				bPathTooLong = true;
		}
	}

	if (bPathTooLong)
	{
		AfxMessageBox(FormatString(IDS_PATH_TOO_LONG, szPath), MB_ICONEXCLAMATION | MB_OK);
		return NULL;
	}

	TCHAR szLinkName[_MAX_PATH];
	if (AfxResolveShortcut(GetMainWnd(), szPath, szLinkName, _MAX_PATH))
		lstrcpy(szPath, szLinkName);

	// find the highest confidence
	CDocTemplate::Confidence bestMatch = CDocTemplate::noAttempt;
	CDocTemplate* pBestTemplate = NULL;
	CDocument* pOpenDocument = NULL;
	CMainFrame* pOldMainFrm = (CMainFrame*) theApp.m_pMainWnd;

	POSITION pos = m_templateList.GetHeadPosition();
	while (pos != NULL)
	{
		CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetNext(pos);
		ASSERT_KINDOF(CDocTemplate, pTemplate);

		CDocTemplate::Confidence match;
		ASSERT(pOpenDocument == NULL);
		match = pTemplate->MatchDocType(szPath, pOpenDocument);
		if (match > bestMatch)
		{
			bestMatch = match;
			pBestTemplate = pTemplate;
		}
		if (match == CDocTemplate::yesAlreadyOpen)
			break;
	}

	if (pOpenDocument != NULL)
	{
		POSITION pos = pOpenDocument->GetFirstViewPosition();
		if (pos != NULL)
		{
			CView* pView = pOpenDocument->GetNextView(pos);
			ASSERT_VALID(pView);

			CMainFrame* pMainFrm = (CMainFrame*) pView->GetTopLevelFrame();
			pMainFrm->ActivateDocument(pOpenDocument);
		}
		else
			TRACE(_T("Error: Can not find a view for document to activate.\n"));
	}

	if (pOpenDocument == NULL)
	{
		if (pBestTemplate == NULL)
		{
			AfxMessageBox(AFX_IDP_FAILED_TO_OPEN_DOC);
			return NULL;
		}

		pOpenDocument = pBestTemplate->OpenDocumentFile(szPath);
	}

	if (pOpenDocument != NULL)
	{
		CDjVuDoc* pDoc = (CDjVuDoc*) pOpenDocument;
		CDjVuView* pView = pDoc->GetDjVuView();
		CMainFrame* pMainFrm = pView->GetMainFrame();

		// CDocManager::OnDDECommand shows the previous main window.
		// If it was in the fullscreen mode, hide it back.
		if (pOldMainFrm != NULL && pOldMainFrm != pMainFrm && pOldMainFrm->IsFullscreenMode())
			pOldMainFrm->ShowWindow(SW_HIDE);

		if (!strPage.IsEmpty())
			pView->GoToURL(MakeUTF8String(_T("#") + strPage));
	}

	return pOpenDocument;
}