?? audcreindex.cpp
字號:
/*
* 文件名: checkDup.cpp
*
* 版權所有 (C) 2002 神州數碼(中國)有限公司
*
* 描述:稽核查重主程序
*
* 作者:戴大剛
*
* 創建日期:2006-03-14
*
* 版本:ver1.0
*
* 參考文檔:
*
* 修訂記錄:
* 修訂日期 修訂人 修訂內容
*
*
*/
// 該模版適合于所有預處理,標準批價,查重,入庫,下發處理等模塊
// 該類模塊特點是輸出,輸入文件一一對應
// 該類模版并不適合上發,分揀
// 在需要程序數據一致性的地方如查重需要編寫redo,beginTran,commitTran函數
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <malloc.h>
#include <dirent.h>
#include <sys/stat.h>
#include <stdarg.h>
/* 引用公共函數申明的頭文件 */
#include "public.h"
/* 引用查重程序相關類型定義 */
#include "audCreIndex.h"
/* 儲存已經初始化的索引文件名 */
/* 在處理一個話單文件結束后需要重新刷新這些文件 */
/* 索引文件最大128天,應該已經足夠 */
typeIndexName pIndexName[1280];
int totalIndexFiles;
/* 定義程序運行必選參數 */
char inputPath[128+1]; // 輸入路徑
char filePattern[128+1]; // 輸入文件匹配條件
char outputPath[128+1]; // 輸出路徑
char tmpPath[128+1]; // 臨時路徑
char logPath[128+1]; // 日志路徑
char indexPath[128+1]; // 日志路徑
char bakPath[128+1]; // 備份路徑
char errorPath[128+1]; // 錯誤路徑
char staticPath[128+1]; // 靜態數據路徑
char idxNamePrefix[12+1]; // 索引文件名前綴
char procID[2+1]; // 進程序號
int runFlag; // 程序運行方式
int sleepTime; // 沒有文件處理時程序睡眠時間
char orgfileName[128+1];
/* runFlag 定義: 為 1 ,備份文件,守護進程運行 */
/* runFlag 定義: 為 2 ,不備份文件,非守護進程運行 */
/* runFlag 定義: 為 3 ,不備份文件,守護進程運行 */
/* runFlag 定義: 其他 ,備份文件,非守護進程運行 */
/* 下面定義應用程序需要的特殊運行參數 */
/* 針對每一個應用程序都會不同 */
char errMsg[128+1]; // 全局的出錯信息,用于描述拋出的異常
typeLogBuf pLogBuf; // 日志輸出相關參數
typedef struct{
char origenName[24+1]; //原始清單文件名
int totalNum; //總話單數
int curNum; //成功話單數
}typeChkDupLogBuf;
/* 記錄對應于原始清單日志 */
typeChkDupLogBuf pLogCkDpBuf[1024];
int totalLogBuf;
/* 根據應用程序需要從配置文件里讀取相關參數 */
int readCfgFile(char * programName,typeCfgParam * pCfgParam)
{
char buf[128+1];
char tmpChar[56+1];
programName[strlen(programName)-4]='\0'; //去除.cfg后綴
if(strstr(programName,"audCreMobIdx")==NULL && strstr(programName,"audCreNetIdx")==NULL)
{
printf("Cfg file is error! please check it!\n");
return -1;
}
memset(tmpChar,0,sizeof(tmpChar));
if(strncmp(programName,"../cfg/",7)==0)
{
memcpy(tmpChar,&programName[7],strlen(programName)-7);
}
else
{
memcpy(tmpChar,programName,strlen(programName));
}
memset(buf,0,sizeof(buf));
sprintf(buf,"%s.%s",tmpChar,"inputPath");
memset(inputPath,0,sizeof(inputPath));
if( readCfgParam(inputPath,buf,pCfgParam)<0) return -1;
memset(buf,0,sizeof(buf));
sprintf(buf,"%s.%s",tmpChar,"filePattern");
memset(filePattern,0,sizeof(filePattern));
if( readCfgParam(filePattern,buf,pCfgParam)<0) return -1;
memset(buf,0,sizeof(buf));
sprintf(buf,"%s.%s",tmpChar,"outputPath");
memset(outputPath,0,sizeof(outputPath));
if( readCfgParam(outputPath,buf,pCfgParam)<0) return -1;
memset(buf,0,sizeof(buf));
sprintf(buf,"%s.%s",tmpChar,"tmpPath");
memset(tmpPath,0,sizeof(tmpPath));
if( readCfgParam(tmpPath,buf,pCfgParam)<0) return -1;
memset(buf,0,sizeof(buf));
sprintf(buf,"%s.%s",tmpChar,"logPath");
memset(logPath,0,sizeof(logPath));
if( readCfgParam(logPath,buf,pCfgParam)<0) return -1;
memset(buf,0,sizeof(buf));
sprintf(buf,"%s.%s",tmpChar,"bakPath");
memset(bakPath,0,sizeof(bakPath));
if( readCfgParam(bakPath,buf,pCfgParam)<0) return -1;
memset(buf,0,sizeof(buf));
sprintf(buf,"%s.%s",tmpChar,"errorPath");
memset(errorPath,0,sizeof(errorPath));
if( readCfgParam(errorPath,buf,pCfgParam)<0) return -1;
memset(buf,0,sizeof(buf));
sprintf(buf,"%s.%s",tmpChar,"indexPath");
memset(indexPath,0,sizeof(indexPath));
if( readCfgParam(indexPath,buf,pCfgParam)<0) return -1;
memset(buf,0,sizeof(buf));
sprintf(buf,"%s.%s",tmpChar,"idxNamePrefix");
memset(idxNamePrefix,0,sizeof(idxNamePrefix));
if( readCfgParam(idxNamePrefix,buf,pCfgParam)<0) return -1;
rtrim(idxNamePrefix);
idxNamePrefix[3]='\0';
memset(buf,0,sizeof(buf));
sprintf(buf,"%s.%s",tmpChar,"procID");
memset(procID,0,sizeof(procID));
if( readCfgParam(procID,buf,pCfgParam)<0) return -1;
memset(buf,0,sizeof(buf));
sprintf(buf,"%s.%s",tmpChar,"runFlag");
runFlag = 0;
if( readCfgParam(&runFlag,buf,pCfgParam)<0) return -1;
memset(buf,0,sizeof(buf));
sprintf(buf,"%s.%s",tmpChar,"sleepTime");
sleepTime = 30;
if( readCfgParam(&sleepTime,buf,pCfgParam)<0) return -1;
/* 如果該應用程序需要其他參數配合,在下面添加 */
return 0;
}
/* 程序不正常關閉,在重啟后需要作會滾操作*/
void redoHandler(char * programName)
{
char fullPathName[128+1];
FILE * fp;
char buf[128+1];
char tmpFileName[128+1];
char objPath[128+1];
memset(fullPathName,0,sizeof(fullPathName));
sprintf(fullPathName,"%s/%s.break",tmpPath,programName);
/* 沒有斷點文件,返回 */
if( access(fullPathName,F_OK) != 0 ) return;
if( (fp=fopen(fullPathName,"r")) == NULL ){
memset(buf,0,sizeof(buf));
sprintf(buf,"Can't open break file %s for read!",fullPathName);
throw (char *)buf;
}
while(memset(buf,0,sizeof(buf)),fgets(buf,256,fp)!=NULL){
memset(tmpFileName,0,sizeof(tmpFileName));
memset(objPath,0,sizeof(objPath));
getTextField(tmpFileName,buf,',',1);
getTextField(objPath,buf,',',2);
if( access(tmpFileName,F_OK) == 0 ) mvfile(tmpFileName,objPath);
}
fclose(fp);
remove(fullPathName);
}
/* 在程序需要維護數據一致性的地方開始事務 */
void beginTran(char * programName,char * fileName)
{
FILE * fp;
char fullPathName[128+1];
char newPathName[128+1];
char buf[128+1];
int k;
memset(fullPathName,0,sizeof(fullPathName));
sprintf(fullPathName,"%s/.%s.break",tmpPath,programName);
if( (fp=fopen(fullPathName,"w")) == NULL ){
memset(buf,0,sizeof(buf));
sprintf(buf,"Can't open break file %s for write!",fullPathName);
throw (char *)buf;
}
/* 記錄輸入文件 */
memset(buf,0,sizeof(buf));
sprintf(buf,"%s/%s.lst,%s\n",inputPath,fileName,bakPath);
fputs(buf,fp);
/* 記錄輸出文件 */
memset(buf,0,sizeof(buf));
sprintf(buf,"%s/%s.chk,%s\n",tmpPath,fileName,outputPath);
fputs(buf,fp);
/* 記錄錯誤文件 */
memset(buf,0,sizeof(buf));
sprintf(buf,"%s/%s.dup,%s\n",tmpPath,fileName,errorPath);
fputs(buf,fp);
/* 記錄索引文件 */
for(k=0;k<totalIndexFiles;k++){
memset(buf,0,sizeof(buf));
sprintf(buf,"%s/%s%s.idx,%s\n",tmpPath,idxNamePrefix,pIndexName[k].callDate,indexPath);
fputs(buf,fp);
}
fclose(fp);
memset(newPathName,0,sizeof(newPathName));
sprintf(newPathName,"%s/%s.break",tmpPath,programName);
rename(fullPathName,newPathName);
}
/* 在程序需要維護數據一致性的地方提交事務 */
void commitTran(char * programName)
{
char fullPathName[128+1];
memset(fullPathName,0,sizeof(fullPathName));
sprintf(fullPathName,"%s/%s.break",tmpPath,programName);
remove(fullPathName);
}
/* 獲取程序調度標志 */
int getProgramStatu(char * programName,char * tmpPath)
{
char fullPathName[128+1];
memset(fullPathName,0,sizeof(fullPathName));
sprintf(fullPathName,"%s/%s.stop",tmpPath,programName);
/* 有停止進程標志文件存在,程序需要退出 */
if( access(fullPathName,F_OK) == 0 ){
remove(fullPathName);
return 0;
}
return 1;
}
/* 返回值小于0為錯誤,錯誤代碼范圍為301-310 */
/* E301 計費號碼msisdn非13開頭,不足11位 */
/* E302 重單 */
/* E303 時間不合法 */
/* 追加到日志記錄 */
void addToLogBuf(char * origenName,int isDup)
{
/* 對無主清單特殊處理,文件名太多,忽略 */
if( totalLogBuf > 1020 ) return ;
if( strncmp(pLogCkDpBuf[totalLogBuf].origenName,origenName,24) == 0){
pLogCkDpBuf[totalLogBuf].totalNum ++;
pLogCkDpBuf[totalLogBuf].curNum += ( 1 - isDup );
}
else {
if( strlen(pLogCkDpBuf[totalLogBuf].origenName) != 0 ) totalLogBuf++;
memcpy(pLogCkDpBuf[totalLogBuf].origenName,origenName,24);
pLogCkDpBuf[totalLogBuf].origenName[24]='\0';
pLogCkDpBuf[totalLogBuf].totalNum = 1;
pLogCkDpBuf[totalLogBuf].curNum = ( 1 - isDup );
}
//printf("END [%4d][%s]\n",totalLogBuf,pLogBuf[totalLogBuf].origenName);getchar();
}
/* 向索引數據中追加索引 */
/* 1 追加成功,非重單 */
/* -1 追加失敗,是重單 */
int addToIndexList(typeMoreKeyList * pMoreKeyList,int pos,char * sKey1,char * sKey2)
{
typeOneKeyList * * p;
int key1,key2;
int k;
int i;
key1 = atoi(sKey1);
key2 = atoi(sKey2);
/* 首先依次循環定長數組 */
//printf("pos =%d,key1=%d,key2=%d",pos,key1,key2);getchar();
for(i=0;i<4*ONEDAYONEUSERCDR;i++){
if( (pMoreKeyList[pos].pKeyArray)[i].key1 == 0 &&
(pMoreKeyList[pos].pKeyArray)[i].key2 == 0 ){
(pMoreKeyList[pos].pKeyArray)[i].key1 = key1;
(pMoreKeyList[pos].pKeyArray)[i].key2 = key2;
return 1; /* 非重單 */
}
if( (pMoreKeyList[pos].pKeyArray)[i].key1 == key1 &&
(pMoreKeyList[pos].pKeyArray)[i].key2 == key2 ){
return -1;
}
}
/* 其次依次循環動態鏈表 */
for(p=&(pMoreKeyList[pos].pKeyList);(*p)!=NULL;p=&((*p)->p)){
if( (*p)->key1 == key1 && (*p)->key2 == key2 ){
return -1;
}
}
(*p) = new typeOneKeyList ;
if( (*p) == NULL){
throw (char *)"new typeOneKeyList fail!" ;
}
(*p)->key1 = key1;
(*p)->key2 = key2;
(*p)->p = NULL;
return 1; /* 非重單 */
}
/* 根據通話日期初始化索引數據 */
void readIndexFile(char * callDate,char * indexPath,typeMoreKeyList * * pMoreKeyList,char * idxNamePrefix)
{
char fullPathName[128+1];
FILE * fpIdx;
char buf[256+1];
char tmpBuf[16+1];
char msisdn[11+1];
char callTime[6+1];
char sKey1[9+1];
char sKey2[9+1];
int k;
int i;
int pos;
/* 為一天的索引申請內存空間 */
(*pMoreKeyList) = new typeMoreKeyList[10000];
if( pMoreKeyList == NULL ){
memset(buf,0,sizeof(buf));
sprintf(buf,"Can't new index pMoreKeyList!\n");
throw (char *)buf;
}
//printf("ONEDAYONEUSERCDR=%d",ONEDAYONEUSERCDR);getchar();
/* 索引關鍵字全部初始化為0 */
for(k=0;k<10000;k++){
for(i=0;i<4*ONEDAYONEUSERCDR;i++) {
((*pMoreKeyList)[k].pKeyArray)[i].key1 = 0;
((*pMoreKeyList)[k].pKeyArray)[i].key2 = 0;
}
(*pMoreKeyList)[k].pKeyList = NULL;
}
/* 打開索引文件用于讀寫索引數據 */
memset(fullPathName,0,sizeof(fullPathName));
if ((strncmp(orgfileName,"p",1) == 0) || (strncmp(orgfileName,"m",1) == 0))
sprintf(fullPathName,"%s/%s%10.10s.idx01",indexPath,idxNamePrefix,callDate);
if ((strncmp(orgfileName,"q",1) == 0) || (strncmp(orgfileName,"w",1) == 0))
sprintf(fullPathName,"%s/%s%10.10s.idx02",indexPath,idxNamePrefix,callDate);
if(access(fullPathName,F_OK) != 0) return;
if( (fpIdx=fopen(fullPathName,"r")) == NULL){
memset(buf,0,sizeof(buf));
sprintf(buf,"Can't init index file %s!\n",fullPathName);
throw (char *)buf;
}
//printf("fullPathName=%s\n",fullPathName);getchar();
while(memset(buf,0,sizeof(buf)),fgets(buf,256,fpIdx)!=NULL){
subStrCpy(msisdn,buf,0,9);
subStrCpy(callTime,buf,10,6);
subStrCpy(sKey2,buf,17,9);
subStrCpy(tmpBuf,msisdn,5,4);
pos = atoi(tmpBuf);
memset(sKey1,0,sizeof(sKey1));
sprintf(sKey1,"%5.5s%4.4s",msisdn,callTime+2);
//printf("%s\n",buf);getchar();
//printf("Index: %9d,%s,%s\n",pos,sKey1,sKey2);getchar();
addToIndexList(*pMoreKeyList,pos,sKey1,sKey2);
//getchar();
}
fclose(fpIdx);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -