📄 sysconfig.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 "GVCapture.h"
#include "SysConfig.h"
#include "ConfigFile.h"
#include "md5.h"
#include ".\sysconfig.h"
#include "TestNetDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSysConfig dialog
CSysConfig::CSysConfig(CWnd* pParent /*=NULL*/)
: CDialog(CSysConfig::IDD, pParent)
{
//{{AFX_DATA_INIT(CSysConfig)
mstrChannelName = _T("");
mstrPassword = _T("");
mstrSPAddress = _T("");
miUserID = 0;
// mStrMMSAddr = _T("");
miReconnectTime = 0;
//}}AFX_DATA_INIT
}
void CSysConfig::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSysConfig)
DDX_Text(pDX, IDC_CHANNELNAME, mstrChannelName);
DDV_MaxChars(pDX, mstrChannelName, 20);
DDX_Text(pDX, IDC_PASSWORD, mstrPassword);
DDV_MaxChars(pDX, mstrPassword, 40);
DDX_Text(pDX, IDC_SPADDRESS, mstrSPAddress);
DDX_Text(pDX, IDC_USERID, miUserID);
DDX_Text(pDX, IDC_RE_TIME, miReconnectTime);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSysConfig, CDialog)
//{{AFX_MSG_MAP(CSysConfig)
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_TEST_NET, OnBnClickedTestNet)
ON_BN_CLICKED(IDOK, OnBnClickedOk)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSysConfig message handlers
void CSysConfig::OnOK()
{
UpdateData();
if (!JudgeChannelName(mstrChannelName.GetBuffer(0)))
{
return ;
}
//该种加密是不可呢的,不能解密。
MD5 loMd5((unsigned char*)mstrPassword.GetBuffer(0),strlen(mstrPassword.GetBuffer(0)));
mstrPassword = loMd5.hex_digest();
if (!JudgePassword(mstrPassword.GetBuffer(0)))
{
return ;
}
//保存为ini文件
char lszEXEPath[MAX_PATH];
GetCurrentCfgFile(lszEXEPath);
SaveSysConfig(lszEXEPath);
CDialog::OnOK();
}
//得到当前配置文件路径
bool CSysConfig::GetCurrentCfgFile(char *aszFileName)
{
GetModuleFileName(NULL, aszFileName, MAX_PATH);
int liLengthPath = strlen(aszFileName);
while (aszFileName[liLengthPath - 1] != '\\')
{
--liLengthPath;
}
if (liLengthPath > 0)
{
aszFileName[liLengthPath] = '\0';
strcat(aszFileName, "CaptureServer.cfg");
return true;
}
return false;
}
bool CSysConfig::SaveSysConfig(char *aszFileName)
{
//将所得的变量转换
char lszChannelName[DEF_CHANNELNAME_LENGTH + 1] = {0};
char lszPassword[DEF_KEY_LENGTH + 1] = {0};
char lszSPAddress[DEF_ADDRESS_LENGTH + 1] = {0};
//char lszmStrMMSAddr[DEF_ADDRESS_LENGTH + 1] = {0};
strncpy(lszChannelName, mstrChannelName, DEF_CHANNELNAME_LENGTH);
strncpy(lszPassword, mstrPassword, DEF_KEY_LENGTH);
strncpy(lszSPAddress, mstrSPAddress, DEF_ADDRESS_LENGTH);
// strncpy(lszmStrMMSAddr ,mStrMMSAddr, DEF_ADDRESS_LENGTH);
int liWriteLen = 0;
char lszTemp[DEF_TEMP_LENGTH + 1] = {0}; //用来定义临时的拷贝buf
FILE* lfpFile = NULL;
lfpFile = fopen(aszFileName, "w");
if(!lfpFile)
{
MessageBox("写文件出错,可能该文件不可读","错误");
return false;
}
//组成字符串文件头和长度
strcpy(lszTemp, "[CaptureServer]\nspAddress=");
strcat(lszTemp, lszSPAddress);
strcat(lszTemp,"\n");
strcat(lszTemp,"password=");
strcat(lszTemp,lszPassword);
strcat(lszTemp,"\n");
strcat(lszTemp,"channelName=");
strcat(lszTemp,lszChannelName);
strcat(lszTemp,"\n");
//strcat(lszTemp,"MMSAddress=");
//strcat(lszTemp,lszmStrMMSAddr);
strcat(lszTemp,"\n");
liWriteLen = fwrite(lszTemp, sizeof(char), strlen(lszTemp), lfpFile);
if (liWriteLen != (int)strlen(lszTemp))
{
return false;
}
//写用户ID
ZeroMemory(lszTemp, sizeof(DEF_TEMP_LENGTH));
sprintf(lszTemp,"userID=%d\n", miUserID);
liWriteLen = fwrite(lszTemp, sizeof(char), strlen(lszTemp), lfpFile);
if (liWriteLen != (int)strlen(lszTemp))
{
return false;
}
//写重新连接时间
ZeroMemory(lszTemp, sizeof(DEF_TEMP_LENGTH));
sprintf(lszTemp,"ReconnectTime=%d\n", miReconnectTime);
liWriteLen = (int)fwrite(lszTemp, sizeof(char), strlen(lszTemp), lfpFile);
if (liWriteLen != (int)strlen(lszTemp))
{
return false;
}
if (0 != fclose(lfpFile))
{
return false;
}
return true;
}
BOOL CSysConfig::OnInitDialog()
{
CDialog::OnInitDialog();
char lszEXEPath[MAX_PATH] = {0};
GetCurrentCfgFile(lszEXEPath);
ConfigFile cfgFile(lszEXEPath);
if(cfgFile.fileNotFound)
{
MessageBox("无法找到配置文件CaptureServer.cfg.", "错误", MB_OK|MB_ICONERROR);
return FALSE;
}
mstrSPAddress= cfgFile.Value("CaptureServer", "spAddress").c_str();
if(cfgFile.stringNotFound) {
MessageBox("配置文件中必须存在spAddress一项。", "错误", MB_OK|MB_ICONERROR);
return FALSE;
}
mstrChannelName = cfgFile.Value("CaptureServer", "channelName").c_str();
if(cfgFile.stringNotFound) {
MessageBox( "配置文件中必须存在channelName一项。", "错误", MB_OK|MB_ICONERROR);
return FALSE;
}
string reconnect = cfgFile.Value("CaptureServer", "ReconnectTime").c_str();
if(!cfgFile.fileNotFound)
{
miReconnectTime = atoi(reconnect.data());
}
string id = cfgFile.Value("CaptureServer","userID");
if(cfgFile.fileNotFound){
MessageBox( "配置文件中必须存在userID一项。", "错误", MB_OK|MB_ICONERROR);
return FALSE;
}
miUserID = atoi(id.data());
//读入的是32位密码
mstrPassword = cfgFile.Value("CaptureServer", "password").c_str();
if(cfgFile.fileNotFound){
MessageBox("配置文件中必须存在password一项目。", "错误", MB_OK|MB_ICONERROR);
return FALSE;
}
/*
//读入MMSAddress
mStrMMSAddr = cfgFile.Value("CaptureServer", "MMSAddress").c_str();
if(cfgFile.fileNotFound)
{
MessageBox( "配置文件中必须存在MMSAddress一项目。", "错误", MB_OK|MB_ICONERROR);
return FALSE;
}
*/
UpdateData(false);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
bool CSysConfig::JudgeChannelName(string astrChannelName)
{
string::const_iterator it = astrChannelName.begin();
BYTE tmpChar;
for(; it != astrChannelName.end(); it++)
{
tmpChar = static_cast<BYTE>(*it);
if(isdigit(tmpChar) || isalpha(tmpChar))
continue;
if(tmpChar == '[' || tmpChar == ']')
continue;
//GB18030-2000编码标准的第一个字节的编码范围在0x81 ~ 0xFE之间;第二个字节的编码范围在0x40 ~ 0x7E和0x80 ~ 0xFE之间。
//Big5编码标准的第一个字节的编码范围在0xA1 ~ 0xF9 之间;第二个字节的编码范围在0x40 ~ 0x7E 和0xA1 ~ 0xFE之间。
//两者的并集是第一个字节[0x81, 0xfe],第二个字节[0x40, 0x7e]、[0x80, 0xfe];
if(tmpChar>=0x81 && tmpChar <= 0xfe && it+1 != astrChannelName.end()) {
tmpChar = static_cast<BYTE>(*(it+1));
if( (tmpChar>=0x40 && tmpChar <= 0x7e) || (tmpChar>=0x80 && tmpChar <= 0xfe))
{
it++; // 跳过一个汉字字符
continue;
}
}
MessageBox("配置文件中channelName一项只能由字母、数字、汉字和方括弧组成。", "错误", MB_OK|MB_ICONERROR);
return false;
}
return true;
}
//判断ip正确性
bool CSysConfig::JudgeIP(string astrIP)
{
string::const_iterator it = astrIP.begin();
BYTE tmpChar;
for(; it != astrIP.end(); it++)
{
tmpChar = static_cast<BYTE>(*it);
if(isdigit(tmpChar) ||':' == tmpChar || '.' == tmpChar)
continue;
MessageBox("配置文件中服务器地址一项只能由数字和分隔符“:”组成。", "错误", MB_OK|MB_ICONERROR);
return false;
}
return true;
}
bool CSysConfig::JudgePassword(string astrPassword)
{
if(astrPassword.length() != 32)
{
MessageBox("加密过程出错,配置文件中Password一项md5加密后其长度必须等于32字符。", "错误", MB_OK|MB_ICONERROR);
return false;
}
return true;
}
void CSysConfig::OnBnClickedTestNet()
{
UpdateData();
if (false == JudgeIP((LPCTSTR)mstrSPAddress))
{
return;
}
CTestNetDlg loTestNet;
loTestNet.GetAddr(mstrSPAddress);
loTestNet.DoModal();
}
void CSysConfig::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -