⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 my_api.c

📁 unix 下用pro*c tuxedo 开发的东西
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
*	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"


//Do nothing, previously was initialize errlog
void InitDepartSrcFile()
{
}


//Do nothing, previously was close errlog object when main exit
void EndDepartSrcFile()
{
}


//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);
}


//
//This function was added by Mr. zhujingkun at 2000,04,02
//Check wether there are files preceeded by "YX"
//
int ExistYXFile( const char* sYXJSDIR )
{
	char cCommand[500];

	sprintf( cCommand, "ls %s > %s", sYXJSDIR,YXFILE_LIST );
	system( cCommand );
	if( GetFileSize( YXFILE_LIST ) == 0 )
		return(0);
	else
		return(1);
}


//
//This function was added by Mr. zhujingkun on 2000.04.02
//Take the file names from the filelist file,the call "Pcs_Snd_YX1.x" to process them
//Add sending sequence on 2000.04.28 by Mr. zhujingkun
//
void myapi_ProcessYXFile( const char* cYXFileList )
{
	FILE* hFileList;
	char cThisLine[MAX_LINE_LEN+1], cThisItem[MAX_LINE_LEN+1];
	char* pThisLine;
	char cCommand[500];


	hFileList = fopen( cYXFileList, "r" );
	if( hFileList == NULL )
	{
		ErrorLog("Pcs_Depart_Src_File.x: Open YX filelist file error\n");
		return;
	}
	
	while( myapi_ReadALine( cThisLine, hFileList ) )
	{
		strcpy( cThisItem, cThisLine );
		if( strncmp( cThisItem, "YX", 2 ) )  //not YX File
		{
			//move the unknow file to garbage directory
			sprintf( cCommand, "mv -f %s%s %s%s\n", TXJ_RCV_DATA_DIR, cThisItem, GARBAGE_DIRECTORY, cThisItem);
			system( cCommand );
			continue;
		}
		//Add the full path to the YXFILE
		sprintf( cCommand, "mv -f %s%s %s%s\n", TXJ_RCV_DATA_DIR, cThisItem, DEPART_DIRECTORY, cThisItem);
		system( cCommand );
		sprintf( cThisItem, "%s%s", DEPART_DIRECTORY, cThisLine );

		//Get sequence number by calling a function
		sprintf( g_sSequence, "%s", GetSequence() );
		if( fork() == 0 )
		{
			//Here, add a parameter to process.
			if( -1 == execl( "/usr/bkfx/bin/Pcs_Snd_YX1.x","Pcs_Snd_YX1.x", cThisItem, g_sSequence, NULL ) );
			{
#ifdef DEBUG
			 	printf("Pcs_Snd_YX1.x not found.\n ");
#endif
				ErrorLog("Pcs_Depart_Src_File.x: Pcs_Snd_YX1.x not found.\n ");
			}
			exit(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 file size
 */
size_t GetFileSize( char *cFileName )
{
	size_t ulSize;
	FILE *hThisFile;
	char cNextChar;
	char cErrorMessage[500];
	
	hThisFile = fopen( cFileName, "r" );
	if( hThisFile == NULL )
	{
		sprintf( cErrorMessage,"PCSRestoreapi: GetFileSize- Cannot open the file: %s!\n",cFileName );
		ErrorLog( cErrorMessage );
		return(0);
	}
	
	ulSize=0;
	while( (cNextChar = fgetc(hThisFile)) != EOF ) ulSize++;
	fclose( hThisFile );
	return( ulSize );
}

//
//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], cDescription[200];
	int iInterval, i;
	char* pLine;
	
	 
	hTimeInterval = fopen( CONFIG_FILE_FOR_TIMEINTERVAL, "r" );
	if( hTimeInterval == NULL )
	{
		sprintf( cDescription, "Pcs_Depart_Src_File: Cannot open file: %s,The file may not exist!", CONFIG_FILE_FOR_TIMEINTERVAL);
		ErrorLog( cDescription );
		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' )
			{
				fclose( hTimeInterval );
				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) 		
			{
				fclose( hTimeInterval );
				return(DEFAULT_TIMEINTERVAL);
			}
			else
			{
				fclose( hTimeInterval );
				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[200];
	int iBeginFlag, iEndFlag;
	FILE* hOriginalFile;
	
	hOriginalFile = fopen( sFileName, "r" );
	if( hOriginalFile == NULL )
	{
		sprintf( sDescription, "LOG_ERROR: PCS_Depart_Src_File: Cannot open file: %s,The file may not exist!\n",\
sFileName );
		ErrorLog( sDescription );
		return;
	}
	
	//Get the current sequence by calling the function
	sprintf( g_sSequence, "%i", GetSequence());
//	printf( "The current sequence is: %s\n", g_sSequence );

	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
	//				printf("Process BKML File.\n");
					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, iRet;
	char sDescription[200];
	char cFileName[MAX_LINE_LEN+1], cCommand[200], cArgv0[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 )
	{
		sprintf( sDescription, "LOG_ERROR: PCS_Depart_Src_File: Cannot create file: %s!,Maybe there\
 is no enough disk space.",cFileName );
		ErrorLog( 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 );
			sprintf( sDescription, "LOG_ERROR: PCS_Depart_Src_File: Cannot write file: %s!",cFileName );
			ErrorLog( sDescription );
			continue;
		}
		if( EOF == fputc( '\n', hYSSJFile ) )
		{
			sprintf( sDescription, "LOG_ERROR: PCS_Depart_Src_File: Cannot write file: %s!",cFileName );
			ErrorLog( 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 );
#ifdef DEBUG
			system( cCommand );
#endif			
*/


/*Change from system to fork(), execl()*/
			GetArgv0( cArgv0, cProgramArray[iIndex] );
			printf("%s %s %s %s\n", cProgramArray[iIndex], cArgv0, cFileName, g_sSequence);
			iRet = fork();
			if( iRet == 0 )  //I'm child
			{
				execl( cProgramArray[iIndex], cArgv0, cFileName, g_sSequence, NULL );
				exit(0);
			}
			else if( iRet == -1 )  //Error, cannot create process
			{
				ErrorLog( "LOG_ERROR: PCS_Depart_Src_File: Cannot create sub_process: YSSMFile!\n" );
			}
			return;			
		}
	}
	
	//If cannot find the correspond process program
	ErrorLog( "LOG_ERROR: PCS_Depart_Src_File: Cannot find the program file to process: YSSMFile!\n" );

}



//
//"XXX2"
//Form TJBB file, do not close handle.
//
void myapi_Form_TJBBFile( char* cLine, FILE* hOriginalFile )
{

	int iIndex, iRet;
	char sDescription[200];
	char cFileName[MAX_LINE_LEN+1], cCommand[200], cArgv0[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 )
	{
		sprintf( sDescription, "LOG_ERROR: PCS_Depart_Src_File: Cannot create file: %s!,Maybe there\
 is no enough disk space.",cFileName );
		ErrorLog( 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 );
			sprintf( sDescription, "LOG_ERROR: PCS_Depart_Src_File: Cannot write file: %s!",cFileName );
			ErrorLog( sDescription );
			continue;
		}
		if( EOF == fputc( '\n', hTJBBFile ) )
		{
			sprintf( sDescription, "LOG_ERROR: PCS_Depart_Src_File: Cannot write file: %s!",cFileName );
			ErrorLog( 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			
*/

/*Change from system to fork(), execl()*/
			GetArgv0( cArgv0, cProgramArray[iIndex] );
			iRet = fork();
			if( iRet == 0 )  //I'm child
			{
				execl( cProgramArray[iIndex], cArgv0, cFileName, g_sSequence, NULL );
				exit(0);
			}
			else if( iRet == -1 )  //Error, cannot create process
			{
				ErrorLog( "LOG_ERROR: PCS_Depart_Src_File: Cannot create sub_process: TJBBFile!\n" );
			}

			return;			
		}
	}
	
	//If cannot find the correspond process program
	ErrorLog( "LOG_ERROR: PCS_Depart_Src_File: Cannot find the program file to process: TJBBFile!\n" );

}



//
//"XXX3"
//Form GH file, do not close handle.
//
void myapi_Form_GHFile( char* cLine, FILE* hOriginalFile )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -