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

📄 moduleview.cpp

📁 Resource editor base speadrum Chinese mobile
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ModuleView.cpp : implementation file
//

#include "stdafx.h"
#include "resourceeditor.h"
#include "ModuleView.h"
#include "Converter.h"
#include "FileDlg.h"
#include "DirFileInfo.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define FILE_BUFF_SIZE 128 //128个uint32
const _TCHAR CModuleView::SZ_FILE_EXT[] = _T(".res");
const _TCHAR CModuleView::SZ_FILTER[]   = _T("resource files (*.res)|*.res|All Files (*.*)|*.*||");

/////////////////////////////////////////////////////////////////////////////
// CModuleView

IMPLEMENT_DYNCREATE(CModuleView, CListView)

CModuleView::CModuleView()
{
	memset(&module_total_info,0,sizeof(MMI_MODULE_TOTAL_INFO_T));
	module_total_info.total_usedsize=sizeof(MMI_MODULE_TOTAL_INFO_T);
	m_pPrjFactory = NULL;
}

CModuleView::~CModuleView()
{
}


BEGIN_MESSAGE_MAP(CModuleView, CListView)
	//{{AFX_MSG_MAP(CModuleView)
	ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
	ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
	ON_COMMAND(IDM_OUT_CLEAR, OnOutClear)
	ON_COMMAND(IDM_OUT_GOTO, OnOutGoto)
	ON_COMMAND(IDM_MODULE_DEL, OnModuleDel)
	ON_COMMAND(IDM_MODULE_DOWN, OnModuleDown)
	ON_COMMAND(IDM_MODULE_UP, OnModuleUp)
	ON_MESSAGE(WM_ADD_LIST,OnAddToList)
	ON_COMMAND(IDM_MODULE_ADD, OnModuleAdd)
	ON_UPDATE_COMMAND_UI(IDM_MODULE_ADD, OnUpdateModuleAdd)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CModuleView drawing

void CModuleView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CModuleView diagnostics

#ifdef _DEBUG
void CModuleView::AssertValid() const
{
	CListView::AssertValid();
}

void CModuleView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CModuleView message handlers

void CModuleView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	CListCtrl &ls = GetListCtrl();
	CString str;
	int i;
    i=ls.GetSelectionMark();
	if (i<=0)
	{
		return;
	}
	str= g_theApp.m_prjarray[i-1]; 

	if ((""==str)||(i<=0))
	{
		return;
	}
	
//	if (g_theApp.m_bCanShowView)
//	{
		g_theApp.m_isclickopen=TRUE;
		g_theApp.m_list_clickbinfile=str;
		g_theApp.OnFileOpen();
//	}
	RecordMoudleInfo();
	*pResult = 0;
}

void CModuleView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
    LPNMITEMACTIVATE pnia = (LPNMITEMACTIVATE)pNMHDR;

    m_nCurItem = pnia->iItem;

	CMenu menu;
	VERIFY( menu.LoadMenu(IDM_POPUP_MODULE) );

	CMenu * pMenu = menu.GetSubMenu(0);

    CListCtrl &lst = GetListCtrl();

    if( lst.GetItemCount() == 1 || m_nCurItem < 1  )
    {
        pMenu->EnableMenuItem( IDM_MODULE_DEL, MF_BYCOMMAND | MF_GRAYED );
		pMenu->EnableMenuItem( IDM_MODULE_UP, MF_BYCOMMAND | MF_GRAYED );
		pMenu->EnableMenuItem( IDM_MODULE_DOWN, MF_BYCOMMAND | MF_GRAYED );
    }

    if( m_nCurItem == 1 && lst.GetItemCount() >= 2)
    {
		pMenu->EnableMenuItem( IDM_MODULE_UP, MF_BYCOMMAND | MF_GRAYED );
    }

    if( m_nCurItem == lst.GetItemCount()-1 && lst.GetItemCount() >= 2)
	{
		pMenu->EnableMenuItem( IDM_MODULE_DOWN, MF_BYCOMMAND | MF_GRAYED );
	}
	

    POINT pt = pnia->ptAction;
    ClientToScreen(&pt);
    pMenu->TrackPopupMenu(TPM_LEFTALIGN, pt.x, pt.y, this);	
	*pResult = 0;
}

void CModuleView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// TODO: Add your specialized code here and/or call the base class
	//test code
	/*CListCtrl &ls = GetListCtrl();
		char ctest[5];
		int test;
        
		for (int i=0;i<10;++i)
		{
			ls.InsertItem(0,"test");
		}
		test=ls.GetItemCount();
		sprintf(ctest,"%d",test);
	MessageBox(ctest);*/
	CString module_id="Module List:";
	AddModuleToList(module_id);
}

BOOL CModuleView::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
    cs.style |= LVS_REPORT;
    cs.style |= LVS_SHOWSELALWAYS;
	cs.style |= LVS_LIST;
	cs.style |= LVS_SINGLESEL;
	return CListView::PreCreateWindow(cs);
}

void CModuleView::OnOutClear() 
{
	// TODO: Add your command handler code here
	
}

void CModuleView::OnOutGoto() 
{
	// TODO: Add your command handler code here
	
}


void CModuleView::AddModuleToList(LPCTSTR module_id)
{
	CString filename=module_id;
	int nleft=filename.ReverseFind(_T('\\'));
	filename=filename.Right((filename.GetLength()-nleft-1));
	CListCtrl &ls = GetListCtrl();
	ls.InsertItem(ls.GetItemCount(),filename);
}
uint32 CModuleView ::CalcModuleSize(LPCTSTR filename)
{
	uint32 usedsize=0;
	static _TCHAR m_szErrMsg[256];
	_ASSERTE( &filename != NULL );	
	CString strErr;
	//the prj and bin file save together
	FILE *PFile =_tfopen(filename, _T("rb"));
	if( PFile == NULL )
	{
		strErr.Format(IDS_ERR_OPEN_PRJ_FOR_SIZE,filename);
		AfxMessageBox(strErr);
		return 0;
	}
	
	PRJ_BIN_ADDR_DATA prj_info;
	if (fread(&prj_info,sizeof(PRJ_BIN_ADDR_DATA),1,PFile)!=1)
	{
		strErr.Format(IDS_ERR_READ_PRJ_FOR_SIZE,filename);
		AfxMessageBox(strErr);
        fclose(PFile);
		return 0;
	}
	usedsize=prj_info.win_addr-prj_info.arm_addr;
	fclose(PFile);
	return usedsize;
    
}
void CModuleView::RecordMoudleInfo(void)
{
	CListCtrl &ls=GetListCtrl();
	int module_count=0,i=0;
	uint32 usedsize=0;
	CString filename;
	char msg_str[32];
	//the first list item is the title of "Module List:"
	//so it not be calculate
	g_theApp.m_module_id.RemoveAll();
	memset(&module_total_info,0,sizeof(MMI_MODULE_TOTAL_INFO_T));
	module_total_info.total_usedsize=sizeof(MMI_MODULE_TOTAL_INFO_T);
	module_count=ls.GetItemCount();
	for (i=1;i<module_count;++i)
	{
		int nsize=g_theApp.m_prjarray.GetSize();
		filename=g_theApp.m_prjarray[i-1];		
		
		usedsize=CalcModuleSize((LPCTSTR)filename);
		module_total_info.mmi_module_adrr[i-1]=module_total_info.total_usedsize;
		module_total_info.total_usedsize+=usedsize;
	
		SaveModuleId((LPCTSTR)filename);
		sprintf(msg_str,"%4d",module_total_info.mmi_module_adrr[i-1]);

		memset(msg_str,0,sizeof(msg_str));
		sprintf(msg_str,"%4d",module_total_info.total_usedsize);
	}
    
}
BOOL CModuleView::SaveResourceFile(LPCTSTR pszFileName)
{
	CWaitCursor waitCursor;
	SaveResourceResFile(pszFileName);
	SaveResourceBinFile(pszFileName);
	SaveResourceMapFile(pszFileName);
	return TRUE;
}
BOOL CModuleView::SaveResourceResFile(LPCTSTR pszFileName)
{
	CString Resfilename,filename;
	int module_count=0,i=0;
	_TCHAR m_szErrMsg[256];
	Resfilename=pszFileName;
	Resfilename.Replace(_T(".bin"),_T(".res"));
	FILE *PFile =_tfopen(Resfilename, _T("wb"));
    char szVer[] = "RF01"; //四个字节 表示版本 Unicode版
	if(fwrite(szVer,4,1,PFile)!=1)
	{
		_stprintf(m_szErrMsg, _T("Writ res file version Fail!"));
		MessageBox(m_szErrMsg);
		fclose(PFile);
		return FALSE;
	}
	_TCHAR Prjname[256]={0};
    CListCtrl &ls=GetListCtrl();
    module_count=ls.GetItemCount();
	
	if(fwrite(&module_count,sizeof(int),1,PFile)!=1)
	{
		_stprintf(m_szErrMsg, _T("Writ res file list count\"%s\" Fail!"), filename);
		MessageBox(m_szErrMsg);
		fclose(PFile);
		return FALSE;
	}
	for (i=1;i<module_count;++i)
	{
		CString relative_path;
		int nsize=g_theApp.m_prjarray.GetSize();
		filename=g_theApp.m_prjarray[i-1];
		relative_path=g_theApp.ConvertAbs2Rel(filename,g_theApp.m_resBasePath);

		_stprintf(Prjname, _T("%s\r\n"), relative_path);
		if(fwrite(Prjname,256,1,PFile)!=1)
		{
			_stprintf(m_szErrMsg, _T("Write res file \"%s\" Fail!"), filename);
			MessageBox(m_szErrMsg);
			fclose(PFile);
		    return FALSE;
		}
	}
	fclose(PFile);
	return TRUE;
}
BOOL CModuleView::SaveResourceBinFile(LPCTSTR pszFileName)
{
    FILE * pArmFile = _tfopen(pszFileName, _T("wb"));
	FILE * pSFile = NULL;
	CListCtrl &ls=GetListCtrl();
	CString strErr;
	CString sfilname;
	int module_count=0,i=0;
	uint32 file_buff=0;
	uint32 buff_size=sizeof(uint32),j=0;
	CString hfilename=(CString)pszFileName;
	int nLeft = hfilename.ReverseFind(_T('\\'));
	if( nLeft > 0 )
	{
		hfilename = hfilename.Left(nLeft);
		hfilename.Insert(nLeft,_T("\\mmi_module.h"));
	}
	RecordMoudleInfo();
	//MessageBox((LPCTSTR)hfilename);
	WriteModuleHFile((LPCTSTR)hfilename);
	module_count=ls.GetItemCount();
    if( pArmFile == NULL )
    {
		strErr.Format(IDS_ERR_CREATE_FILE_FAIL,pszFileName);
        AfxMessageBox(strErr);
        return FALSE;
    }
	CString winbinfile;
	winbinfile=(CString)pszFileName;
	winbinfile.Insert((winbinfile.GetLength())-4,_T("_win"));
	FILE * pWinFile = _tfopen(winbinfile, _T("wb"));
	if( pWinFile == NULL )
    {        
		strErr.Format(IDS_ERR_CREATE_FILE_FAIL,winbinfile);
        AfxMessageBox(strErr);
        fclose(pArmFile);
        return FALSE;
    }
	//const BOOL bBigEdn = g_theApp.m_MMIRes.m_Resource.ToolUsedInfo.bBigEndian;
	const BOOL bBigEdn=TRUE;
	CConverter conv;
	uint32 naddr=0;
	//write win bin header file
	naddr=Addr4ByteAlign(sizeof(MMI_MODULE_TOTAL_INFO_T));
	fwrite(&module_total_info,naddr,1,pWinFile);
    //write arm bin header file
    module_total_info.total_usedsize = conv.ConvEdn_INT((DWORD)module_total_info.total_usedsize);
	for(i=0;i<module_count-1;++i)
	{
		module_total_info.mmi_module_adrr[i]= conv.ConvEdn_INT((DWORD)module_total_info.mmi_module_adrr[i]);
	}
    naddr=0;
	naddr=Addr4ByteAlign(sizeof(MMI_MODULE_TOTAL_INFO_T));
	fwrite(&module_total_info,naddr,1,pArmFile);


	for (i=1;i<module_count;++i)
	{
		sfilname=g_theApp.m_prjarray[i-1]; //ls.GetItemText(i,0);
		pSFile=_tfopen((LPCTSTR)sfilname, _T("rb"));
		if( pSFile == NULL )
		{			
			strErr.Format(IDS_ERR_CREATE_FILE_FAIL,sfilname);
			AfxMessageBox(strErr);
            fclose(pArmFile);
			fclose(pWinFile);
			return FALSE;
		}
		PRJ_BIN_ADDR_DATA prj_info;
        if (fread(&prj_info,sizeof(PRJ_BIN_ADDR_DATA),1,pSFile)!=1)
		{			
			strErr.Format(IDS_ERR_READ_PRJ_FOR_SIZE,sfilname);
			AfxMessageBox(strErr);
			fclose(pSFile);
			fclose(pArmFile);
			fclose(pWinFile);
			return FALSE;
		}
		fseek(pSFile,prj_info.arm_addr,SEEK_SET);
		buff_size=sizeof(uint32);
		j=0;
		while((uint32)ftell(pSFile)<prj_info.win_addr)
		{
			fread(&file_buff,1,buff_size,pSFile);
			fwrite(&file_buff,1,buff_size,pArmFile);
		}
		
		int read_num=0;
		while (!feof(pSFile))
		{
			read_num=fread(&file_buff,1,buff_size,pSFile);
			fwrite(&file_buff,1,read_num,pWinFile);

		}
		/*while (buff_size)
		{
			memset(file_buff,0,buff_size);
			if(fread(file_buff,buff_size,1,pSFile))
			{
				fwrite(file_buff,buff_size,1,pDFile);
				j=j+buff_size;
			}
			else
			{
				fseek(pSFile,j,SEEK_SET);
				buff_size=buff_size/2;
			}
		}*/
		fclose(pSFile);
	}
	fclose(pArmFile);
	fclose(pWinFile);
	return TRUE;
}
void CModuleView::SaveModuleId(LPCTSTR pszFileName)
{

	LPCTSTR pFind = _tcsrchr(pszFileName, _T('\\'));
    if( NULL == pFind )
        pFind = pszFileName;
    else
        pFind++;

    CString strDirName = pFind;
    int nLeft = strDirName.ReverseFind(_T('.'));
    if( nLeft > 0 )
        strDirName = strDirName.Left(nLeft);
    strDirName.Insert(0,_T("MMI_MODULE_"));
    strDirName.MakeUpper();
	g_theApp.m_module_id.Add(strDirName);
    //MessageBox(g_theApp.module_id[0]);
    //return strDirName; 
}

//保存资源的统计信息xxx.map
BOOL CModuleView::SaveResourceMapFile(LPCTSTR pszFileName)
{
	CString Mapfilename,filename;
	int module_count=0,i=0;
	//_TCHAR m_szErrMsg[256];
	Mapfilename=pszFileName;
	Mapfilename.Replace(_T(".bin"),_T(".map"));
	FILE *pFile =_tfopen(Mapfilename, _T("wb"));
	module_count = g_theApp.m_prjarray.GetSize();
	CString strCurPrj = g_theApp.m_strPrjFileName;
	int **pSize;
	pSize = new int*[module_count];	
	for (i=0;i<module_count;++i)
	{
		*(pSize+i) = new int[4];
		memset(*(pSize+i),0,4*sizeof(int));
		filename=g_theApp.m_prjarray[i];
		if(filename.CompareNoCase(g_theApp.m_strPrjFileName)==0)
            MapPrjInfo(filename,pFile,pSize[i]);
        else
		{			
			g_theApp.m_isclickopen=TRUE;
			g_theApp.m_list_clickbinfile=filename;
			g_theApp.OnFileOpen();
			RecordMoudleInfo();
			MapPrjInfo(filename,pFile,pSize[i]);
		}
	}

⌨️ 快捷键说明

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