📄 fileindentpage.cpp
字号:
// FileIndentPage.cpp : 实现文件
//
#include "stdafx.h"
#include "windent.h"
#include "FileIndentPage.h"
#include ".\fileindentpage.h"
#include "windentdlg.h"
extern "C"{
#include "indent.h"
#include "output.h"
}
#include <assert.h>
#include <shlwapi.h>
// CFileIndentPage 对话框
IMPLEMENT_DYNAMIC(CFileIndentPage, CDialog)
CFileIndentPage::CFileIndentPage(CWnd* pParent /*=NULL*/)
: CDialog(CFileIndentPage::IDD, pParent)
{
}
CFileIndentPage::~CFileIndentPage()
{
}
void CFileIndentPage::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_FILELIST, m_FileList);
}
BEGIN_MESSAGE_MAP(CFileIndentPage, CDialog)
ON_BN_CLICKED(IDC_ADD, OnBnClickedAdd)
ON_BN_CLICKED(IDC_REMOVE, OnBnClickedRemove)
ON_NOTIFY(NM_CLICK, IDC_FILELIST, OnNMClickFilelist)
ON_BN_CLICKED(IDC_REMOVEALL, OnBnClickedRemoveall)
ON_BN_CLICKED(IDC_FORMATFILE, OnBnClickedFormatfile)
END_MESSAGE_MAP()
// CFileIndentPage 消息处理程序
BOOL CFileIndentPage::OnInitDialog()
{
CDialog::OnInitDialog();
m_FileList.InsertColumn(0,"FileName",LVCFMT_LEFT,315);
m_FileList.InsertColumn(1,"Status",LVCFMT_LEFT,100);
UpdateUI();
return TRUE;
}
void CFileIndentPage::OnBnClickedAdd()
{
CFileDialog dlg(TRUE,NULL,NULL,
OFN_ALLOWMULTISELECT,
"C/C++ Files|*.cpp;*.c;*.h;*.cxx|All Files(*.*)|*.*",
this);
if (dlg.DoModal()==IDOK)
{
POSITION pos=dlg.GetStartPosition();
int i=m_FileList.GetItemCount();
do
{
CString filename=dlg.GetNextPathName(pos);
LVFINDINFO fi;
fi.flags=LVFI_STRING;
fi.psz=filename;
if (m_FileList.FindItem(&fi,-1)==-1) //防止添加重复文件
m_FileList.InsertItem(i++,filename);
}while(pos!=NULL);
}
UpdateUI();
}
void CFileIndentPage::OnBnClickedRemove()
{
POSITION pos=m_FileList.GetFirstSelectedItemPosition();
while(pos!=NULL)
{
int i=m_FileList.GetNextSelectedItem(pos);
m_FileList.DeleteItem(i);
pos=m_FileList.GetFirstSelectedItemPosition();
}
UpdateUI();
}
void CFileIndentPage::UpdateUI(void)
{
int count=m_FileList.GetItemCount();
int selectcount=m_FileList.GetSelectedCount();
GetDlgItem(IDC_REMOVE)->EnableWindow(selectcount!=0);
GetDlgItem(IDC_REMOVEALL)->EnableWindow(count!=0);
}
void CFileIndentPage::OnNMClickFilelist(NMHDR *pNMHDR, LRESULT *pResult)
{
UpdateUI();
*pResult = 0;
}
void CFileIndentPage::OnBnClickedRemoveall()
{
m_FileList.DeleteAllItems();
UpdateUI();
}
void CFileIndentPage::OnBnClickedFormatfile()
{
bool bForce=IsDlgButtonChecked(IDC_FORCE)!=0;
bool bBackup=IsDlgButtonChecked(IDC_BACKUP)!=0;
CWaitCursor wait; //let cursor become hand
int iCount=m_FileList.GetItemCount();
user_options_ty* ps= ((CWindentDlg*)this->GetParent()->GetParent())->GetSetting();
for (int i=0;i<iCount;++i)
{
CString filename=m_FileList.GetItemText(i,0);
int result=indent_file(filename,ps);
bool bsave=false;
bool needsave=true;
bool warn=result<4 && result!=0;
if (result==0 || //no error
(bForce && result<4) //err but save
)
{
needsave=true;
try
{
if (bBackup) //need backup?
{
BackupFile(filename);
}
MEMFILE* mf=get_output();//open memfile
//CFile::Remove(filename);
CStdioFile newfile(filename,CFile::modeCreate|CFile::modeWrite|CFile::typeText);
int isize=mf_length(mf);
unsigned char* buf=(unsigned char*)malloc(isize);
mf_read(mf,buf,isize);
newfile.Write(buf,isize);
free(buf);
newfile.Close();
bsave=true;
}
catch (CFileException * e)
{
TRACE("io error:%s\n",e->m_strFileName);
bsave=false;
}
}
else needsave=false;
char *str=NULL;
if (!needsave)
str="Indent Fatal Error,Not Save!";
else if (warn && bsave)
str="Indent Warn,But Save!";
else if (!warn && bsave)
str="Successful!";
else if (!bsave)
str="IO error!";
else
str="";
m_FileList.SetItemText(i,1,str);
}
}
void CFileIndentPage::BackupFile(const CString& filename)
{
CString backname=filename;
backname.Append(".bak");
while (PathFileExists(backname))
{
backname.Append(".bak");
}
assert(CopyFile(filename,backname,TRUE));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -