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

📄 my_api.c

📁 unix 下用pro*c tuxedo 开发的东西
💻 C
📖 第 1 页 / 共 2 页
字号:
{

	int iIndex, iRet;
	char sDescription[200];
	char cFileName[MAX_LINE_LEN+1], cCommand[200], cArgv0[200];
	FILE* hGHFile;
	
	//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	
	hGHFile = fopen( cFileName, "w" );
	if( hGHFile == 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 GH 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, hGHFile ) )
		{
			printf( "%s\n", cLine );
			sprintf( sDescription, " LOG_ERROR: PCS_Depart_Src_File: Cannot write file: %s!",cFileName );
			ErrorLog( sDescription );
			continue;
		}
		if( EOF == fputc( '\n', hGHFile ) )
		{
			sprintf( sDescription, "LOG_ERROR: PCS_Depart_Src_File: Cannot write file: %s!",cFileName );
			ErrorLog( sDescription );
			continue;
		}
	}

	fclose( hGHFile );
		
	//Find the currespond program name to continue process.
	for( iIndex=0; iIndex<TOTAL_FILE_TYPE; iIndex++ )
	{
		if( !strncmp( cTypeArray[iIndex], "GHFILE", 6 ) )
		{
/*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: GHFile!\n" );
			}

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

}



//
//"XXX4"
//Form BKML file, do not close handle.
//
void myapi_Form_BKMLFile( char* cLine, FILE* hOriginalFile )
{

	int iIndex, iRet;
	char sDescription[200];
	char cFileName[MAX_LINE_LEN+1], cCommand[200], cArgv0[200];
	char cShortFileName[MAX_LINE_LEN+1];
	FILE* hBKMLFile;
	
	//The first line should be the file name.
	if( !myapi_ReadALine( cLine, hOriginalFile ) ) return;
	strcpy( cShortFileName, cLine );
	
	//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	
	hBKMLFile = fopen( cFileName, "w" );
	if( hBKMLFile == 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 BKML 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, hBKMLFile ) )
		{
//			printf( "%s\n", cLine );
			sprintf( sDescription, "LOG_ERROR: PCS_Depart_Src_File: Cannot write file: %s!",cFileName );
			ErrorLog( sDescription );
			continue;
		}
		if( EOF == fputc( '\n', hBKMLFile ) )
		{
			sprintf( sDescription, "LOG_ERROR: PCS_Depart_Src_File: Cannot write file: %s!",cFileName );
			ErrorLog( sDescription );
			continue;
		}
	}

	fclose( hBKMLFile );
		
	//Find the currespond program name to continue process.
	if( !strncmp( cShortFileName, "BSFX", 4 ) )
	{
		for( iIndex=0; iIndex<TOTAL_FILE_TYPE; iIndex++ )
		{
			if( !strncmp( cTypeArray[iIndex], "BSFXFILE", 10 ) )
			{
/*			
			sprintf( cCommand, "%s %s&\n", cProgramArray[iIndex], \
cFileName );
			printf( "%s", cCommand );
#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: BSFXFile!\n" );
				}

				return;			
			}
		}
		//If cannot find the correspond process program
		ErrorLog( "LOG_ERROR: PCS_Depart_Src_File: Cannot find the program file to process: BSFXBKMLFile!\n" );
	}
	else if( !strncmp( cShortFileName, "BKML", 4 ) )
	{
		for( iIndex=0; iIndex<TOTAL_FILE_TYPE; iIndex++ )
		{
			if( !strncmp( cTypeArray[iIndex], "BKJHMLFILE", 10 ) )
			{
/*			
			sprintf( cCommand, "%s %s&\n", cProgramArray[iIndex], \
cFileName );
			printf("%s", cCommand);
#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: BKJHMLFile!\n" );
				}

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

}



//
//"XXX5"
//Form SNML file, do not close handle.
//
void myapi_Form_SNMLFile( char* cLine, FILE* hOriginalFile )
{

	int iIndex, iRet;
	char sDescription[200];
	char cFileName[MAX_LINE_LEN+1], cCommand[200], cArgv0[200];
	FILE* hSNMLFile;
	
	//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	
	hSNMLFile = fopen( cFileName, "w" );
	if( hSNMLFile == 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 SNML 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, hSNMLFile ) )
		{
//			printf( "%s\n", cLine );
			sprintf( sDescription, "LOG_ERROR: PCS_Depart_Src_File: Cannot write file: %s!",cFileName );
			ErrorLog( sDescription );
			continue;
		}
		if( EOF == fputc( '\n', hSNMLFile ) )
		{
			sprintf( sDescription, "LOG_ERROR: PCS_Depart_Src_File: Cannot write file: %s!",cFileName );
			ErrorLog( sDescription );
			continue;
		}
	}

	fclose( hSNMLFile );
		
	//Find the currespond program name to continue process.
	for( iIndex=0; iIndex<TOTAL_FILE_TYPE; iIndex++ )
	{
		if( !strncmp( cTypeArray[iIndex], "SNMLFILE", 8 ) )
		{
/*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: SNMLFile!\n" );
			}
		
			return;			
		}
	}
	
	//If cannot find the correspond process program
	ErrorLog( "LOG_ERROR: PCS_Depart_Src_File: Cannot find the program file to process: SNMLFile!\n" );

}


//
//"X0X0"
//Form YSSJ file, do not close handle.
//This function has benn absoleted
//
void myapi_Form_YSSJFile( char* cLine, FILE* hOriginalFile )
{

	int iIndex;
	char* sDescription;
	char cFileName[MAX_LINE_LEN+1], cCommand[200];
	FILE* hYSSJFile;
	
	
	//Find the currespond program name to continue process.
	for( iIndex=0; iIndex<TOTAL_FILE_TYPE; iIndex++ )
	{
		if( !strncmp( cTypeArray[iIndex], "YSSJFILE", 8 ) )
		{
			break;			
		}
	}
	//if cannot find the corresponding program
	if( iIndex >= TOTAL_FILE_TYPE )
	{
//		sDescription = (char *)malloc(1000);
//		ProcessError( LOG_ERROR, "PCS_Depart_Src_File: Cannot find the program file to process: YSSJFile!\n" );
//		free( sDescription );
		return;
	}
	
	hYSSJFile = NULL;
	//The first line should be the file name.
	while( myapi_ReadALine( cLine, hOriginalFile ) )
	{
		if( myapi_IsFileName( cLine ) )
		{
			//End process
			if( (cLine[0] == 'X') && (cLine[1] == 'X') && (cLine[2] == 'X'))
				break;

			if( hYSSJFile != NULL )
			{
				fclose( hYSSJFile );
				sprintf( cCommand, "%s %s&\n", cProgramArray[iIndex], \
cFileName );
#ifndef DEBUG
				system( cCommand );
#endif				
			}
			
			sprintf( cFileName, "%s%s", DEPART_DIRECTORY, cLine );
			//Add suffix to form a unique file name if the file already exists
			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;
			}
		}
		else
		{
			//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;
			}
		}
	}
	if( hYSSJFile != NULL )
	{
		fclose( hYSSJFile );
		sprintf( cCommand, "%s %s&\n", cProgramArray[iIndex], \
cFileName );
#ifndef DEBUG
		system( cCommand );
#endif		
	}
}



//
//"XXX6"
//Form ZFJBH file, do not close handle.
//
void myapi_Form_ZFJBHFile( char* cLine, FILE* hOriginalFile )
{

	int iIndex, iRet;
	char sDescription[200];
	char cFileName[MAX_LINE_LEN+1], cCommand[200], cArgv0[200];
	FILE* hZFJBHFile;
	
	//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	
	hZFJBHFile = fopen( cFileName, "w" );
	if( hZFJBHFile == 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 ZFJBH 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, hZFJBHFile ) )
		{
//			printf( "%s\n", cLine );
			sprintf( sDescription, "LOG_ERROR: PCS_Depart_Src_File: Cannot write file: %s!",cFileName );
			ErrorLog( sDescription );
			continue;
		}
		if( EOF == fputc( '\n', hZFJBHFile ) )
		{
			sprintf( sDescription, "LOG_ERROR: PCS_Depart_Src_File: Cannot write file: %s!",cFileName );
			ErrorLog( sDescription );
			continue;
		}
	}

	fclose( hZFJBHFile );
		
	//Find the currespond program name to continue process.
	for( iIndex=0; iIndex<TOTAL_FILE_TYPE; iIndex++ )
	{
		if( !strncmp( cTypeArray[iIndex], "ZFJBHFILE", 9 ) )
		{
/*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: ZFJBHFile!\n" );
			}

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

}


//
//To determine wether the string is a filename or file content
//Return 1 if the string contain no ','
//Return 0 otherwise
//
int myapi_IsFileName( char *cALine )
{
	char *pLine;
	pLine = cALine;
	while( *pLine != '\0' )
	{
		if( *pLine == ',' ) return (0);
		pLine++;
	}
	return(1);
}


//
//To generic a unique file name based on the file name given.
//First, add suffix "0",then add the suffix by number "1", until there
//is no such file. If 9999 is reached by suffix, "0" is assigned.
//
void myapi_FormUniqueName( char* cFileName )
{
	int i=0;
	FILE* hFileName;
	char cFileContent[100], *pTempFile, *pFileContent;
	
	strcpy( cFileContent, cFileName );
	
	while( MyExistFile( cFileName ) )
	{
		sprintf( cFileName, "%s%i",cFileContent, i++ );
		if( i > 9999 )
		{
			i=0;
			sprintf( cFileName, "%s0", cFileContent );
			return;
		}
	}
}

void GetArgv0( char* cArgv0, char* cPathName )
{
	char* pPath;
	pPath = cPathName;
	while( *pPath != '\0' )
	{
		strcpy( cArgv0, pPath );
		while( (*pPath != '/') && (*pPath != '\0') )
		{
			pPath++;
		}
		if( *pPath == '/' ) pPath++;
	}
}


//
//Read sequence number from config file
//If cannot open then create
//Add the number by 1 then write it back to file
//
int GetSequence()
{
	FILE* hSequence;
	char cSequence[11];
	static int iSequence = 0;

	//fetch the current sequence from sequence file
	hSequence = fopen( SEQUENCE_FILE_NAME, "r" );
	if( hSequence == NULL )
	{
		//means: the file does not exist,then create for write 
		hSequence = fopen( SEQUENCE_FILE_NAME, "w" );
		if( hSequence == NULL )  //Create failed.
		{
			ErrorLog("Pcs_Depart_Src_File.x: Cannot create sequence file!\n");
			//use the static variable instead
			iSequence++;
			if( iSequence == 65535 ) iSequence = 1;
		}
		else  //Create new ok!
		{
			iSequence++;
			if( iSequence == 65535 ) iSequence = 1;
			sprintf( cSequence, "%i\r\n", iSequence );
			fputs( cSequence, hSequence );
			fclose( hSequence );
		}
	}
	else  //Open the file sucessfully
	{
		if( myapi_ReadALine( cSequence, hSequence ) )
		{
			iSequence = atoi( cSequence ) + 1;
			if( iSequence == 65535 ) iSequence = 1;
			fclose( hSequence );
			hSequence = fopen( SEQUENCE_FILE_NAME, "w" );
			if( hSequence != NULL )
			{
				sprintf( cSequence, "%i\r\n", iSequence );
				fputs( cSequence, hSequence );
				fclose( hSequence );
			}
		}
		else
		{
			fclose( hSequence );
			iSequence++;
		}
	}
//	printf( "Sequence number is: %i\n", iSequence );
	return( iSequence );
}

⌨️ 快捷键说明

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