// the minimized window.
HCURSOR CKursDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CKursDlg::OnBtnsource()
{
CFileDialog dlg(TRUE, NULL, NULL, 0,
"C++ and C files (*.cpp; *.c; *.h; *.hpp)|*.cpp;*.c;*.h;*.hpp|Pascal files (*.pas)|*.pas|Basic files (*.bas)|*.bas|Assembler files (*.asm)|*.asm|All files|*.*||", this);
CString lang;
CSelectLangDlg select;
if(dlg.DoModal() == IDOK)
{
m_name = dlg.GetPathName();
SetDlgItemText(IDC_SOURCE, m_name);
int point_pos = m_name.ReverseFind('.');
if(point_pos == -1)
{
if(select.DoModal())
m_lang = select.m_language + 1;
else
m_lang = LANG_CPP;
}
else
{
lang = m_name.Right(m_name.GetLength() - point_pos - 1);
if((lang == "c") || (lang == "cpp") || (lang == "h") || (lang == "hpp"))
m_lang = LANG_CPP;
else if(lang == "pas")
m_lang = LANG_PASCAL;
else if(lang == "bas")
m_lang = LANG_BASIC;
else if(lang == "asm")
m_lang = LANG_ASM;
else
{
if(select.DoModal())
m_lang = select.m_language + 1;
else
m_lang = LANG_CPP;
}
}
point_pos = m_name.ReverseFind('\');
m_dest = m_name.Left(point_pos);
m_dest += "\output.";
m_dest += lang;
SetDlgItemText(IDC_DEST, m_dest);
}
}
void CKursDlg::OnBtnmake()
{
GetDlgItemText(IDC_SOURCE, m_name);
GetDlgItemText(IDC_DEST, m_dest);
CFile source(m_name, CFile::modeRead | CFile::shareDenyRead | CFile::shareDenyWrite | CFile::normal);
CString text;
char buffer;
UINT i;
for(i = 0; i < source.GetLength(); i++)
{
source.Read(&buffer, 1);
text += buffer;
}
text.GetBuffer(0)[source.GetLength()] = 0;
switch(m_lang)
{
case LANG_CPP:
{
RemoveOneLineComments(text, "//", FALSE, "");
if(RemoveMultiLineComments(text, "/*", "*/") == -1)
{
AfxMessageBox("Неправильныемногострочныекоментарии", MB_ICONERROR);
return;
}
break;
}
case LANG_PASCAL:
{
RemoveOneLineComments(text, "//", FALSE, "");
if(RemoveMultiLineComments(text, "{", "}") == -1)
{
AfxMessageBox("Неправильные многострочные коментарии", MB_ICONERROR);
return;
}
break;
}
case LANG_BASIC:
{
RemoveOneLineComments(text, "'", TRUE, "_");
RemoveOneLineComments(text, "REM ", TRUE, "_");
RemoveOneLineComments(text, "rem ", TRUE, "_");
RemoveOneLineComments(text, "Rem ", TRUE, "_");
break;
}
case LANG_ASM:
{
RemoveOneLineComments(text, ";", FALSE, "");
break;
}
}
source.Close();
CFile dest(m_dest, CFile::modeCreate | CFile::modeReadWrite | CFile::normal);
dest.Write(text.GetBuffer(0), text.GetLength());
AfxMessageBox("Комментарииудалены", MB_ICONINFORMATION);
}
int CKursDlg::RemoveOneLineComments(CString& text, CString comment, BOOL bHasLineWraps, CString wrap)
{
CArray<CString, CString&> linelist;
CString line, full = text;
char *buffer = full.LockBuffer();
char *token = strtok(buffer, "\r\n");
while(token)
{
linelist.Add(CString(token));
token = strtok(NULL, "\r\n");
}
full.UnlockBuffer();
int i, find, count;
BOOL bWrap = FALSE;
for(i = 0; i < linelist.GetSize(); i++)
{
if(linelist[i].Find(comment, 0) == 0)
{
if(bHasLineWraps)
{
if(linelist[i].Find(wrap, 0) == linelist[i].GetLength() - wrap.GetLength())
bWrap = TRUE;
}
linelist.RemoveAt(i);
i--;
continue;
}
else if((find = linelist[i].Find(comment, 0)) > 0)
{
if(bHasLineWraps)
{
if(linelist[i].Find(wrap, 0) == linelist[i].GetLength() - wrap.GetLength())
bWrap = TRUE;
}
count = linelist[i].GetLength() - find;
linelist[i].Delete(find, count);
continue;
}
if(bHasLineWraps && bWrap)
{
if(linelist[i].Find(wrap, 0) == linelist[i].GetLength() - wrap.GetLength())
bWrap = TRUE;
else
bWrap = FALSE;
linelist.RemoveAt(i);
i--;
}
}
text = "";
for(i = 0; i < linelist.GetSize(); i++)
{
text += linelist[i];
text += "\r\n";
}
linelist.RemoveAll();
return 0;
}
int CKursDlg::RemoveMultiLineComments(CString &text, CString opencomment, CString closecomment)
{
int open, close;
while((open = text.Find(opencomment, 0)) != -1)
{
if((close = text.Find(closecomment, open)) != -1)
{
text.Delete(open, close - open + closecomment.GetLength());
}
else
return -1;
}
return 0;
}
void CKursDlg::OnBtndest()
{
CFileDialog dlg(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"C++ and C files (*.cpp; *.c; *.h; *.hpp)|*.cpp;*.c;*.h;*.hpp|Pascal files (*.pas)|*.pas|Basic files (*.bas)|*.bas|Assembler files (*.asm)|*.asm|All files|*.*||", this);
if(dlg.DoModal() == IDOK)
{
m_dest = dlg.GetPathName();
SetDlgItemText(IDC_DEST, m_dest);
}
}
Файл SelectLangDlg;
// SelectLangDlg.cpp : implementation file
//
#include "stdafx.h"
#include "kurs.h"
#include "SelectLangDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSelectLangDlg dialog
CSelectLangDlg::CSelectLangDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSelectLangDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSelectLangDlg)
m_language = 0;
//}}AFX_DATA_INIT
}
void CSelectLangDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSelectLangDlg)
DDX_Radio(pDX, IDC_CPP, m_language);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSelectLangDlg, CDialog)
//{{AFX_MSG_MAP(CSelectLangDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSelectLangDlg message handlers
void CSelectLangDlg::OnOK()
{
UpdateData();
CDialog::OnOK();
}
BOOL CSelectLangDlg::OnInitDialog()
{
CDialog::OnInitDialog();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}