📄 fileintf_sh.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_SH.h"
#ifdef ACE_WIN32
#include <process.h>
#else
#include <unistd.h>
#endif /* ACE_WIN32 */
int SaveDB(UserInterface * userInterface);
int GetValueFromShareDir();
int GetValueFromFTP_SH()
{
RTRACE(2, "Begin ftp loop");
if ( CGlobal::Instance()->m_isShutdown == 1 )
return -1;
PFTPClient ftp;
CFileIntf_SH fileIntf;
fstream fdSuccess, fdFailure;
PString filePrefix;
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
filePrefix = fileArray[j].Mid(0,2);
if (CGlobal::Instance()->m_areacode == 3) //ShangHai 97
{
if (strcmp(filePrefix, "SM") != 0 && strcmp(filePrefix, "TD") != 0)
continue;
} //过滤非工单和退单的文件
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;
}
// get the line from sheet file
PStringArray line = str.Lines();
PString sSuccess="", sFailure="";
int iSuccessNum, iFailNum;
if (strcmp(filePrefix, "SM") == 0) //正常工单
{
fileIntf.userInterface.isBackSheet = 0;
iSuccessNum = 0;
iFailNum = 0;
for(int k=0;k<line.GetSize();k++)
{
if ( k == 0 )
{
char buf1[61], buf2[61];
char * pDest;
::sscanf(line[k], "%s%s", buf1, buf2);
pDest = strstr(buf2, "-");
*pDest = 0;
strcat(buf2, pDest+1);
strncpy(fileIntf.sheet.sendDate, buf2, sizeof(fileIntf.sheet.sendDate)-1);
continue;
}
// fetch the value to classs UserInterface
if(line[k].Trim() == "")
continue;
if (k == line.GetSize() -1 )//过滤最后一行的汉字
{
continue;
}
if (line[k].GetSize() < 62) //该行数据不合法
{
sFailure = sFailure + line[k];
sFailure = sFailure + "\tretCode= line length < 61\n";
RTRACE(2, "Get sheet item failed! line length too short. Order = " << ++iFailNum );
RTRACE(5, line[k]);
continue;
}
fileIntf.GetValueFromLine(line[k]);
if (fileIntf.SheetToUserInterface() != 0) //该行数据不合法
{
sFailure = sFailure + line[k];
sFailure = sFailure + "\tretCode= line inavlid\n";
RTRACE(2, "Get sheet item failed! line invalid. Order = " << ++iFailNum );
RTRACE(5, line[k]);
continue;
}
// operate database
int iRes = SaveDB(&fileIntf.userInterface);
if ( iRes == 0 )
{
// write the right feeback to temp file
sSuccess = sSuccess + line[k];
sSuccess = sSuccess + "\n";
RTRACE(2, "Get Sheet item successfully! Order = " << ++iSuccessNum);
RTRACE(5, line[k]);
}
else
{
// write the wrong feeback to temp file
char temp[4];
sprintf(temp, "%d", iRes);
sFailure = sFailure + line[k];
sFailure = sFailure + "\tretCode=" + (PString)temp + "\n";
RTRACE(2, "Get sheet item failed!Order = " << ++iFailNum << "\tretCode = " << iRes);
RTRACE(5, line[k]);
}
} //end of read file for loop
}
else //退单
{
fileIntf.userInterface.isBackSheet = 1;
iSuccessNum = 0;
iFailNum = 0;
for(int k=0;k<line.GetSize();k++)
{
if ( k == 0 )
{
char buf1[61], buf2[61];
char * pDest;
::sscanf(line[k], "%s%s", buf1, buf2);
pDest = strstr(buf2, "-");
*pDest = 0;
strcat(buf2, pDest+1);
strncpy(fileIntf.sheet_TD.sendDate, buf2, sizeof(fileIntf.sheet_TD.sendDate)-1);
continue;
}
// fetch the value to classs UserInterface
if(line[k].Trim() == "")
continue;
if (k == line.GetSize() -1 )//过滤最后一行的汉字
{
continue;
}
if (line[k].GetSize() < 52) //该行数据不合法
{
sFailure = sFailure + line[k];
sFailure = sFailure + "\tretCode= line length < 51\n";
RTRACE(2, "Get sheet_TD item failed! line length too short! Order = " << ++iFailNum );
RTRACE(5, line[k]);
continue;
}
fileIntf.GetValueFromLine_TD(line[k]);
if (fileIntf.SheetToUserInterface_TD() != 0) //该行数据不合法
{
sFailure = sFailure + line[k];
sFailure = sFailure + "\tretCode= line ivalid\n";
RTRACE(2, "Get sheet_TD item failed! line invalid! Order = " << ++iFailNum );
RTRACE(5, line[k]);
continue;
}
// operate database
int iRes = SaveDB(&fileIntf.userInterface);
if ( iRes == 0 )
{
// write the right process sheet to temp file
sSuccess = sSuccess + line[k];
sSuccess = sSuccess + "\n";
RTRACE(2, "Get Sheet_TD item successfully! Order = " << ++iSuccessNum);
RTRACE(5, line[k]);
}
else
{
// write the wrong process to temp file
char temp[4];
sprintf(temp, "%d", iRes);
sFailure = sFailure + line[k];
sFailure = sFailure + "\tretCode=" + (PString)temp + "\n";
RTRACE(2, "Get sheet_TD item failed! Order = " << ++iFailNum << "\tretCode = " << iRes);
RTRACE(5, line[k]);
}
} //end of read file for loop
}
// write file
char temp[6];
RTRACE(6, "Write to success file " << sSuccess);
sprintf(temp, "%d", iSuccessNum);
sSuccess = sSuccess + "\n\nTotal process suceeded record number: " + (PString)temp;
fdSuccess << sSuccess;
RTRACE(6, "Write to failure file " << sFailure);
sprintf(temp, "%d", iFailNum);
sFailure = sFailure + "\n\nTotal process failed record number: " + (PString)temp;
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_SH()
{
CFileIntf_SH 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_SH::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);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -