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

📄 renameprojectdlg.cpp

📁 VCProjectRename,Visual C++6 工程重新命名
💻 CPP
字号:
// RenameProjectDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RenameProject.h"
#include "RenameProjectDlg.h"

//#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
//#include <dklt/debug.h>
using namespace std;


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//
// Get the version info of dll from version header
//
///------------------------------------------------------
string GetDllBuildVersion(HWND hDlg) {
///------------------------------------------------------
 DWORD j;
 int i;
 string tmp = AfxGetAppName();
		tmp += ".exe";

		i = GetFileVersionInfoSize((char*)tmp.c_str(),&j);

 char *buf = new char[i];

			GetFileVersionInfo((char*)tmp.c_str(),0,i,buf);

 char *pVar;
 unsigned int q;

if (!VerQueryValue( buf,"\\" ,(void**)&pVar,&q)) {
//	ERRORMSG ("ERROR: << VerQueryValue()");
}

VS_FIXEDFILEINFO *finfo = 0;
finfo=(VS_FIXEDFILEINFO*)pVar;

	 ostringstream ss;

	 ss << "build: " 
		<< (((unsigned int)finfo->dwFileVersionMS&0xffff0000) >>16) << "." 
		<< ((unsigned int)finfo->dwFileVersionMS&0x00ff)<< "." 
		<< (((unsigned int)finfo->dwFileVersionLS&0xffff0000) >>16) << "." 
		<< ((unsigned int)finfo->dwFileVersionLS&0x00ff);

 delete buf;

return ss.str();
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CString	m_strCopyright;
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	string tmp ="Copyright (C) 2002 DKLT ";
		   tmp += GetDllBuildVersion(m_hWnd);
    //{{AFX_DATA_INIT(CAboutDlg)
	m_strCopyright = _T(tmp.c_str());
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	DDX_Text(pDX, IDC_COPYRIGHT, m_strCopyright);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CRenameProjectDlg dialog

CRenameProjectDlg::CRenameProjectDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRenameProjectDlg::IDD, pParent),
fname("hello world",255)
{
	string tmp =GetDllBuildVersion(m_hWnd);

	//{{AFX_DATA_INIT(CRenameProjectDlg)
	m_strDirectory = _T("");
	m_strName = _T("");
	m_strInfoBlock = _T("");
	m_strBuild = _T(tmp.c_str());
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRenameProjectDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRenameProjectDlg)
	DDX_Text(pDX, IDC_DIRECTORY, m_strDirectory);
	DDX_Text(pDX, IDC_NEWNAME, m_strName);
	DDX_Text(pDX, IDC_INFOBLOCK, m_strInfoBlock);
	DDX_Text(pDX, IDC_BUILD, m_strBuild);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRenameProjectDlg, CDialog)
	//{{AFX_MSG_MAP(CRenameProjectDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON2, OnButtonBrowse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRenameProjectDlg message handlers

BOOL CRenameProjectDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CRenameProjectDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CRenameProjectDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CRenameProjectDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}



//-----------------------------------
int FindTillEOL(const string buf, const string target, int pos) {
//-----------------------------------
	int eol = buf.find("\n",pos+1);

	// replace 2nd =.\"project".dsp 
	pos = buf.find(target,pos);		// pos is a local variable

	// if pass eol, means not found
	if (pos > eol) 
		return -1;
	
	return pos;
}


void CRenameProjectDlg::OnButtonBrowse() 
{
UpdateData(true);	// update from gui
	// TODO: Add your control notification handler code here
	OPENFILENAME ofn;

	ofn.lStructSize       = sizeof(OPENFILENAME);
	ofn.hwndOwner         = 0;
//GetSafeHwnd();
	ofn.lpstrFilter       = NULL;
	ofn.lpstrFilter       = \
_T("\
Project files (*.dsw)\0\
*.dsw;\0\
All files (*.*)\0\
*.*\0\
\0");
	ofn.lpstrCustomFilter = NULL;
	ofn.nFilterIndex      = 1;
	ofn.lpstrFile         = &fname[0];	// HERE
	ofn.nMaxFile          = MAX_PATH;
	ofn.lpstrInitialDir   = NULL;
	ofn.lpstrTitle        = NULL;
	ofn.lpstrFileTitle    = NULL;
	ofn.lpstrDefExt       = _T("dsw");
	ofn.Flags             = OFN_FILEMUSTEXIST | OFN_READONLY | OFN_PATHMUSTEXIST;

	BOOL RetVal= GetOpenFileName((LPOPENFILENAME)&ofn);

m_strDirectory = fname.c_str();
UpdateData(false);	// put to gui
}





//-----------------------
void CRenameProjectDlg::OnOK()  {
//-----------------------
UpdateData(true);	// grap info from gui
if (m_strDirectory.IsEmpty() && m_strName.IsEmpty()) {
	CDialog::OnOK();
	return;
}

string *pbuf = new string("0",7000);
string& buf = *pbuf;


	/// find location of project files

	/// .dsp .dsw
	/// replace all instance of existing name with new
	/// name	



//ERRORMSG("here");

	//// @@@@@@@@@@@@@@@@@@@@@@@@@@
	//// process .dsw file here
	//// @@@@@@@@@@@@@@@@@@@@@@@@@@
	////
	ifstream idsw(fname.c_str(), ios::binary);

		// get length of file
		int filesize;
		filesize  =idsw.seekg(0,ios::end).tellg();
		filesize -=idsw.seekg(0,ios::beg).tellg();

		// clip file buffer to too large
		if (filesize>buf.length()) {
//			ERRORMSG("filesize > buf.length() ... shrinking");
			filesize = buf.length();
		}

	idsw.read(&buf[0],filesize);
	idsw.close();

	/// update gui
	m_strInfoBlock += "\r\n";
	m_strInfoBlock += filesize;
	m_strInfoBlock += "\r\n";
	m_strInfoBlock += buf.c_str();
	UpdateData(false);


/*
.dsp file... this line is of interest
Project: "flowTrack"=.\flowTrack.dsp - Package Owner=<4>
*/
	UpdateData(true);	// grap info from gui

	if ( m_strName.IsEmpty()  )
		m_strName = "@@@@";


	///
	/// parse and replace names
	///
	int sol = buf.find("Project:",0);	
	int eol = buf.find("\n",sol);
	int soq =buf.find('"',sol+1);
	int eoq =buf.find('"',soq+1);

	string oldName = buf.substr(soq+1, eoq-soq-1);
	oldName +=".dsp";

	// replace 1st "project" name
	buf.replace(soq+1, eoq-soq-1, m_strName);


	// replace 2nd =.\"project".dsp 
	int namePos=buf.find(oldName,sol);	
	string tmp =m_strName + ".dsp" ;

	// directory name could be same as .dsp name
	//	for (int count=0; namePos < eol && (namePos !=-1); count++);

	if (namePos < eol) {	// within the same line
	buf.replace(namePos, oldName.length() ,tmp);	
	}
//	DEBUGMSG("fname: " << fname);
//	DEBUGMSG("oldName: " << oldName);

	oldName.erase(oldName.length()-4,4);
	oldName += ".dsw";

	tmp = m_strName + ".dsw";

	// get .dsp full path
	int pathPos = fname.find(oldName);
	string strPath = fname.substr(0,pathPos);
	string strNewDswPath = strPath + tmp;

		ofstream ofs(strNewDswPath.c_str());
		ofs << buf;
		ofs.close();

	// .dsw renaming complete at this stage

	// opening 2nd file for processing

	//// @@@@@@@@@@@@@@@@@@@@@@@@@@@@
	//// Process .dsp file here
	//// @@@@@@@@@@@@@@@@@@@@@@@@@@@@
	////
	oldName.erase(oldName.length()-4,4);
	oldName += ".dsp";

	tmp = strPath + oldName;
	ifstream ifs( tmp.c_str(), ios::binary);

		// get length of file
		filesize  =ifs.seekg(0,ios::end).tellg();
		filesize -=ifs.seekg(0,ios::beg).tellg();

		// clip file buffer to too large
		if (filesize>buf.length()) {
//			ERRORMSG("filesize > buf.length() ... shrinking");
			filesize = buf.length();
		}

	ifs.read(&buf[0],filesize);
	ifs.close();

	/// draw to gui too
	m_strInfoBlock = "\r\n";
	m_strInfoBlock += filesize;
	m_strInfoBlock += "\r\n";
	m_strInfoBlock += buf.c_str();
	UpdateData(false);	// reflect changes to gui


// condition one
//----------------------------
// Contains "CFG"
//
//!IF  "$(CFG)" == "RenameProject - Win32 Release"
//CFG=RenameProject - Win32 Debug
//!ELSEIF  "$(CFG)" == "RenameProject - Win32 Debug"
	{{{{
	for (int pos=1; pos >0; ) {
		int nextPos = buf.find("CFG",pos);
		int replacePos = FindTillEOL(buf, oldName, nextPos);
		if (replacePos !=-1) {
			buf.replace(replacePos, oldName.length(),m_strName);
		}
//		DEBUGMSG( pos );
		pos=nextPos+1;
	}
	}}}}
// condition two
//----------------------------
// Contains "Debug" or "Release"
//!MESSAGE "RenameProject - Win32 Release" (based on "Win32 (x86) Application")
//!MESSAGE "RenameProject - Win32 Debug" (based on "Win32 (x86) Application")
//# Name "RenameProject - Win32 Release"
//# Name "RenameProject - Win32 Debug"
//!MESSAGE NMAKE /f "RenameProject.mak".
//!MESSAGE NMAKE /f "RenameProject.mak" CFG="RenameProject - Win32 Debug"

	oldName.erase(oldName.length()-4,4);

	for (int pos=1; pos >0; ) {
		int nextPos = buf.find(oldName,pos);
		int replacePos = FindTillEOL(buf, "-", nextPos);
		if (replacePos !=-1) {
			buf.replace(nextPos, oldName.length(),m_strName);
		}
//		DEBUGMSG( pos );
		pos=nextPos+1;
	}

	// dump content to new .dsp file 
	//
	{{{
		tmp =m_strName + ".dsp" ;
		strNewDswPath = strPath + tmp;

		ofstream ofs(strNewDswPath.c_str());
		ofs << buf;
		ofs.close();
	}}}

	delete pbuf;

CDialog::OnOK();
}

⌨️ 快捷键说明

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