📄 getzzlcommander.cpp
字号:
/*
* Openmysee
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "stdafx.h"
#include "GetZZLCommander.h"
#include "GetZZLDisplayDlg.h"
CGetZZLCommander::CGetZZLCommander() :miCount(0)
{
mhGetZZL = NULL;
miStopFlag = 1;
mlstSourceFile.clear();
mbClose = FALSE;
CreateDebugInfo();
mbRunThread = FALSE;
}
CGetZZLCommander::~CGetZZLCommander()
{
if (NULL != mhGetZZL)
{
::CloseGetZZL(mhGetZZL);
mhGetZZL = NULL;
}
mhGetZZL = NULL;
list<STRU_SOURCE_FILE>::iterator litSourceFile = mlstSourceFile.begin();
while (litSourceFile != mlstSourceFile.end())
{
delete (*litSourceFile).mszSourceFile;
(*litSourceFile).mszSourceFile = NULL;
delete (*litSourceFile).mszSourceFileName;
(*litSourceFile).mszSourceFileName = NULL;
++litSourceFile;
}
}
//创建调试信息(日志)
bool CGetZZLCommander::CreateDebugInfo()
{
char strPath[255];
char strLogFileName[255];
//首先设置日志打印选项
CDebugTrace::SetTraceLevel(6);
CDebugTrace::SetTraceOptions(CDebugTrace::GetTraceOptions() \
| CDebugTrace::Timestamp & ~CDebugTrace::LogLevel \
& ~CDebugTrace::FileAndLine | CDebugTrace::AppendToFile\
& ~CDebugTrace::PrintToConsole);
if (!GetModuleFileName(NULL,strPath, 255))
{
TRACE1("CMediaCenter::CreateDebugInfo:GetModuleFileName()函数返回失败!\n");
return false;
}
//从strPath中去掉文件名,从而取得可执行文件的路径;
int nPosition = 0;
nPosition = (int)(strrchr(strPath,'\\') - strPath);
strPath[nPosition+1] = '\0';
//生成日志目录
strcat(strPath, "日志文件\\");
if(TRUE != CreateDirectory(strPath, NULL))
{
int i = GetLastError();
if (ERROR_ACCESS_DENIED != i && ERROR_ALREADY_EXISTS != i)
{
MessageBox(NULL, "创建文件夹失败", "错误", MB_OK|MB_ICONSTOP);
}
}
//生成TRACE文件名
SYSTEMTIME loSystemTime;
GetLocalTime(&loSystemTime);
sprintf(strLogFileName, "%sGetZZLDisplay%4d%02d%02d%s", strPath,loSystemTime.wYear,\
loSystemTime.wMonth,loSystemTime.wDay,".log");
//sprintf(strLogFileName, "%sUTMedia%4d%02d%02d%s", strPath,loSystemTime.wYear,loSystemTime.wMonth,loSystemTime.wDay,".log");
//strcat(strPath, "GetZZLDisplay.log");
CDebugTrace::SetLogFileName(strLogFileName);
TraceLog1("===============GetZZLDisplay%4d年%02d%月%02d日处理日志====================\n",\
loSystemTime.wYear, loSystemTime.wMonth,loSystemTime.wDay);
TraceLog1("===============北京高维视讯 ================================\n");
TraceLog1("===============作者: 谢洲为xiezhouwei@hotmail.com=====================\n");
return true;
}
void CGetZZLCommander::CreateInstance()
{
if (NULL == mhGetZZL)
{
mhGetZZL = CreateGetZZL();
}
}
//获取源文件名
BOOL CGetZZLCommander::SetSourceFile(CString& astrSourceFile,CString& astrSourceFileName)
{
astrSourceFileName.Trim();
CAuto lock(&mocritsec);
STRU_SOURCE_FILE lstruSourceFile = {0};
lstruSourceFile.mszSourceFile = new char[MAX_PATH + 1];
lstruSourceFile.mszSourceFileName = new char[MAX_PATH + 1];
ZeroMemory(lstruSourceFile.mszSourceFile, sizeof(lstruSourceFile.mszSourceFile));
ZeroMemory(lstruSourceFile.mszSourceFileName, sizeof(lstruSourceFile.mszSourceFileName));
strncpy(lstruSourceFile.mszSourceFile, astrSourceFile, MAX_PATH);
strncpy(lstruSourceFile.mszSourceFileName, astrSourceFileName, MAX_PATH);
list<STRU_SOURCE_FILE>::iterator litSourceFile = mlstSourceFile.begin();
while (litSourceFile != mlstSourceFile.end())
{
if (astrSourceFile == (*litSourceFile).mszSourceFile)
{
return FALSE;
}
++litSourceFile;
}
mlstSourceFile.push_back(lstruSourceFile);
TraceLog1("添加文件“%s”\n", lstruSourceFile.mszSourceFileName);
return TRUE;
}
void CGetZZLCommander::DelSourceFile(CString& astrSourceFileName)
{
CAuto lock(&mocritsec);
list<STRU_SOURCE_FILE>::iterator litSourceFile = mlstSourceFile.begin();
while (litSourceFile != mlstSourceFile.end())
{
if (astrSourceFileName == (*litSourceFile).mszSourceFileName)
{
TraceLog1("删除文件“%s”\n", (*litSourceFile).mszSourceFileName);
delete (*litSourceFile).mszSourceFile;
(*litSourceFile).mszSourceFile = NULL;
delete (*litSourceFile).mszSourceFileName;
(*litSourceFile).mszSourceFileName = NULL;
mlstSourceFile.erase(litSourceFile);
break;
}
++ litSourceFile;
}
}
//设置回调
int CGetZZLCommander::GetZZLState(void* apUserPara,int aiState)
{
CGetZZLCommander* lpGetZZLCommander = (CGetZZLCommander*)apUserPara;
switch(aiState)
{
case 1:
lpGetZZLCommander->miStopFlag = 1;
//MessageBox(NULL, "已经停止", "通告",MB_OK);
lpGetZZLCommander->Stop();
break;
case -1:
lpGetZZLCommander->miStopFlag = -1;
default:
lpGetZZLCommander->miStopFlag = 0;
}
return 1;
}
//开始运行
BOOL CGetZZLCommander::RunCommader(CWnd* apWnd)
{
ASSERT(apWnd);
mpWnd = apWnd;
BOOL lbIsSuccess = TRUE;
if (0 == mlstSourceFile.size())
{
MessageBox(NULL, "用户没有添加任何文件,请重新开始","错误",MB_OK|MB_ICONSTOP);
return FALSE;
}
if ('\0'== (LPCTSTR)mstrBuffer[0])
{
MessageBox(NULL, "缓存路径不能为空","错误",MB_OK|MB_ICONSTOP);
return FALSE;
}
if (TRUE == mbRunThread)
{
return lbIsSuccess;
}
mbRunThread = TRUE;
DWORD dwThreadId = 0;
HANDLE hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ConvertZZL,this,0,&dwThreadId);
if( hThread == NULL)
{
return FALSE;
}
mbRunThread = FALSE;
return lbIsSuccess;
}
void CGetZZLCommander::ConvertZZL( CGetZZLCommander* pParam)
{
CGetZZLCommander* lpCommander = (CGetZZLCommander*) pParam;
char lszInfo[1024] = {0};
lpCommander->mocritsec.Enter();
lpCommander->mbClose = FALSE;
lpCommander->mocritsec.Leave();
list<STRU_SOURCE_FILE>::iterator litSourceFile = lpCommander->mlstSourceFile.begin();
int i = lpCommander->miCount;
while (0 != i)
{
litSourceFile ++;
i --;
}
int liError = 0;
while (litSourceFile != lpCommander->mlstSourceFile.end())
{
lpCommander->CreateInstance();
ASSERT(lpCommander->mhGetZZL);
SetStateProc(lpCommander->mhGetZZL, &GetZZLState, lpCommander);
if (TRUE == lpCommander->mbClose)
{
break;
}
//CreateInstance();
TraceLog1("开始处理%s文件\n",(*litSourceFile).mszSourceFileName);
if (NULL == (*litSourceFile).mszSourceFile || NULL == (*litSourceFile).mszSourceFileName)
{
MessageBox(NULL, "要转换的源文件或文件名读取错误", "错误", MB_OK|MB_ICONSTOP);
TraceLog1("要转换的源文件或文件名读取错误\n");
TraceLog1("结束处理%s文件,该文件处理失败\n",(*litSourceFile).mszSourceFileName);
lpCommander->mbRunThread = FALSE;
return ;
}
if (NULL == (LPCTSTR)lpCommander->mstrBuffer || TRUE == lpCommander->mstrBuffer.IsEmpty())
{
MessageBox(NULL, "保存路径出错,请设置保存路径", "错误", MB_OK|MB_ICONSTOP);
TraceLog1("保存路径出错,请设置保存路径\n");
TraceLog1("结束处理%s文件,该文件处理失败\n",(*litSourceFile).mszSourceFileName);
lpCommander->mbRunThread = FALSE;
return ;
}
ZeroMemory(&lszInfo,sizeof(lszInfo));
sprintf(lszInfo,"共%d个文件\n“%s” \n正在转换......, 第%d个文件,已有%d个文件失败。", lpCommander->mlstSourceFile.size(), (*litSourceFile).mszSourceFileName, 1+i, liError);
lpCommander->mpWnd->GetDlgItem(IDC_STATE_TEXT)->SetWindowText(lszInfo);
if (TRUE == (*litSourceFile).mbFinished)
{
TraceLog1("文件“%s”已经被处理过了。如果还想重复处理,请重新打开本程序\n", (*litSourceFile).mszSourceFileName);
++ litSourceFile;
continue;
}
if (FALSE == SetFileName(lpCommander->mhGetZZL, (*litSourceFile).mszSourceFile, lpCommander->mstrBuffer,(*litSourceFile).mszSourceFileName))
{
if (litSourceFile != lpCommander->mlstSourceFile.end())
{
++ liError;
//::CloseGetZZL(lpCommander->mhGetZZL);
//lpCommander->mhGetZZL = NULL;
TraceLog1("结束处理%s文件,该文件处理失败,共%d个文件失败\n",(*litSourceFile).mszSourceFileName, liError);
(*litSourceFile).mbFinished = TRUE;
++ litSourceFile;
continue;
}
lpCommander->mpWnd->GetDlgItem(IDC_BTN_ADD)->EnableWindow(TRUE);
lpCommander->mpWnd->GetDlgItem(IDC_BTN_DEL)->EnableWindow(TRUE);
lpCommander->mbRunThread = FALSE;
return;
}
//lbIsSuccess = Run(mhGetZZL);
Run(lpCommander->mhGetZZL);
lpCommander->miStopFlag = 0;
while(0 == lpCommander->miStopFlag && FALSE == lpCommander->mbClose)
{
lpCommander->mpWnd->GetDlgItem(IDC_STATE_TEXT)->SetWindowText(lszInfo);
Sleep(1000);
}
TraceLog1("结束处理%s文件。\n",(*litSourceFile).mszSourceFileName, liError);
if (FALSE == lpCommander->mbClose)
{
(*litSourceFile).mbFinished = TRUE;
}
//::CloseGetZZL(mhGetZZL);
//mhGetZZL = NULL;
++ litSourceFile;
++ i;
if (NULL != lpCommander->mhGetZZL)
{
::CloseGetZZL(lpCommander->mhGetZZL);
lpCommander->mhGetZZL = NULL;
}
}
char lstrtemp[MAX_PATH];
if (FALSE == lpCommander->mbClose)
{
sprintf(lstrtemp, "转化结束,共%d个文件。", lpCommander->mlstSourceFile.size());
lpCommander->miCount = 0;
}
else
{
strcpy(lstrtemp, "转化停止");
}
lpCommander->mpWnd->GetDlgItem(IDC_BTN_ADD)->EnableWindow(TRUE);
lpCommander->mpWnd->GetDlgItem(IDC_BTN_DEL)->EnableWindow(TRUE);
lpCommander->mpWnd->GetDlgItem(IDC_STATE_TEXT)->SetWindowText(NULL);
MessageBox(NULL, lstrtemp,"ZZL转化Graph",MB_OK|MB_ICONINFORMATION);
TraceLog1("%s\n",lstrtemp);
lpCommander->mbRunThread = FALSE;
}
void CGetZZLCommander::Stop()
{
if (NULL != mhGetZZL)
{
::Stop(mhGetZZL);
}
}
void CGetZZLCommander::SetClose()
{
CAuto lock(&mocritsec);
mbClose = TRUE;
}
void CGetZZLCommander::SetBuffer(CString& astrBuffer)
{
mstrBuffer = astrBuffer;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -