📄 fileintf.cpp
字号:
//////////////////////////////////////////////////////////////////
// History:
// 2001.10.24 Start it. (shencan@263.net)
// History:
// 2001.11.07
// add the directory of Ftp getting. (zoohoo@163.com)
// add the code for ignoring error of ftp.(try{}catch(...)...)
// History:
// 2002.05.20
// Unit the file to the Interface97. (zoohoo@163.com)
//////////////////////////////////////////////////////////////////
#if (_MSC_VER >= 1200)
#pragma warning( disable : 4800 ) // one performance warning off
#pragma warning( disable : 4786 ) // warning about too long debug symbol off
#endif
#include "gk.h"
#include "fstream.h"
#include "fileIntf.h"
#ifdef ACE_WIN32
#include <process.h>
#else
#include <unistd.h>
#endif /* ACE_WIN32 */
int SaveDB(UserInterface * userInterface);
int GetValueFromShareDir();
int GetValueFromFTP()
{
RTRACE(2, "Begin ftp loop");
if ( CGlobal::Instance()->m_isShutdown == 1 )
return -1;
PFTPClient ftp;
CFileIntf fileIntf;
fstream fdSuccess, fdFailure;
int j = 0;
RTRACE(2, "FtpSrv NUMBER: " << CGlobal::Instance()->m_ftpNum);
for(int i=0;i<CGlobal::Instance()->m_ftpNum;i++)
{
try
{
if ( ftp.Connect(PIPSocket::Address(CGlobal::Instance()->m_negGkList[i].m_ip),
CGlobal::Instance()->m_negGkList[i].m_port) == false )
{
RTRACE(2, "Connect fail");
continue;
//return 0;
}
if ( ftp.LogIn(CGlobal::Instance()->m_username[i], CGlobal::Instance()->m_password[i]) == false )
{
RTRACE(2, "Login fail");
continue;
//return 0;
}
else
RTRACE(4,
CGlobal::Instance()->m_username[i] << ":" <<
CGlobal::Instance()->m_password[i] <<
" login successfully");
if( !ftp.ChangeDirectory(CGlobal::Instance()->m_getFilePath))
{
RTRACE(2, "Can't change to the directory "
<< CGlobal::Instance()->m_getFilePath );
continue;
}
else
RTRACE(4, "Change to the directory "
<< CGlobal::Instance()->m_getFilePath );
PStringArray fileArray = ftp.GetDirectoryNames(PFTP::ShortNames); //, PFTP::NormalPort);
RTRACE(5, "Files: " << fileArray);
// treat those files.
for ( j=0; j<fileArray.GetSize(); j++) {
// begin to get every file, and save it to local disk
// CGlobal::Instance()->m_serviceType = 700;
// CGlobal::Instance()->GetServiceTypeFromFileName(fileArray[j]);
// RTRACE(5, fileArray[j] << " FileType:" << CGlobal::Instance()->m_serviceType);
PTCPSocket * pSock;
pSock = ftp.GetFile(fileArray[j]); //, PFTP::NormalPort);
if ( pSock == NULL )
{
RTRACE(2, "Read file: " << fileArray[j] << " fail!");
continue;
}
PString str;
int count = 0;
while(pSock->Read(str.GetPointer(count+1000)+count, 1000))
count += pSock->GetLastReadCount();
str.SetSize(count+1);
delete pSock;
RTRACE(5, "Response:" << ftp.ReadResponse());
PFile save;
PString pfileName;
pfileName = CGlobal::Instance()->m_localDir;
pfileName += "\\";
pfileName += fileArray[j];
RTRACE(5, pfileName);
save.Open(pfileName, PFile::WriteOnly);
save.WriteString(str);
save.Close();
RTRACE(4, "Read size:" << count+1);
RTRACE(6, ", context:\n" << str);
// open temporary file for success & failure
PString fSuccess = fileArray[j];
PString fFailure = fileArray[j];
fileIntf.SetBackFileName(&fSuccess, "_s");
fileIntf.SetBackFileName(&fFailure, "_f");
// fSuccess = CGlobal::Instance()->m_localDir + fSuccess;
// fFailure = CGlobal::Instance()->m_localDir + fFailure;
RTRACE(4, "write the file " << fSuccess);
RTRACE(4, "write the file " << fFailure);
// open back file
fdSuccess.open(CGlobal::Instance()->m_localDir
+ fSuccess, ios::out);
if(fdSuccess.bad() || !fdSuccess.good())
{
RTRACE(2, "Can't open file " << fSuccess);
continue;
}
// open back file
fdFailure.open(CGlobal::Instance()->m_localDir
+ fFailure, ios::out);
if(fdFailure.bad() || !fdFailure.good())
{
RTRACE(2, "Can't open file " << fSuccess);
fdSuccess.close();
continue;
}
/*
if(fdSuccess.is_open && )
{
RTRACE(2, "Create file " << fSuccess << " failed");
continue;
}
if(fdFailure == NULL)
{
RTRACE(2, "Create file " << fSuccess << " failed");
fdSuccess.close();
continue;
}
*/
// get the line from cdr file
PStringArray line = str.Lines();
PString sSuccess="", sFailure="";
for(int k=0;k<line.GetSize();k++)
{
// fetch the value to classs UserInterface
if(line[k].Trim() == "")
continue;
fileIntf.GetValueFromLine(line[k]);
// operate database
int iRes = SaveDB(&fileIntf.userInterface);
char operateTypeTmp[3];
sprintf(operateTypeTmp, "%d", fileIntf.userInterface.operateType);
if ( iRes == 0 )
{
// write the right feeback to temp file
sSuccess = sSuccess +
(PString)fileIntf.userInterface.serviceId +
(PString)"\t" +
(PString)fileIntf.userInterface.userGroup +
(PString)"\t" +
(PString)fileIntf.userInterface.areaNo +
(PString)"\t" +
(PString)fileIntf.userInterface.teleNumber +
(PString)"\t" +
(PString)fileIntf.userInterface.oldTeleNum +
(PString)"\t" +
(PString)operateTypeTmp +
(PString)"\n";
RTRACE(5, "Get cdr successfully");
RTRACE(5, line[k]);
}
else
{
// write the wrong feeback to temp file
char temp[4];
sprintf(temp, "%d", iRes);
sFailure = sFailure +
(PString)fileIntf.userInterface.serviceId +
(PString)"\t" +
(PString)fileIntf.userInterface.userGroup +
(PString)"\t" +
(PString)fileIntf.userInterface.areaNo +
(PString)"\t" +
(PString)fileIntf.userInterface.teleNumber +
(PString)"\t" +
(PString)fileIntf.userInterface.oldTeleNum +
(PString)"\t" +
(PString)operateTypeTmp +
(PString)"\t" +
(PString)temp + "\n";
RTRACE(5, "Get cdr failed");
RTRACE(5, line[k]);
}
}
// write file
RTRACE(6, "Write to success file " << sSuccess);
fdSuccess << sSuccess;
RTRACE(6, "Write to failure file " << sFailure);
fdFailure << sFailure;
fdSuccess.close();
fdFailure.close();
// delete this file.
if ( ftp.ExecuteCommand(PFTP::DELE, fileArray[j])/100 == 2 )
{
RTRACE(5, "Ftp Del success: " << fileArray[j]);
}
else
{
RTRACE(2, "Ftp Del fail: " << fileArray[j]);
}
// put the backfeed file
if( !ftp.ChangeDirectory(CGlobal::Instance()->m_putFilePath))
{
RTRACE(2, "Can't change to the directory "
<< CGlobal::Instance()->m_getFilePath );
continue;
}
else
RTRACE(4, "Change to the directory "
<< CGlobal::Instance()->m_getFilePath);
pSock = ftp.PutFile(fSuccess);
if(pSock == NULL)
RTRACE(2, "Put back success file failed");
else
{
count = 0;
pSock->Write((const char *)sSuccess, sSuccess.GetLength());
// while(pSock->Write(sSuccess.GetPointer(count+1000)+count, 1000))
// count += pSock->GetLastWriteCount();
// sSuccess.SetSize(count+1);
delete pSock;
RTRACE(5, "Response:" << ftp.ReadResponse());
RTRACE(5, "Put back file successfully");
/*
#ifdef ACE_WIN32
_unlink(fSuccess);
#else
unlink(fSuccess);
#endif
RTRACE(2, "Delete local file " << fSuccess);
*/
}
pSock = ftp.PutFile(fFailure);
if(pSock == NULL)
RTRACE(2, "Put back failure file failed");
else
{
count = 0;
pSock->Write((const char *)sFailure, sFailure.GetLength());
// while(pSock->Write(sFailure.GetPointer(count+1000)+count, 1000))
// count += pSock->GetLastWriteCount();
// sFailure.SetSize(count+1);
delete pSock;
RTRACE(5, "Response:" << ftp.ReadResponse());
RTRACE(5, "Put back failure file successfully");
/*
#ifdef ACE_WIN32
_unlink(fFailure);
#else
unlink(fFailure);
#endif
RTRACE(2, "Delete file " << fFailure);
*/
}
if( !ftp.ChangeDirectory(CGlobal::Instance()->m_getFilePath))
{
RTRACE(2, "Can't change to the directory "
<< CGlobal::Instance()->m_getFilePath );
continue;
}
else
RTRACE(4, "Change to the directory "
<< CGlobal::Instance()->m_getFilePath );
}
ftp.Close();
}
catch(...)
{
RTRACE(2, "Connect to Ftp Server " << PIPSocket::Address(CGlobal::Instance()->m_negGkList[i].m_ip) << " error");
RTRACE(2, "Unknown Error!");
}
}
return 0;
}
// Realize interface97 through the option of shared direcotry
int GetValueFromShareDir()
{
CFileIntf fileIntf;
RTRACE(2, "Begin loop from getting files from share dir");
if ( CGlobal::Instance()->m_isShutdown == 1 )
return -1;
RTRACE(2, "Share dir : " << CGlobal::Instance()->m_shareDir);
PDirectory * direc = new PDirectory();
if(!direc->Exists(CGlobal::Instance()->m_shareDir))
{
RTRACE(1, "Warning! The share diretory " << CGlobal::Instance()->m_shareDir
<< " does not exist! ");
delete direc;
return 0;
}
/*
if(CGlobal::Instance()->m_changeDir == 0)
{
RTRACE(3, "Change to " << CGlobal::Instance()->m_shareDir);
direc->Change(CGlobal::Instance()->m_shareDir);
CGlobal::Instance()->m_changeDir = 1;
}
else
{
*/
RTRACE(3, "Open files in shared directory");
if(direc->Open())
{
if(!direc->IsSubDir())
{
RTRACE(5, direc->GetEntryName());
fileIntf.SaveToFile(direc->GetEntryName());
}
}
while(direc->Next())
{
if(!direc->IsSubDir())
{
RTRACE(5, direc->GetEntryName());
fileIntf.SaveToFile(direc->GetEntryName());
}
}
direc->Close();
// }
delete direc;
return 0;
}
///////////////////////////////////////////////////////
//Realize the class of CFileIntf
///////////////////////////////////////////////////////
int CFileIntf::SaveToFile(PString fileName)
{
/* PString newFile, oldFile;
PStringArray str;
int len;
char tmp[BUFSIZE];
newFile = CGlobal::Instance()->m_localDir;
len = newFile.GetLength();
if(newFile[len-1] != '\\')
newFile += "\\";
newFile += fileName;
oldFile = CGlobal::Instance()->m_shareDir;
len = oldFile.GetLength();
if(oldFile[len-1] != '\\')
oldFile += "\\";
oldFile += fileName;
RTRACE(3, "Read from " << oldFile);
RTRACE(3, "Write to " << newFile);
::fstream fold(oldFile, ios::in | ios::nocreate );
::fstream fnew(newFile, ios::out | ios::app );
RTRACE(6, "File open");
if(fnew.bad() || !fnew.good())
{
RTRACE(1, "open " << newFile << " error\n");
return -1;
}
if(fold.bad() || !fold.good())
{
RTRACE(1, "open " << oldFile << " error\n");
return -1;
}
int i=0;
while(!fold.eof())
{
fold.getline(tmp, BUFSIZE);
str[i] = PString(tmp);
fnew << str[i] << "\n";
i ++;
}
fold.close();
fnew.close();
RTRACE(6, "context:\n" << str);
if ( SaveDB(str) == 0 )
{
RTRACE(5, "Get cdr success");
// delete this file.
if(PFile::Remove(oldFile))
{
RTRACE(2, "Del success: " << oldFile);
}
else
{
RTRACE(2, "Del fail: " << oldFile);
}
}
*/
return 0;
}
int CFileIntf::GetValueFromLine(PString line)
{
RTRACE(6, "Enter GetValueFromLine");
RTRACE(4, line);
char buffer[21];
int num = 0;
int i, j=0;
for(i=0;i<(int)strlen(line);i++)
{
if(line[i] == '\t')
{
num ++;
memcpy(buffer,
(const unsigned char *)line.Mid(j),
(i-j)<sizeof(buffer)?(i-j):sizeof(buffer));
(i-j)<sizeof(buffer)?buffer[i-j]=0:buffer[sizeof(buffer)] = 0;
//buffer[i-j] = 0;
j = i+1;
SetInterfaceValue(num, buffer);
}
}
for(i=num+1;i<=COLUMN_NUMBER;i++) //the omitted columns would be emptied
SetInterfaceValue(i, "");
return 0;
}
void CFileIntf::SetInterfaceValue(int num, char * buffer)
{
switch(num)
{
case 1:
strcpy(userInterface.serviceId, buffer);
break;
case 2:
strcpy(userInterface.userGroup, buffer);
break;
case 3:
strcpy(userInterface.areaNo, buffer);
break;
case 4:
strcpy(userInterface.teleNumber, buffer);
break;
case 5:
strcpy(userInterface.oldTeleNum, buffer);
break;
case 6:
userInterface.operateType = atoi(buffer);
break;
case 7:
strcpy(userInterface.genTime, buffer);
break;
case 8:
strcpy(userInterface.endTime, buffer);
break;
case 9:
strcpy(userInterface.userType, buffer);
break;
case 10:
strcpy(userInterface.password, buffer);
break;
case 11:
userInterface.flag = atoi(buffer);
break;
case 12:
userInterface.remainTime = atoi(buffer);
break;
case 13:
strcpy(userInterface.aCLICP, buffer);
break;
case 14:
strcpy(userInterface.aCLContent, buffer);
break;
case 15:
userInterface.sendRecv = atoi(buffer);
break;
case 16:
strcpy(userInterface.addString, buffer);
break;
}
}
// get the back file name
// if the file name is like 'xxxxx.xxx'
// the the back file name is 'xxxxxchr.xxx'
void CFileIntf::SetBackFileName(PString * filename, PString chr)
{
int site = filename->FindLast('.');
filename->Splice(chr, site, 0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -