📄 #my_api.c#
字号:
/*
* File name : my_api.c
* Module ID :
* Module Name :
* Purpose : Some function called from NCS_Depart_Src_File.c
* Author : Mr. Zhujingkun
* Date Created : 1999,05,28
* Version : Version 1.0
* Environment : Digital UNIX
* Portability : UNIX Platform
* Warnings : no
* References : tip.txt and readme.txt
* Calling Syntax :
* Parameters :
* Returns :
* Calling Function:
* Called Functions: NCS_Depart_Src_File.c
* Datastores and usages:
* Report : None
* Screens : None
* Messages Files : None
* Change Log :
* Change No. 01 :
*/
#include "stdio.h"
#include "stdlib.h"
#include "NCS_Depart_Src_File.h"
#include "my_api.h"
#include "errlog.h"
#include "unistd.h"
//Initialize errlog
void InitDepartSrcFile()
{
int retcode;
char *PID = "Depart source file every certain time.";
retcode = InitErrLogObj( PID, 2 );
if( retcode != 0 )
{
printf("ErrorLog Initialize error!\n");
}
}
//Close errlog object when main exit
void EndDepartSrcFile()
{
CloseErrLogObj();
}
//Check wether a file is exist
int MyExistFile( const char* sFile )
{
FILE *hFileToOpen;
hFileToOpen = fopen( sFile, "r" );
if( hFileToOpen != (FILE*)NULL )
{
fclose( hFileToOpen );
return(1);
}
else
return(0);
}
//
//Read a line to a string from a file, \n is excluded.
//return 1 if sucessful, 0 otherwise
//Do not close the file.
//
int myapi_ReadALine( char* cLine, FILE* hTable )
{
char cChar;
int i=0;
cLine[0] = '\0';
while( (cChar = fgetc( hTable )) != '\n' )
{
if( cChar == EOF ) return(0);
cLine[i++] = cChar;
//truncated if superlong
if( i >= MAX_LINE_LEN ) i=0;
cLine[i] = '\0';
}
return(1);
}
//
//Get the Typename and ProcessProgramName int structure from line content
//comments......
void myapi_GetItem(char* cLine, char* cType, char* cProgram )
{
char cTypeName[MAX_LINE_LEN+1], cProgramName[MAX_LINE_LEN+1];
char* pTemp;
char* pLine = cLine;
pTemp = cTypeName;
//remove the leading blanks
while( (*pLine == ' ') || (*pLine == '\t') ) pLine++;
if( pLine[0] == '\0' ) return;
//assign the TypeName to cTypeName
while( (*pLine != ' ') && (*pLine != '\t') && (*pLine != '\0') )
*pTemp++ = *pLine++;
*pTemp = '\0';
if( cTypeName[0] == '\0' ) return;
pTemp = cProgramName;
//remove the between blanks
while( (*pLine == ' ') || (*pLine == '\t') ) pLine++;
if( pLine[0] == '\0' ) return;
//assign the ProgramName to cProgramName
while( *pLine != '\0' )
*pTemp++ = *pLine++;
*pTemp = '\0';
if( cProgramName[0] == '\0' ) return;
//string will be truncated if cTypeName is longer than [TYPE_NAME_LEN]
if( strlen(cTypeName)<=TYPE_NAME_LEN ) strcpy(cType, cTypeName);
else
{
strncpy( cType, cTypeName, TYPE_NAME_LEN );
cType[TYPE_NAME_LEN] = '\0';
}
if( strlen(cProgramName) <= PROGRAM_NAME_LEN ) strcpy(cProgram, cProgramName);
else
{
strncpy( cProgram, cProgramName, PROGRAM_NAME_LEN );
cProgram[PROGRAM_NAME_LEN] = '\0';
}
return;
}
//
//Get the Time_Interval specified in the file and return it
//
int GetTimeInterval(char* cType)
{
FILE *hTimeInterval;
char cLine[MAX_LINE_LEN+1], cCaption[100], cValue[21];
int iInterval, i;
char* pLine;
hTimeInterval = fopen( CONFIG_FILE_FOR_TIMEINTERVAL, "r" );
if( hTimeInterval == NULL )
{
ProcessError(LOG_ERROR, "GetTimeInterval: this is %s message:Cannot open file: %s,The file may not exist!", "ERROR", CONFIG_FILE_FOR_TIMEINTERVAL);
return(DEFAULT_TIMEINTERVAL);
}
while( myapi_ReadALine( cLine, hTimeInterval ) )
{
if( cLine[0] == '#' ) continue;
if( cLine[0] == NULL ) continue;
pLine = cLine;
while( (*pLine == ' ') || (*pLine == '\t') ) pLine++;
i=0;
while( (*pLine != ' ') && (*pLine != '\t') && (*pLine != '\0') )
{
cCaption[i] = *pLine;
i++;
pLine++;
}
cCaption[i] = '\0';
if( strcmp( cCaption, cType ) == 0 ) /*Match*/
{
while( (*pLine == ' ') || (*pLine == '\t') ) pLine++;
if( *pLine == '\0' )
return(DEFAULT_TIMEINTERVAL);
i=0;
while( (*pLine != ' ') && (*pLine != '\t') && (*pLine != '\0') )
{
cValue[i] = *pLine;
i++;
pLine++;
}
cValue[i] = '\0';
iInterval = atoi(cValue);
if(iInterval <= 0)
{
return(DEFAULT_TIMEINTERVAL);
}
else
return(iInterval);
}
else
continue;
}
fclose( hTimeInterval );
return(DEFAULT_TIMEINTERVAL);
}
//
//Depart the seperate files from the original one.
//
void myapi_ProcessOriginalFile( const char* sFileName )
{
char cLine[MAX_LINE_LEN+1], cCommand[100];
char* sDescription;
int iBeginFlag, iEndFlag;
FILE* hOriginalFile;
hOriginalFile = fopen( sFileName, "r" );
if( hOriginalFile == NULL )
{
sDescription = (char *)malloc(200);
sprintf( sDescription, "PCS_Depart_Src_File: Cannot open file: %s,The file may not exist!\n",\
sFileName );
ProcessError( LOG_ERROR, sDescription );
free( sDescription );
return;
}
iBeginFlag=0;
myapi_ReadALine( cLine, hOriginalFile );
while( 1 )
{
//depart files.
if( (cLine[0]=='X') && (cLine[1]=='X') && (cLine[2]=='X') && \
(cLine[3]=='X') )
{
iBeginFlag = 1;
switch( cLine[4] ){
case '1': //YSSM
myapi_Form_YSSMFile( cLine, hOriginalFile );
break;
case '2': //TJBB
myapi_Form_TJBBFile( cLine, hOriginalFile );
break;
case '3': //GH
myapi_Form_GHFile( cLine, hOriginalFile );
break;
case '4': //BKML
myapi_Form_BKMLFile( cLine, hOriginalFile );
break;
case '5': //SNML, never used.
myapi_Form_SNMLFile( cLine, hOriginalFile );
break;
case '6': //ZFJBH
myapi_Form_ZFJBHFile( cLine, hOriginalFile );
break;
default:
myapi_ReadALine( cLine, hOriginalFile );
break;
}
if ( (strlen(cLine)==4) || (strlen(cLine)==5) )
continue;
else
{
if( !myapi_ReadALine( cLine, hOriginalFile ) ) break;
}
}
else
{
if( !myapi_ReadALine( cLine, hOriginalFile ) )
break;
}
}
//Close and delete the original file.
fclose( hOriginalFile );
sprintf( cCommand, "rm -f %s",sFileName );
#ifndef DEBUG
system( cCommand );
#endif
}
//
//Record the error message into errorlog file
//Need to truncate or delete as the size grow
//
void ErrorLog( const char* sErrorMessage )
{
FILE* hErrorFile;
char cDateTime[100];
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( sErrorMessage, hErrorFile );
fclose( hErrorFile );
sprintf( cDateTime, "date >> %s\n", ERROR_LOG_FILE );
system( cDateTime );
}
//
//"XXXX1"
//Form YSSM file, do not close handle.
//"cLine" can return the line content that indicate the file type that followed
//Read file till "XXXX*" encountered. Include YSSJ that followed
//
void myapi_Form_YSSMFile( char* cLine, FILE* hOriginalFile )
{
int iIndex;
char* sDescription;
char cFileName[MAX_LINE_LEN+1], cCommand[200];
FILE* hYSSJFile;
//The first line should be the file name.
if( !myapi_ReadALine( cLine, hOriginalFile ) ) return;
//Add suffix to form a unique file name if the file already exists
sprintf( cFileName, "%s%s", DEPART_DIRECTORY, cLine );
myapi_FormUniqueName( cFileName );
//Create a new file for write
hYSSJFile = fopen( cFileName, "w" );
if( hYSSJFile == NULL )
{
sDescription = (char *)malloc(1000);
sprintf( sDescription, "PCS_Depart_Src_File: Cannot create file: %s!,Maybe there\
is no enough disk space.",cFileName );
ProcessError( LOG_ERROR, sDescription );
free( sDescription );
return;
}
//Write the content to YSSJ file
while( myapi_ReadALine( cLine, hOriginalFile ) )
{
//End process
if( (cLine[0] == 'X') && (cLine[1] == 'X') && (cLine[2] == 'X') )
break;
//Normal write
if( EOF == fputs( cLine, hYSSJFile ) )
{
printf( "%s\n", cLine );
sDescription = (char *)malloc(1000);
sprintf( sDescription, "PCS_Depart_Src_File: Cannot write file: %s!",cFileName );
ProcessError( LOG_ERROR, sDescription );
free( sDescription );
continue;
}
if( EOF == fputc( '\n', hYSSJFile ) )
{
sDescription = (char *)malloc(1000);
sprintf( sDescription, "PCS_Depart_Src_File: Cannot write file: %s!",cFileName );
ProcessError( LOG_ERROR, sDescription );
free( sDescription );
continue;
}
}
fclose( hYSSJFile );
//Find the currespond program name to continue process.
for( iIndex=0; iIndex<TOTAL_FILE_TYPE; iIndex++ )
{
if( !strncmp( cTypeArray[iIndex], "YSSMFILE", 8 ) )
{
sprintf( cCommand, "%s %s&\n", cProgramArray[iIndex], \
cFileName );
#ifndef DEBUG
system( cCommand );
#endif
return;
}
}
//If cannot find the correspond process program
// sDescription = (char *)malloc(1000);
ProcessError( LOG_ERROR, "PCS_Depart_Src_File: Cannot find the program file to process: YSSMFile!\n" );
// free( sDescription );
}
//
//"XXX2"
//Form TJBB file, do not close handle.
//
void myapi_Form_TJBBFile( char* cLine, FILE* hOriginalFile )
{
int iIndex;
char* sDescription;
char cFileName[MAX_LINE_LEN+1], cCommand[200];
FILE* hTJBBFile;
//The first line should be the file name.
if( !myapi_ReadALine( cLine, hOriginalFile ) ) return;
//Add suffix to form a unique file name if the file already exists
sprintf( cFileName, "%s%s", DEPART_DIRECTORY, cLine );
myapi_FormUniqueName( cFileName );
//Create a new file for write
hTJBBFile = fopen( cFileName, "w" );
if( hTJBBFile == NULL )
{
sDescription = (char *)malloc(1000);
sprintf( sDescription, "PCS_Depart_Src_File: Cannot create file: %s!,Maybe there\
is no enough disk space.",cFileName );
ProcessError( LOG_ERROR, sDescription );
free( sDescription );
return;
}
//Write the content to TJBB file
while( myapi_ReadALine( cLine, hOriginalFile ) )
{
//End process
if( (cLine[0] == 'X') && (cLine[1] == 'X') && (cLine[2] == 'X') )
break;
//Normal write
if( EOF == fputs( cLine, hTJBBFile ) )
{
printf( "%s\n", cLine );
sDescription = (char *)malloc(1000);
sprintf( sDescription, "PCS_Depart_Src_File: Cannot write file: %s!",cFileName );
ProcessError( LOG_ERROR, sDescription );
free( sDescription );
continue;
}
if( EOF == fputc( '\n', hTJBBFile ) )
{
sDescription = (char *)malloc(1000);
sprintf( sDescription, "PCS_Depart_Src_File: Cannot write file: %s!",cFileName );
ProcessError( LOG_ERROR, sDescription );
free( sDescription );
continue;
}
}
fclose( hTJBBFile );
//Find the currespond program name to continue process.
for( iIndex=0; iIndex<TOTAL_FILE_TYPE; iIndex++ )
{
if( !strncmp( cTypeArray[iIndex], "TJBBFILE", 8 ) )
{
sprintf( cCommand, "%s %s&\n", cProgramArray[iIndex], \
cFileName );
#ifndef DEBUG
system( cCommand );
#endif
return;
}
}
//If cannot find the correspond process program
// sDescription = (char *)malloc(1000);
ProcessError( LOG_ERROR, "PCS_Depart_Src_File: Cannot find the program file to process: TJBBFile!\n" );
// free( sDescription );
}
//
//"XXX3"
//Form GH file, do not close handle.
//
void myapi_Form_GHFile( char* cLine, FILE* hOriginalFile )
{
int iIndex;
char* sDescription;
char cFileName[MAX_LINE_LEN+1], cCommand[200];
FILE* hGHFile;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -