📄 myapi.pc
字号:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* File name : PCS_Snd_SNML.x *
* Module ID : *
* Module Name : *
* Purpose : Get SNML Data from SNMLFILE And Insert into DB *
* Author : Mr. laishihong *
* Date Created : 1999,07,14 *
* Version : Version 1.0 *
* Environment : Digital UNIX *
* Portability : UNIX Platform *
* Warnings : *
* References : *
* Calling Syntax : *
* Parameters : None *
* Returns : void *
* Calling Function: *
* Called Functions: When system startup *
* Datastores and usages: * *
* (a) Input *
* (b) OutPut *
* Report : None *
* Screens : None *
* Messages Files : None *
* Change Log : *
* Change No. *
* Date *
* Author *
* Reason For Change *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
#include "stdio.h"
#include <stdlib.h>
#include <sqlcpr.h>
#include "/usr/bkfx/include/Pcs_Snd_SNML/cmdef.h"
#include "/usr/bkfx/include/Pcs_Snd_SNML/Pcs_Snd_SNML.h"
#include "/usr/bkfx/include/Pcs_Snd_SNML/Function_define.h"
/*define the global varible which will used in the function of myapi_ReadALine()*/
long turn=0;
int sequence;
char *freememory;
FILE *FreeFileHandle;
char filename[MAX_LINE_LEN];
int init_time=0;
int whichline;
char bkdh[8];
int sjbh_flag;
/* connect to the database and initialize the errorlog file */
void InitSndSNML1()
{
/* define the varible to connect to the database*/
VARCHAR userid[20];
VARCHAR password[20];
strcpy( userid.arr, "PCSDBA" );
userid.len = strlen( userid.arr );
strcpy( password.arr, "DBA" );
password.len = strlen( password.arr );
EXEC SQL WHENEVER SQLERROR STOP;
EXEC SQL CONNECT :userid IDENTIFIED BY :password;
#ifdef DEBUG
ProcessError(LOG_NORMAL, "Connected to Oracle as user:%s!", userid.arr);
#endif
}
/* get the province code from the data file name */
void myapi_GetRecYearFlagFromFileName( char *cRecYear, char *cFileName )
{
char cExt[2];
char *pFileName;
int i;
/* Initialize the RecProvince_Code */
cRecYear[0] = '\0';
pFileName = cFileName;
while( (*pFileName != '.') && (*pFileName != '\0') ) pFileName++;
if(*pFileName == '.') pFileName++;
i=0;
while( *pFileName != '\0' )
{
cExt[i++] = *pFileName++;
if( i==1 ) break;
}
cExt[i] = '\0';
strcpy( cRecYear, cExt );
}
/* get the file size */
size_t myapi_GetFileSize( FILE *hTable )
{
size_t ulSize;
char cNextChar;
ulSize=0;
while( (cNextChar = fgetc( hTable )) != EOF ) ulSize++;
rewind(hTable);
return( ulSize );
}
/* read the file data into the buffer */
void ReadSNMLFileData(FILE* hTable, size_t Length, char *buf_data)
{
char *sDescription;
if( fread(buf_data, Length , 1, hTable) != 1)
{
if(feof(hTable))
{ ProcessError(LOG_ERROR,"Premature end of file.",END);
free( freememory );
fclose(FreeFileHandle);
EXEC SQL ROLLBACK RELEASE;
movefile();
exit (1);
}
else
{
ProcessError(LOG_ERROR,"file read error!",END);
free( freememory );
fclose(FreeFileHandle);
EXEC SQL ROLLBACK RELEASE;
movefile();
exit (1);
}
}
}
/* write the errorlog file when the database is wrong */
void sqlerror()
{
char ERR_CODE[10];
char Error_Msg[100];
EXEC SQL WHENEVER SQLERROR CONTINUE;
ProcessError(LOG_ERROR, "database error:%s!", sqlca.sqlerrm.sqlerrmc );
sprintf(Error_Msg,"line: %d and bkdh: %s", whichline, bkdh);
ProcessError(LOG_NORMAL, "Didn't insert into DB sucessfully: %s!",Error_Msg);
strncpy(ERR_CODE,sqlca.sqlerrm.sqlerrmc,9);
ERR_CODE[9]='\0';
if( strcmp(ERR_CODE,"ORA-03127")==0|| strcmp(ERR_CODE,"ORA-03114")==0|| strcmp(ERR_CODE,"ORA-03123")==0|| strcmp(ERR_CODE,"ORA-01034")==0|| strcmp(ERR_CODE,"ORA-01089")==0||strcmp(ERR_CODE,"ORA-01012")==0||strcmp(ERR_CODE,"ORA-01089") ==0||strcmp( ERR_CODE, "ORA-01033") ==0 )
{
sleep(INIT_INTERVAL);
init_time++;
if (init_time == 6)
{
ProcessError(LOG_NORMAL, "Could not connect to the database, so exit now!",END);
sprintf(Error_Msg,"%d",whichline);
ProcessError(LOG_NORMAL, "Please reload the data from the line %s!",Error_Msg);
if ( freememory != NULL )
free( freememory );
fclose( FreeFileHandle );
EXEC SQL ROLLBACK RELEASE;
movefile();
exit(1);
}
InitSndSNML1();
return;
}
return;
}
/* free the memory and close the file etc. */
void terminate()
{
ProcessError(LOG_NORMAL, "The application is terminated by user!",END);
if ( freememory != NULL )
free( freememory );
fclose( FreeFileHandle );
EXEC SQL ROLLBACK RELEASE;
movefile();
exit(1);
}
/* insert a record into the t_SNML table */
int InsertSNMLRec( struct stSNMLFileRec *stFileRecord )
{
EXEC SQL WHENEVER SQLERROR DO sqlerror();
EXEC SQL INSERT INTO T_SNML ( BKDH, YXNF, BKMC ) VALUES (:stFileRecord->bkdh,
:stFileRecord->yxnf, :stFileRecord->bkmc);
return(1);
}
/* Read a line to a string from the buffer , \n is excluded.
return 1 if sucessful, 0 when the end of the buffer */
int myapi_ReadALine( char* cLine, char* FileLength )
{
int i=0;
cLine[0] = '\0';
while (FileLength[turn] =='\n')
turn++;
while( FileLength[turn] !='\n')
{
if( FileLength[turn] == '\0' )
return(0);
cLine[i++] = FileLength[turn++];
/* truncated if superlong */
if( i >= MAX_LINE_LEN ) i=0;
cLine[i] = '\0';
}
turn++;
return(1);
}
/* depart the string to fill the SNML record
return 1 if the string is a legal record, 0 otherwise */
int myapi_FillFileRecord( char *cLine, struct stSNMLFileRec *stFileRecord )
{
char cElement[MAX_LINE_LEN+1];
char tempchar[10];
sequence = 0;
ReadBufMsg(cLine, 6, cElement );
trim(cElement);
strncpy( stFileRecord->bkdh, cElement, 7 );
ReadBufMsg(cLine, 40, cElement );
trim(cElement);
strncpy( stFileRecord->bkmc, cElement, 41 );
ReadBufMsg(cLine, 2, cElement );
trim(cElement);
strncpy( stFileRecord->fbkjbh, cElement, 3 );
ReadBufMsg(cLine, 1, cElement );
if (cElement[0] == '\0')
{
sjbh_flag = 0;
return 1;
}
else
{
cElement[1] = '\0';
ReadBufMsg(cLine, 1, tempchar );
sprintf(stFileRecord->sjbh, "%s%s", cElement, tempchar );
return 1;
}
}
/* insert the record into the t_SNMLFB table */
int InsertSNMLFBRec(struct stSNMLFileRec *stFileRecord )
{
int rec_count, i;
char sjbh[3], bsbh[3];
EXEC SQL WHENEVER SQLERROR DO sqlerror();
if ( sjbh_flag == 0 )
{
EXEC SQL INSERT INTO T_SNMLFB (BKDH, SJBH, YXNF, FBKJBH ) VALUES (:stFileRecord->bkdh,
'99', :stFileRecord->yxnf, :stFileRecord->fbkjbh);
}
else
{
EXEC SQL INSERT INTO T_SNMLFB (BKDH, SJBH, YXNF, FBKJBH ) VALUES (:stFileRecord->bkdh,
:stFileRecord->sjbh, :stFileRecord->yxnf, :stFileRecord->fbkjbh);
}
EXEC SQL COMMIT;
return(1);
}
void ProcessError( char *LOG, char *sErrorMessage1, char *sErrorMessage2 )
{
FILE* hErrorFile;
char cDateTime[100],tempstr[100];
int i,j,len,n;
long filesize;
char bakfile[100];
tempstr[0]='\0';
len=strlen(sErrorMessage1);
for(i=0;i<=len;i++)
{
if(sErrorMessage1[i]=='%')
{
n=i;
if(len!=i+1)
{
for(j=0;j<200&&sErrorMessage1[i+2]!='\0';j++,i++)
tempstr[j]=sErrorMessage1[i+2];
tempstr[j]='\0';
}
break;
}
if(sErrorMessage1[i]=='\0')
{
n=i;
tempstr[0] = '\0';
break;
}
}
sErrorMessage1[n]='\0';
hErrorFile = fopen( ERROR_LOG_FILE, "a" );
if( hErrorFile == NULL )
{
printf( "Critical! Cannot open ERROR_LOG_FILE for append or cannot create!\n");
return;
}
fseek( hErrorFile, 0 ,SEEK_END );
filesize = ftell(hErrorFile);
if (filesize > 5242880)
{
fclose(hErrorFile );
sprintf(bakfile, "mv %s %s.bak", ERROR_LOG_FILE, ERROR_LOG_FILE);
system(bakfile);
hErrorFile = fopen( ERROR_LOG_FILE, "a" );
if( hErrorFile == NULL )
{
printf( "Critical! Cannot open ERROR_LOG_FILE for append or cannot create!\n");
return;
}
}
fseek( hErrorFile, 0 ,SEEK_END );
fputs( "\n",hErrorFile);
fputs( LOG ,hErrorFile);
printf("\n%s",LOG);
fputs( sErrorMessage1,hErrorFile );
printf("%s",sErrorMessage1);
fputs( sErrorMessage2,hErrorFile );
printf("%s",sErrorMessage2);
fputs( tempstr,hErrorFile );
printf("%s\n",tempstr);
fputs( "\n",hErrorFile);
fclose( hErrorFile );
sprintf( cDateTime, "date >> %s\n", ERROR_LOG_FILE );
system( cDateTime );
system( "date");
}
void movefile()
{
char cCommand[MAX_LINE_LEN];
/* move the source fsbkjhml data file to the backup directory*/
sprintf( cCommand, "mv -f %s %s", filename, BACKUP_DIRECTORY );
system( cCommand );
}
int ReadBufMsg( char *BKML_Msg, int n, char *element )
{
int i;
element[0]='\0';
for(i=0; i<n; i++)
{
if( BKML_Msg[sequence] !='\0' && BKML_Msg[sequence] !='\n')
{
element[i] = BKML_Msg[sequence++];
}
else
{
/* ProcessError(LOG_NORMAL,"It's the end of the buffer!",END);
*/
return(0);
}
}
element[i] = '\0';
return(1);
}
/*delete the ' ' or '\n' of a string at the begin and end*/
void trim(char *CAline)
{
int i,j;
char temp_string[100];
i=strlen(CAline);
if( CAline[i]!='\0' )
ProcessError(LOG_ERROR,"It's not a string ended by '\0'!",END);
/*delete the end ' ' or '\n' */
while(i>0)
{
if(CAline[i-1]==' '||CAline[i-1]=='\n')
CAline[i-1]='\0';
else
break;
i--;
}
i=0;j=0;
/*delete the head ' ' or '\n' */
while( CAline[i]!='\0' )
{
if(CAline[i]==' '||CAline[i]=='\n')
i++;
else
{
for (j=0;CAline[i]!='\0';i++,j++)
CAline[j]=CAline[i];
break;
}
}
CAline[j]='\0';
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -