⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commands.cpp

📁 压缩工程文件打包源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:

	// parse the dsp manually :(
	CStdioFile file;

	if (file.Open(sProjectFile, CFile::modeRead | CFile::typeText))
	{
		// find the first source file
		CString sFile;
		
		while (GetNextFile(file, sFile))
			aFiles.Add(sFile);
	}

	return aFiles.GetSize();
}

BOOL CCommands::GetNextFile(CStdioFile& file, CString& sFilePath)
{
	CString sLine;
	sFilePath.Empty();

	// read to start of next source
	while (file.ReadString(sLine) && sLine.Find("# Begin Source File") == -1);

	// then read to actual file
	while (file.ReadString(sLine) && sLine.Find("SOURCE=") == -1);

	// extract path to right of 'SOURCE='
	if (sLine.Find("SOURCE=") != -1)
	{
		sFilePath = sLine.Mid(lstrlen("SOURCE="));

		// cleanup
		sFilePath.Replace('\"', ' ');
		sFilePath.TrimLeft();
		sFilePath.TrimRight();

		TRACE ("%s\n", sFilePath);
	}

	return !sFilePath.IsEmpty();
}

void CCommands::PrepareFilePaths(CString sProjectFile, CStringArray& aFiles, CStringArray& aFailures)
{
	char szDrive[_MAX_DRIVE], szFolder[MAX_PATH];
	_splitpath(sProjectFile, szDrive, szFolder, NULL, NULL);

	CString sProjFolder(szDrive);
	sProjFolder += szFolder;

	int nFile = aFiles.GetSize();
	aFailures.RemoveAll();

	while (nFile--)
	{
		CString sFile = aFiles[nFile];

		// for '.\' just remove and prepend the project path
		if (sFile.Find(".\\") == 0)
		{
			sFile = sProjFolder + sFile.Mid(2);
			aFiles.SetAt(nFile, sFile);
		}
		// for all instances of '..' remove the deepest subfolder from the project path
		// before prepending
		else if (sFile.Find("..\\") == 0)
		{
			BOOL bDone = FALSE;
			CString sProjTemp(sProjFolder);

			while (!bDone)
			{
				int nDotFind = sFile.Find("..");

				if (nDotFind == -1)
					bDone = TRUE;
				else
				{
					int nFind = sProjTemp.ReverseFind('\\');

					if (nFind == -1)
						bDone = TRUE;
					else
					{
						CString sSubFolder = sProjTemp.Mid(nFind + 1);
						sProjTemp = sProjTemp.Left(nFind);

						if (sSubFolder.IsEmpty()) // it was a trailing '\\'
							continue;

						// else remove the '..'
						sFile = sFile.Mid(nDotFind + 2);
					}
				}
			}

			sFile = sProjTemp + sFile;

			// if there are any '..' left, it failed
			if (sFile.Find("..") != -1)
			{
				aFailures.Add(aFiles[nFile]);
				aFiles.RemoveAt(nFile);
			}
			else
				aFiles.SetAt(nFile, sFile);
		}
	}
}

CString CCommands::GetFileRoot(CString sProjectFile, const CStringArray& aFiles)
{
	// iteratively build a path which represents the largest path common to all files
	// ignore files on different drives to the dsp
	char szDrive[_MAX_DRIVE];
	_splitpath(sProjectFile, szDrive, NULL, NULL, NULL);
	_strlwr(szDrive);

	int nFind = sProjectFile.Find('\\');
	BOOL bMismatch = FALSE;
	CString sCommonFolder, sNextTry;

	while (nFind != -1 && !bMismatch)
	{
		sNextTry = sProjectFile.Left(nFind + 1); // includes '\\'
		sNextTry.MakeLower();
	
		// test every file for a match
		int nFile = aFiles.GetSize();

		while (nFile-- && !bMismatch)
		{
			CString sFile = aFiles[nFile];
			sFile.MakeLower();

			// ignore files on different drives to the dsp
			if (sFile.Find(szDrive) != 0)
				continue;

			bMismatch = (sFile.Find(sNextTry) == -1);
		}

		if (!bMismatch)
		{
			sCommonFolder = sNextTry; // fits all
			nFind = sProjectFile.Find('\\', nFind + 1);
		}
	}

	return sCommonFolder;
}


STDMETHODIMP CCommands::PZOpenProjectCommandMethod()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));
	CWaitCursor cursor;

	IGenericProject* pProject = NULL;
	
	if (SUCCEEDED(m_pApplication->get_ActiveProject((IDispatch**)&pProject)))
	{
		m_pApplication->ExecuteCommand(L"WorkspaceClose");
		pProject->Release();
	}

	// see if the user cancelled
	if (SUCCEEDED(m_pApplication->get_ActiveProject((IDispatch**)&pProject)))
	{
		pProject->Release();
		return S_OK;
	}

	OpenProject();

	return S_OK;
}

void CCommands::OpenProject()
{
	CFileDialog dialog(TRUE, "zip", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST, 
						"Zip Files (*.zip)|*.zip||");

	dialog.m_ofn.lpstrTitle = "ProjectZip - Open Zip File";

	if (dialog.DoModal() == IDOK)
	{
		CString sZipPath = dialog.GetPathName();

		// ask user where to extract to
		CFolderDialog dialog("Select Folder to Extract To");

		if (dialog.DoModal() == IDOK)
		{
			// append the zip's filetitle
			char szName[_MAX_FNAME];
			_splitpath(sZipPath, NULL, NULL, szName, NULL);

			CString sOutputFolder;
			sOutputFolder.Format("%s\\%s", dialog.GetFolderPath(), szName);

			// check before overwriting
			if (COptionsDlg::GetPromptOverwrite() && (GetFileAttributes(sOutputFolder) != 0xffffffff))
			{
				CString sMessage;
				sMessage.Format("The folder '%s' already exists.\n\nAre you sure that you want to overwrite it and the files it contains?", sOutputFolder);

				if (IDNO == MessageBox(NULL, sMessage, "ProjectZip", MB_YESNO))
					return;
			}

			CUnzipper uzip;

			if (uzip.OpenZip(sZipPath) && uzip.UnzipTo(sOutputFolder))
			{
				CStringArray aDswFiles, aDspFiles;

				int nFound = FindProjectFiles(uzip, sOutputFolder, aDswFiles, aDspFiles);
				CString sProjectFile;

				if (!nFound)
				{
					CString sMessage;
					sMessage.Format("The file '%s' does not appear to contain a project file.\n\nWould you like to browse the extracted files?", sZipPath);

					if (IDNO == MessageBox(NULL, sMessage, "ProjectZip", MB_YESNO))
						return;

					// show open dialog
					CFileDialog dialog(TRUE, NULL, sOutputFolder + "\\.", 0, "Project Files (*.dsw, *.dsp)|*.dsw;*.dsp||");

					dialog.m_ofn.lpstrTitle = "ProjectZip - Open Project File";

					if (dialog.DoModal() == IDOK)
						sProjectFile = dialog.DoModal();
				}
				else // open it
				{
					int nNumDsw = aDswFiles.GetSize();
					int nNumDsp = aDspFiles.GetSize();

					// try workspace files first
					if (nNumDsw)
					{
						if (nNumDsw == 1)
							sProjectFile = aDswFiles[0];

						else if (COptionsDlg::GetPromptIfMulti())
						{
							CSelectFileDlg dialog(aDswFiles);

							if (dialog.DoModal() == IDOK)
								sProjectFile = dialog.GetPathName();
						}
						else // make a guess
						{
							// see if there's a dsw sharing the same name as the zip file
							sProjectFile = aDswFiles[0]; // default if we can't find a match

							for (int nFile = 0; nFile < aDswFiles.GetSize(); nFile++)
							{
								char szDswName[_MAX_FNAME];
								_splitpath(aDswFiles[nFile], NULL, NULL, szDswName, NULL);

								if (stricmp(szName, szDswName) == 0)
								{
									sProjectFile = aDswFiles[nFile];
									break;
								}
							}
						}
					}
					else // nNumDsp
					{
						if (nNumDsp == 1)
							sProjectFile = aDspFiles[0];

						else if (COptionsDlg::GetPromptIfMulti())
						{
							CSelectFileDlg dialog(aDspFiles);

							if (dialog.DoModal() == IDOK)
								sProjectFile = dialog.GetPathName();
						}
						else // make a guess
						{
							// see if there's a dsp sharing the same name as the zip file
							sProjectFile = aDspFiles[0]; // default if we can't find a match

							for (int nFile = 0; nFile < aDspFiles.GetSize(); nFile++)
							{
								char szDswName[_MAX_FNAME];
								_splitpath(aDspFiles[nFile], NULL, NULL, szDswName, NULL);

								if (stricmp(szName, szDswName) == 0)
								{
									sProjectFile = aDspFiles[nFile];
									break;
								}
							}
						}
					}
				}

				// open the file
				if (!sProjectFile.IsEmpty())
				{
					IDocuments* pDocs = NULL; 

					if (SUCCEEDED(m_pApplication->get_Documents((IDispatch**)&pDocs)))
					{					
						IGenericDocument* pDoc = NULL; 
						HRESULT hr = pDocs->Open(_bstr_t(sProjectFile), CComVariant("Auto"), _variant_t(false), (IDispatch**)&pDoc);

						if (FAILED(hr))
						{
							CString sMessage;
							sMessage.Format("The project '%s' could not be opened.", sProjectFile);

							MessageBox(NULL, sMessage, "ProjectZip", MB_OK);
						}
					}
				}
			}
		}
	}
}

int CCommands::FindProjectFiles(CUnzipper& uzip, LPCTSTR szFolder, CStringArray& aDswFiles, CStringArray& aDspFiles)
{
	aDswFiles.RemoveAll();
	aDspFiles.RemoveAll();

	return FindProjectFiles(uzip, szFolder, aDswFiles, "dsw") + 
			FindProjectFiles(uzip, szFolder, aDspFiles, "dsp");
}

int CCommands::FindProjectFiles(CUnzipper& uzip, LPCTSTR szFolder, CStringArray& aFiles, LPCTSTR szExt)
{
	if (!szExt || !lstrlen(szExt))
		return 0;

	if (uzip.GotoFirstFile(szExt))
	{
		do
		{
			UZ_FileInfo fi;

			if (uzip.GetFileInfo(fi) && !fi.bFolder)
			{
				char szFullPath[MAX_PATH];
				_makepath(szFullPath, NULL, szFolder, fi.szFileName, NULL);

				aFiles.Add(szFullPath);
			}
		}
		while (uzip.GotoNextFile(szExt));
	}

	return aFiles.GetSize();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -