`
laobian
  • 浏览: 1331 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

c with file searching

阅读更多
#include <stdio.h>
#include <string.h>

int readLine(char* line, FILE* fp);
int idStep(char* procLine, char* stepName, char* pgmName, int maxLen);
void idParm(char* dsn, char* reDsn, int dsnLen);
int idDisp(char* procLine, int maxLen);
int idDsn(char* procLine, int maxLen, int* dsnPos);
void getDsn(char* procLine , char* dsn, char* ddName, int maxLen, int* dsnLen, int initPos);
void searchFromBegain(char* stepName, char* inSName,
                      char* dsn, int currentLine, int dsnLen, FILE* fp);
void searchBelow(char (*stpNames)[9], int* cnt,
                      char* dsn, int dsnLen, FILE* fp);
int searhDsn(char* procLine, char* dsn, int maxLen, int dsnLen);
void writeHeader1(FILE* fp);
void writeHeader2(FILE* fp);
void writeHeader3(FILE* fp, char stepName[8], char prgName[8]);
void writeLine(FILE* fp, char ddName[], char dsName[], char io[],
               char input[], char output[]);


void main(int argN, char** argC)
{
	FILE* fp;
	FILE* tp;

	char procLine[81] = {'\0'};
	char stepName[9] = {'\0'};
	char pgmName[9] = {'\0'};
	char input[9] = {'\0'};
	char output[20][9] = {'\0'};
	char dsn[60] = {'\0'};
	char ddName[9] = {'\0'};
	char IO[2] = {'\0'};
	int rint = 0;
	int dsnPos = 0;
	int dsnLen = 0;
    int offset = 0;
    int curLine = 0;
    int ioInd = 0;
    int readYes = 0;
    int cnt = 0;
    int index = 1;
	fp = fopen("d:\\aaat.txt","r");
	if(NULL == fp){
		printf("open %s failed\n","d:\\aaat.txt");
		return;
	}
	tp = fopen("d:\\proc.txt","w");
	if(NULL == tp){
		printf("open %s failed\n","d:\\proc.txt");
		return;
	}

    writeHeader1(tp);


	while(1){
		if(readYes == 0){
		    rint = readLine(procLine,fp);
			offset += rint+1;
			curLine++;
		}
		if(rint < 1){
	/*	   printf("outer most loop reach the end of the file \n");*/
		   break;
	    }
		printf("%d : ", curLine);
        if(1 == idStep(procLine,stepName,pgmName,rint)){
	/*		printf("%s \n",procLine);*/
            writeHeader2(tp);
            writeHeader3(tp,stepName,pgmName);
			while(1){
				rint = readLine(procLine,fp);
				if(feof(fp))
				printf("\nend\n");
				if(rint < 1){
	/*				printf("the second layer loop reaches the end of file \n");*/
					break;
				}
				offset += rint+1;
       			curLine++;
				if('*' == *(procLine + 2)){
				}else if(1 == idStep(procLine,stepName,pgmName,rint)){
	/*			    printf("the previous step reaches the end \n");*/
				    readYes = 1;
  				    break;
			    }else if(1 == idDsn(procLine,rint,&dsnPos)){
					getDsn(procLine,dsn,ddName,rint,&dsnLen,dsnPos);
					while(1){
						ioInd = idDisp(procLine,rint);
						if(-1 ==ioInd){
							rint = readLine(procLine,fp);
							if(rint < 1){
	/*							printf("the inner most loop reaches the end of file \n");*/
								break;
							}
							offset += rint+1;
							curLine++;
						    if(1 == idDsn(procLine,rint,&dsnPos))
						        break;
						}
						if(0 == ioInd){
						    searchFromBegain(stepName,input,dsn,curLine,dsnLen,fp);
						    IO[0] = 'I';
							memset(output[0],' ',8);
							fseek(fp,offset,0);
							break;
						}
						if(1 == ioInd){
							searchBelow(output,&cnt,dsn,dsnLen,fp);
							IO[0] = 'O';
							memset(input,' ',8);
							fseek(fp,offset,0);
							break;
						}
                        if(-2 == ioInd){
							IO[0] = 'I';
							strcpy(input,"PRIORRUN");
							memset(output[0],' ',8);
							break;
						}
					}
					writeLine(tp,ddName,dsn,IO,input,output[0]);
					if(cnt >= 2){
						memset(ddName,' ',8);
						memset(dsn,' ',59);
						IO[0] = ' ';
						while(index < cnt)
						    writeLine(tp,ddName,dsn,IO,input,output[index++]);
					}
				}
			}
		}else
		     readYes = 0;
	}
	fclose(fp);
	fclose(tp);

}

int readLine(char* line, FILE* fp)
{
	int i = 0;
	char ch = '\0';
	ch = (char)fgetc(fp);
	while('\n' != ch && ch != EOF){
		*(line + i++) = ch;
		ch = (char)fgetc(fp);
	}
	if('\n' == ch)
	    *(line + i++) = ch;
	return i;
}

int idStep(char* procLine, char* stepName, char* pgmName, int maxLen)
{
	int i = 3;
	int stepFlag = 0;
	int j = 0;
	if( *(procLine + 2) != '*'){
        while(1){
    		if(i> maxLen - 4){
				break;
			}
			if(0 == strncmp(procLine +i++," EXEC ", 6)){
				i += 5;
    			if(0 == strncmp(procLine + i,"PGM=",4)){
					strncpy(stepName, procLine + 2, 8);
					i += 4;
					while(j<8){
						if(',' != *(procLine + i)
						   && '\n' != *(procLine + i)
						   && ' ' != *(procLine + i))
							*(pgmName + j++) = *(procLine + i++);
						else
						    break;
				    }
				    *(pgmName + j) = '\0';
				}
		        stepFlag = 1;
				break;
			}
		}
	}
	return stepFlag;
}

void idParm(char* dsn, char* reDsn, int dsnLen)
{
	int i = 0;
	while(i<dsnLen){
		if(0 == strncmp(dsn + i,"&Q",2))
		{
			strncat(reDsn,dsn,i);
			strcat(reDsn,"PROD");
			strncat(reDsn,(dsn+i+6),dsnLen-i-6);
			break;
		}
		if(0 == strncmp(dsn + i,"&E",2))
		{
			strncat(reDsn,dsn,i);
			strcat(reDsn,"P");
			strncat(reDsn,(dsn+i+4),dsnLen-i-4);
			break;
		}
		i++;
	}
}

int searhDsn(char* procLine, char* dsn, int maxLen, int dsnLen)
{
	int i = 0;
	int foundFlag = 0;
	while(i<maxLen - dsnLen)
	{
		if(0 == strncmp(procLine+i++,dsn,dsnLen))
		{
			foundFlag = 1;
			break;
		}
	}
	return foundFlag;
}

/* ioFlag = 0: O; ioFlag = 1: I */
int idDisp(char* procLine, int maxLen)
{
	int i = 5;
	int ioFlag = -1;
	if('*' != *(procLine+2))
	{
		while(i<maxLen-5){
			if(0 == strncmp(procLine + i++, "DISP=",5))
			{
				i += 4;
				if(0 == strncmp(procLine + i, "(N",2))
				   ioFlag = 1;
				else if(0 == strncmp(procLine + i, "(M",2))
				   ioFlag = -2;
			    else
				   ioFlag = 0;
				break;
			}
		}
	}
	return ioFlag;
}

int idDsn(char* procLine, int maxLen, int* dsnPos)
{
	int i = 8;
	int dsnFlag = 0;
	if('*' != *(procLine + 2)){
		while(1){
    		if(i>maxLen-5)
    	   		break;
			if(0 == strncmp(procLine + i++," DSN=",5)){
				*dsnPos = i+4;
				dsnFlag = 1;
				break;
			}
		}
	}
	return dsnFlag;
}

void getDsn(char* procLine , char* dsn, char* ddName, int maxLen, int* dsnLen, int initPos)
{
	int j = 0;
	int k = 2;
	while(' ' != *(procLine + k))
	{
		*(ddName + j++) = *(procLine + k++);
	}
	*(ddName+j) = '\0';
	j = 0;
	while(',' != *(procLine + initPos) && maxLen >= initPos)
	{
		*(dsn + j++) = *(procLine + initPos++);
	}
	*(dsn + j) = '\0';
	*dsnLen = j;
}

void searchFromBegain(char* stepName, char* inSName,
                      char* dsn, int currentLine, int dsnLen, FILE* fp)
{
	char procLine[80] = {'\0'};
	char holdSName[9] = {'\0'};
	char nextSName[9] = {'\0'};
	char nextpgm[9] = {'\0'};
	char pgmName[9] = {'\0'};
	int lineNum = 0;
	int maxLen = 0;
	int readYes = 0;
	fseek(fp,0,0);
	while(lineNum < currentLine){
		if(readYes == 0){
		    maxLen = readLine(procLine,fp);
		    lineNum++;
		}
		if(maxLen < 1 ){
	/*		printf("no more lines in the proc");*/
			return;
		}
		if(1 == idStep(procLine,holdSName,pgmName,maxLen) && 0 != strncmp(pgmName,"IEFBR14",7)){
			if(0 == strncmp(holdSName,stepName,8)){
	/*			printf("no other more steps before");*/
				strcpy(inSName,"PRIORRUN");
				return;
			}
			while(1){
				maxLen = readLine(procLine,fp);
				if(maxLen < 1 ){
	/*				printf("no more lines in the proc");*/
					break;
				}
				lineNum++;
				if('*' == *(procLine+2)){
				}else if(1 == idStep(procLine,nextSName,nextpgm,maxLen)){
	/*				printf("the inner step %s got the end\n",holdSName);*/
					readYes = 1;
					break;
				}else if(1 == searhDsn(procLine,dsn,maxLen,dsnLen)){
	/*				printf("the dsn: %s has been find, and the step name is %s\n",dsn,holdSName);*/
					strncpy(inSName,holdSName,8);
					return;
				}
			}
		}else
		     readYes = 0;
	}
}

void searchBelow(char (*stpNames)[9], int* cnt,
                      char* dsn, int dsnLen, FILE* fp)
{
	char procLine[80] = {'\0'};
	char holdSName[9] = {'\0'};
	char pgmName[9] = {'\0'};
	char nextSName[9] = {'\0'};
	char nextpgm[9] = {'\0'};
	int maxLen = 0;
	int readYes = 0;
	int i = 0;
	while(1){
		if(readYes == 0)
		    maxLen = readLine(procLine,fp);
		if(maxLen < 1 ){
/*			printf("no more lines in the proc");*/
			return;
		}
		if(1 == idStep(procLine,holdSName,pgmName,maxLen) && 0 != strncmp(pgmName,"IEFBR14",7)){
			while(1){
				maxLen = readLine(procLine,fp);
				if(maxLen < 1 ){
/*					printf("no more lines in the proc");*/
					break;
				}
				if('*' == *(procLine+2)){
				}else if(1 == idStep(procLine,nextSName,nextpgm,maxLen)){
/*					printf("the inner step %s got the end\n",holdSName);*/
					readYes = 1;
					break;
				}else if(1 == searhDsn(procLine,dsn,maxLen,dsnLen)){
/*				    printf("the dsn: %s has been find, and the step name is %s\n",dsn,holdSName);*/
					strncpy(stpNames[i++],holdSName,8);
/*					i += 9;*/
					*cnt++;
					readYes = 0;
					break;
				}
			}
		}else
		     readYes = 0;
	}
}

void writeHeader1(FILE* fp)
{
	char* str = "***********************************************************************\n";
	fwrite(str,1,strlen(str),fp);
}

void writeHeader2(FILE* fp)
{
	char* str = "\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n";
	fwrite(str,1,strlen(str),fp);
}

void writeHeader3(FILE* fp, char stepName[8], char prgName[8])
{
    char* ptr;
    char line1[73] = {'\n'};
    char line2[73] = {'\n'};
    char* str1 = "\nSTEP NARRATIVE: \n";
    char* str2 = "\nRESTART INSTRUCTIONS: \n";
    char* str3 = "\nDDNAME     DATASET NAME                   I/O   INPUT FROM  OUTPUT TO \n";
    char* str4 = "\n------   --------------------------------  ---  ----------  --------- \n";
    char stName[9] = {'\0'};
    memcpy(stName,stepName,8);
    ptr = line1;
    strcat(ptr,"STEP: ");
    strcat(ptr,stName);
    strcat(ptr,"                     TITLE: \n");
	fwrite(line1,1,strlen(ptr),fp);
	ptr = line2;
	strcat(ptr,"PRGM: ");
	strcat(ptr,prgName);
	strcat(ptr,"\n");
	fwrite(line2,1,strlen(ptr),fp);
	fwrite(str1,1,strlen(str1),fp);
	fwrite(str2,1,strlen(str2),fp);
	fwrite(str3,1,strlen(str3),fp);
	fwrite(str4,1,strlen(str4),fp);
}

void writeLine(FILE* fp, char ddName[], char dsName[], char io[],
               char input[], char output[])
{
	char line[90] = {'\n'};
	strcat(line, ddName);
	strcat(line, " ");
	strcat(line, dsName);
	strcat(line, " ");
	strcat(line, io);
	strcat(line, " ");
	strcat(line, input);
	strcat(line, " ");
	strcat(line, output);
	strcat(line,"\n");
	fwrite(line,1,strlen(line),fp);
}

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    Foundations for Analytics with Python O-Reilly-2016-Clinton W. Brownley

    It transitions to an illustration of potential problems with this method of parsing and then presents an example of how to avoid these potential problems by parsing a CSV file with Python’s csv ...

    VclZip pro v3.10.1

    - Now completely compatible with PKZip and WinZip split archives file naming format. - For backwards compatability you can tell VCLZip to use the old VCLZip filenaming format by using the BlockMode ...

    Address Book with Multiple Field Search(个人信息管理系统)

    For convenience, assume that none of the fields have spaces or commas in them, so you can read a comma separated file without worrying about those (non-algorithm related) complications. No need to ...

    C.Programming.The.Ultimate.Way.to.Learn.C.1500481114

    File Handling In C. Chapter 13. Miscellaneous Topics. Chapter 14. Storage Class. Chapter 15. Algorithms. Chapter 16. Unsolved Practical Problems. Chapter 17. PART-II-120+ Practical Code Chapter-Wise....

    Professional.MFC.with.VC6

    Searching CStrings Extracting the Contents of CString CString Formatting Working Directly with the Buffer Using Normal String Functions Empty Strings CStrings and COM Binary Data in CString ...

    UE(官方下载)

    You can use these functions to insert a file into the current file, delete the active file, send the file through email, or insert a string into the file at every specified increment HTML preview ...

    shellcode帮助工具,直接把exe转shellcode

    -len &lt;length&gt; convert the input file with the length (Default: 0 - MAX) -en [encoder] encode shellcode (Default: XorDword) -de [encoder] decode shellcode (Default: Auto-Detection) -ex &lt;chars&gt; ...

    Microsoft Codeview and Utilities User's Guide

    15.1.3 Managing Libraries with a Response File 15.1.4 Terminating the LIB Session 15.2 Performing Library-Management Tasks with LIB 15.2.1 Creating a Library File 15.2.2 Changing a Library File 15.2.3...

    occam一维反演

    C JANUARY 1989 (WITH THANKS TO C.DEGROOT-HEDLIN), (VERSION 1.5) C SEPTEMBER 1989, (VERSION 1.5.2) C APRIL 1992 (MORE THANKS TO CATHERINE AND ALSO WSE) (VERSION 2.0) C C IF YOU OBTAIN THIS CODE FROM A ...

    wrar571.exe

    "File names to find" mask after searching inside of archive. So it did not display archive names for masks like *.rar and applied this mask only to files inside of archives; h) when running an ...

    C.Programming.The.Definitive.Beginners.Reference.1500481009

    File Handling In C. Chapter 13. Miscellaneous Topics. Chapter 14. Storage Class. Chapter 15. Algorithms. Chapter 16. Unsolved Practical Problems. Chapter 17. PART-II-120+ Practical Code Chapter-Wise....

    C.in.Depth.Easy.Beginners.To.Experts.Guide.1500481033

    File Handling In C. Chapter 13. Miscellaneous Topics. Chapter 14. Storage Class. Chapter 15. Algorithms. Chapter 16. Unsolved Practical Problems. Chapter 17. PART-II-120+ Practical Code Chapter-Wise....

    强大的免费的十六进制编辑器

    file with some other hex editor (not to speak about Wordpad), then use XVI32... - XVI32 allows to edit files up to 2 GB (enough virtual memory provided, of course) - For your convenience, XVI32 ...

    Python Cookbook, 2nd Edition

    Searching and Replacing Text in a File Recipe 2.4. Reading a Specific Line from a File Recipe 2.5. Counting Lines in a File Recipe 2.6. Processing Every Word in a File Recipe 2.7. Using ...

    [Mastering.Node.js(2013.11) 精通Node.js

    Appendix C: Creating Your Own C++ Add-ons 307 Hello World 309 Creating a calculator 311Table of Contents [ vii ] Implementing callbacks 313 Closing thoughts 314 Links and resources 315 Index 317

Global site tag (gtag.js) - Google Analytics