/***********************************************************
* Copyright (c) 1999,2002 .
* Descriptions:
* _loadpara.c
* 加载相关参数.
* Known bugs:
*
* Change log:
* who MM/DD/YYYY why
* wangjs 03/20/2005 create
**********************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "stat_main.h"
/***********************************************************
* Function: 获取命令行参数.
* Parameter:
* [O]exePath :主进程所在路径
* [O]exeName :主进程名
* [O]lockFile :LOCK文件名
* [I]argc :参数个数
* [I]argv :参数序列
* Returns:
* 0 :程序继续运行
* -1 :程序需要退出
**********************************************************/
short GetArgParam(char *exePath, char *exeName, char *lockFile, int argc, char *argv[])
{
char tmpStr[100], cmdStr[100];
FILE *stream;
/*获取可执行文件名及路径*/
SeparatePathFile(exePath, exeName, argv[0]);
sprintf( lockFile, "%s.%s.pid", exePath, argv[1]+2);
if ( (argc != 3) && (argc != 2) && (!(argc == 4 && strcmp(argv[3],"-t")==0)) && (!(argc == 4 && strncmp(argv[3],">",1)==0)) )
{
printf("please input %s -h to view help\n",exeName);
return -1;
}
if (argc == 4 ||argc == 3 )
{
sprintf( lockFile, "%s.%s.pid", exePath, argv[2]);
return 0;
}
/*显示帮助信息*/
if (strcmp(argv[1], "-h") == 0)
{
printf( "Usage:\n");
printf("\t%s stat_id cfg_file year month day\n",exeName);
printf("\t\t[2] cfg_file: login file\n");
printf("\t\t[1] task_no : task no \n");
//printf("\t\t[3] year : YYYY\n");
//printf("\t\t[4] month : MM\n");
//printf("\t\t[5] day : DD\n\n");
printf("\t%s cfg_file task_no -t : print test sql\n",exeName);
printf("\t%s -h :to view help\n",exeName);
printf("\t%s -v :to view version\n",exeName);
printf("\t%s -qstat_id :to stop the program of stat_id.\n",exeName );
return -1;
}
/*显示版本信息*/
else if (strcmp(argv[1], "-v") == 0)
{
printf("\t%s Version 1.0 \n", exeName);
printf("\tCompiled at %s %s\n", __DATE__, __TIME__);
printf("\tCopyrights hold by jisx.\n");
exit(0);
}
/*退出程序*/
else if (strncmp(argv[1], "-q", 2) == 0)
{
if(CheckUniqueCopy(lockFile) != 0) /*程序已运行*/
{
stream = fopen(lockFile,"r");
if(stream!=NULL)
{
memset(tmpStr, 0, sizeof(tmpStr));
fgets(tmpStr,sizeof(tmpStr),stream);
fclose(stream);
sprintf(cmdStr, "kill %s", tmpStr);
system(cmdStr);
sprintf(cmdStr, "rm -f %s%c", lockFile, '\0');
system(cmdStr);
}
printf("The Program '%s' exit success.\n",argv[1]+2);
}
else
printf("The Program '%s' not exist.\n",argv[1]+2);
return -1;
}
/*非法参数*/
else
{
printf("please input %s -h to view help\n",exeName);
return -1;
}
}
/***********************************************************
* Function: 替换常量.
* Parameter:
* [IO]inStr :需替换的字符串
* Returns:
* none
**********************************************************/
void RepConstParam(char *inStr)
{
if(strstr(inStr, "${") != NULL)
{
strRep(inStr, "${MM}", gc_month);
strRep(inStr, "${YYYY}", gc_year);
strRep(inStr, "${YY}", gc_year+2);
strRep(inStr, "${DD}", gc_day);
strRep(inStr, "${DEAL_DATE}", gc_statTime);
//替换环境变量 by jisx @ 20130822
//RepEnv(inStr);
}
return;
}
/***********************************************************
* Function: 查找指定的变量.
* Parameter:
* [O]outStr :输出的操作结果描述
* [O]outV :输出指定变量对应的值
* [I]inParam :指定的变量名称
* [I]rec :存放变量值的数组
* [I]rowNum :数组的总长度
* Returns:
* 0 :找到
* -1 :未找到
**********************************************************/
short GetParamValue(char *outStr, char *outV, char *inParam, TABLE_RECORD *rec, int rowNum)
{
int i;
for(i=0; i<rowNum; i++)
{
//printf( "inParam=[%s],field=[%s]\n", inParam, (rec+2*i)->field );
if(strcmp(inParam, (rec+2*i)->field)==0)
{
sprintf(outV, "%s", (rec+2*i+1)->field);
return 0;
}
}
if((strcmp(inParam, "before_insert")==0)||(strcmp(inParam, "after_insert")==0))
{
outV[0]='\0';
return 0;
}
sprintf(outStr, "the param \"%s\" is not found", inParam);
return -1;
}
/***********************************************************
* Function: 从param表中读取入口参数.
* Parameter:
* [O]outStr :输出的操作结果描述
* [O]PT :存储参数的结构体指针
* [I]DBSession :数据库事务指针
* [I]inTableName :参数表表名
* [I]procID :本进程的进程标识
* Returns:
* 0 :成功
* -1 :操作失败
**********************************************************/
short GetParamFromTable(char *outStr, PARAM_TABLE *PT,
DB_SESSION *DBSession, char *inTableName, char *procID)
{
char tmpSql[300], tmpStr[100];
int tmpColCount, i, rowNum;
TABLE_RECORD tmpRec[2];
TABLE_RECORD *rec;
short flag;
//printf( "Line %d:DBSession->outdesc=[%s]\n", __LINE__, DBSession->outdesc );
i = 0;
i += sprintf(tmpSql + i, "select param_name,param_value ");
i += sprintf(tmpSql + i, "from %s ", inTableName);
i += sprintf(tmpSql + i, "where program_name='sync_data' ");
i += sprintf(tmpSql + i, "order by param_name ");
//printf("tmpSql:[%s]\n", tmpSql);
rowNum = GetCountOfTable(outStr, DBSession, tmpSql);/*取表的行数*/
flag=GetRowFromTable(outStr, DBSession, tmpRec, &tmpColCount, tmpSql, NULL, 0);
if(flag != 0)
{
#ifdef DEBUG_INFO
printf("Error Sql: %s\n", tmpSql);
#endif
return -1;
}
/*取出所有参数*/
rec = malloc( rowNum * 2 * sizeof( TABLE_RECORD ) );
memset(rec, 0, rowNum * 2 * sizeof( TABLE_RECORD ) );
memset(PT, 0, sizeof(PARAM_TABLE));
for(i=0; i< rowNum; i++)
{
RepConstParam(tmpRec[1].field); /*替换特殊字符*/
sprintf( (rec+i*2)->field, "%s", tmpRec[0].field);
sprintf( (rec+i*2+1)->field, "%s", tmpRec[1].field);
//printf( "[%s]=[%s]\n", tmpRec[0].field, tmpRec[1].field );
flag = FetchData(outStr, DBSession);
if(flag != 0) break;
}
/*取日志路径*/
if( (GetParamValue(outStr, PT->runlogPath, "runlog_path", rec, rowNum) !=0)||
(CheckValidPath(outStr, PT->runlogPath) != 0) )
{
free(rec);
return -1;
}
/*初始化日志文件*/
Initlog(PT->runlogPath, procID);
/*取相关参数*/
if( (GetParamValue(outStr, tmpStr, "proc_num", rec, rowNum) !=0)||
(GetParamValue(outStr, tmpStr+20, "max_rec_num", rec, rowNum) !=0)||
(GetParamValue(outStr, tmpStr+40, "is_insertdb", rec, rowNum) !=0)||
(GetParamValue(outStr, PT->before_insert, "before_insert", rec, rowNum) !=0)||
(GetParamValue(outStr, PT->af